From: Vadim Kochan <[email protected]>

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 <[email protected]>
---
 tprintf.c | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)

diff --git a/tprintf.c b/tprintf.c
index 0ca2375..917b781 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;
+       int color_opens = 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) {
+               if (buffer[i] == 033) {
+                       if ((i + 1) < buffer_use && buffer[i + 1] == '[')
+                               color_opens++;
+               }
+
+               if (!color_opens && line_count >= term_len) {
+
                        __tprintf_flush_newline();
                        line_count = term_starting_size;
 
@@ -82,6 +89,9 @@ static void __tprintf_flush(void)
                                i++;
                }
 
+               if (color_opens > 0 && buffer[i] == 'm')
+                       color_opens--;
+
                fputc(buffer[i], stdout);
                line_count++;
        }
-- 
2.3.1

-- 
You received this message because you are subscribed to the Google Groups 
"netsniff-ng" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to