commit 81203d554763a279dfc53ff7daf9b13ac426fcc9
parent 52d836949e0b7440258ea23ae635613bed6bae41
Author: Brian Swetland <swetland@frotz.net>
Date: Sat, 4 Mar 2023 14:24:19 -0800
tui: minor tidying up
Diffstat:
3 files changed, 27 insertions(+), 20 deletions(-)
diff --git a/tui/test.c b/tui/test.c
@@ -10,10 +10,13 @@ void handle_line(char* line, unsigned len) {
}
if (!strcmp(line, "exit")) {
tui_exit();
+ exit(-1);
}
}
int main(int argc, char** argv) {
- tui_loop(handle_line);
+ tui_init();
+ while (tui_handle_event(handle_line) == 0) ;
+ tui_exit();
return 0;
}
diff --git a/tui/tui.c b/tui/tui.c
@@ -166,13 +166,6 @@ static int handle_event(UX* ux, void (*cb)(char*, unsigned) ) {
return 0;
}
-static int running = 1;
-
-// TODO: handle calls from outside of tui_handle();
-void tui_exit(void) {
- running = 0;
-}
-
static UX ux = {
.list = {
.prev = &ux.list,
@@ -180,21 +173,23 @@ static UX ux = {
},
};
-void tui_loop(void (*cb)(char*, unsigned)) {
+void tui_init(void) {
if (tb_init()) {
fprintf(stderr, "termbox init failed\n");
return;
}
-
tb_select_input_mode(TB_INPUT_ESC | TB_INPUT_SPACE);
-
repaint(&ux);
+}
- while (running && (handle_event(&ux, cb) == 0)) ;
-
+void tui_exit(void) {
tb_shutdown();
}
+int tui_handle_event(void (*cb)(char*, unsigned)) {
+ return handle_event(&ux, cb);
+}
+
static void tui_logline(uint8_t* text, unsigned len) {
LINE* line = malloc(sizeof(LINE));
if (line == NULL) return;
@@ -233,7 +228,7 @@ void tui_ch_destroy(tui_ch_t* ch) {
free(ch);
}
-void tui_ch_vaprintf(tui_ch_t* ch, const char* fmt, va_list ap) {
+void tui_ch_vprintf(tui_ch_t* ch, const char* fmt, va_list ap) {
char tmp[1024];
int n = vsnprintf(tmp, sizeof(tmp), fmt, ap);
char *x = tmp;
@@ -257,7 +252,7 @@ void tui_ch_vaprintf(tui_ch_t* ch, const char* fmt, va_list ap) {
void tui_ch_printf(tui_ch_t* ch, const char* fmt, ...) {
va_list ap;
va_start(ap, fmt);
- tui_ch_vaprintf(ch, fmt, ap);
+ tui_ch_vprintf(ch, fmt, ap);
va_end(ap);
}
@@ -265,9 +260,18 @@ void tui_printf(const char* fmt, ...) {
va_list ap;
tui_ch_t ch = { 0 };
va_start(ap, fmt);
- tui_ch_vaprintf(&ch, fmt, ap);
+ tui_ch_vprintf(&ch, fmt, ap);
va_end(ap);
if (ch.len > 0) {
tui_logline(ch.buffer, ch.len);
}
}
+
+void tui_vprintf(const char* fmt, va_list ap) {
+ tui_ch_t ch = { 0 };
+ tui_ch_vprintf(&ch, fmt, ap);
+ if (ch.len > 0) {
+ tui_logline(ch.buffer, ch.len);
+ }
+}
+
diff --git a/tui/tui.h b/tui/tui.h
@@ -5,15 +5,15 @@
#include <stdarg.h>
-void tui_loop(void (*callback)(char* line, unsigned len));
-
+void tui_init(void);
void tui_exit(void);
+int tui_handle_event(void (*callback)(char* line, unsigned len));
// Write a line (or multiple lines separated by '\n') to the TUI log
// Non-printing and non-ascii characters are ignored.
// Lines larger than the TUI log max width (128) are truncated.
void tui_printf(const char* fmt, ...);
-
+void tui_vprintf(const char* fmt, va_list ap);
// TUI Channels provide a way for different entities to use a
// printf() interface to send log lines to the TUI without
@@ -30,5 +30,5 @@ typedef struct tui_ch tui_ch_t;
int tui_ch_create(tui_ch_t** ch, unsigned flags);
void tui_ch_destroy(tui_ch_t* ch);
void tui_ch_printf(tui_ch_t* ch, const char* fmt, ...);
-void tui_ch_vaprintf(tui_ch_t* ch, const char* fmt, va_list ap);
+void tui_ch_vprintf(tui_ch_t* ch, const char* fmt, va_list ap);