Skip to content

Commit 99020cc

Browse files
author
Joe Merten
committed
added support for extended colors (xterm 256 and 24 bit rgb)
extended color like `esc[38;5;<index>m` or `esc[38;2;<r>;<g>;<b>m`
1 parent eeda18c commit 99020cc

File tree

1 file changed

+63
-1
lines changed

1 file changed

+63
-1
lines changed

jansi/src/main/java/org/fusesource/jansi/AnsiOutputStream.java

+63-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.io.OutputStream;
2323
import java.io.UnsupportedEncodingException;
2424
import java.util.ArrayList;
25+
import java.util.Iterator;
2526

2627
/**
2728
* A ANSI output stream extracts ANSI escape codes written to
@@ -214,6 +215,21 @@ private void reset(boolean skipBuffer) throws IOException {
214215
state = LOOKING_FOR_FIRST_ESC_CHAR;
215216
}
216217

218+
/**
219+
* Helper for processEscapeCommand() to iterate over integer options
220+
* @param optionsIterator the underlying iterator
221+
* @throws IOException if no more non-null values left
222+
*/
223+
private int getNextOptionInt(Iterator<Object> optionsIterator) throws IOException {
224+
for (;;) {
225+
if (!optionsIterator.hasNext())
226+
throw new IllegalArgumentException();
227+
Object arg = optionsIterator.next();
228+
if (arg != null)
229+
return ((Integer)arg).intValue();
230+
}
231+
}
232+
217233
/**
218234
*
219235
* @param options
@@ -269,7 +285,9 @@ private boolean processEscapeCommand(ArrayList<Object> options, int command) thr
269285
}
270286

271287
int count = 0;
272-
for (Object next : options) {
288+
Iterator<Object> optionsIterator = options.iterator();
289+
while (optionsIterator.hasNext()) {
290+
Object next = optionsIterator.next();
273291
if (next != null) {
274292
count++;
275293
int value = ((Integer) next).intValue();
@@ -281,6 +299,38 @@ private boolean processEscapeCommand(ArrayList<Object> options, int command) thr
281299
processSetForegroundColor(value - 90, true);
282300
} else if (100 <= value && value <= 107) {
283301
processSetBackgroundColor(value - 100, true);
302+
} else if (value == 38 || value == 48) {
303+
// extended color like `esc[38;5;<index>m` or `esc[38;2;<r>;<g>;<b>m`
304+
int arg2or5 = getNextOptionInt(optionsIterator);
305+
if (arg2or5 == 2) {
306+
// 24 bit color style like `esc[38;2;<r>;<g>;<b>m`
307+
int r = getNextOptionInt(optionsIterator);
308+
int g = getNextOptionInt(optionsIterator);
309+
int b = getNextOptionInt(optionsIterator);
310+
if (r >= 0 && r <= 255 && g >= 0 && g <= 255 && b >= 0 && b <= 255) {
311+
if (value == 38)
312+
processSetForegroundColorExt(r, g, b);
313+
else
314+
processSetBackgroundColorExt(r, g, b);
315+
} else {
316+
throw new IllegalArgumentException();
317+
}
318+
}
319+
else if (arg2or5 == 5) {
320+
// 256 color style like `esc[38;5;<index>m`
321+
int paletteIndex = getNextOptionInt(optionsIterator);
322+
if (paletteIndex >= 0 && paletteIndex <= 255) {
323+
if (value == 38)
324+
processSetForegroundColorExt(paletteIndex);
325+
else
326+
processSetBackgroundColorExt(paletteIndex);
327+
} else {
328+
throw new IllegalArgumentException();
329+
}
330+
}
331+
else {
332+
throw new IllegalArgumentException();
333+
}
284334
} else {
285335
switch (value) {
286336
case 39:
@@ -417,13 +467,25 @@ protected void processSetForegroundColor(int color) throws IOException {
417467
protected void processSetForegroundColor(int color, boolean bright) throws IOException {
418468
}
419469

470+
protected void processSetForegroundColorExt(int paletteIndex) throws IOException {
471+
}
472+
473+
protected void processSetForegroundColorExt(int r, int g, int b) throws IOException {
474+
}
475+
420476
protected void processSetBackgroundColor(int color) throws IOException {
421477
processSetBackgroundColor(color, false);
422478
}
423479

424480
protected void processSetBackgroundColor(int color, boolean bright) throws IOException {
425481
}
426482

483+
protected void processSetBackgroundColorExt(int paletteIndex) throws IOException {
484+
}
485+
486+
protected void processSetBackgroundColorExt(int r, int g, int b) throws IOException {
487+
}
488+
427489
protected void processDefaultTextColor() throws IOException {
428490
}
429491

0 commit comments

Comments
 (0)