From 0e9641e0f51233987176a3ec32830db8cfce7ea8 Mon Sep 17 00:00:00 2001 From: nick black Date: Wed, 23 Jun 2021 21:01:19 -0400 Subject: [PATCH] direct mode: kill a bunch of style-related duplication --- src/lib/direct.c | 91 ++++++++++++++++-------------------------------- 1 file changed, 30 insertions(+), 61 deletions(-) diff --git a/src/lib/direct.c b/src/lib/direct.c index 9c2386d40..77c464535 100644 --- a/src/lib/direct.c +++ b/src/lib/direct.c @@ -864,25 +864,43 @@ char* ncdirect_readline(ncdirect* n, const char* prompt){ } static inline int -ncdirect_style_emit(ncdirect* n, unsigned stylebits, FILE* out){ +ncdirect_style_emit(ncdirect* n, unsigned* stylebits, FILE* out){ int r = -1; const char* esc; - if(stylebits == 0 && (esc = get_escape(&n->tcache, ESCAPE_SGR0))){ + if(*stylebits == 0 && (esc = get_escape(&n->tcache, ESCAPE_SGR0))){ r = term_emit(esc, n->ttyfp, false); }else if( (esc = get_escape(&n->tcache, ESCAPE_SGR)) ){ - r = term_emit(tiparm(esc, stylebits & NCSTYLE_STANDOUT, - stylebits & NCSTYLE_UNDERLINE, - stylebits & NCSTYLE_REVERSE, - stylebits & NCSTYLE_BLINK, - stylebits & NCSTYLE_DIM, - stylebits & NCSTYLE_BOLD, - stylebits & NCSTYLE_INVIS, + r = term_emit(tiparm(esc, *stylebits & NCSTYLE_STANDOUT, + *stylebits & NCSTYLE_UNDERLINE, + *stylebits & NCSTYLE_REVERSE, + *stylebits & NCSTYLE_BLINK, + *stylebits & NCSTYLE_DIM, + *stylebits & NCSTYLE_BOLD, + *stylebits & NCSTYLE_INVIS, 0 /* protect */, 0), out, false); }else{ // no sgr, interesting. return failure if our stylebits were provided? // back off to individual enablers? FIXME return 0; } + // sgr will blow away non-sgr properties if they were set beforehand + n->stylemask &= ~(NCSTYLE_ITALIC | NCSTYLE_STRUCK | NCSTYLE_UNDERCURL); + if(term_setstyle(n->ttyfp, n->stylemask, *stylebits, NCSTYLE_ITALIC, + get_escape(&n->tcache, ESCAPE_SITM), + get_escape(&n->tcache, ESCAPE_RITM))){ + return -1; + } + if(term_setstyle(n->ttyfp, n->stylemask, *stylebits, NCSTYLE_STRUCK, + get_escape(&n->tcache, ESCAPE_SMXX), + get_escape(&n->tcache, ESCAPE_RMXX))){ + return -1; + } + if(term_setstyle(n->ttyfp, n->stylemask, *stylebits, NCSTYLE_UNDERCURL, + get_escape(&n->tcache, ESCAPE_SMULX), + get_escape(&n->tcache, ESCAPE_SMULNOX))){ + return -1; + } + n->stylemask = *stylebits; // sgr resets colors, so set them back up if not defaults if(r == 0){ // emitting an sgr resets colors. if we want to be default, that's no @@ -921,23 +939,7 @@ int ncdirect_styles_on(ncdirect* n, unsigned stylebits){ int ncdirect_on_styles(ncdirect* n, unsigned stylebits){ uint32_t stylemask = n->stylemask | stylebits; - if(ncdirect_style_emit(n, stylemask, n->ttyfp) == 0){ - if(term_setstyle(n->ttyfp, n->stylemask, stylemask, NCSTYLE_ITALIC, - get_escape(&n->tcache, ESCAPE_SITM), - get_escape(&n->tcache, ESCAPE_RITM))){ - return 0; - } - if(term_setstyle(n->ttyfp, n->stylemask, stylemask, NCSTYLE_STRUCK, - get_escape(&n->tcache, ESCAPE_SMXX), - get_escape(&n->tcache, ESCAPE_RMXX))){ - return -1; - } - if(term_setstyle(n->ttyfp, n->stylemask, stylemask, NCSTYLE_UNDERCURL, - get_escape(&n->tcache, ESCAPE_SMULX), - get_escape(&n->tcache, ESCAPE_SMULNOX))){ - return -1; - } - n->stylemask = stylemask; + if(ncdirect_style_emit(n, &stylemask, n->ttyfp) == 0){ return 0; } return -1; @@ -954,23 +956,7 @@ unsigned ncdirect_styles(ncdirect* n){ // turn off any specified stylebits int ncdirect_off_styles(ncdirect* n, unsigned stylebits){ uint32_t stylemask = n->stylemask & ~stylebits; - if(ncdirect_style_emit(n, stylemask, n->ttyfp) == 0){ - if(term_setstyle(n->ttyfp, n->stylemask, stylemask, NCSTYLE_ITALIC, - get_escape(&n->tcache, ESCAPE_SITM), - get_escape(&n->tcache, ESCAPE_RITM))){ - return -1; - } - if(term_setstyle(n->ttyfp, n->stylemask, stylemask, NCSTYLE_STRUCK, - get_escape(&n->tcache, ESCAPE_SMXX), - get_escape(&n->tcache, ESCAPE_RMXX))){ - return -1; - } - if(term_setstyle(n->ttyfp, n->stylemask, stylemask, NCSTYLE_UNDERCURL, - get_escape(&n->tcache, ESCAPE_SMULX), - get_escape(&n->tcache, ESCAPE_SMULNOX))){ - return -1; - } - n->stylemask = stylemask; + if(ncdirect_style_emit(n, &stylemask, n->ttyfp) == 0){ return 0; } return -1; @@ -986,26 +972,9 @@ int ncdirect_set_styles(ncdirect* n, unsigned stylebits){ return -1; } uint32_t stylemask = stylebits; - if(ncdirect_style_emit(n, stylemask, n->ttyfp)){ - return -1; - } - n->stylemask &= !(NCSTYLE_ITALIC | NCSTYLE_STRUCK); // sgr clears both - if(term_setstyle(n->ttyfp, n->stylemask, stylemask, NCSTYLE_ITALIC, - get_escape(&n->tcache, ESCAPE_SITM), - get_escape(&n->tcache, ESCAPE_RITM))){ - return -1; - } - if(term_setstyle(n->ttyfp, n->stylemask, stylemask, NCSTYLE_STRUCK, - get_escape(&n->tcache, ESCAPE_SMXX), - get_escape(&n->tcache, ESCAPE_RMXX))){ - return -1; - } - if(term_setstyle(n->ttyfp, n->stylemask, stylemask, NCSTYLE_UNDERCURL, - get_escape(&n->tcache, ESCAPE_SMULX), - get_escape(&n->tcache, ESCAPE_SMULNOX))){ + if(ncdirect_style_emit(n, &stylemask, n->ttyfp)){ return -1; } - n->stylemask = stylemask; return 0; }