1 24 25 package org.objectweb.cjdbc.console.text; 26 27 import java.io.PrintStream ; 28 29 37 public class ColorPrinter 38 { 39 private static final int ATTR_BRIGHT = 1; 41 private static final int ATTR_DIM = 2; 42 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_BLUE = 34; 52 private static final int FG_MAGENTA = 35; 53 56 65 private static final String PREFIX = "\u001b["; 66 private static final String SUFFIX = "m"; 67 private static final char SEPARATOR = ';'; 68 private static final String END_COLOR = PREFIX + SUFFIX; 69 70 private static final String STD_COLOR = PREFIX + ATTR_DIM + SEPARATOR 71 + FG_BLACK + SUFFIX; 72 private static final String ERR_COLOR = PREFIX + ATTR_BRIGHT + SEPARATOR 73 + FG_RED + SUFFIX; 74 private static final String INFO_COLOR = PREFIX + ATTR_BRIGHT + SEPARATOR 77 + FG_GREEN + SUFFIX; 78 private static final String STATUS_COLOR = PREFIX + ATTR_DIM + SEPARATOR 79 + FG_MAGENTA + SUFFIX; 80 private static final String PROMPT_COLOR = PREFIX + ATTR_BRIGHT + SEPARATOR 81 + FG_BLUE + SUFFIX; 82 83 90 93 public static final int STD = 0; 94 97 public static final int ERROR = 1; 98 101 public static final int INFO = 2; 102 105 public static final int STATUS = 3; 106 109 public static final int PROMPT = 4; 110 111 118 public static final void printMessage(final String message, 119 final PrintStream stream, final int color) 120 { 121 printMessage(message, stream, color, true); 122 } 123 124 132 public static final void printMessage(final String message, 133 final PrintStream stream, final int color, boolean endline) 134 { 135 final String strmessage = ColorPrinter.getColoredMessage(message, color); 136 if (endline) 137 stream.println(strmessage); 138 else 139 stream.print(strmessage); 140 } 141 142 149 public static final String getColoredMessage(final String message, 150 final int color) 151 { 152 final StringBuffer msg = new StringBuffer (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 : msg.insert(0, STD_COLOR); 177 msg.append(END_COLOR); 178 break; 179 } 180 return msg.toString(); 181 } 182 } 183 | Popular Tags |