1 19 package jline; 20 21 import java.io.*; 22 import java.util.*; 23 24 25 30 public class ANSIBuffer 31 { 32 private boolean ansiEnabled = true; 33 private final StringBuffer ansiBuffer = new StringBuffer (); 34 private final StringBuffer plainBuffer = new StringBuffer (); 35 36 37 public ANSIBuffer () 38 { 39 } 40 41 42 public ANSIBuffer (String str) 43 { 44 append (str); 45 } 46 47 48 public void setAnsiEnabled (boolean ansi) 49 { 50 this.ansiEnabled = ansiEnabled; 51 } 52 53 54 public boolean getAnsiEnabled () 55 { 56 return this.ansiEnabled; 57 } 58 59 60 public String getAnsiBuffer () 61 { 62 return ansiBuffer.toString (); 63 } 64 65 66 public String getPlainBuffer () 67 { 68 return plainBuffer.toString (); 69 } 70 71 72 public String toString (boolean ansi) 73 { 74 return ansi ? getAnsiBuffer () : getPlainBuffer (); 75 } 76 77 78 public String toString () 79 { 80 return toString (ansiEnabled); 81 } 82 83 84 public ANSIBuffer append (String str) 85 { 86 ansiBuffer.append (str); 87 plainBuffer.append (str); 88 return this; 89 } 90 91 92 public ANSIBuffer attrib (String str, int code) 93 { 94 ansiBuffer.append (ANSICodes.attrib (code)) 95 .append (str) 96 .append (ANSICodes.attrib (ANSICodes.OFF)); 97 plainBuffer.append (str); 98 99 return this; 100 } 101 102 103 public ANSIBuffer red (String str) 104 { 105 return attrib (str, ANSICodes.FG_RED); 106 } 107 108 109 public ANSIBuffer blue (String str) 110 { 111 return attrib (str, ANSICodes.FG_BLUE); 112 } 113 114 115 public ANSIBuffer green (String str) 116 { 117 return attrib (str, ANSICodes.FG_GREEN); 118 } 119 120 121 public ANSIBuffer black (String str) 122 { 123 return attrib (str, ANSICodes.FG_BLACK); 124 } 125 126 127 public ANSIBuffer yellow (String str) 128 { 129 return attrib (str, ANSICodes.FG_YELLOW); 130 } 131 132 133 public ANSIBuffer magenta (String str) 134 { 135 return attrib (str, ANSICodes.FG_MAGENTA); 136 } 137 138 139 public ANSIBuffer cyan (String str) 140 { 141 return attrib (str, ANSICodes.FG_CYAN); 142 } 143 144 145 public ANSIBuffer bold (String str) 146 { 147 return attrib (str, ANSICodes.BOLD); 148 } 149 150 151 public ANSIBuffer underscore (String str) 152 { 153 return attrib (str, ANSICodes.UNDERSCORE); 154 } 155 156 157 public ANSIBuffer blink (String str) 158 { 159 return attrib (str, ANSICodes.BLINK); 160 } 161 162 163 public ANSIBuffer reverse (String str) 164 { 165 return attrib (str, ANSICodes.REVERSE); 166 } 167 168 169 170 public static class ANSICodes 171 { 172 public static int OFF = 0; 173 public static int BOLD = 1; 174 public static int UNDERSCORE = 4; 175 public static int BLINK = 5; 176 public static int REVERSE = 7; 177 public static int CONCEALED = 8; 178 179 public static int FG_BLACK = 30; 180 public static int FG_RED = 31; 181 public static int FG_GREEN = 32; 182 public static int FG_YELLOW = 33; 183 public static int FG_BLUE = 34; 184 public static int FG_MAGENTA = 35; 185 public static int FG_CYAN = 36; 186 public static int FG_WHITE = 37; 187 public static char ESC = 27; 189 190 213 public static String setmode (int mode) 214 { 215 return ESC + "[=" + mode + "h"; 216 } 217 218 223 public static String resetmode (int mode) 224 { 225 return ESC + "[=" + mode + "l"; 226 } 227 228 231 public static String clrscr () 232 { 233 return ESC + "[2J"; 234 } 235 236 240 public static String clreol () 241 { 242 return ESC + "[K"; 243 } 244 245 250 public static String left (int n) 251 { 252 return ESC + "[" + n + "D"; 253 } 254 255 260 public static String right (int n) 261 { 262 return ESC + "[" + n + "C"; 263 } 264 265 270 public static String up (int n) 271 { 272 return ESC + "[" + n + "A"; 273 } 274 275 279 public static String down (int n) 280 { 281 return ESC + "[" + n + "B"; 282 } 283 284 289 public static String gotoxy (int row, int column) 290 { 291 return ESC + "[" + row + ";" + column + "H"; 292 } 293 294 297 public static String save () 298 { 299 return ESC + "[s"; 300 } 301 302 305 public static String restore () 306 { 307 return ESC + "[u"; 308 } 309 310 346 public static String attrib (int attr) 347 { 348 return ESC + "[" + attr + "m"; 349 } 350 351 454 public static String setkey (String code, String value) 455 { 456 return ESC + "[" + code + ";" + value + "p"; 457 } 458 } 459 460 461 public static void main (String [] args) 462 throws Exception 463 { 464 BufferedReader reader = new BufferedReader ( 466 new InputStreamReader (System.in)); 467 System.out.print (ANSICodes.setkey ("97", "97;98;99;13") 468 + ANSICodes.attrib (ANSICodes.OFF)); 469 System.out.flush (); 470 String line; 471 while ((line = reader.readLine ()) != null) 472 { 473 System.out.println ("GOT: " + line); 474 } 475 } 476 } 477 478 | Popular Tags |