001: /*
002: * SSHTools - Java SSH2 API
003: *
004: * Copyright (C) 2002-2003 Lee David Painter and Contributors.
005: *
006: * Contributions made by:
007: *
008: * Brett Smith
009: * Richard Pernavas
010: * Erwin Bolwidt
011: *
012: * This program is free software; you can redistribute it and/or
013: * modify it under the terms of the GNU General Public License
014: * as published by the Free Software Foundation; either version 2
015: * of the License, or (at your option) any later version.
016: *
017: * This program is distributed in the hope that it will be useful,
018: * but WITHOUT ANY WARRANTY; without even the implied warranty of
019: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
020: * GNU General Public License for more details.
021: *
022: * You should have received a copy of the GNU General Public License
023: * along with this program; if not, write to the Free Software
024: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
025: */
026: /**
027: * SSHTools - Java SSH API The contents of this package has been derived from
028: * the TelnetD library available from http://sourceforge.net/projects/telnetd
029: * The original license of the source code is as follows: TelnetD library
030: * (embeddable telnet daemon) Copyright (C) 2000 Dieter Wimberger This library
031: * is free software; you can either redistribute it and/or modify it under the
032: * terms of the GNU Lesser General Public License version 2.1,1999 as
033: * published by the Free Software Foundation (see copy received along with the
034: * library), or under the terms of the BSD-style license received along with
035: * this library.
036: */package com.sshtools.daemon.terminal;
037:
038: /**
039: *
040: *
041: * @author $author$
042: * @version $Revision: 1.13 $
043: */
044: public final class Colorizer {
045: private static Object Self; //Singleton instance reference
046: private static int testcount = 0;
047: private static Colorizer myColorizer;
048:
049: //Constants
050: private static final int
051: /*black*/S = 30;
052:
053: //Constants
054: private static final int s = 40;
055:
056: //Constants
057: private static final int
058: /*red*/R = 31;
059:
060: //Constants
061: private static final int r = 41;
062:
063: //Constants
064: private static final int
065: /*green*/G = 32;
066:
067: //Constants
068: private static final int g = 42;
069:
070: //Constants
071: private static final int
072: /*yellow*/Y = 33;
073:
074: //Constants
075: private static final int y = 43;
076:
077: //Constants
078: private static final int
079: /*blue*/B = 34;
080:
081: //Constants
082: private static final int b = 44;
083:
084: //Constants
085: private static final int
086: /*magenta*/M = 35;
087:
088: //Constants
089: private static final int m = 45;
090:
091: //Constants
092: private static final int
093: /*cyan*/C = 36;
094:
095: //Constants
096: private static final int c = 46;
097:
098: //Constants
099: private static final int
100: /*white*/W = 37;
101:
102: //Constants
103: private static final int w = 47;
104:
105: //Constants
106: private static final int
107: /*bold*/f = 1;
108:
109: //Constants
110: private static final int
111: /*!bold*/d = 22;
112:
113: //Constants
114: private static final int
115: /*italic*/i = 3;
116:
117: //Constants
118: private static final int
119: /*!italic*/j = 23;
120:
121: //Constants
122: private static final int
123: /*underlined*/u = 4;
124:
125: //Constants
126: private static final int
127: /*!underlined*/v = 24;
128:
129: //Constants
130: private static final int
131: /*blink*/e = 5;
132:
133: //Constants
134: private static final int
135: /*steady*/n = 25;
136:
137: //Constants
138: private static final int
139: /*hide*/h = 8;
140:
141: //Constants
142: private static final int
143: /*all out*/a = 0;
144: private int[] colortranslation; //translation table
145: private int leng;
146:
147: private Colorizer() {
148: colortranslation = new int[128];
149: colortranslation[83] = S;
150: colortranslation[82] = R;
151: colortranslation[71] = G;
152: colortranslation[89] = Y;
153: colortranslation[66] = B;
154: colortranslation[77] = M;
155: colortranslation[67] = C;
156: colortranslation[87] = W;
157: colortranslation[115] = s;
158: colortranslation[114] = r;
159: colortranslation[103] = g;
160: colortranslation[121] = y;
161: colortranslation[98] = b;
162: colortranslation[109] = m;
163: colortranslation[99] = c;
164: colortranslation[119] = w;
165: colortranslation[102] = f;
166: colortranslation[100] = d;
167: colortranslation[105] = i;
168: colortranslation[106] = j;
169: colortranslation[117] = u;
170: colortranslation[118] = v;
171: colortranslation[101] = e;
172: colortranslation[110] = n;
173: colortranslation[104] = h;
174: colortranslation[97] = a;
175: Self = this ;
176: }
177:
178: //constructor
179: public String colorize(String str, boolean support) {
180: StringBuffer out = new StringBuffer(str.length() + 20);
181: int parsecursor = 0;
182: int foundcursor = 0;
183: boolean done = false;
184:
185: while (!done) {
186: foundcursor = str.indexOf(ColorHelper.MARKER_CODE,
187: parsecursor);
188:
189: if (foundcursor != -1) {
190: out.append(str.substring(parsecursor, foundcursor));
191:
192: if (support) {
193: out.append(addEscapeSequence(str.substring(
194: foundcursor + 1, foundcursor + 2)));
195: }
196:
197: parsecursor = foundcursor + 2;
198: } else {
199: out.append(str.substring(parsecursor, str.length()));
200: done = true;
201: }
202: }
203:
204: /*
205: * This will always add a "reset all" escape sequence
206: * behind the input string.
207: * Basically this is a good idea, because developers tend to
208: * forget writing colored strings properly.
209: */
210: if (support) {
211: out.append(addEscapeSequence("a"));
212: }
213:
214: return out.toString();
215: }
216:
217: //colorize
218: private String addEscapeSequence(String attribute) {
219: StringBuffer tmpbuf = new StringBuffer(10);
220: byte[] tmpbytes = attribute.getBytes();
221: int key = (int) tmpbytes[0];
222: tmpbuf.append((char) 27);
223: tmpbuf.append((char) 91);
224: tmpbuf.append((new Integer(colortranslation[key])).toString());
225: tmpbuf.append((char) 109);
226:
227: return tmpbuf.toString();
228: }
229:
230: //addEscapeSequence
231: public static Colorizer getReference() {
232: if (Self != null) {
233: return (Colorizer) Self;
234: } else {
235: return new Colorizer();
236: }
237: }
238:
239: //getReference
240: private static void announceResult(boolean res) {
241: if (res) {
242: System.out.println("[#" + testcount + "] ok.");
243: } else {
244: System.out.println("[#" + testcount
245: + "] failed (see possible StackTrace).");
246: }
247: }
248:
249: //announceResult
250: private static void announceTest(String what) {
251: testcount++;
252: System.out.println("Test #" + testcount + " [" + what + "]:");
253: }
254:
255: //announceTest
256: private static void bfcolorTest(String color) {
257: System.out.println("->"
258: + myColorizer.colorize(ColorHelper.boldcolorizeText(
259: "COLOR", color), true) + "<-");
260: }
261:
262: //bfcolorTest
263: private static void fcolorTest(String color) {
264: System.out.println("->"
265: + myColorizer.colorize(ColorHelper.colorizeText(
266: "COLOR", color), true) + "<-");
267: }
268:
269: //fcolorTest
270: private static void bcolorTest(String color) {
271: System.out.println("->"
272: + myColorizer.colorize(ColorHelper.colorizeBackground(
273: " ", color), true) + "<-");
274: }
275:
276: //bcolorTest
277: public static void main(String[] args) {
278: try {
279: announceTest("Instantiation");
280: myColorizer = Colorizer.getReference();
281: announceResult(true);
282: announceTest("Textcolor Tests");
283: fcolorTest(ColorHelper.BLACK);
284: fcolorTest(ColorHelper.RED);
285: fcolorTest(ColorHelper.GREEN);
286: fcolorTest(ColorHelper.YELLOW);
287: fcolorTest(ColorHelper.BLUE);
288: fcolorTest(ColorHelper.MAGENTA);
289: fcolorTest(ColorHelper.CYAN);
290: fcolorTest(ColorHelper.white);
291: announceResult(true);
292: announceTest("Bold textcolor Tests");
293: bfcolorTest(ColorHelper.BLACK);
294: bfcolorTest(ColorHelper.RED);
295: bfcolorTest(ColorHelper.GREEN);
296: bfcolorTest(ColorHelper.YELLOW);
297: bfcolorTest(ColorHelper.BLUE);
298: bfcolorTest(ColorHelper.MAGENTA);
299: bfcolorTest(ColorHelper.CYAN);
300: bfcolorTest(ColorHelper.white);
301: announceResult(true);
302: announceTest("Background Tests");
303: bcolorTest(ColorHelper.BLACK);
304: bcolorTest(ColorHelper.RED);
305: bcolorTest(ColorHelper.GREEN);
306: bcolorTest(ColorHelper.YELLOW);
307: bcolorTest(ColorHelper.BLUE);
308: bcolorTest(ColorHelper.MAGENTA);
309: bcolorTest(ColorHelper.CYAN);
310: bcolorTest(ColorHelper.white);
311: announceResult(true);
312: announceTest("Mixed Color Tests");
313: System.out.println("->"
314: + myColorizer.colorize(ColorHelper.colorizeText(
315: "COLOR", ColorHelper.white,
316: ColorHelper.BLUE), true) + "<-");
317: System.out.println("->"
318: + myColorizer.colorize(ColorHelper.colorizeText(
319: "COLOR", ColorHelper.YELLOW,
320: ColorHelper.GREEN), true) + "<-");
321: System.out.println("->"
322: + myColorizer.colorize(
323: ColorHelper
324: .boldcolorizeText("COLOR",
325: ColorHelper.white,
326: ColorHelper.BLUE), true)
327: + "<-");
328: System.out.println("->"
329: + myColorizer.colorize(ColorHelper
330: .boldcolorizeText("COLOR",
331: ColorHelper.YELLOW,
332: ColorHelper.GREEN), true) + "<-");
333: announceResult(true);
334: announceTest("Style Tests");
335: System.out.println("->"
336: + myColorizer.colorize(
337: ColorHelper.boldText("Bold"), true) + "<-");
338: System.out.println("->"
339: + myColorizer.colorize(ColorHelper
340: .italicText("Italic"), true) + "<-");
341: System.out
342: .println("->"
343: + myColorizer
344: .colorize(
345: ColorHelper
346: .underlinedText("Underlined"),
347: true) + "<-");
348: System.out.println("->"
349: + myColorizer.colorize(ColorHelper
350: .blinkingText("Blinking"), true) + "<-");
351: announceResult(true);
352: announceTest("Visible length test");
353:
354: String colorized = ColorHelper.boldcolorizeText("STRING",
355: ColorHelper.YELLOW);
356: System.out.println("->"
357: + myColorizer.colorize(colorized, true) + "<-");
358: System.out.println("Visible length="
359: + ColorHelper.getVisibleLength(colorized));
360: colorized = ColorHelper.boldcolorizeText("BANNER",
361: ColorHelper.white, ColorHelper.BLUE)
362: + ColorHelper.colorizeText("COLOR",
363: ColorHelper.white, ColorHelper.BLUE)
364: + ColorHelper.underlinedText("UNDER");
365: System.out.println("->"
366: + myColorizer.colorize(colorized, true) + "<-");
367: System.out.println("Visible length="
368: + ColorHelper.getVisibleLength(colorized));
369: announceResult(true);
370:
371: if (false) {
372: throw new Exception(); //this will shut up jikes
373: }
374: } catch (Exception e) {
375: announceResult(false);
376: e.printStackTrace();
377: }
378: }
379:
380: //main (test routine)
381: }
382:
383: //class Colorizer
|