001/* 002 * Copyright (C) 2009-2020 the original author(s). 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.fusesource.jansi; 017 018import java.io.IOException; 019import java.io.PrintStream; 020import java.io.UnsupportedEncodingException; 021 022import org.fusesource.jansi.io.AnsiOutputStream; 023 024/** 025 * Simple PrintStream holding an AnsiOutputStream. 026 * This allows changing the mode in which the underlying AnsiOutputStream operates. 027 */ 028public class AnsiPrintStream extends PrintStream { 029 030 public AnsiPrintStream(AnsiOutputStream out, boolean autoFlush) { 031 super(out, autoFlush); 032 } 033 034 public AnsiPrintStream(AnsiOutputStream out, boolean autoFlush, String encoding) throws UnsupportedEncodingException { 035 super(out, autoFlush, encoding); 036 } 037 038 protected AnsiOutputStream getOut() { 039 return (AnsiOutputStream) out; 040 } 041 042 public AnsiType getType() { 043 return getOut().getType(); 044 } 045 046 public AnsiColors getColors() { 047 return getOut().getColors(); 048 } 049 050 public AnsiMode getMode() { 051 return getOut().getMode(); 052 } 053 054 public void setMode(AnsiMode ansiMode) { 055 getOut().setMode(ansiMode); 056 } 057 058 public boolean isResetAtUninstall() { 059 return getOut().isResetAtUninstall(); 060 } 061 062 public void setResetAtUninstall(boolean resetAtClose) { 063 getOut().setResetAtUninstall(resetAtClose); 064 } 065 066 /** 067 * Returns the width of the terminal associated with this stream or 0. 068 * @since 2.2 069 */ 070 public int getTerminalWidth() { 071 return getOut().getTerminalWidth(); 072 } 073 074 public void install() throws IOException { 075 getOut().install(); 076 } 077 078 public void uninstall() throws IOException { 079 // If the system output stream has been closed, out should be null, so avoid a NPE 080 AnsiOutputStream out = getOut(); 081 if (out != null) { 082 out.uninstall(); 083 } 084 } 085 086 @Override 087 public String toString() { 088 return "AnsiPrintStream{" 089 + "type=" + getType() 090 + ", colors=" + getColors() 091 + ", mode=" + getMode() 092 + ", resetAtUninstall=" + isResetAtUninstall() 093 + "}"; 094 } 095}