чување 6f96792eebfa8cd08f60dc2bc9c220d464103087
родитељ 8e24a3a7bd6e3ae05ff151607819ab3301796182
Аутор: Страхиња Радић <contact@strahinja.org>
Датум: Sun, 28 May 2023 13:57:53 +0200
Better parsing of ANSI codes (ls example from manpage)
Signed-off-by: Страхиња Радић <contact@strahinja.org>
Diffstat:
| M | TODO | | | 2 | +- |
| M | table.c | | | 65 | ++++++++++++++++++++++++++++++++++++++++++++++++++--------------- |
измењених датотека: 2, додавања: 51(+), брисања: 16(-)
diff --git a/TODO b/TODO
@@ -1,7 +1,7 @@
TODO
====
-[ ] Parse ANSI codes in a more sophisticated manner (ls example from the
+[x] Parse ANSI codes in a more sophisticated manner (ls example from the
manpage)
diff --git a/table.c b/table.c
@@ -80,7 +80,8 @@ Alignment* align = NULL;
int align_header = 0;
size_t align_size = 0;
int border_mode = 0;
-int handle_ansi = 1;
+int no_ansi = 0;
+int in_sgr = 0;
int msdos = 0;
int pause = 0;
int expand_tabs = 0;
@@ -645,7 +646,7 @@ do_arg:
else if (!strcmp(arg, "no-ansi"))
{
arg += strlen("no-ansi");
- handle_ansi = 0;
+ no_ansi = 1;
}
else if (!strcmp(arg, "pause"))
{
@@ -708,7 +709,7 @@ do_arg:
msdos = 1;
break;
case 'n':
- handle_ansi = 0;
+ no_ansi = 1;
break;
case 'p':
pause = 1;
@@ -965,12 +966,13 @@ not_first:
current_table_column = 0;
current_rune_column = 0;
column_start = 0;
+ in_sgr = 0;
u8len = u32_char_to_u8(u8ch, *table_symbols[current_symbol_set][3]);
u8ch[u8len] = 0;
printf("%s", u8ch);
- if (handle_ansi && lineno == 0)
+ if (!no_ansi && lineno == 0)
printf("%s", ANSI_SGR_BOLD_ON);
do_line:
@@ -1001,20 +1003,52 @@ do_line:
continue_line:
u8_char_to_u32(&uch, pline, &ch_len);
- if (uch == delimiter && !in_quote
+ if (uch == (u32)L'\e' && *(pline+1) == '[')
+ {
+ in_sgr = 1;
+ if (!no_ansi)
+ poutbuf += sprintf((char*)poutbuf, "%c", *pline);
+ pline++;
+ colno++;
+ }
+ else if (uch == (u32)L'm')
+ {
+ if (!no_ansi || !in_sgr)
+ poutbuf += sprintf((char*)poutbuf, "%c", *pline);
+ if (!in_sgr)
+ outbufrunelen++;
+ in_sgr = 0;
+ pline++;
+ colno++;
+ }
+ else if (in_sgr)
+ {
+ if (!no_ansi)
+ {
+ pch_end = pline + ch_len;
+ while (pline != pch_end)
+ poutbuf += sprintf((char*)poutbuf, "%c",
+ *pline++);
+ }
+ else
+ pline += ch_len;
+ colno += ch_len;
+ }
+ else if (uch == delimiter && !in_quote
&& current_table_column < table_columns - 1)
{
size_t colwidth = current_column_width();
- if (handle_ansi && lineno == 0)
+ if (!no_ansi && lineno == 0)
poutbuf += sprintf((char*)poutbuf, "%s",
ANSI_SGR_BOLD_OFF);
print_aligned(outbuf, outbufrunelen, colwidth,
- (lineno == 0 && !align_header) ? ALIGN_LEFT
+ (lineno == 0 && !align_header)
+ ? ALIGN_LEFT
: (current_table_column < align_size
- ? align[current_table_column]
- : ALIGN_LEFT),
+ ? align[current_table_column]
+ : ALIGN_LEFT),
¤t_rune_column);
while (within_column(column_start, current_rune_column))
@@ -1043,7 +1077,7 @@ continue_line:
current_table_column++;
}
- if (handle_ansi && lineno == 0)
+ if (!no_ansi && lineno == 0)
poutbuf += sprintf((char*)poutbuf, "%s",
ANSI_SGR_BOLD_ON);
@@ -1081,7 +1115,7 @@ continue_line:
done_line:;
- if (handle_ansi && lineno == 0)
+ if (!no_ansi && lineno == 0)
printf("%s", ANSI_SGR_BOLD_OFF);
if (*outbuf)
@@ -1089,15 +1123,16 @@ done_line:;
size_t colwidth = format ? *(format + current_table_column)
: format_value;
- if (handle_ansi && lineno == 0)
+ if (!no_ansi && lineno == 0)
poutbuf += sprintf((char*)poutbuf, "%s",
ANSI_SGR_BOLD_OFF);
print_aligned(outbuf, outbufrunelen, colwidth,
- (lineno == 0 && !align_header) ? ALIGN_LEFT
+ (lineno == 0 && !align_header)
+ ? ALIGN_LEFT
: (current_table_column < align_size
- ? align[current_table_column]
- : ALIGN_LEFT),
+ ? align[current_table_column]
+ : ALIGN_LEFT),
¤t_rune_column);
*outbuf = 0;