KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > continuent > sequoia > console > text > ColorPrinter


1 /**
2  * Sequoia: Database clustering technology.
3  * Copyright (C) 2002-2004 French National Institute For Research In Computer
4  * Science And Control (INRIA).
5  * Contact: sequoia@continuent.org
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  * Initial developer(s): Nicolas Modrzyk.
20  * Contributor(s): Mathieu Peltier.
21  */

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

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

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

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

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

88   /**
89    * Standard color
90    */

91   public static final int STD = 0;
92   /**
93    * Error color
94    */

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

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

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

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

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

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

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