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