From e6ca5ebd5e4df60afc12ca7f96857c08f62cca15 Mon Sep 17 00:00:00 2001 From: nick black Date: Fri, 17 Sep 2021 20:23:28 -0400 Subject: [PATCH] build ncdirect_dump_cellplane() out of fbuf #2167 --- src/lib/direct.c | 80 ++++++++++++++++++++++++++++++++++++++-------- src/lib/internal.h | 2 ++ src/lib/render.c | 26 +++++++++++---- 3 files changed, 89 insertions(+), 19 deletions(-) diff --git a/src/lib/direct.c b/src/lib/direct.c index f340ec309..57701b0e5 100644 --- a/src/lib/direct.c +++ b/src/lib/direct.c @@ -130,6 +130,11 @@ int ncdirect_cursor_down(ncdirect* nc, int num){ return ret; } +static inline int +ncdirect_cursor_down_f(ncdirect* nc, int num, fbuf* f){ + return emit_scrolls(&nc->tcache, num, f); +} + int ncdirect_clear(ncdirect* nc){ const char* clearscr = get_escape(&nc->tcache, ESCAPE_CLEAR); if(clearscr){ @@ -465,6 +470,54 @@ ncdirect_dump_sprixel(ncdirect* n, const ncplane* np, int xoff, int* y, fbuf* f) return 0; } +static int +ncdirect_set_bg_default_f(ncdirect* nc, fbuf* f){ + if(ncdirect_bg_default_p(nc)){ + return 0; + } + const char* esc; + if((esc = get_escape(&nc->tcache, ESCAPE_BGOP)) != NULL){ + if(fbuf_emit(f, esc) < 0){ + return -1; + } + }else if((esc = get_escape(&nc->tcache, ESCAPE_OP)) != NULL){ + if(fbuf_emit(f, esc) < 0){ + return -1; + } + if(!ncdirect_fg_default_p(nc)){ + if(ncdirect_set_fg_rgb_f(nc, ncchannels_fg_rgb(nc->channels), f)){ + return -1; + } + } + } + ncchannels_set_bg_default(&nc->channels); + return 0; +} + +static int +ncdirect_set_fg_default_f(ncdirect* nc, fbuf* f){ + if(ncdirect_fg_default_p(nc)){ + return 0; + } + const char* esc; + if((esc = get_escape(&nc->tcache, ESCAPE_FGOP)) != NULL){ + if(fbuf_emit(f, esc) < 0){ + return -1; + } + }else if((esc = get_escape(&nc->tcache, ESCAPE_OP)) != NULL){ + if(fbuf_emit(f, esc) < 0){ + return -1; + } + if(!ncdirect_bg_default_p(nc)){ + if(ncdirect_set_bg_rgb_f(nc, ncchannels_bg_rgb(nc->channels), f)){ + return -1; + } + } + } + ncchannels_set_fg_default(&nc->channels); + return 0; +} + static int ncdirect_dump_cellplane(ncdirect* n, const ncplane* np, fbuf* f){ int dimy, dimx; @@ -484,17 +537,18 @@ ncdirect_dump_cellplane(ncdirect* n, const ncplane* np, fbuf* f){ return -1; } if(ncchannels_fg_alpha(channels) == NCALPHA_TRANSPARENT){ - ncdirect_set_fg_default(n); + ncdirect_set_fg_default_f(n, f); }else{ - ncdirect_set_fg_rgb(n, ncchannels_fg_rgb(channels)); + ncdirect_set_fg_rgb_f(n, ncchannels_fg_rgb(channels), f); } if(ncchannels_bg_alpha(channels) == NCALPHA_TRANSPARENT){ - ncdirect_set_bg_default(n); + ncdirect_set_bg_default_f(n, f); }else{ - ncdirect_set_bg_rgb(n, ncchannels_bg_rgb(channels)); + ncdirect_set_bg_rgb_f(n, ncchannels_bg_rgb(channels), f); } //fprintf(stderr, "%03d/%03d [%s] (%03dx%03d)\n", y, x, egc, dimy, dimx); - if(fprintf(n->ttyfp, "%s", strlen(egc) == 0 ? " " : egc) < 0){ + size_t egclen = strlen(egc); + if(fbuf_putn(f, egclen == 0 ? " " : egc, egclen == 0 ? 1 : egclen) < 0){ free(egc); return -1; } @@ -504,27 +558,27 @@ ncdirect_dump_cellplane(ncdirect* n, const ncplane* np, fbuf* f){ // each line of output; this is necessary if our output is lifted out and // used in something e.g. paste(1). // FIXME replace with a SGR clear - ncdirect_set_fg_default(n); - ncdirect_set_bg_default(n); - if(ncfputc('\n', n->ttyfp) == EOF){ + ncdirect_set_fg_default_f(n, f); + ncdirect_set_bg_default_f(n, f); + if(fbuf_putc(f, '\n') < 0){ return -1; } if(y == toty){ - if(ncdirect_cursor_down(n, 1)){ + if(ncdirect_cursor_down_f(n, 1, f)){ return -1; } } } // restore the previous colors if(fgdefault){ - ncdirect_set_fg_default(n); + ncdirect_set_fg_default_f(n, f); }else{ - ncdirect_set_fg_rgb(n, fgrgb); + ncdirect_set_fg_rgb_f(n, fgrgb, f); } if(bgdefault){ - ncdirect_set_bg_default(n); + ncdirect_set_bg_default_f(n, f); }else{ - ncdirect_set_bg_rgb(n, bgrgb); + ncdirect_set_bg_rgb_f(n, bgrgb, f); } return 0; } diff --git a/src/lib/internal.h b/src/lib/internal.h index cf04c3f6e..4f7010240 100644 --- a/src/lib/internal.h +++ b/src/lib/internal.h @@ -1607,6 +1607,8 @@ ncdirect_bg_palindex_p(const ncdirect* nc){ return ncchannels_bg_palindex_p(ncdirect_channels(nc)); } +int ncdirect_set_fg_rgb_f(ncdirect* nc, unsigned rgb, fbuf* f); +int ncdirect_set_bg_rgb_f(ncdirect* nc, unsigned rgb, fbuf* f); int term_fg_rgb8(const tinfo* ti, fbuf* f, unsigned r, unsigned g, unsigned b); const struct blitset* lookup_blitset(const tinfo* tcache, ncblitter_e setid, bool may_degrade); diff --git a/src/lib/render.c b/src/lib/render.c index 19b15f60e..3d5fc79cf 100644 --- a/src/lib/render.c +++ b/src/lib/render.c @@ -1603,7 +1603,7 @@ char* notcurses_at_yx(notcurses* nc, int yoff, int xoff, uint16_t* stylemask, ui return egc; } -int ncdirect_set_bg_rgb(ncdirect* nc, unsigned rgb){ +int ncdirect_set_bg_rgb_f(ncdirect* nc, unsigned rgb, fbuf* f){ if(rgb > 0xffffffu){ return -1; } @@ -1611,22 +1611,29 @@ int ncdirect_set_bg_rgb(ncdirect* nc, unsigned rgb){ && ncchannels_bg_rgb(nc->channels) == rgb){ return 0; } + if(term_bg_rgb8(&nc->tcache, f, (rgb & 0xff0000u) >> 16u, (rgb & 0xff00u) >> 8u, rgb & 0xffu)){ + return -1; + } + ncchannels_set_bg_rgb(&nc->channels, rgb); + return 0; +} + +int ncdirect_set_bg_rgb(ncdirect* nc, unsigned rgb){ fbuf f = {}; if(fbuf_init_small(&f)){ return -1; } - if(term_bg_rgb8(&nc->tcache, &f, (rgb & 0xff0000u) >> 16u, (rgb & 0xff00u) >> 8u, rgb & 0xffu)){ + if(ncdirect_set_bg_rgb_f(nc, rgb, &f)){ fbuf_free(&f); return -1; } if(fbuf_finalize(&f, nc->ttyfp) < 0){ return -1; } - ncchannels_set_bg_rgb(&nc->channels, rgb); return 0; } -int ncdirect_set_fg_rgb(ncdirect* nc, unsigned rgb){ +int ncdirect_set_fg_rgb_f(ncdirect* nc, unsigned rgb, fbuf* f){ if(rgb > 0xffffffu){ return -1; } @@ -1634,18 +1641,25 @@ int ncdirect_set_fg_rgb(ncdirect* nc, unsigned rgb){ && ncchannels_fg_rgb(nc->channels) == rgb){ return 0; } + if(term_fg_rgb8(&nc->tcache, f, (rgb & 0xff0000u) >> 16u, (rgb & 0xff00u) >> 8u, rgb & 0xffu)){ + return -1; + } + ncchannels_set_fg_rgb(&nc->channels, rgb); + return 0; +} + +int ncdirect_set_fg_rgb(ncdirect* nc, unsigned rgb){ fbuf f = {}; if(fbuf_init_small(&f)){ return -1; } - if(term_fg_rgb8(&nc->tcache, &f, (rgb & 0xff0000u) >> 16u, (rgb & 0xff00u) >> 8u, rgb & 0xffu)){ + if(ncdirect_set_fg_rgb_f(nc, rgb, &f)){ fbuf_free(&f); return -1; } if(fbuf_finalize(&f, nc->ttyfp) < 0){ return -1; } - ncchannels_set_fg_rgb(&nc->channels, rgb); return 0; }