KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > cjdbc > console > text > ColorPrinter


1 /**
2  * C-JDBC: Clustered JDBC.
3  * Copyright (C) 2002-2004 French National Institute For Research In Computer
4  * Science And Control (INRIA).
5  * Contact: c-jdbc@objectweb.org
6  *
7  * This library is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as published by the
9  * Free Software Foundation; either version 2.1 of the License, or any later
10  * version.
11  *
12  * This library is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
15  * for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this library; if not, write to the Free Software Foundation,
19  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
20  *
21  * Initial developer(s): Nicolas Modrzyk.
22  * Contributor(s): Mathieu Peltier.
23  */

24
25 package org.objectweb.cjdbc.console.text;
26
27 import java.io.PrintStream JavaDoc;
28
29 /**
30  * This class defines a ColorPrinter
31  *
32  * @author <a HREF="mailto:Nicolas.Modrzyk@inrialpes.fr">Nicolas Modrzyk </a>
33  * @author <a HREF="mailto:mathieu.peltier@emicnetworks.com">Mathieu Peltier
34  * </a>*
35  * @version 1.0
36  */

37 public class ColorPrinter
38 {
39   // private static final int ATTR_NORMAL = 0;
40
private static final int ATTR_BRIGHT = 1;
41   private static final int ATTR_DIM = 2;
42   // private static final int ATTR_UNDERLINE = 3;
43
// private static final int ATTR_BLINK = 5;
44
// private static final int ATTR_REVERSE = 7;
45
// private static final int ATTR_HIDDEN = 8;
46

47   private static final int FG_BLACK = 30;
48   private static final int FG_RED = 31;
49   private static final int FG_GREEN = 32;
50   // private static final int FG_YELLOW = 33;
51
private static final int FG_BLUE = 34;
52   private static final int FG_MAGENTA = 35;
53   // private static final int FG_CYAN = 36;
54
// private static final int FG_WHITE = 37;
55

56   // private static final int BG_BLACK = 40;
57
// private static final int BG_RED = 41;
58
// private static final int BG_GREEN = 42;
59
// private static final int BG_YELLOW = 44;
60
// private static final int BG_BLUE = 44;
61
// private static final int BG_MAGENTA = 45;
62
// private static final int BG_CYAN = 46;
63
// private static final int BG_WHITE = 47;
64

65   private static final String JavaDoc PREFIX = "\u001b[";
66   private static final String JavaDoc SUFFIX = "m";
67   private static final char SEPARATOR = ';';
68   private static final String JavaDoc END_COLOR = PREFIX + SUFFIX;
69
70   private static final String JavaDoc STD_COLOR = PREFIX + ATTR_DIM + SEPARATOR
71                                                + FG_BLACK + SUFFIX;
72   private static final String JavaDoc ERR_COLOR = PREFIX + ATTR_BRIGHT + SEPARATOR
73                                                + FG_RED + SUFFIX;
74   // private static final String verboseColor = PREFIX + ATTR_BRIGHT + SEPARATOR
75
// + FG_BLACK + SUFFIX;
76
private static final String JavaDoc INFO_COLOR = PREFIX + ATTR_BRIGHT + SEPARATOR
77                                                + FG_GREEN + SUFFIX;
78   private static final String JavaDoc STATUS_COLOR = PREFIX + ATTR_DIM + SEPARATOR
79                                                + FG_MAGENTA + SUFFIX;
80   private static final String JavaDoc PROMPT_COLOR = PREFIX + ATTR_BRIGHT + SEPARATOR
81                                                + FG_BLUE + SUFFIX;
82
83   // private static final String warnColor = PREFIX + ATTR_DIM + SEPARATOR
84
// + FG_MAGENTA + SUFFIX;
85
// private static final String INFO_COLOR = PREFIX + ATTR_DIM + SEPARATOR
86
// + FG_CYAN + SUFFIX;
87
// private static final String debugColor = PREFIX + ATTR_DIM + SEPARATOR
88
// + FG_BLUE + SUFFIX;
89

90   /**
91    * Standard color
92    */

93   public static final int STD = 0;
94   /**
95    * Error color
96    */

97   public static final int ERROR = 1;
98   /**
99    * Info color for commands
100    */

101   public static final int INFO = 2;
102   /**
103    * status color for commands
104    */

105   public static final int STATUS = 3;
106   /**
107    * status color for commands
108    */

109   public static final int PROMPT = 4;
110
111   /**
112    * Print a message in color
113    *
114    * @param message Message to print
115    * @param stream Stream where to send the message
116    * @param color Color for the message
117    */

118   public static final void printMessage(final String JavaDoc message,
119       final PrintStream JavaDoc stream, final int color)
120   {
121     printMessage(message, stream, color, true);
122   }
123
124   /**
125    * Print a message in color
126    *
127    * @param message Message to print
128    * @param stream Stream where to send the message
129    * @param color Color for the message
130    * @param endline true if a carriage return should be appended to the message
131    */

132   public static final void printMessage(final String JavaDoc message,
133       final PrintStream JavaDoc stream, final int color, boolean endline)
134   {
135     final String JavaDoc strmessage = ColorPrinter.getColoredMessage(message, color);
136     if (endline)
137       stream.println(strmessage);
138     else
139       stream.print(strmessage);
140   }
141
142   /**
143    * Get colored message.
144    *
145    * @param message message to print
146    * @param color color for the message
147    * @return Message with color tags
148    */

149   public static final String JavaDoc getColoredMessage(final String JavaDoc message,
150       final int color)
151   {
152     final StringBuffer JavaDoc msg = new StringBuffer JavaDoc(message);
153     switch (color)
154     {
155       case STD :
156         msg.insert(0, STD_COLOR);
157         msg.append(END_COLOR);
158         break;
159       case ERROR :
160         msg.insert(0, ERR_COLOR);
161         msg.append(END_COLOR);
162         break;
163       case INFO :
164         msg.insert(0, INFO_COLOR);
165         msg.append(END_COLOR);
166         break;
167       case STATUS :
168         msg.insert(0, STATUS_COLOR);
169         msg.append(END_COLOR);
170         break;
171       case PROMPT :
172         msg.insert(0, PROMPT_COLOR);
173         msg.append(END_COLOR);
174         break;
175       default : // Use STD as default
176
msg.insert(0, STD_COLOR);
177         msg.append(END_COLOR);
178         break;
179     }
180     return msg.toString();
181   }
182 }
183
Popular Tags