Support simple 8 bit ansi escape sequences

See https://en.wikipedia.org/wiki/ANSI_escape_code#8-bit
Support standard and high intensity colors. The default
ncurses colors ARE the high intensity colors - represented
via an 8 bit ansi escape sequence here.
This commit is contained in:
Nipunn Koorapati 2020-07-02 06:05:24 +00:00 committed by Craig Small
parent 6285bdd3b5
commit a23c941d8b

52
watch.c
View File

@ -138,36 +138,36 @@ static void init_ansi_colors(void)
static int process_ansi_color_escape_sequence(char** escape_sequence) {
// process SGR ANSI color escape sequence
// Eg 8-bit
// 38;5;⟨n⟩ (set fg color to n)
// 48;5;⟨n⟩ (set bg color to n)
//
// Eg 24-bit (not yet implemented)
// ESC[ 38;2;⟨r⟩;⟨g⟩;⟨b⟩ m Select RGB foreground color
// ESC[ 48;2;⟨r⟩;⟨g⟩;⟨b⟩ m Select RGB background color
int num;
// process SGR ANSI color escape sequence
// Eg 8-bit
// 38;5;⟨n⟩ (set fg color to n)
// 48;5;⟨n⟩ (set bg color to n)
//
// Eg 24-bit (not yet implemented)
// ESC[ 38;2;⟨r⟩;⟨g⟩;⟨b⟩ m Select RGB foreground color
// ESC[ 48;2;⟨r⟩;⟨g⟩;⟨b⟩ m Select RGB background color
int num;
if ((*escape_sequence)[0] != ';')
return 0; /* not understood */
if ((*escape_sequence)[0] != ';')
return 0; /* not understood */
if ((*escape_sequence)[1] == '5') {
// 8 bit! ANSI specifies a predefined set of 256 colors here.
if ((*escape_sequence)[2] != ';')
return 0; /* not understood */
num = strtol((*escape_sequence) + 3, escape_sequence, 10);
if (num >= 0 && num <= 7) {
// 0-7 are standard colors same as SGR 30-37
return num + 1;
}
if ((*escape_sequence)[1] == '5') {
// 8 bit! ANSI specifies a predefined set of 256 colors here.
if ((*escape_sequence)[2] != ';')
return 0; /* not understood */
num = strtol((*escape_sequence) + 3, escape_sequence, 10);
if (num >= 0 && num <= 7) {
// 0-7 are standard colors same as SGR 30-37
return num + 1;
}
// Remainder aren't yet implemented
// 8- 15: high intensity colors (as in ESC [ 9097 m)
// 16-231: 6 × 6 × 6 cube (216 colors): 16 + 36 × r + 6 × g + b (0 ≤ r, g, b ≤ 5)
// 232-255: grayscale from black to white in 24 steps
}
// Remainder aren't yet implemented
// 8- 15: high intensity colors (as in ESC [ 9097 m)
// 16-231: 6 × 6 × 6 cube (216 colors): 16 + 36 × r + 6 × g + b (0 ≤ r, g, b ≤ 5)
// 232-255: grayscale from black to white in 24 steps
}
return 0; /* not understood */
return 0; /* not understood */
}