Skip to content

Commit e75c083

Browse files
C0rn3jBenBE
authored andcommitted
Try to keep text highlighting with highlighted processes
This tries to keep special markup like large numbers and other special markup inside a process row even when such a line has been highlighted by being selected, followed, or otherwise marked in the list view. Alternate take on PR #434.
1 parent 9c316cc commit e75c083

File tree

3 files changed

+79
-1
lines changed

3 files changed

+79
-1
lines changed

Panel.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ void Panel_draw(Panel* this, bool force_redraw, bool focus, bool highlightSelect
289289
}
290290
if (item.highlightAttr) {
291291
attrset(item.highlightAttr);
292-
RichString_setAttr(&item, item.highlightAttr);
292+
RichString_setAttr_preserveWithStandout(&item, item.highlightAttr);
293293
this->selectedLen = itemLen;
294294
}
295295
mvhline(y + line, x, ' ', this->w);

RichString.c

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,10 @@ void RichString_rewind(RichString* this, int count) {
6262
RichString_setLen(this, this->chlen - count);
6363
}
6464

65+
void RichString_setAttr_preserveWithStandout(RichString* this, int attrs) {
66+
RichString_setAttrn_preserveWithStandout(this, attrs, 0, this->chlen - 1);
67+
}
68+
6569
#ifdef HAVE_LIBNCURSESW
6670

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

167+
void RichString_setAttrn_preserveWithStandout(RichString* this, int attrs, int start, int finish) {
168+
finish = CLAMP(finish, 0, this->chlen - 1);
169+
170+
// Extract the foreground and background color indexes from the passed attrs
171+
short passed_color_pair_number = (short)PAIR_NUMBER(attrs);
172+
short passed_fg_color = -1, passed_bg_color = -1;
173+
if (passed_color_pair_number != 0) {
174+
pair_content(passed_color_pair_number, &passed_fg_color, &passed_bg_color);
175+
}
176+
177+
cchar_t* ch = this->chptr + start;
178+
for (int i = start; i <= finish; i++) {
179+
// Extract foreground and background color indexes from the current char
180+
short currentCharPairNum = (short)PAIR_NUMBER(ch->attr);
181+
short before_fg_color = -1, before_bg_color = -1;
182+
if (currentCharPairNum != 0) {
183+
pair_content(currentCharPairNum, &before_fg_color, &before_bg_color);
184+
}
185+
186+
// TODO: When text color matches higlight, the resulting STANDOUT is the same as on default text,
187+
// so we at least set italics
188+
chtype attrToPass = A_STANDOUT;
189+
if (before_fg_color == passed_bg_color) {
190+
attrToPass |= A_ITALIC;
191+
}
192+
// If current char is not a space and its ColorPair Index is not the default 0,
193+
// apply our own attrToPass with STANDOUT + optionally ITALICS,
194+
// instead of the passed attrs, which has the BG highlight color
195+
ch->attr = (ch->chars[0] != L' ' && currentCharPairNum != 0)
196+
? (ch->attr | attrToPass)
197+
: (unsigned int)attrs;
198+
ch++;
199+
}
200+
}
201+
163202
void RichString_appendChr(RichString* this, int attrs, char c, int count) {
164203
int from = this->chlen;
165204
int newLen = from + count;
@@ -210,6 +249,41 @@ void RichString_setAttrn(RichString* this, int attrs, int start, int charcount)
210249
}
211250
}
212251

252+
void RichString_setAttrn_preserveWithStandout(RichString* this, int attrs, int start, int finish) {
253+
finish = CLAMP(finish, 0, this->chlen - 1);
254+
255+
// Extract the foreground and background color indexes from the passed attrs
256+
short passed_color_pair_number = (short)PAIR_NUMBER(attrs);
257+
short passed_fg_color = -1, passed_bg_color = -1;
258+
if (passed_color_pair_number != 0) {
259+
pair_content(passed_color_pair_number, &passed_fg_color, &passed_bg_color);
260+
}
261+
262+
chtype* ch = this->chptr + start;
263+
for (int i = start; i <= finish; i++) {
264+
// Extract foreground and background color indexes from the current char
265+
short currentCharPairNum = (short)PAIR_NUMBER(*ch);
266+
short before_fg_color = -1, before_bg_color = -1;
267+
if (currentCharPairNum != 0) {
268+
pair_content(currentCharPairNum, &before_fg_color, &before_bg_color);
269+
}
270+
271+
// TODO: When text color matches higlight, the resulting STANDOUT is the same as on default text,
272+
// so we at least set italics
273+
chtype attrToPass = A_STANDOUT;
274+
if (before_fg_color == passed_bg_color) {
275+
attrToPass |= A_ITALIC;
276+
}
277+
// If current char is not a space and its ColorPair Index is not the default 0,
278+
// apply our own attrToPass with STANDOUT + optionally ITALICS,
279+
// instead of the passed attrs, which has the BG highlight color
280+
*ch = ((*ch & A_CHARTEXT) != L' ' && currentCharPairNum != 0)
281+
? *ch | attrToPass
282+
: (*ch & A_CHARTEXT) | (unsigned int)attrs;
283+
ch++;
284+
}
285+
}
286+
213287
void RichString_appendChr(RichString* this, int attrs, char c, int count) {
214288
int from = this->chlen;
215289
int newLen = from + count;

RichString.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,14 @@ void RichString_rewind(RichString* this, int count);
5252

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

55+
void RichString_setAttrn_preserveWithStandout(RichString* this, int attrs, int start, int finish);
56+
5557
int RichString_findChar(const RichString* this, char c, int start);
5658

5759
void RichString_setAttr(RichString* this, int attrs);
5860

61+
void RichString_setAttr_preserveWithStandout(RichString* this, int attrs);
62+
5963
void RichString_appendChr(RichString* this, int attrs, char c, int count);
6064

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

0 commit comments

Comments
 (0)