AuD
Lecture 'Algorithmen und Datenstrukturen' (code examples)
util/Terminal.java
Go to the documentation of this file.
1package aud.util;
2
3import java.io.PrintStream;
4
5public class Terminal {
6
7 public static final int BLACK=0;
8 public static final int RED=1;
9 public static final int GREEN=2;
10 public static final int YELLOW=3;
11 public static final int BLUE=4;
12 public static final int MAGENTA=5;
13 public static final int CYAN=6;
14 public static final int WHITE=7;
15
16 public static final int HIGHLIGHT=8;
17
18 public static final int HI_BLACK=8;
19 public static final int HI_RED=9;
20 public static final int HI_GREEN=10;
21 public static final int HI_YELLOW=11;
22 public static final int HI_BLUE=12;
23 public static final int HI_MAGENTA=13;
24 public static final int HI_CYAN=14;
25 public static final int HI_WHITE=15;
26
28 public static final String[] FGCOLOR={
29 "\033[30m", // black
30 "\033[31m", // red
31 "\033[32m", // green
32 "\033[33m", // yellow
33 "\033[34m", // blue
34 "\033[35m", // magenta
35 "\033[36m", // cyan
36 "\033[37m", // white
37 "\033[90m", // hight intensity black
38 "\033[91m", // hight intensity red
39 "\033[92m", // hight intensity green
40 "\033[93m", // hight intensity yellow
41 "\033[94m", // hight intensity blue
42 "\033[95m", // hight intensity magenta
43 "\033[96m", // hight intensity cyan
44 "\033[97m", // hight intensity white
45 };
47 public static final String[] BGCOLOR={
48 "\033[40m", // black
49 "\033[41m", // red
50 "\033[42m", // green
51 "\033[43m", // yellow
52 "\033[44m", // blue
53 "\033[45m", // magenta
54 "\033[46m", // cyan
55 "\033[47m", // white
56 "\033[100m", // high intensity black
57 "\033[101m", // high intensity red
58 "\033[102m", // high intensity green
59 "\033[103m", // high intensity yellow
60 "\033[104m", // high intensity blue
61 "\033[105m", // high intensity magenta
62 "\033[106m", // high intensity cyan
63 "\033[107m", // high intensity white
64 };
66 public static final String NORMAL="\033[0m";
68 public static final String BOLD="\033[1m";
69
71 public static final String CLEAR_SCREEN="\033[2J\033[1;1H";
73 public static final String HOME="\033[1;1H";
74
76 public static final String SHOW_CURSOR="\033[?25h";
78 public static final String HIDE_CURSOR="\033[?25l";
79
80
82 public boolean invert_color=false;
84 public boolean invert_foreground_background=false;
85
87 public PrintStream out=System.out;
88
90 boolean ansi_=true;
92 int cur_fg_ = -1;
94 int cur_bg_ = -1;
96 int cur_font_ = -1;
97
101 public Terminal(boolean ansi) {
102 ansi_=ansi;
103 if (!ansi)
104 System.err.println("Assuming dumb terminal.");
105 }
110 public Terminal() {
111 this(!System.getProperty("os.name").startsWith("Windows"));
112 }
113
115 public boolean dumb() { return !ansi_; }
116
118 public void fg(int color) {
119 assert(color>=0);
120
121 if (!ansi_)
122 cur_fg_=-1;
123 else {
125 color=invert_color ? table.length-color%table.length : color;
126 out.print(table[color]);
127 cur_fg_=color;
128 }
129 }
130
132 public void bg(int color) {
133 assert(color>=0);
134
135 if (!ansi_)
136 cur_bg_=-1;
137 else {
139 color=invert_color ? table.length-color%table.length : color;
140 out.print(table[color]);
141 cur_bg_=color;
142 }
143 }
144
146 public void cls(boolean clear) {
147 if (ansi_)
148 out.print(clear ? CLEAR_SCREEN : HOME);
149 else
150 out.print("\n\n");
151 }
153 public void bold(boolean b) {
154 if (ansi_)
155 cur_font_=-1;
156 else {
157 boolean cur_b=(cur_font_==1);
158 if (cur_b!=b) {
159 out.print(b ? BOLD : NORMAL);
160 cur_font_=b ? 1 : 0;
161 }
162 }
163 }
165 public void cls() { cls(true); }
166
168 public void reset() {
169 fg(WHITE);
170 bg(BLACK);
171 bold(false);
172 }
173
175 public void showCursor() {
176 if (ansi_)
177 out.print(SHOW_CURSOR);
178 }
180 public void hideCursor() {
181 if (ansi_)
182 out.print(HIDE_CURSOR);
183 }
184
186 public static Terminal instance = new Terminal();
187};
static final int HI_BLUE
static final String HOME
reset cursor position to upper left corner
static final int GREEN
static final int BLACK
PrintStream out
output stream, user is responsible for flush
static final String BOLD
set normal text
void cls(boolean clear)
clear screen (for clear=false, move only cursor)
boolean invert_foreground_background
swap foreground and background
void hideCursor()
hide cursor
static final int HI_WHITE
void showCursor()
show cursor
static final String[] BGCOLOR
set background color
static final int RED
void reset()
reset to black on white, normal font
Terminal(boolean ansi)
Constructor.
static final String HIDE_CURSOR
hide cursor
static final int HI_GREEN
Terminal()
Constructor: guess availability of terminal emulation.
static final int HI_MAGENTA
static final int BLUE
static final String NORMAL
set bold text
static final int HI_YELLOW
boolean invert_color
invert colors
static final String[] FGCOLOR
set foreground color
static final int WHITE
static final int YELLOW
void cls()
clear screen (cls(true))
void fg(int color)
set foreground color
static final int HIGHLIGHT
static final int CYAN
static final String CLEAR_SCREEN
clear screen and reset cursor position to upper left corner
static final int HI_BLACK
static final String SHOW_CURSOR
show cursor
static final int MAGENTA
boolean dumb()
Is this a dumb terminal without colors?
void bold(boolean b)
switch use of bold font
static Terminal instance
singleton instance
static final int HI_RED
static final int HI_CYAN
void bg(int color)
set background color