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.12 $
043: */
044: public class ColorHelper {
045: /** */
046: public static final String INTERNAL_MARKER = "\001";
047:
048: /** */
049: public static final int MARKER_CODE = 1;
050:
051: /** */
052: public static final String BLACK = "S";
053:
054: /** */
055: public static final String RED = "R";
056:
057: /** */
058: public static final String GREEN = "G";
059:
060: /** */
061: public static final String YELLOW = "Y";
062:
063: /** */
064: public static final String BLUE = "B";
065:
066: /** */
067: public static final String MAGENTA = "M";
068:
069: /** */
070: public static final String CYAN = "C";
071:
072: /** */
073: public static final String white = "W";
074:
075: /** */
076: public static final String BOLD = "f";
077:
078: /** */
079: public static final String BOLD_OFF = "d"; //normal color or normal intensity
080:
081: /** */
082: public static final String ITALIC = "i";
083:
084: /** */
085: public static final String ITALIC_OFF = "j";
086:
087: /** */
088: public static final String UNDERLINED = "u";
089:
090: /** */
091: public static final String UNDERLINED_OFF = "v";
092:
093: /** */
094: public static final String BLINK = "e";
095:
096: /** */
097: public static final String BLINK_OFF = "n";
098:
099: /** */
100: public static final String RESET_ALL = "a";
101:
102: /**
103: *
104: *
105: * @param str
106: * @param color
107: *
108: * @return
109: */
110: public static String colorizeText(String str, String color) {
111: return INTERNAL_MARKER + color + str + INTERNAL_MARKER
112: + RESET_ALL;
113: }
114:
115: //colorizeText
116: public static String colorizeBackground(String str, String color) {
117: return INTERNAL_MARKER + color.toLowerCase() + str
118: + INTERNAL_MARKER + RESET_ALL;
119: }
120:
121: //colorizeBackground
122: public static String colorizeText(String str, String fgc, String bgc) {
123: return INTERNAL_MARKER + fgc + INTERNAL_MARKER
124: + bgc.toLowerCase() + str + INTERNAL_MARKER + RESET_ALL;
125: }
126:
127: //colorizeText
128: public static String boldcolorizeText(String str, String color) {
129: return INTERNAL_MARKER + BOLD + INTERNAL_MARKER + color + str
130: + INTERNAL_MARKER + RESET_ALL;
131: }
132:
133: //colorizeBoldText
134: public static String boldcolorizeText(String str, String fgc,
135: String bgc) {
136: return INTERNAL_MARKER + BOLD + INTERNAL_MARKER + fgc
137: + INTERNAL_MARKER + bgc.toLowerCase() + str
138: + INTERNAL_MARKER + RESET_ALL;
139: }
140:
141: //colorizeBoldText
142: public static String boldText(String str) {
143: return INTERNAL_MARKER + BOLD + str + INTERNAL_MARKER
144: + BOLD_OFF;
145: }
146:
147: //boldText
148: public static String italicText(String str) {
149: return INTERNAL_MARKER + ITALIC + str + INTERNAL_MARKER
150: + ITALIC_OFF;
151: }
152:
153: //italicText
154: public static String underlinedText(String str) {
155: return INTERNAL_MARKER + UNDERLINED + str + INTERNAL_MARKER
156: + UNDERLINED_OFF;
157: }
158:
159: //underlinedText
160: public static String blinkingText(String str) {
161: return INTERNAL_MARKER + BLINK + str + INTERNAL_MARKER
162: + BLINK_OFF;
163: }
164:
165: //blinkingText
166: public static long getVisibleLength(String str) {
167: int counter = 0;
168: int parsecursor = 0;
169: int foundcursor = 0;
170: boolean done = false;
171:
172: while (!done) {
173: foundcursor = str.indexOf(MARKER_CODE, parsecursor);
174:
175: if (foundcursor != -1) {
176: //increment counter
177: counter++;
178:
179: //parseon from the next char
180: parsecursor = foundcursor + 1;
181: } else {
182: done = true;
183: }
184: }
185:
186: return (str.length() - (counter * 2));
187: }
188:
189: //getVisibleLength
190: }
191:
192: //class ColorHelper
|