commit f79c989fd7b8ab805931f8b6e50240d06d24fca7
parent 65ce53868833de66b4270e56c3a3e31b4f0a4615
Author: FRIGN <dev@frign.de>
Date: Fri, 11 Jul 2014 13:54:19 +0200
Get rid of ncurses
What catpoint needs can easily be accomplished with termios, while
getting rid of the heavy ncurses-dependency.
All termios-tweaks used are POSIX-compliant.
Diffstat:
M | catpoint.c | | | 36 | +++++++++++++++++++----------------- |
1 file changed, 19 insertions(+), 17 deletions(-)
diff --git a/catpoint.c b/catpoint.c
@@ -5,11 +5,11 @@
#include <sys/mman.h>
#include <err.h>
-#include <curses.h>
#include <fcntl.h>
#include <locale.h>
#include <stdio.h>
#include <stdlib.h>
+#include <termios.h>
#include <unistd.h>
struct slide {
@@ -20,7 +20,7 @@ struct slide {
static void
printslide(struct slide *s)
{
- printw("%.*s", (int)s->siz, s->buf);
+ printf("%.*s", (int)s->siz, s->buf);
}
int
@@ -29,6 +29,7 @@ main(int argc, char *argv[])
int c, i, fd;
struct slide *s;
struct stat sb;
+ static struct termios oldterm, newterm;
if (argc == 1)
errx(1, "usage: %s file ...", argv[0]);
@@ -54,31 +55,29 @@ main(int argc, char *argv[])
close(fd);
}
- /* init curses */
- initscr();
- cbreak();
- noecho();
- nonl();
- intrflush(stdscr, FALSE);
- keypad(stdscr, TRUE);
- curs_set(FALSE); /* hide cursor */
+ /* set up terminal */
+ tcgetattr(STDIN_FILENO, &oldterm);
+ newterm = oldterm;
+ newterm.c_lflag &= ~(ECHO | ICANON);
+ tcsetattr(STDIN_FILENO, TCSANOW, &newterm);
+ printf("\e[?25l"); /* hide cursor */
/* start */
i = 0;
show:
/* display slide */
- clear();
+ printf("\x1b[2J\x1b[H"); /* clear */
printslide(&s[i]);
again:
- c = getch();
+ c = getchar();
switch (c) {
case 'q':
break;
/* next */
case 'l':
case 'j':
- case KEY_RIGHT:
- case KEY_DOWN:
+ case 'C': /* right */
+ case 'B': /* down */
if (i < argc - 1) {
i++;
goto show;
@@ -87,13 +86,15 @@ again:
/* prev */
case 'h':
case 'k':
- case KEY_LEFT:
- case KEY_UP:
+ case 'D': /* left */
+ case 'A': /* up */
if (i > 0) {
i--;
goto show;
}
goto again;
+ case '\033':
+ case '[':
default:
goto again;
}
@@ -102,7 +103,8 @@ again:
for (i = 0; argv[i] != NULL; i++)
munmap(s[i].buf, s[i].siz);
- endwin(); /* restore terminal */
+ printf("\e[?25h"); /* unhide cursor */
+ tcsetattr(STDIN_FILENO, TCSANOW, &oldterm); /* restore terminal */
return (0);
}