Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,27 @@ public class Ansi {
public static final String DEC_SET = "h";
public static final String DEC_RST = "l";

// ANSI style codes
public static final int STYLE_RESET = 0;
public static final int STYLE_BOLD = 1;
public static final int STYLE_FAINT = 2;
public static final int STYLE_ITALIC = 3;
public static final int STYLE_UNDERLINE = 4;
public static final int STYLE_BLINK = 5;
public static final int STYLE_INVERSE = 7;
public static final int STYLE_HIDDEN = 8;
public static final int STYLE_STRIKETHROUGH = 9;
public static final int STYLE_DOUBLE_UNDERLINE = 21;
public static final int STYLE_NORMAL = 22;
public static final int STYLE_ITALIC_OFF = 23;
public static final int STYLE_UNDERLINE_OFF = 24;
public static final int STYLE_BLINK_OFF = 25;
public static final int STYLE_INVERSE_OFF = 27;
public static final int STYLE_HIDDEN_OFF = 28;
public static final int STYLE_STRIKETHROUGH_OFF = 29;
public static final int STYLE_OVERLINE = 53;
public static final int STYLE_OVERLINE_OFF = 55;

// Positive mode numbers are official ANSI modes
public static final int MODE_ECHO = 12;

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

public static String csi(String post, int... params) {
public static String csi(String post, Object... params) {
return csi((String) null, post, params);
}

public static String csi(String pre, String post, int... params) {
public static String csi(String pre, String post, Object... params) {
try {
return csi(new StringBuilder(), pre, post, params).toString();
} catch (IOException e) {
throw new RuntimeException(e);
}
}

public static Appendable csi(Appendable appendable, String post, int... params)
public static Appendable csi(Appendable appendable, String post, Object... params)
throws IOException {
return csi(appendable, null, post, params);
}

public static Appendable csi(Appendable appendable, String pre, String post, int... params)
public static Appendable csi(Appendable appendable, String pre, String post, Object... params)
throws IOException {
appendable.append(CSI);
if (pre != null) {
appendable.append(pre);
}
for (int i = 0; i < params.length; i++) {
if (i > 0) appendable.append(";");
appendable.append(Integer.toString(params[i]));
appendable.append(params[i].toString());
}
if (post != null) {
appendable.append(post);
}
return appendable;
}

public static String style(Object... params) {
return csi("", "m", params);
}

public static String resetStyle() {
return style(STYLE_RESET);
}

public static String styled(String text, Object... params) {
return style(params) + text + resetStyle();
}

public static String modeQuery(int mode) {
if (mode >= 0) {
return csi("", "$p", mode);
Expand Down
14 changes: 7 additions & 7 deletions colors/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,11 @@ Color[] result = TermColors.queryPalette(terminal, in, ansi16);
### Set colours

```java
TermColors.setForeground(terminal, Color.ofRgb8(220, 220, 220));
TermColors.setBackground(terminal, Color.ofRgb8(30, 30, 30));
TermColors.setCursor(terminal, Color.ofRgb8(255, 165, 0));
TermColors.setForeground(terminal, Color.rgb8(220, 220, 220));
TermColors.setBackground(terminal, Color.rgb8(30, 30, 30));
TermColors.setCursor(terminal, Color.rgb8(255, 165, 0));

TermColors.setColor(terminal, 1, Color.ofRgb8(204, 0, 0)); // redefine ANSI red
TermColors.setColor(terminal, 1, Color.rgb8(204, 0, 0)); // redefine ANSI red
```

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

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

Conversion helpers:
Expand Down
Loading
Loading