build ncdirect_dump_cellplane() out of fbuf #2167

This commit is contained in:
nick black 2021-09-17 20:23:28 -04:00 committed by nick black
parent 8c12ca0c06
commit e6ca5ebd5e
3 changed files with 96 additions and 26 deletions

View File

@ -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;
}

View File

@ -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);

View File

@ -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;
}
fbuf f = {};
if(fbuf_init_small(&f)){
return -1;
}
if(term_bg_rgb8(&nc->tcache, &f, (rgb & 0xff0000u) >> 16u, (rgb & 0xff00u) >> 8u, rgb & 0xffu)){
fbuf_free(&f);
return -1;
}
if(fbuf_finalize(&f, nc->ttyfp) < 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_fg_rgb(ncdirect* nc, unsigned rgb){
int ncdirect_set_bg_rgb(ncdirect* nc, unsigned rgb){
fbuf f = {};
if(fbuf_init_small(&f)){
return -1;
}
if(ncdirect_set_bg_rgb_f(nc, rgb, &f)){
fbuf_free(&f);
return -1;
}
if(fbuf_finalize(&f, nc->ttyfp) < 0){
return -1;
}
return 0;
}
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;
}