summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--l.c24
1 files changed, 15 insertions, 9 deletions
diff --git a/l.c b/l.c
index 2248da6..c707aea 100644
--- a/l.c
+++ b/l.c
@@ -12,6 +12,8 @@
#define ESC "\033"
#define CSI ESC "["
+#define prn(...) dprintf(ttyfd, __VA_ARGS__)
+
int ttyfd;
/* enable/disable "raw" mode */
@@ -42,7 +44,7 @@ int cpr(int *x, int *y) {
char c, xbuf[4], ybuf[4];
int i;
- write (1, CSI "6n", 2+2);
+ prn(CSI "6n");
/* get CSI */
read(1, &c, 1);
@@ -81,9 +83,12 @@ void left() {}
int main() {
char c, *p;
- char *ex = "file1 file2 file3\r\nfile4 file5 file6\r\nfile7";
- int i, r, x, x2, y, y2;
+ 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";
ttyfd = open("/dev/tty", O_RDWR);
if (ttyfd == -1) err(1, "open");
@@ -95,25 +100,25 @@ int main() {
if (r == -1) goto quit;
/* print output */
- dprintf(ttyfd, "%s", ex);
+ prn("%s", ex);
/* get height of output */
p = ex - 1;
- i = 0;
+ height = 0;
while (*(++p) != '\0')
- if (*p == '\n') i++;
+ if (*p == '\n') height++;
/* get height of terminal */
r = ioctl(ttyfd, TIOCGWINSZ, &w);
if (r == -1) err(1, "ioctl");
/* correct cursor position if original cursor was near bottom */
- r = y + i - w.ws_row;
+ r = y + height - w.ws_row;
if (r > 0)
y = y - r;
- /* restore original cursor position */
- dprintf(ttyfd, "%s%d;%dH", CSI, y, x); /* restore cursor position (CUP) */
+ /* restore original cursor position (CUP) */
+ prn("%s%d;%dH", CSI, y, x);
while (read(0, &c, 1) != 0) {
switch (c) {
@@ -133,5 +138,6 @@ int main() {
}
quit:
+ prn(CSI "J"); /* delete from cursor to end of display (ED) */
raw(false);
}