cut last too long line
authorLaurent Mazet <mazet@softndesign.org>
Mon, 6 May 2024 21:20:55 +0000 (23:20 +0200)
committerLaurent Mazet <mazet@softndesign.org>
Mon, 6 May 2024 21:20:55 +0000 (23:20 +0200)
cmore.c

diff --git a/cmore.c b/cmore.c
index d19de9f4465406db534e2eb9886184a013648403..9b08436e1998da67c86d16c220739edb22a12c8f 100644 (file)
--- a/cmore.c
+++ b/cmore.c
@@ -4,6 +4,7 @@
 /* winlnk: cmd.o debug.o tui.o -lpdcurses */
 
 #include <stdlib.h>
+#include <string.h>
 #include <unistd.h>
 
 #include "cmd.h"
@@ -33,7 +34,7 @@ void display (char **lines, int xmax, int ymax)
         clsbody ();
         int i = 0;
         int eol = 0;
-        for (i = 0; i <= ymax; i++) {
+        for (i = 0; i < ymax - 1; i++) {
             if (lines [skip + i] == NULL) {
                 break;
             }
@@ -43,11 +44,26 @@ void display (char **lines, int xmax, int ymax)
             bodymsg (lines[skip + i]);
             eol = (lines[skip + i][xmax - 1 ] == '\0');
         }
+        if (lines [skip + i] != NULL) {
+            if (eol) {
+                bodymsg ("\n");
+            }
+            char *str = (char *) calloc (1, xmax + 1);
+            strcpy (str, lines[skip + i]);
+            if (str[xmax - 1] != '\0') {
+                VERBOSE (DEBUG, fprintf (stdout, "last line too long\n"));
+                strcpy (str + xmax - 3, ">>");
+            }
+            bodymsg (str);
+            free (str);
+        }
+
         if (second > 0) {
             DoExit ();
             break;
         }
         int key = waitforkey ();
+        VERBOSE (DEBUG, fprintf (stdout, "key pressed: 0x%04x (%c)\n", key, (key >= ' ') && (key < 256) ? key : '.'));
         switch (key) {
         case KEY_ESC:
 #ifndef __PDCURSES__
@@ -81,8 +97,6 @@ void display (char **lines, int xmax, int ymax)
             stop = 1;
             break;
 #endif
-        default:
-            printf ("key pressed: %c (0x%04x)\n", (char)key, key);
         }
     }
 
@@ -260,5 +274,7 @@ int main (int argc, char *argv[])
 /* test: (sleep 1; echo -n q) | cmore.exe -c 'ip addr' */
 /* test: (sleep 1; echo -ne '\e\e') | cmore.exe -f tui.c */
 /* test: (sleep 1; echo -n kkkkk; sleep 1; echo -n i; sleep 1; echo -n q) | cmore.exe -c 'ip addr' */
+/* test: (sleep 1; echo -n q) | cmore.exe -c 'echo -n a; for i in $(seq 1 '$(( $COLUMNS*($LINES-4)-2 ))'); do echo -n .; done; echo -n z' -v 5 | grep -q 'last line too long' */
+/* test: (sleep 1; echo -n q) | cmore.exe -c 'echo -n a; for i in $(seq 1 '$(( $COLUMNS*($LINES-4)-3 ))'); do echo -n .; done; echo -n z' -v 5 | grep -q 'last line too long'; test $? -eq 1 */
 
 /* vim: set ts=4 sw=4 et: */