52938
|
1 |
/*
|
|
2 |
* Copyright (c) 2002-2018, the original author or authors.
|
|
3 |
*
|
|
4 |
* This software is distributable under the BSD license. See the terms of the
|
|
5 |
* BSD license in the documentation provided with this software.
|
|
6 |
*
|
58903
|
7 |
* https://opensource.org/licenses/BSD-3-Clause
|
52938
|
8 |
*/
|
|
9 |
package jdk.internal.org.jline.utils;
|
|
10 |
|
|
11 |
/**
|
|
12 |
* Text styling.
|
|
13 |
*
|
|
14 |
* @author <a href="mailto:gnodet@gmail.com">Guillaume Nodet</a>
|
|
15 |
*/
|
|
16 |
public class AttributedStyle {
|
|
17 |
|
|
18 |
public static final int BLACK = 0;
|
|
19 |
public static final int RED = 1;
|
|
20 |
public static final int GREEN = 2;
|
|
21 |
public static final int YELLOW = 3;
|
|
22 |
public static final int BLUE = 4;
|
|
23 |
public static final int MAGENTA = 5;
|
|
24 |
public static final int CYAN = 6;
|
|
25 |
public static final int WHITE = 7;
|
|
26 |
|
|
27 |
public static final int BRIGHT = 8;
|
|
28 |
|
|
29 |
static final int F_BOLD = 0x00000001;
|
|
30 |
static final int F_FAINT = 0x00000002;
|
|
31 |
static final int F_ITALIC = 0x00000004;
|
|
32 |
static final int F_UNDERLINE = 0x00000008;
|
|
33 |
static final int F_BLINK = 0x00000010;
|
|
34 |
static final int F_INVERSE = 0x00000020;
|
|
35 |
static final int F_CONCEAL = 0x00000040;
|
|
36 |
static final int F_CROSSED_OUT = 0x00000080;
|
|
37 |
static final int F_FOREGROUND = 0x00000100;
|
|
38 |
static final int F_BACKGROUND = 0x00000200;
|
|
39 |
static final int F_HIDDEN = 0x00000400;
|
|
40 |
|
|
41 |
static final int MASK = 0x000007FF;
|
|
42 |
|
|
43 |
static final int FG_COLOR_EXP = 16;
|
|
44 |
static final int BG_COLOR_EXP = 24;
|
|
45 |
static final int FG_COLOR = 0xFF << FG_COLOR_EXP;
|
|
46 |
static final int BG_COLOR = 0xFF << BG_COLOR_EXP;
|
|
47 |
|
|
48 |
public static final AttributedStyle DEFAULT = new AttributedStyle();
|
|
49 |
public static final AttributedStyle BOLD = DEFAULT.bold();
|
|
50 |
public static final AttributedStyle BOLD_OFF = DEFAULT.boldOff();
|
|
51 |
public static final AttributedStyle INVERSE = DEFAULT.inverse();
|
|
52 |
public static final AttributedStyle INVERSE_OFF = DEFAULT.inverseOff();
|
|
53 |
public static final AttributedStyle HIDDEN = DEFAULT.hidden();
|
|
54 |
public static final AttributedStyle HIDDEN_OFF = DEFAULT.hiddenOff();
|
|
55 |
|
|
56 |
final int style;
|
|
57 |
final int mask;
|
|
58 |
|
|
59 |
public AttributedStyle() {
|
|
60 |
this(0, 0);
|
|
61 |
}
|
|
62 |
|
|
63 |
public AttributedStyle(AttributedStyle s) {
|
|
64 |
this(s.style, s.mask);
|
|
65 |
}
|
|
66 |
|
|
67 |
public AttributedStyle(int style, int mask) {
|
|
68 |
this.style = style;
|
|
69 |
this.mask = mask & MASK | ((style & F_FOREGROUND) != 0 ? FG_COLOR : 0)
|
|
70 |
| ((style & F_BACKGROUND) != 0 ? BG_COLOR : 0);
|
|
71 |
}
|
|
72 |
|
|
73 |
public AttributedStyle bold() {
|
|
74 |
return new AttributedStyle(style | F_BOLD, mask | F_BOLD);
|
|
75 |
}
|
|
76 |
|
|
77 |
public AttributedStyle boldOff() {
|
|
78 |
return new AttributedStyle(style & ~F_BOLD, mask | F_BOLD);
|
|
79 |
}
|
|
80 |
|
|
81 |
public AttributedStyle boldDefault() {
|
|
82 |
return new AttributedStyle(style & ~F_BOLD, mask & ~F_BOLD);
|
|
83 |
}
|
|
84 |
|
|
85 |
public AttributedStyle faint() {
|
|
86 |
return new AttributedStyle(style | F_FAINT, mask | F_FAINT);
|
|
87 |
}
|
|
88 |
|
|
89 |
public AttributedStyle faintOff() {
|
|
90 |
return new AttributedStyle(style & ~F_FAINT, mask | F_FAINT);
|
|
91 |
}
|
|
92 |
|
|
93 |
public AttributedStyle faintDefault() {
|
|
94 |
return new AttributedStyle(style & ~F_FAINT, mask & ~F_FAINT);
|
|
95 |
}
|
|
96 |
|
|
97 |
public AttributedStyle italic() {
|
|
98 |
return new AttributedStyle(style | F_ITALIC, mask | F_ITALIC);
|
|
99 |
}
|
|
100 |
|
|
101 |
public AttributedStyle italicOff() {
|
|
102 |
return new AttributedStyle(style & ~F_ITALIC, mask | F_ITALIC);
|
|
103 |
}
|
|
104 |
|
|
105 |
public AttributedStyle italicDefault() {
|
|
106 |
return new AttributedStyle(style & ~F_ITALIC, mask & ~F_ITALIC);
|
|
107 |
}
|
|
108 |
|
|
109 |
public AttributedStyle underline() {
|
|
110 |
return new AttributedStyle(style | F_UNDERLINE, mask | F_UNDERLINE);
|
|
111 |
}
|
|
112 |
|
|
113 |
public AttributedStyle underlineOff() {
|
|
114 |
return new AttributedStyle(style & ~F_UNDERLINE, mask | F_UNDERLINE);
|
|
115 |
}
|
|
116 |
|
|
117 |
public AttributedStyle underlineDefault() {
|
|
118 |
return new AttributedStyle(style & ~F_UNDERLINE, mask & ~F_UNDERLINE);
|
|
119 |
}
|
|
120 |
|
|
121 |
public AttributedStyle blink() {
|
|
122 |
return new AttributedStyle(style | F_BLINK, mask | F_BLINK);
|
|
123 |
}
|
|
124 |
|
|
125 |
public AttributedStyle blinkOff() {
|
|
126 |
return new AttributedStyle(style & ~F_BLINK, mask | F_BLINK);
|
|
127 |
}
|
|
128 |
|
|
129 |
public AttributedStyle blinkDefault() {
|
|
130 |
return new AttributedStyle(style & ~F_BLINK, mask & ~F_BLINK);
|
|
131 |
}
|
|
132 |
|
|
133 |
public AttributedStyle inverse() {
|
|
134 |
return new AttributedStyle(style | F_INVERSE, mask | F_INVERSE);
|
|
135 |
}
|
|
136 |
|
|
137 |
public AttributedStyle inverseNeg() {
|
|
138 |
int s = (style & F_INVERSE) != 0 ? style & ~F_INVERSE : style | F_INVERSE;
|
|
139 |
return new AttributedStyle(s, mask | F_INVERSE);
|
|
140 |
}
|
|
141 |
|
|
142 |
public AttributedStyle inverseOff() {
|
|
143 |
return new AttributedStyle(style & ~F_INVERSE, mask | F_INVERSE);
|
|
144 |
}
|
|
145 |
|
|
146 |
public AttributedStyle inverseDefault() {
|
|
147 |
return new AttributedStyle(style & ~F_INVERSE, mask & ~F_INVERSE);
|
|
148 |
}
|
|
149 |
|
|
150 |
public AttributedStyle conceal() {
|
|
151 |
return new AttributedStyle(style | F_CONCEAL, mask | F_CONCEAL);
|
|
152 |
}
|
|
153 |
|
|
154 |
public AttributedStyle concealOff() {
|
|
155 |
return new AttributedStyle(style & ~F_CONCEAL, mask | F_CONCEAL);
|
|
156 |
}
|
|
157 |
|
|
158 |
public AttributedStyle concealDefault() {
|
|
159 |
return new AttributedStyle(style & ~F_CONCEAL, mask & ~F_CONCEAL);
|
|
160 |
}
|
|
161 |
|
|
162 |
public AttributedStyle crossedOut() {
|
|
163 |
return new AttributedStyle(style | F_CROSSED_OUT, mask | F_CROSSED_OUT);
|
|
164 |
}
|
|
165 |
|
|
166 |
public AttributedStyle crossedOutOff() {
|
|
167 |
return new AttributedStyle(style & ~F_CROSSED_OUT, mask | F_CROSSED_OUT);
|
|
168 |
}
|
|
169 |
|
|
170 |
public AttributedStyle crossedOutDefault() {
|
|
171 |
return new AttributedStyle(style & ~F_CROSSED_OUT, mask & ~F_CROSSED_OUT);
|
|
172 |
}
|
|
173 |
|
|
174 |
public AttributedStyle foreground(int color) {
|
|
175 |
return new AttributedStyle(style & ~FG_COLOR | F_FOREGROUND | ((color << FG_COLOR_EXP) & FG_COLOR), mask | F_FOREGROUND);
|
|
176 |
}
|
|
177 |
|
|
178 |
public AttributedStyle foregroundOff() {
|
|
179 |
return new AttributedStyle(style & ~FG_COLOR & ~F_FOREGROUND, mask | F_FOREGROUND);
|
|
180 |
}
|
|
181 |
|
|
182 |
public AttributedStyle foregroundDefault() {
|
|
183 |
return new AttributedStyle(style & ~FG_COLOR & ~F_FOREGROUND, mask & ~(F_FOREGROUND | FG_COLOR));
|
|
184 |
}
|
|
185 |
|
|
186 |
public AttributedStyle background(int color) {
|
|
187 |
return new AttributedStyle(style & ~BG_COLOR | F_BACKGROUND | ((color << BG_COLOR_EXP) & BG_COLOR), mask | F_BACKGROUND);
|
|
188 |
}
|
|
189 |
|
|
190 |
public AttributedStyle backgroundOff() {
|
|
191 |
return new AttributedStyle(style & ~BG_COLOR & ~F_BACKGROUND, mask | F_BACKGROUND);
|
|
192 |
}
|
|
193 |
|
|
194 |
public AttributedStyle backgroundDefault() {
|
|
195 |
return new AttributedStyle(style & ~BG_COLOR & ~F_BACKGROUND, mask & ~(F_BACKGROUND | BG_COLOR));
|
|
196 |
}
|
|
197 |
|
|
198 |
/**
|
|
199 |
* The hidden flag can be used to embed custom escape sequences.
|
|
200 |
* The characters are considered being 0-column long and will be printed as-is.
|
|
201 |
* The user is responsible for ensuring that those sequences do not move the cursor.
|
|
202 |
*
|
|
203 |
* @return the new style
|
|
204 |
*/
|
|
205 |
public AttributedStyle hidden() {
|
|
206 |
return new AttributedStyle(style | F_HIDDEN, mask | F_HIDDEN);
|
|
207 |
}
|
|
208 |
|
|
209 |
public AttributedStyle hiddenOff() {
|
|
210 |
return new AttributedStyle(style & ~F_HIDDEN, mask | F_HIDDEN);
|
|
211 |
}
|
|
212 |
|
|
213 |
public AttributedStyle hiddenDefault() {
|
|
214 |
return new AttributedStyle(style & ~F_HIDDEN, mask & ~F_HIDDEN);
|
|
215 |
}
|
|
216 |
|
|
217 |
public int getStyle() {
|
|
218 |
return style;
|
|
219 |
}
|
|
220 |
|
|
221 |
public int getMask() {
|
|
222 |
return mask;
|
|
223 |
}
|
|
224 |
|
|
225 |
@Override
|
|
226 |
public boolean equals(Object o) {
|
|
227 |
if (this == o) return true;
|
|
228 |
if (o == null || getClass() != o.getClass()) return false;
|
|
229 |
AttributedStyle that = (AttributedStyle) o;
|
|
230 |
if (style != that.style) return false;
|
|
231 |
return mask == that.mask;
|
|
232 |
|
|
233 |
}
|
|
234 |
|
|
235 |
@Override
|
|
236 |
public int hashCode() {
|
|
237 |
int result = style;
|
|
238 |
result = 31 * result + mask;
|
|
239 |
return result;
|
|
240 |
}
|
|
241 |
}
|