Added some functionality, including scrolling.

This commit is contained in:
Bruce Hill 2020-04-18 19:12:26 -07:00
parent 3c5fb65994
commit d86a21678a
2 changed files with 38 additions and 8 deletions

31
btui.h
View File

@ -33,6 +33,17 @@ int btui_move_cursor(btui_t *bt, int x, int y);
char *btui_keyname(int key, char *buf);
int btui_keynamed(const char *name);
int btui_set_attributes(btui_t *bt, unsigned long attrs);
int btui_set_fg_rgb(btui_t *bt, unsigned char r, unsigned char g, unsigned char b);
int btui_set_bg_rgb(btui_t *bt, unsigned char r, unsigned char g, unsigned char b);
int btui_set_fg_hex(btui_t *bt, int hex);
int btui_set_bg_hex(btui_t *bt, int hex);
#define btui_printf(bt, ...) fprintf((bt)->out, __VA_ARGS__)
#define btui_flush(bt) fflush((bt)->out)
#define btui_clear(bt) fputs("\033[2J", (bt)->out)
#define btui_clear_below(bt) fputs("\033[J", (bt)->out)
#define btui_clear_above(bt) fputs("\033[1J", (bt)->out)
#define btui_clear_eol(bt) fputs("\033[K", (bt)->out)
#define btui_clear_line(bt) fputs("\033[2K", (bt)->out)
// Terminal escape sequences:
#define T_WRAP "7"
@ -472,23 +483,23 @@ int btui_move_cursor(btui_t *bt, int x, int y)
return fprintf(bt->out, "\033[%d;%dH", y+1, x+1);
}
int set_fg_rgb(btui_t *bt, unsigned char r, unsigned char g, unsigned char b)
int btui_set_fg_rgb(btui_t *bt, unsigned char r, unsigned char g, unsigned char b)
{
return fprintf(bt->out, "\033[38;2;%d;%d;%dm", r, g, b);
}
int set_bg_rgb(btui_t *bt, unsigned char r, unsigned char g, unsigned char b)
int btui_set_bg_rgb(btui_t *bt, unsigned char r, unsigned char g, unsigned char b)
{
return fprintf(bt->out, "\033[48;2;%d;%d;%dm", r, g, b);
}
int set_fg_hex(btui_t *bt, int hex)
int btui_set_fg_hex(btui_t *bt, int hex)
{
return fprintf(bt->out, "\033[38;2;%d;%d;%dm",
(hex >> 16) & 0xFF, (hex >> 8) & 0xFF, hex & 0xFF);
}
int set_bg_hex(btui_t *bt, int hex)
int btui_set_bg_hex(btui_t *bt, int hex)
{
return fprintf(bt->out, "\033[48;2;%d;%d;%dm",
(hex >> 16) & 0xFF, (hex >> 8) & 0xFF, hex & 0xFF);
@ -553,5 +564,17 @@ int btui_set_attributes(btui_t *bt, unsigned long attrs)
return printed;
}
int btui_scroll(btui_t *bt, int firstline, int lastline, int scroll_amount)
{
if (scroll_amount > 0) {
return fprintf(bt->out, "\033[%d;%dr\033[%dS\033[r",
firstline, lastline, scroll_amount);
} else if (scroll_amount < 0) {
return fprintf(bt->out, "\033[%d;%dr\033[%dT\033[r",
firstline, lastline, -scroll_amount);
}
return 0;
}
#endif
// vim: ts=4 sw=0 et cino=L2,l1,(0,W4,m1

15
test.c
View File

@ -10,19 +10,26 @@ int main(void)
int i = 0;
while (!done) {
btui_move_cursor(bt, 0, bt->height-1);
fprintf(bt->out, "Update %d, size = %dx%d", i++, bt->width, bt->height);
fflush(bt->out);
btui_printf(bt, "Update %d, size = %dx%d", i++, bt->width, bt->height);
btui_flush(bt);
int key = btui_getkey(bt, NULL, NULL);
switch (key) {
case 'q': case KEY_CTRL_C: done = 1; break;
case -1: break;
case KEY_ARROW_DOWN:
btui_scroll(bt, 1, bt->height-1, +1);
break;
case KEY_ARROW_UP:
btui_scroll(bt, 1, bt->height-1, -1);
break;
default: {
char buf[256] = {0};
btui_keyname(key, buf);
btui_move_cursor(bt, x, y++);
btui_set_attributes(bt, BTUI_FG_YELLOW | BTUI_BOLD);
fprintf(bt->out, "Pressed: %s", buf);
//btui_set_attributes(bt, BTUI_FG_YELLOW | BTUI_BOLD);
btui_set_fg_hex(bt, 0xacff40);
btui_printf(bt, "Pressed: %s", buf);
btui_set_attributes(bt, BTUI_NORMAL);
fflush(bt->out);
break;