From 81b58234ee38933aa49ea75cfbbd2f7b9fe24b9d Mon Sep 17 00:00:00 2001 From: nick black Date: Sat, 25 Jan 2020 01:37:51 -0500 Subject: [PATCH] direct mode: reset terminal, check vals #77 --- src/lib/notcurses.c | 9 ++++++++ src/lib/render.c | 6 ++++++ src/poc/dirgb.c | 52 +++++++++++++++++++++++++++++++++------------ 3 files changed, 53 insertions(+), 14 deletions(-) diff --git a/src/lib/notcurses.c b/src/lib/notcurses.c index f4051f951..6bf444eac 100644 --- a/src/lib/notcurses.c +++ b/src/lib/notcurses.c @@ -918,6 +918,15 @@ err: int ncdirect_stop(ncdirect* nc){ int ret = 0; if(nc){ + if(nc->op && term_emit("op", nc->op, nc->ttyfp, true)){ + ret = -1; + } + if(nc->sgr0 && term_emit("sgr0", nc->sgr0, nc->ttyfp, true)){ + ret = -1; + } + if(nc->oc && term_emit("oc", nc->oc, nc->ttyfp, true)){ + ret = -1; + } free(nc); } return ret; diff --git a/src/lib/render.c b/src/lib/render.c index 5dc4fe160..465142cf0 100644 --- a/src/lib/render.c +++ b/src/lib/render.c @@ -609,10 +609,16 @@ term_fg_rgb8(bool RGBflag, const char* setaf, int colors, FILE* out, } int ncdirect_bg_rgb8(ncdirect* nc, unsigned r, unsigned g, unsigned b){ + if(r > 255 || g > 255 || b > 255){ + return -1; + } return term_bg_rgb8(nc->RGBflag, nc->setab, nc->colors, nc->ttyfp, r, g, b); } int ncdirect_fg_rgb8(ncdirect* nc, unsigned r, unsigned g, unsigned b){ + if(r > 255 || g > 255 || b > 255){ + return -1; + } return term_fg_rgb8(nc->RGBflag, nc->setaf, nc->colors, nc->ttyfp, r, g, b); } diff --git a/src/poc/dirgb.c b/src/poc/dirgb.c index 650a7bd8c..985cfa4c5 100644 --- a/src/poc/dirgb.c +++ b/src/poc/dirgb.c @@ -5,6 +5,40 @@ #include #include +static int +print_b(struct ncdirect* nc, int r, int g, int total){ + int b = total - (r + g); + if(b > 255){ + return 0; + } + int ret = ncdirect_fg_rgb8(nc, r, g, b); + if(ret){ + return -1; + } + printf("X"); + return 0; +} + +static int +print_gb(struct ncdirect* nc, int r, int total){ + for(int g = 0xf ; g <= total - r && g < 256 ; g += 16){ + if(print_b(nc, r, g, total)){ + return -1; + } + } + return 0; +} + +static int +print_rgb(struct ncdirect* nc, int total){ + for(int r = 0xf ; r <= total && r < 256 ; r += 16){ + if(print_gb(nc, r, total)){ + return -1; + } + } + return 0; +} + // direct-mode generation of 4096 RGB foregrounds int main(void){ if(!setlocale(LC_ALL, "")){ @@ -14,20 +48,10 @@ int main(void){ if(!nc){ return EXIT_FAILURE; } - for(int r = 0 ; r < 256 ; r += 16){ - for(int g = 0 ; g < 256 ; g += 16){ - for(int b = 0 ; b < 256 ; b += 16){ - int ret = ncdirect_fg_rgb8(nc, r, g, b); - if(ret){ - ncdirect_stop(nc); - return EXIT_FAILURE; - } - printf("X"); - if(fflush(stdout) == EOF){ - fprintf(stderr, "Error flushing stdout (%s)\n", strerror(errno)); - return EXIT_FAILURE; - } - } + for(int t = 0xf ; t < 768 ; t += 16){ + if(print_rgb(nc, t)){ + ncdirect_stop(nc); + return EXIT_FAILURE; } } printf("\n");