From af422dd6dd02fd676985134d191edd70aabf6317 Mon Sep 17 00:00:00 2001 From: Benny Baumann Date: Fri, 25 Dec 2020 14:03:57 +0100 Subject: [PATCH] Keep the bold attribute when highlighting selections --- Panel.c | 2 +- RichString.c | 22 ++++++++++++++++++++++ RichString.h | 4 ++++ 3 files changed, 27 insertions(+), 1 deletion(-) diff --git a/Panel.c b/Panel.c index fd9de2b11..831452f36 100644 --- a/Panel.c +++ b/Panel.c @@ -278,7 +278,7 @@ void Panel_draw(Panel* this, bool focus) { } if (item.highlightAttr) { attrset(item.highlightAttr); - RichString_setAttr(&item, item.highlightAttr); + RichString_setAttr_preserveBold(&item, item.highlightAttr); this->selectedLen = itemLen; } mvhline(y + line, x, ' ', this->w); diff --git a/RichString.c b/RichString.c index 904b44b65..1a0d6cef0 100644 --- a/RichString.c +++ b/RichString.c @@ -69,6 +69,15 @@ inline void RichString_setAttrn(RichString* this, int attrs, int start, int fini } } +inline void RichString_setAttrn_preserveBold(RichString* this, int attrs, int start, int finish) { + cchar_t* ch = this->chptr + start; + finish = CLAMP(finish, 0, this->chlen - 1); + for (int i = start; i <= finish; i++) { + ch->attr = (ch->attr & A_BOLD) ? (attrs | A_BOLD) : attrs; + ch++; + } +} + int RichString_findChar(RichString* this, char c, int start) { wchar_t wc = btowc(c); cchar_t* ch = this->chptr + start; @@ -100,6 +109,15 @@ void RichString_setAttrn(RichString* this, int attrs, int start, int finish) { } } +void RichString_setAttrn_preserveBold(RichString* this, int attrs, int start, int finish) { + chtype* ch = this->chptr + start; + finish = CLAMP(finish, 0, this->chlen - 1); + for (int i = start; i <= finish; i++) { + *ch = (*ch & 0xff) | attrs | (*ch & A_BOLD); + ch++; + } +} + int RichString_findChar(RichString* this, char c, int start) { chtype* ch = this->chptr + start; for (int i = start; i < this->chlen; i++) { @@ -123,6 +141,10 @@ void RichString_setAttr(RichString* this, int attrs) { RichString_setAttrn(this, attrs, 0, this->chlen - 1); } +void RichString_setAttr_preserveBold(RichString* this, int attrs) { + RichString_setAttrn_preserveBold(this, attrs, 0, this->chlen - 1); +} + void RichString_append(RichString* this, int attrs, const char* data) { RichString_writeFrom(this, attrs, data, this->chlen, strlen(data)); } diff --git a/RichString.h b/RichString.h index 12b095400..c690f8c10 100644 --- a/RichString.h +++ b/RichString.h @@ -44,12 +44,16 @@ typedef struct RichString_ { void RichString_setAttrn(RichString* this, int attrs, int start, int finish); +void RichString_setAttrn_preserveBold(RichString* this, int attrs, int start, int finish); + int RichString_findChar(RichString* this, char c, int start); void RichString_prune(RichString* this); void RichString_setAttr(RichString* this, int attrs); +void RichString_setAttr_preserveBold(RichString* this, int attrs); + void RichString_append(RichString* this, int attrs, const char* data); void RichString_appendn(RichString* this, int attrs, const char* data, int len);