From 545fcfb1ee7b4004707da7183399ad822365c7a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?John=20Ankarstr=C3=B6m?= Date: Fri, 6 Nov 2020 23:30:07 +0100 Subject: make it work with pipes --- l.c | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/l.c b/l.c index 2bcbfd9..46fac3e 100644 --- a/l.c +++ b/l.c @@ -56,14 +56,14 @@ int cpr(int *x, int *y) { prn(CSI "6n"); /* get CSI */ - read(1, &c, 1); + read(ttyfd, &c, 1); if (c != '\033') return -1; - read(1, &c, 1); + read(ttyfd, &c, 1); if (c != '[') return -1; /* get x */ i = 0; - while (read(1, &c, 1) && c != ';') { + while (read(ttyfd, &c, 1) && c != ';') { if (i > 3 || !isdigit(c)) return -1; ybuf[i++] = c; } @@ -72,7 +72,7 @@ int cpr(int *x, int *y) { /* get y */ i = 0; - while (read(1, &c, 1) && c != 'R') { + while (read(ttyfd, &c, 1) && c != 'R') { if (i > 3 || !isdigit(c)) return -1; xbuf[i++] = c; } @@ -118,7 +118,10 @@ int main() { /* save original cursor position */ r = cpr(&x, &y); - if (r == -1) goto quit; + if (r == -1) { + fprintf(stderr, "could not retrieve cursor position"); + goto quit; + } /* print output */ for (i = 0; i < height; i++) @@ -161,16 +164,19 @@ int main() { if (c == 'D') left(); break; case 13: /* enter */ - goto quit; phrase = lines[current - 1]; - break; + goto quit; } } - quit: - //prn("%s%d;%dH", CSI, y, x); - //prn(CSI "J"); /* delete from cursor to end of display (ED) */ + prn("%s%d;%dH", CSI, y, x); + prn(CSI "J"); /* delete from cursor to end of display (ED) */ raw(false); if (phrase != NULL) printf("%s\n", phrase); + return 0; +die: + raw(false); + puts("died"); + return 0; } -- cgit v1.2.3