summaryrefslogtreecommitdiff
path: root/tprintf.c
diff options
context:
space:
mode:
authorVadim Kochan <vadim4j@gmail.com>2015-05-05 10:44:57 +0300
committerTobias Klauser <tklauser@distanz.ch>2015-05-05 11:55:29 +0200
commit0757f825dd4b0e3653a98264bbe3933d54b3c1ef (patch)
tree8b4bb216ab0460baa32b465808925123369ae854 /tprintf.c
parentd245603ab331de51d774f3d69ba3689155b0eb53 (diff)
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 <vadim4j@gmail.com> [tk: add comments] Signed-off-by: Tobias Klauser <tklauser@distanz.ch>
Diffstat (limited to 'tprintf.c')
-rw-r--r--tprintf.c13
1 files changed, 12 insertions, 1 deletions
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++;
}