From 0757f825dd4b0e3653a98264bbe3933d54b3c1ef Mon Sep 17 00:00:00 2001 From: Vadim Kochan Date: Tue, 5 May 2015 10:44:57 +0300 Subject: tprintf: Fix color breaking in less mode Automatic new line indentation can break terminal ESC color sequence by inserting new line within it. Fixed by considering that color ESC sequence is not closed by 'm' and only after it is closed - print new line with spaces. Signed-off-by: Vadim Kochan [tk: add comments] Signed-off-by: Tobias Klauser --- tprintf.c | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) (limited to 'tprintf.c') diff --git a/tprintf.c b/tprintf.c index 0ca2375..43f862a 100644 --- a/tprintf.c +++ b/tprintf.c @@ -66,6 +66,7 @@ static void __tprintf_flush(void) size_t i; static ssize_t line_count = 0; ssize_t term_len = term_curr_size; + size_t color_open = 0; for (i = 0; i < buffer_use; ++i) { if (buffer[i] == '\n') { @@ -73,7 +74,13 @@ static void __tprintf_flush(void) line_count = -1; } - if (line_count == term_len) { + /* Start of an color escape sequence? */ + if (buffer[i] == 033) { + if ((i + 1) < buffer_use && buffer[i + 1] == '[') + color_open++; + } + + if (color_open == 0 && line_count >= term_len) { __tprintf_flush_newline(); line_count = term_starting_size; @@ -82,6 +89,10 @@ static void __tprintf_flush(void) i++; } + /* End of the color escape sequence? */ + if (color_open > 0 && buffer[i] == 'm') + color_open--; + fputc(buffer[i], stdout); line_count++; } -- cgit v1.2.3-54-g00ecf