Skip to content

Commit a634eb8

Browse files
committed
feat: now model basic and indexed colors
As well as the existing rgb colors
1 parent 2424bf4 commit a634eb8

7 files changed

Lines changed: 819 additions & 301 deletions

File tree

‎ansiparser/src/main/java/org/codejive/miniterm/ansiparser/Ansi.java‎

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,27 @@ public class Ansi {
1313
public static final String DEC_SET = "h";
1414
public static final String DEC_RST = "l";
1515

16+
// ANSI style codes
17+
public static final int STYLE_RESET = 0;
18+
public static final int STYLE_BOLD = 1;
19+
public static final int STYLE_FAINT = 2;
20+
public static final int STYLE_ITALIC = 3;
21+
public static final int STYLE_UNDERLINE = 4;
22+
public static final int STYLE_BLINK = 5;
23+
public static final int STYLE_INVERSE = 7;
24+
public static final int STYLE_HIDDEN = 8;
25+
public static final int STYLE_STRIKETHROUGH = 9;
26+
public static final int STYLE_DOUBLE_UNDERLINE = 21;
27+
public static final int STYLE_NORMAL = 22;
28+
public static final int STYLE_ITALIC_OFF = 23;
29+
public static final int STYLE_UNDERLINE_OFF = 24;
30+
public static final int STYLE_BLINK_OFF = 25;
31+
public static final int STYLE_INVERSE_OFF = 27;
32+
public static final int STYLE_HIDDEN_OFF = 28;
33+
public static final int STYLE_STRIKETHROUGH_OFF = 29;
34+
public static final int STYLE_OVERLINE = 53;
35+
public static final int STYLE_OVERLINE_OFF = 55;
36+
1637
// Positive mode numbers are official ANSI modes
1738
public static final int MODE_ECHO = 12;
1839

@@ -37,39 +58,51 @@ public class Ansi {
3758
public static final int MODE_UNICODE = -2027;
3859
public static final int MODE_SYSTEMTHEME = -2031;
3960

40-
public static String csi(String post, int... params) {
61+
public static String csi(String post, Object... params) {
4162
return csi((String) null, post, params);
4263
}
4364

44-
public static String csi(String pre, String post, int... params) {
65+
public static String csi(String pre, String post, Object... params) {
4566
try {
4667
return csi(new StringBuilder(), pre, post, params).toString();
4768
} catch (IOException e) {
4869
throw new RuntimeException(e);
4970
}
5071
}
5172

52-
public static Appendable csi(Appendable appendable, String post, int... params)
73+
public static Appendable csi(Appendable appendable, String post, Object... params)
5374
throws IOException {
5475
return csi(appendable, null, post, params);
5576
}
5677

57-
public static Appendable csi(Appendable appendable, String pre, String post, int... params)
78+
public static Appendable csi(Appendable appendable, String pre, String post, Object... params)
5879
throws IOException {
5980
appendable.append(CSI);
6081
if (pre != null) {
6182
appendable.append(pre);
6283
}
6384
for (int i = 0; i < params.length; i++) {
6485
if (i > 0) appendable.append(";");
65-
appendable.append(Integer.toString(params[i]));
86+
appendable.append(params[i].toString());
6687
}
6788
if (post != null) {
6889
appendable.append(post);
6990
}
7091
return appendable;
7192
}
7293

94+
public static String style(Object... params) {
95+
return csi("", "m", params);
96+
}
97+
98+
public static String resetStyle() {
99+
return style(STYLE_RESET);
100+
}
101+
102+
public static String styled(String text, Object... params) {
103+
return style(params) + text + resetStyle();
104+
}
105+
73106
public static String modeQuery(int mode) {
74107
if (mode >= 0) {
75108
return csi("", "$p", mode);

‎colors/README.md‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,11 @@ Color[] result = TermColors.queryPalette(terminal, in, ansi16);
4848
### Set colours
4949

5050
```java
51-
TermColors.setForeground(terminal, Color.ofRgb8(220, 220, 220));
52-
TermColors.setBackground(terminal, Color.ofRgb8(30, 30, 30));
53-
TermColors.setCursor(terminal, Color.ofRgb8(255, 165, 0));
51+
TermColors.setForeground(terminal, Color.rgb8(220, 220, 220));
52+
TermColors.setBackground(terminal, Color.rgb8(30, 30, 30));
53+
TermColors.setCursor(terminal, Color.rgb8(255, 165, 0));
5454

55-
TermColors.setColor(terminal, 1, Color.ofRgb8(204, 0, 0)); // redefine ANSI red
55+
TermColors.setColor(terminal, 1, Color.rgb8(204, 0, 0)); // redefine ANSI red
5656
```
5757

5858
Set the full palette from an array (null entries are skipped):
@@ -99,9 +99,9 @@ IntReader in = () -> terminal.read(500); // 500 ms timeout per read
9999
An immutable 16-bit-per-channel RGB colour, matching the precision used in the X11 `rgb:` colour specification that terminals return in OSC responses.
100100

101101
```java
102-
Color red = Color.of(0xFFFF, 0x0000, 0x0000); // 16-bit channels
103-
Color green = Color.ofRgb8(0, 255, 0); // 8-bit, expanded
104-
Color blue = Color.parse("rgb:0000/0000/FFFF"); // from OSC string
102+
Color red = Color.rgb(0xFFFF, 0x0000, 0x0000); // 16-bit channels
103+
Color green = Color.rgb8(0, 255, 0); // 8-bit, expanded
104+
Color blue = Color.RgbColor.parse("rgb:0000/0000/FFFF"); // from OSC string
105105
```
106106

107107
Conversion helpers:

0 commit comments

Comments
 (0)