1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.hadoop.hbase.hbtop.terminal.impl;
19
20 import org.apache.hadoop.hbase.classification.InterfaceAudience;
21 import org.apache.hadoop.hbase.hbtop.terminal.Color;
22
23
24
25
26 @InterfaceAudience.Private
27 public final class EscapeSequences {
28
29 private EscapeSequences() {
30 }
31
32 public static String clearAll() {
33 return "\033[0;37;40m\033[2J";
34 }
35
36 public static String setTitle(String title) {
37 return "\033]2;" + title + "\007";
38 }
39
40 public static String cursor(boolean on) {
41 if (on) {
42 return "\033[?25h";
43 }
44 return "\033[?25l";
45 }
46
47 public static String moveCursor(int column, int row) {
48 return String.format("\033[%d;%dH", row + 1, column + 1);
49 }
50
51 public static String clearRemainingLine() {
52 return "\033[0;37;40m\033[K";
53 }
54
55 public static String color(Color foregroundColor, Color backgroundColor, boolean bold,
56 boolean reverse, boolean blink, boolean underline) {
57
58 int foregroundColorValue = getColorValue(foregroundColor, true);
59 int backgroundColorValue = getColorValue(backgroundColor, false);
60
61 StringBuilder sb = new StringBuilder();
62 if (bold && reverse && blink && !underline) {
63 sb.append("\033[0;1;7;5;");
64 } else if (bold && reverse && !blink && !underline) {
65 sb.append("\033[0;1;7;");
66 } else if (!bold && reverse && blink && !underline) {
67 sb.append("\033[0;7;5;");
68 } else if (bold && !reverse && blink && !underline) {
69 sb.append("\033[0;1;5;");
70 } else if (bold && !reverse && !blink && !underline) {
71 sb.append("\033[0;1;");
72 } else if (!bold && reverse && !blink && !underline) {
73 sb.append("\033[0;7;");
74 } else if (!bold && !reverse && blink && !underline) {
75 sb.append("\033[0;5;");
76 } else if (bold && reverse && blink) {
77 sb.append("\033[0;1;7;5;4;");
78 } else if (bold && reverse) {
79 sb.append("\033[0;1;7;4;");
80 } else if (!bold && reverse && blink) {
81 sb.append("\033[0;7;5;4;");
82 } else if (bold && blink) {
83 sb.append("\033[0;1;5;4;");
84 } else if (bold) {
85 sb.append("\033[0;1;4;");
86 } else if (reverse) {
87 sb.append("\033[0;7;4;");
88 } else if (blink) {
89 sb.append("\033[0;5;4;");
90 } else if (underline) {
91 sb.append("\033[0;4;");
92 } else {
93 sb.append("\033[0;");
94 }
95 sb.append(String.format("%d;%dm", foregroundColorValue, backgroundColorValue));
96 return sb.toString();
97 }
98
99 private static int getColorValue(Color color, boolean foreground) {
100 int baseValue;
101 if (foreground) {
102 baseValue = 30;
103 } else {
104 baseValue = 40;
105 }
106
107 switch (color) {
108 case BLACK:
109 return baseValue;
110
111 case RED:
112 return baseValue + 1;
113
114 case GREEN:
115 return baseValue + 2;
116
117 case YELLOW:
118 return baseValue + 3;
119
120 case BLUE:
121 return baseValue + 4;
122
123 case MAGENTA:
124 return baseValue + 5;
125
126 case CYAN:
127 return baseValue + 6;
128
129 case WHITE:
130 return baseValue + 7;
131
132 default:
133 throw new AssertionError();
134 }
135 }
136
137 public static String normal() {
138 return "\033[0;37;40m";
139 }
140 }