From 9e1088749bee6c101143a17ca853b01c8163ac91 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?John=20Ankarstr=C3=B6m?= Date: Fri, 6 Nov 2020 23:13:26 +0100 Subject: whatever --- l.c | 61 +++++++++++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 47 insertions(+), 14 deletions(-) diff --git a/l.c b/l.c index c707aea..2bcbfd9 100644 --- a/l.c +++ b/l.c @@ -14,8 +14,17 @@ #define prn(...) dprintf(ttyfd, __VA_ARGS__) +char *lines[3] = { + "line 1", + "line 2", + "line 3" +}; + +int current = -1; /* currently selected item */ +int height = 3; /* height of output in rows */ int ttyfd; - +int x, y; /* original cursor position in cols, rows */ + /* enable/disable "raw" mode */ void raw(bool enable) { int r; @@ -23,7 +32,7 @@ void raw(bool enable) { struct termios raw; if (enable) { - r = tcgetattr(0, &orig); + r = tcgetattr(ttyfd, &orig); if (r == -1) err(1, "tcgetattr"); raw = orig; @@ -32,11 +41,11 @@ void raw(bool enable) { raw.c_cflag |= CS8; raw.c_lflag &= ~(ECHO | ICANON | IEXTEN | ISIG); - r = tcsetattr(0, TCSAFLUSH, &raw); + r = tcsetattr(ttyfd, TCSAFLUSH, &raw); if (r == -1) err(1, "tcsetattr"); } else - tcsetattr(0, TCSAFLUSH, &orig); + tcsetattr(ttyfd, TCSAFLUSH, &orig); } /* cursor position report */ @@ -76,19 +85,31 @@ int cpr(int *x, int *y) { return 0; } -void up() {} -void down() {} -void right() {} -void left() {} +/* select item */ +int item(int n) { + if (n < 1 || n > height) return -1; + if (current != -1) + prn("%s", lines[current - 1]); + current = n; + prn("%s%d;%dH", CSI, y + n - 1, 0); + prn(CSI "7m"); + prn("%s", lines[n - 1]); + prn(CSI "0m"); + prn("%s%d;%dH", CSI, y + n - 1, 0); + return 0; +} + +int up() { return item(current-1); } +int down() { return item(current+1); } +int right() {} +int left() {} int main() { - char c, *p; + char c, *p, *phrase; int i, r; struct winsize w; - int height; /* height of output in rows */ - int x, y; /* original cursor position in cols, rows */ - char *ex = "file1 file2 file3\r\nfile4 file5 file6\r\nfile7"; + phrase = NULL; ttyfd = open("/dev/tty", O_RDWR); if (ttyfd == -1) err(1, "open"); @@ -100,9 +121,11 @@ int main() { if (r == -1) goto quit; /* print output */ - prn("%s", ex); + for (i = 0; i < height; i++) + prn("%s\r\n", lines[i]); /* get height of output */ + /* p = ex - 1; height = 0; while (*(++p) != '\0') @@ -120,6 +143,9 @@ int main() { /* restore original cursor position (CUP) */ prn("%s%d;%dH", CSI, y, x); + /* select first item */ + item(1); + while (read(0, &c, 1) != 0) { switch (c) { case 'q': @@ -134,10 +160,17 @@ int main() { if (c == 'C') right(); if (c == 'D') left(); break; + case 13: /* enter */ + goto quit; + phrase = lines[current - 1]; + break; } } quit: - 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); } -- cgit v1.2.3