login

<     >

2015-08-22 12:49:50 (UTC-03:00)

Marcel Rodrigues <marcelgmr@gmail.com>

Postpone autowrapping until next addchar() call.

Some programs will fill a row and then send CR+LF to go to the row
immediately below it. If autowrap acts as soon as we reach the end
of the row, it will result in  doubled CR+LF sequences, so we need
to make  sure the program  is actually  trying to add a  character
after the last column before we wrap the line.

An example of a program that needs this fix is procps-ng's top.

diff --git a/term.c b/term.c
index 0f0fa15..f7bbdc0 100644
--- a/term.c
+++ b/term.c
@@ -126,7 +126,7 @@ char_code(Term *term)
 static int
 within_bounds(Term *term, int row, int col)
 {
-    if (row < 0 || row >= term->rows || col < 0 || col >= term->cols) {
+    if (row < 0 || row >= term->rows || col < 0 || col > term->cols) {
         logfmt("position %d,%d is out of bounds %d,%d\n",
                row+1, col+1, term->rows, term->cols);
         return 0;
@@ -177,6 +177,17 @@ static void
 addchar(Term *term, uint16_t code)
 {
     Cell cell = (Cell) {code, term->attr, term->pair};
+    if (term->col >= term->cols) {
+        if (term->mode & M_AUTOWRAP) {
+            term->col = 0;
+            if (term->row < term->bot)
+                term->row++;
+            else
+                scroll_down(term);
+        } else {
+            term->col = term->cols - 1;
+        }
+    }
     if (!within_bounds(term, term->row, term->col))
         return;
     if (term->mode & M_INSERT) {
@@ -190,15 +201,7 @@ addchar(Term *term, uint16_t code)
     } else {
         term->addr[term->row][term->col] = cell;
     }
-    if (term->col < term->cols - 1) {
-        term->col++;
-    } else if (term->mode & M_AUTOWRAP) {
-        if (term->row < term->bot)
-            term->row++;
-        else
-            scroll_down(term);
-        term->col = 0;
-    }
+    term->col++;
 }
 
 static void