001 /*
002 * Copyright (C) 2010 eXo Platform SAS.
003 *
004 * This is free software; you can redistribute it and/or modify it
005 * under the terms of the GNU Lesser General Public License as
006 * published by the Free Software Foundation; either version 2.1 of
007 * the License, or (at your option) any later version.
008 *
009 * This software is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * You should have received a copy of the GNU Lesser General Public
015 * License along with this software; if not, write to the Free
016 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
017 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
018 */
019
020 package org.crsh.text;
021
022 import org.crsh.util.AppendableWriter;
023
024 import java.io.PrintWriter;
025
026 /**
027 * The shell printer extends the {@link java.io.PrintWriter} and provides support for decorating a char stream.
028 *
029 * @author <a href="mailto:julien.viet@exoplatform.com">Julien Viet</a>
030 * @version $Revision$
031 */
032 public class ShellPrintWriter extends PrintWriter {
033
034 /** . */
035 private final ShellAppendable out;
036
037 public ShellPrintWriter(ShellAppendable out) {
038 super(new AppendableWriter(out));
039
040 //
041 this.out = out;
042 }
043
044 public final boolean isEmpty() {
045 return out.isEmpty();
046 }
047
048 public final void print(Object obj, Color foreground) {
049 out.append(foreground.style);
050 print(obj);
051 out.append(Style.reset);
052 }
053
054 public final void println(Object obj, Color foreground) {
055 print(obj, foreground.style);
056 println();
057 }
058
059 public final void print(Object obj, Color foreground, Color background) {
060 out.append(Style.style(foreground, background));
061 print(obj);
062 out.append(Style.reset);
063 }
064
065 public final void println(Object obj, Color foreground, Color background) {
066 print(obj, Style.style(foreground, background));
067 println();
068 }
069
070 public final void print(Object obj, Decoration decoration) {
071 out.append(decoration.style);
072 print(obj);
073 out.append(Style.reset);
074 }
075
076 public final void println(Object obj, Decoration decoration) {
077 print(obj, decoration.style);
078 println();
079 }
080
081 public final void print(Object obj, Decoration decoration, Color foreground) {
082 print(obj, Style.style(decoration, foreground));
083 println();
084 }
085
086 public final void println(Object obj, Decoration decoration, Color foreground) {
087 print(obj, Style.style(decoration, foreground, null));
088 println();
089 }
090
091 public final void print(Object obj, Decoration decoration, Color foreground, Color background) {
092 print(obj, Style.style(decoration, foreground, background));
093 println();
094 }
095
096 public final void println(Object obj, Decoration decoration, Color foreground, Color background) {
097 print(obj, Style.style(decoration, foreground, background));
098 println();
099 }
100
101 public final void print(Object obj, Style style) {
102 out.append(style);
103 print(obj);
104 out.append(Style.reset);
105 }
106
107 public final void println(Object obj, Style style) {
108 print(obj, style);
109 println();
110 }
111
112 /**
113 * Groovy left shift operator.
114 *
115 * @param o the appended
116 * @return this object
117 */
118 public final ShellPrintWriter leftShift(Object o) {
119 if (o instanceof Style) {
120 out.append((Style)o);
121 } else if (o instanceof Decoration) {
122 out.append(((Decoration)o).style);
123 } else if (o instanceof Color) {
124 out.append(((Color)o).style);
125 } else {
126 print(o);
127 }
128 return this;
129 }
130 }