summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn Ankarström <john@ankarstrom.se>2020-11-06 23:13:26 +0100
committerJohn Ankarström <john@ankarstrom.se>2020-11-06 23:13:26 +0100
commit9e1088749bee6c101143a17ca853b01c8163ac91 (patch)
treec4032bed347c64af697e56c0cbaa0563373dada7
parent867de51629975db34b5745118fb283d126f476ab (diff)
downloadlst-9e1088749bee6c101143a17ca853b01c8163ac91.tar.gz
whatever
-rw-r--r--l.c61
1 files 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);
}