001/* 002 * Copyright (C) 2009-2017 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.OutputStream; 019import java.io.PrintWriter; 020import java.io.Writer; 021import java.util.Locale; 022 023import static org.fusesource.jansi.AnsiRenderer.*; 024 025/** 026 * Print writer which supports automatic ANSI color rendering via {@link AnsiRenderer}. 027 * 028 * @author <a href="mailto:jason@planet57.com">Jason Dillon</a> 029 * @author <a href="http://hiramchirino.com">Hiram Chirino</a> 030 * @since 1.1 031 */ 032public class AnsiRenderWriter 033 extends PrintWriter { 034 035 public AnsiRenderWriter(final OutputStream out) { 036 super(out); 037 } 038 039 public AnsiRenderWriter(final OutputStream out, final boolean autoFlush) { 040 super(out, autoFlush); 041 } 042 043 public AnsiRenderWriter(final Writer out) { 044 super(out); 045 } 046 047 public AnsiRenderWriter(final Writer out, final boolean autoFlush) { 048 super(out, autoFlush); 049 } 050 051 @Override 052 public void write(final String s) { 053 if (test(s)) { 054 super.write(render(s)); 055 } else { 056 super.write(s); 057 } 058 } 059 060 // 061 // Need to prevent partial output from being written while formatting or we will get rendering exceptions 062 // 063 064 @Override 065 public PrintWriter format(final String format, final Object... args) { 066 print(String.format(format, args)); 067 return this; 068 } 069 070 @Override 071 public PrintWriter format(final Locale l, final String format, final Object... args) { 072 print(String.format(l, format, args)); 073 return this; 074 } 075}