001/* 002 * Copyright (C) 2012 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 020package org.crsh.text; 021 022import org.crsh.shell.ScreenContext; 023import org.crsh.text.ui.Element; 024 025import java.io.Closeable; 026import java.io.IOException; 027import java.io.InterruptedIOException; 028import java.io.PrintWriter; 029 030public class RenderPrintWriter extends PrintWriter { 031 032 /** . */ 033 private final RenderWriter out; 034 035 public RenderPrintWriter(ScreenContext out) { 036 super(new RenderWriter(out)); 037 038 // 039 this.out = (RenderWriter)super.out; 040 } 041 042 public RenderPrintWriter(ScreenContext out, Closeable closeable) { 043 super(new RenderWriter(out, closeable)); 044 045 // 046 this.out = (RenderWriter)super.out; 047 } 048 049 public final boolean isEmpty() { 050 return out.isEmpty(); 051 } 052 053 public final void print(Object obj, Color foreground) { 054 try { 055 out.provide(Style.style(foreground)); 056 } 057 catch (InterruptedIOException x) { 058 Thread.currentThread().interrupt(); 059 } 060 catch (IOException x) { 061 setError(); 062 } 063 print(obj); 064 try { 065 out.provide(Style.reset); 066 } 067 catch (InterruptedIOException x) { 068 Thread.currentThread().interrupt(); 069 } 070 catch (IOException x) { 071 setError(); 072 } 073 } 074 075 public final void println(Object obj, Color foreground) { 076 print(obj, Style.style(foreground)); 077 println(); 078 } 079 080 public final void print(Object obj, Color foreground, Color background) { 081 try { 082 out.provide(Style.style(foreground, background)); 083 } 084 catch (InterruptedIOException x) { 085 Thread.currentThread().interrupt(); 086 } 087 catch (IOException x) { 088 setError(); 089 } 090 print(obj); 091 try { 092 out.provide(Style.reset); 093 } 094 catch (InterruptedIOException x) { 095 Thread.currentThread().interrupt(); 096 } 097 catch (IOException x) { 098 setError(); 099 } 100 } 101 102 public final void println(Object obj, Color foreground, Color background) { 103 print(obj, Style.style(foreground, background)); 104 println(); 105 } 106 107 public final void print(Object obj, Decoration decoration) { 108 try { 109 out.provide(Style.style(decoration)); 110 } 111 catch (InterruptedIOException x) { 112 Thread.currentThread().interrupt(); 113 } 114 catch (IOException x) { 115 setError(); 116 } 117 print(obj); 118 try { 119 out.provide(Style.reset); 120 } 121 catch (InterruptedIOException x) { 122 Thread.currentThread().interrupt(); 123 } 124 catch (IOException x) { 125 setError(); 126 } 127 } 128 129 public final void println(Object obj, Decoration decoration) { 130 print(obj, Style.style(decoration)); 131 println(); 132 } 133 134 public final void print(Object obj, Decoration decoration, Color foreground) { 135 print(obj, Style.style(decoration, foreground)); 136 println(); 137 } 138 139 public final void println(Object obj, Decoration decoration, Color foreground) { 140 print(obj, Style.style(decoration, foreground, null)); 141 println(); 142 } 143 144 public final void print(Object obj, Decoration decoration, Color foreground, Color background) { 145 print(obj, Style.style(decoration, foreground, background)); 146 println(); 147 } 148 149 public final void println(Object obj, Decoration decoration, Color foreground, Color background) { 150 print(obj, Style.style(decoration, foreground, background)); 151 println(); 152 } 153 154 public final void print(Object obj, Style style) { 155 try { 156 out.provide(style); 157 } 158 catch (InterruptedIOException x) { 159 Thread.currentThread().interrupt(); 160 } 161 catch (IOException x) { 162 setError(); 163 } 164 print(obj); 165 try { 166 out.provide(Style.reset); 167 } 168 catch (InterruptedIOException x) { 169 Thread.currentThread().interrupt(); 170 } 171 catch (IOException x) { 172 setError(); 173 } 174 } 175 176 public final void println(Object obj, Style style) { 177 print(obj, style); 178 println(); 179 } 180 181 /** 182 * Groovy left shift operator. 183 * 184 * @param o the appended 185 * @return this object 186 */ 187 public final RenderPrintWriter leftShift(Object o) { 188 if (o instanceof Style) { 189 try { 190 out.provide((Style)o); 191 } 192 catch (InterruptedIOException x) { 193 Thread.currentThread().interrupt(); 194 } 195 catch (IOException x) { 196 setError(); 197 } 198 } else if (o instanceof Decoration) { 199 try { 200 out.provide((Style.style((Decoration)o))); 201 } 202 catch (InterruptedIOException x) { 203 Thread.currentThread().interrupt(); 204 } 205 catch (IOException x) { 206 setError(); 207 } 208 } else if (o instanceof Color) { 209 try { 210 out.provide(Style.style((Color)o)); 211 } 212 catch (InterruptedIOException x) { 213 Thread.currentThread().interrupt(); 214 } 215 catch (IOException x) { 216 setError(); 217 } 218 } else { 219 print(o); 220 } 221 return this; 222 } 223 224 public final RenderPrintWriter cls() { 225 try { 226 out.provide(CLS.INSTANCE); 227 } 228 catch (InterruptedIOException x) { 229 Thread.currentThread().interrupt(); 230 } 231 catch (IOException x) { 232 setError(); 233 } 234 return this; 235 } 236 237 @Override 238 public void println(Object x) { 239 print(x); 240 println(); 241 } 242 243 public void show(Element element) { 244 element.render(new RenderAppendable(this.out)); 245 } 246 247 @Override 248 public void print(Object obj) { 249 if (obj instanceof Element) { 250 RenderAppendable out = new RenderAppendable(this.out); 251 ((Element)obj).renderer().render(out); 252 } else { 253 super.print(obj); 254 } 255 } 256}