Skip to content

Commit

Permalink
Add support for colons in SGR codes
Browse files Browse the repository at this point in the history
  • Loading branch information
kovidgoyal committed Nov 5, 2017
1 parent c762f61 commit e90aaa8
Show file tree
Hide file tree
Showing 2 changed files with 133 additions and 12 deletions.
126 changes: 118 additions & 8 deletions kitty/parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,7 @@ dispatch_osc(Screen *screen, PyObject DUMP_UNUSED *dump_callback) {
// CSI mode {{{
#define CSI_SECONDARY \
case ';': \
case ':': \
case '"': \
case '*': \
case '\'': \
Expand Down Expand Up @@ -382,6 +383,119 @@ repr_csi_params(unsigned int *params, unsigned int num_params) {
return buf;
}

static inline void
parse_sgr(Screen *screen, uint32_t *buf, unsigned int num, unsigned int *params, PyObject DUMP_UNUSED *dump_callback) {
enum State { START, NORMAL, MULTIPLE, COLOR, COLOR1, COLOR3 };
enum State state = START;
unsigned int num_params, num_start, i;

#define READ_PARAM { params[num_params++] = utoi(buf + num_start, i - num_start); }
#define SEND_SGR { REPORT_PARAMS(select_graphic_rendition, params, num_params); select_graphic_rendition(screen, params, num_params); state = START; num_params = 0; }

for (i=0, num_start=0, num_params=0; i < num && num_params < MAX_PARAMS; i++) {
switch(buf[i]) {
IS_DIGIT
switch(state) {
case START:
num_start = i;
state = NORMAL;
num_params = 0;
break;
default:
break;
}
break;
case ';':
switch(state) {
case START:
params[num_params++] = 0;
SEND_SGR;
break;
case NORMAL:
READ_PARAM;
switch(params[0]) {
case 38:
case 48:
case 58:
state = COLOR;
num_start = i + 1;
break;
default:
SEND_SGR;
break;
}
break;
case MULTIPLE:
READ_PARAM;
SEND_SGR;
break;
case COLOR:
READ_PARAM;
switch(params[1]) {
case 2:
state = COLOR3;
break;
case 5:
state = COLOR1;
break;
default:
REPORT_ERROR("Invalid SGR color code with unknown color type: %u", params[1]);
return;
}
num_start = i + 1;
break;
case COLOR1:
READ_PARAM;
SEND_SGR;
break;
case COLOR3:
READ_PARAM;
if (num_params == 5) { SEND_SGR; }
else num_start = i + 1;
break;
}
break;
case ':':
switch(state) {
case START:
REPORT_ERROR("Invalid SGR code containing ':' at an invalid location: %u", i);
return;
case NORMAL:
READ_PARAM;
state = MULTIPLE;
num_start = i + 1;
break;
case MULTIPLE:
READ_PARAM;
num_start = i + 1;
break;
case COLOR:
case COLOR1:
case COLOR3:
REPORT_ERROR("Invalid SGR code containing disallowed character: %s", utf8(buf[i]));
return;
}
break;
default:
REPORT_ERROR("Invalid SGR code containing disallowed character: %s", utf8(buf[i]));
return;
}
}
switch(state) {
case COLOR1:
case COLOR3:
case NORMAL:
case MULTIPLE:
READ_PARAM;
SEND_SGR;
break;
default:
break;
}
#undef READ_PARAM
#undef SEND_SGR
}

static inline void
dispatch_csi(Screen *screen, PyObject DUMP_UNUSED *dump_callback) {
#define CALL_CSI_HANDLER1(name, defval) \
Expand Down Expand Up @@ -424,12 +538,6 @@ dispatch_csi(Screen *screen, PyObject DUMP_UNUSED *dump_callback) {
} \
break;

#define CSI_HANDLER_MULTIPLE(name) \
REPORT_PARAMS(name, params, num_params); \
name(screen, params, num_params); \
break;


char start_modifier = 0, end_modifier = 0;
uint32_t *buf = screen->parser_buf, code = screen->parser_buf[screen->parser_buf_pos];
unsigned int num = screen->parser_buf_pos, start, i, num_params=0, p1, p2;
Expand All @@ -439,6 +547,10 @@ dispatch_csi(Screen *screen, PyObject DUMP_UNUSED *dump_callback) {
start_modifier = (char)screen->parser_buf[0];
buf++; num--;
}
if (code == SGR && !start_modifier) {
parse_sgr(screen, buf, num, params, dump_callback);
return;
}
if (num > 0) {
switch(buf[num-1]) {
CSI_SECONDARY
Expand Down Expand Up @@ -506,8 +618,6 @@ dispatch_csi(Screen *screen, PyObject DUMP_UNUSED *dump_callback) {
SET_MODE(screen_set_mode);
case RM:
SET_MODE(screen_reset_mode);
case SGR:
CSI_HANDLER_MULTIPLE(select_graphic_rendition);
case DSR:
CALL_CSI_HANDLER1P(report_device_status, 0, '?');
case SC:
Expand Down
19 changes: 15 additions & 4 deletions kitty_tests/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,19 +115,30 @@ def test_csi_codes(self):
pb('\033[?1000;1004h', ('screen_set_mode', 1000, 1), ('screen_set_mode', 1004, 1))
pb('\033[20;4;20l', ('screen_reset_mode', 20, 0), ('screen_reset_mode', 4, 0), ('screen_reset_mode', 20, 0))
s.reset()
pb('\033[1;3;4;7;9;34;44m', ('select_graphic_rendition', '1 3 4 7 9 34 44 '))

def sgr(params):
return (('select_graphic_rendition', '{} '.format(x)) for x in params.split())

pb('\033[1;3;4;7;9;34;44m', *sgr('1 3 4 7 9 34 44'))
for attr in 'bold italic reverse strikethrough'.split():
self.assertTrue(getattr(s.cursor, attr))
self.ae(s.cursor.decoration, 1)
self.ae(s.cursor.fg, 4 << 8 | 1)
self.ae(s.cursor.bg, 4 << 8 | 1)
pb('\033[38;5;1;48;5;7m', ('select_graphic_rendition', '38 5 1 48 5 7 '))
pb('\033[38;5;1;48;5;7m', ('select_graphic_rendition', '38 5 1 '), ('select_graphic_rendition', '48 5 7 '))
self.ae(s.cursor.fg, 1 << 8 | 1)
self.ae(s.cursor.bg, 7 << 8 | 1)
pb('\033[38;2;1;2;3;48;2;7;8;9m', ('select_graphic_rendition', '38 2 1 2 3 48 2 7 8 9 '))
pb('\033[38;2;1;2;3;48;2;7;8;9m', ('select_graphic_rendition', '38 2 1 2 3 '), ('select_graphic_rendition', '48 2 7 8 9 '))
self.ae(s.cursor.fg, 1 << 24 | 2 << 16 | 3 << 8 | 2)
self.ae(s.cursor.bg, 7 << 24 | 8 << 16 | 9 << 8 | 2)
pb('\033[;2m', ('select_graphic_rendition', '0 2 '))
pb('\033[0;2m', *sgr('0 2'))
pb('\033[;2m', *sgr('0 2'))
pb('\033[1;;2m', *sgr('1 0 2'))
pb('\033[38;5;1m', ('select_graphic_rendition', '38 5 1 '))
pb('\033[38;2;1;2;3m', ('select_graphic_rendition', '38 2 1 2 3 '))
pb('\033[1001:2:1:2:3m', ('select_graphic_rendition', '1001 2 1 2 3 '))
pb('\033[38:2:1:2:3;48:5:9;58;5;7m', (
'select_graphic_rendition', '38 2 1 2 3 '), ('select_graphic_rendition', '48 5 9 '), ('select_graphic_rendition', '58 5 7 '))
c = s.callbacks
pb('\033[5n', ('report_device_status', 5, 0))
self.ae(c.wtcbuf, b'\033[0n')
Expand Down

5 comments on commit e90aaa8

@walles
Copy link

@walles walles commented on e90aaa8 Nov 8, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Found this in the changelog: https://sw.kovidgoyal.net/kitty/changelog/#id85

Add support for colons in SGR color codes. These are generated by some applications such as neovim when they mistakenly identify kitty as a libvte based terminal.

@walles
Copy link

@walles walles commented on e90aaa8 Nov 8, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, this ticket contains a list of terminals that support colons as color delimiters:
#7

Still don't see why but I guess some old things generate colons and terminals just have to live with it...

@kovidgoyal
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Colons are the correct form. Semi-colons the incorrect one, read the ECMA 48 spec for SGR sub-parameters.

@walles
Copy link

@walles walles commented on e90aaa8 Nov 8, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried to find it here, but no luck :(

https://www.ecma-international.org/wp-content/uploads/ECMA-48_5th_edition_june_1991.pdf

Am I looking in the wrong place? I would have expected to find this in 8.3.117.

@kovidgoyal
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See #227 for all the gory details.

Please sign in to comment.