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
020 package org.crsh.text;
021
022 import org.crsh.Pipe;
023
024 import java.io.IOException;
025 import java.util.LinkedList;
026
027 public class RenderAppendable implements Appendable, Pipe<Chunk> {
028
029 /** . */
030 private final RenderingContext context;
031
032 /** . */
033 private LinkedList<Style.Composite> stack;
034
035 public RenderAppendable(RenderingContext context) {
036 this.context = context;
037 }
038
039 private void safeAppend(Chunk chunk) {
040 try {
041 context.provide(chunk);
042 }
043 catch (java.io.IOException e) {
044 // e.printStackTrace();
045 }
046 }
047
048 public int getWidth() {
049 return context.getWidth();
050 }
051
052 public void provide(Chunk element) throws IOException {
053 context.provide(element);
054 }
055
056 public void flush() throws IOException {
057 context.flush();
058 }
059
060 public RenderAppendable append(CharSequence csq) {
061 safeAppend(new Text(csq));
062 return this;
063 }
064
065 public void enterStyle(Style.Composite style) {
066 if (stack == null) {
067 stack = new LinkedList<Style.Composite>();
068 }
069 safeAppend(style);
070 stack.addLast(style);
071 }
072
073 public Style.Composite leaveStyle() {
074 if (stack == null || stack.isEmpty()) {
075 throw new IllegalStateException("Cannot leave non existing style");
076 }
077 Style.Composite last = stack.removeLast();
078 if (stack.size() > 0) {
079
080 // Compute merged
081 Style.Composite merged = getMerged();
082
083 // Compute diff with removed
084 Boolean bold = foo(last.getBold(), merged.getBold());
085 Boolean underline = foo(last.getUnderline(), merged.getUnderline());
086 Boolean blink = foo(last.getBlink(), merged.getBlink());
087
088 // For now we assume that black is the default background color
089 // and white is the default foreground color
090 Color fg = foo(last.getForeground(), merged.getForeground(), Color.def);
091 Color bg = foo(last.getBackground(), merged.getBackground(), Color.def);
092
093 //
094 Style.Composite bilto = Style.style(bold, underline, blink, fg, bg);
095
096 //
097 safeAppend(bilto);
098 } else {
099 safeAppend(Style.reset);
100 }
101 return last;
102 }
103
104 /**
105 * Compute the current merged style.
106 *
107 * @return the merged style
108 */
109 private Style.Composite getMerged() {
110 Style.Composite merged = Style.style();
111 for (Style s : stack) {
112 merged = (Style.Composite)merged.merge(s);
113 }
114 return merged;
115 }
116
117 private Boolean foo(Boolean last, Boolean merged) {
118 if (last != null) {
119 if (merged != null) {
120 return merged;
121 } else {
122 return !last;
123 }
124 } else {
125 return null;
126 }
127 }
128
129 private Color foo(Color last, Color merged, Color def) {
130 if (last != null) {
131 if (merged != null) {
132 return merged;
133 } else {
134 return def;
135 }
136 } else {
137 return null;
138 }
139 }
140
141 public void styleOff() {
142 if (stack != null && stack.size() > 0) {
143 safeAppend(Style.reset);
144 }
145 }
146
147 public void styleOn() {
148 if (stack != null && stack.size() > 0) {
149 safeAppend(getMerged());
150 }
151 }
152
153 public RenderAppendable append(CharSequence csq, int start, int end) {
154 safeAppend(new Text(csq.subSequence(start, end)));
155 return this;
156 }
157
158 public RenderAppendable append(char c) {
159 safeAppend(new Text(Character.toString(c)));
160 return this;
161 }
162 }