From 0d81fb25c7721d6c19fb3bcbbe94c74b089fcb8a Mon Sep 17 00:00:00 2001 From: nick black Date: Sun, 25 Apr 2021 01:17:06 -0400 Subject: [PATCH] textplay: read stdin, play to plane with ncplane_puttext() --- src/poc/textplay.c | 71 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 src/poc/textplay.c diff --git a/src/poc/textplay.c b/src/poc/textplay.c new file mode 100644 index 000000000..2efb9b137 --- /dev/null +++ b/src/poc/textplay.c @@ -0,0 +1,71 @@ +#include +#include + +static const uint32_t LOWCOLOR = 0x004080; +static const uint32_t HICOLOR = 0x00ff80; +static const uint32_t NANOSEC = 1000000000ull / 600; // 600 cps +#define MARGIN 4 + +static struct notcurses* +init(void){ + struct notcurses_options opts = { + .margin_t = MARGIN, + .margin_r = MARGIN, + .margin_b = MARGIN, + .margin_l = MARGIN, + }; + struct notcurses* nc = notcurses_init(&opts, stdout); + return nc; +} + +static int +colorize(struct ncplane* n){ + (void)n; + return 0; +} + +static int +textplay(struct notcurses* nc){ + char* buf = NULL; + size_t buflen = 1; + int c; + struct ncplane* stdn = notcurses_stdplane(nc); + ncplane_set_scrolling(stdn, true); + while((c = getc(stdin)) != EOF){ + char* tmp = realloc(buf, buflen + 1); + if(tmp == NULL){ + free(buf); + return -1; + } + buf = tmp; + buf[buflen - 1] = c; + buf[buflen++] = '\0'; + ncplane_home(stdn); + int pt = ncplane_puttext(stdn, 0, NCALIGN_LEFT, buf, NULL); + if(pt < 0){ + free(buf); + return -1; + } + if(notcurses_render(nc)){ + free(buf); + return -1; + } + struct timespec ts = { + .tv_sec = 0, .tv_nsec = NANOSEC, + }; + clock_nanosleep(CLOCK_MONOTONIC, 0, &ts, NULL); + } + return 0; +} + +int main(void){ + struct notcurses* nc = init(); + if(nc == NULL){ + return EXIT_FAILURE; + } + textplay(nc); + if(notcurses_stop(nc)){ + return EXIT_FAILURE; + } + return EXIT_SUCCESS; +}