Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stopgap solution to preserve colors and attributes on highlighting #1542

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Panel.c
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ void Panel_draw(Panel* this, bool force_redraw, bool focus, bool highlightSelect
}
if (item.highlightAttr) {
attrset(item.highlightAttr);
RichString_setAttr(&item, item.highlightAttr);
RichString_setAttr_preserveWithStandout(&item, item.highlightAttr);
this->selectedLen = itemLen;
}
mvhline(y + line, x, ' ', this->w);
Expand Down
74 changes: 74 additions & 0 deletions RichString.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ void RichString_rewind(RichString* this, int count) {
RichString_setLen(this, this->chlen - count);
}

void RichString_setAttr_preserveWithStandout(RichString* this, int attrs) {
RichString_setAttrn_preserveWithStandout(this, attrs, 0, this->chlen - 1);
}

#ifdef HAVE_LIBNCURSESW

static size_t mbstowcs_nonfatal(wchar_t* restrict dest, const char* restrict src, size_t n) {
Expand Down Expand Up @@ -160,6 +164,41 @@ inline void RichString_setAttrn(RichString* this, int attrs, int start, int char
}
}

void RichString_setAttrn_preserveWithStandout(RichString* this, int attrs, int start, int finish) {
finish = CLAMP(finish, 0, this->chlen - 1);

// Extract the foreground and background color indexes from the passed attrs
short passed_color_pair_number = (short)PAIR_NUMBER(attrs);
short passed_fg_color = -1, passed_bg_color = -1;
if (passed_color_pair_number != 0) {
pair_content(passed_color_pair_number, &passed_fg_color, &passed_bg_color);
}

cchar_t* ch = this->chptr + start;
for (int i = start; i <= finish; i++) {
// Extract foreground and background color indexes from the current char
short currentCharPairNum = (short)PAIR_NUMBER(ch->attr);
short before_fg_color = -1, before_bg_color = -1;
if (currentCharPairNum != 0) {
pair_content(currentCharPairNum, &before_fg_color, &before_bg_color);
}

// TODO: When text color matches higlight, the resulting STANDOUT is the same as on default text,
// so we at least set italics
chtype attrToPass = A_STANDOUT;
if (before_fg_color == passed_bg_color) {
attrToPass |= A_ITALIC;
}
// If current char is not a space and its ColorPair Index is not the default 0,
// apply our own attrToPass with STANDOUT + optionally ITALICS,
// instead of the passed attrs, which has the BG highlight color
ch->attr = (ch->chars[0] != L' ' && currentCharPairNum != 0)
? (ch->attr | attrToPass)
: (unsigned int)attrs;
ch++;
}
}

void RichString_appendChr(RichString* this, int attrs, char c, int count) {
int from = this->chlen;
int newLen = from + count;
Expand Down Expand Up @@ -210,6 +249,41 @@ void RichString_setAttrn(RichString* this, int attrs, int start, int charcount)
}
}

void RichString_setAttrn_preserveWithStandout(RichString* this, int attrs, int start, int finish) {
finish = CLAMP(finish, 0, this->chlen - 1);

// Extract the foreground and background color indexes from the passed attrs
short passed_color_pair_number = (short)PAIR_NUMBER(attrs);
short passed_fg_color = -1, passed_bg_color = -1;
if (passed_color_pair_number != 0) {
pair_content(passed_color_pair_number, &passed_fg_color, &passed_bg_color);
}

chtype* ch = this->chptr + start;
for (int i = start; i <= finish; i++) {
// Extract foreground and background color indexes from the current char
short currentCharPairNum = (short)PAIR_NUMBER(*ch);
short before_fg_color = -1, before_bg_color = -1;
if (currentCharPairNum != 0) {
pair_content(currentCharPairNum, &before_fg_color, &before_bg_color);
}

// TODO: When text color matches higlight, the resulting STANDOUT is the same as on default text,
// so we at least set italics
chtype attrToPass = A_STANDOUT;
if (before_fg_color == passed_bg_color) {
attrToPass |= A_ITALIC;
}
// If current char is not a space and its ColorPair Index is not the default 0,
// apply our own attrToPass with STANDOUT + optionally ITALICS,
// instead of the passed attrs, which has the BG highlight color
*ch = ((*ch & A_CHARTEXT) != L' ' && currentCharPairNum != 0)
? *ch | attrToPass
: (*ch & A_CHARTEXT) | (unsigned int)attrs;
ch++;
}
}

void RichString_appendChr(RichString* this, int attrs, char c, int count) {
int from = this->chlen;
int newLen = from + count;
Expand Down
4 changes: 4 additions & 0 deletions RichString.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,14 @@ void RichString_rewind(RichString* this, int count);

void RichString_setAttrn(RichString* this, int attrs, int start, int charcount);

void RichString_setAttrn_preserveWithStandout(RichString* this, int attrs, int start, int finish);

int RichString_findChar(const RichString* this, char c, int start);

void RichString_setAttr(RichString* this, int attrs);

void RichString_setAttr_preserveWithStandout(RichString* this, int attrs);

void RichString_appendChr(RichString* this, int attrs, char c, int count);

/* All appending and writing functions return the number of written characters (not columns). */
Expand Down
Loading