RichString_setAttrn: refactor to take a length instead of a stop index

Fixes: #459
This commit is contained in:
Christian Göttsche 2021-01-10 11:14:02 +01:00
parent a076488809
commit 3bb731c645
5 changed files with 27 additions and 31 deletions

View File

@ -254,13 +254,13 @@ static void BarMeterMode_draw(Meter* this, int x, int y, int w) {
offset = 0; offset = 0;
for (uint8_t i = 0; i < this->curItems; i++) { for (uint8_t i = 0; i < this->curItems; i++) {
int attr = this->curAttributes ? this->curAttributes[i] : Meter_attributes(this)[i]; int attr = this->curAttributes ? this->curAttributes[i] : Meter_attributes(this)[i];
RichString_setAttrn(&bar, CRT_colors[attr], startPos + offset, startPos + offset + blockSizes[i] - 1); RichString_setAttrn(&bar, CRT_colors[attr], startPos + offset, blockSizes[i]);
RichString_printoffnVal(bar, y, x + offset, startPos + offset, MINIMUM(blockSizes[i], w - offset)); RichString_printoffnVal(bar, y, x + offset, startPos + offset, MINIMUM(blockSizes[i], w - offset));
offset += blockSizes[i]; offset += blockSizes[i];
offset = CLAMP(offset, 0, w); offset = CLAMP(offset, 0, w);
} }
if (offset < w) { if (offset < w) {
RichString_setAttrn(&bar, CRT_colors[BAR_SHADOW], startPos + offset, startPos + w - 1); RichString_setAttrn(&bar, CRT_colors[BAR_SHADOW], startPos + offset, w - offset);
RichString_printoffnVal(bar, y, x + offset, startPos + offset, w - offset); RichString_printoffnVal(bar, y, x + offset, startPos + offset, w - offset);
} }

View File

@ -183,7 +183,8 @@ void Process_fillStarttimeBuffer(Process* this) {
} }
static inline void Process_writeCommand(const Process* this, int attr, int baseattr, RichString* str) { static inline void Process_writeCommand(const Process* this, int attr, int baseattr, RichString* str) {
int start = RichString_size(str), finish = 0; int start = RichString_size(str);
int len = 0;
const char* comm = this->comm; const char* comm = this->comm;
if (this->settings->highlightBaseName || !this->settings->showProgramPath) { if (this->settings->highlightBaseName || !this->settings->showProgramPath) {
@ -192,25 +193,24 @@ static inline void Process_writeCommand(const Process* this, int attr, int basea
if (comm[i] == '/') { if (comm[i] == '/') {
basename = i + 1; basename = i + 1;
} else if (comm[i] == ':') { } else if (comm[i] == ':') {
finish = i + 1; len = i + 1;
break; break;
} }
} }
if (!finish) { if (len == 0) {
if (this->settings->showProgramPath) { if (this->settings->showProgramPath) {
start += basename; start += basename;
} else { } else {
comm += basename; comm += basename;
} }
finish = this->basenameOffset - basename; len = this->basenameOffset - basename;
} }
finish += start - 1;
} }
RichString_appendWide(str, attr, comm); RichString_appendWide(str, attr, comm);
if (this->settings->highlightBaseName) { if (this->settings->highlightBaseName) {
RichString_setAttrn(str, baseattr, start, finish); RichString_setAttrn(str, baseattr, start, len);
} }
} }

View File

@ -73,12 +73,10 @@ static inline int RichString_writeFromAscii(RichString* this, int attrs, const c
return len; return len;
} }
inline void RichString_setAttrn(RichString* this, int attrs, int start, int finish) { inline void RichString_setAttrn(RichString* this, int attrs, int start, int charcount) {
cchar_t* ch = this->chptr + start; int end = CLAMP(start + charcount, 0, this->chlen);
finish = CLAMP(finish, 0, this->chlen - 1); for (int i = start; i < end; i++) {
for (int i = start; i <= finish; i++) { this->chptr[i].attr = attrs;
ch->attr = attrs;
ch++;
} }
} }
@ -110,12 +108,10 @@ static inline int RichString_writeFromAscii(RichString* this, int attrs, const c
return RichString_writeFromWide(this, attrs, data_c, from, len); return RichString_writeFromWide(this, attrs, data_c, from, len);
} }
void RichString_setAttrn(RichString* this, int attrs, int start, int finish) { void RichString_setAttrn(RichString* this, int attrs, int start, int charcount) {
chtype* ch = this->chptr + start; int end = CLAMP(start + charcount, 0, this->chlen);
finish = CLAMP(finish, 0, this->chlen - 1); for (int i = start; i < end; i++) {
for (int i = start; i <= finish; i++) { this->chptr[i] = (this->chptr[i] & 0xff) | attrs;
*ch = (*ch & 0xff) | attrs;
ch++;
} }
} }
@ -148,7 +144,7 @@ void RichString_appendChr(RichString* this, char c, int count) {
} }
void RichString_setAttr(RichString* this, int attrs) { void RichString_setAttr(RichString* this, int attrs) {
RichString_setAttrn(this, attrs, 0, this->chlen - 1); RichString_setAttrn(this, attrs, 0, this->chlen);
} }
int RichString_appendWide(RichString* this, int attrs, const char* data) { int RichString_appendWide(RichString* this, int attrs, const char* data) {

View File

@ -42,7 +42,7 @@ typedef struct RichString_ {
int highlightAttr; int highlightAttr;
} RichString; } RichString;
void RichString_setAttrn(RichString* this, int attrs, int start, int finish); void RichString_setAttrn(RichString* this, int attrs, int start, int charcount);
int RichString_findChar(RichString* this, char c, int start); int RichString_findChar(RichString* this, char c, int start);

View File

@ -528,30 +528,30 @@ static void LinuxProcess_writeCommand(const Process* this, int attr, int baseAtt
if (!lp->mergedCommand.separateComm && commStart == baseStart && highlightBaseName) { if (!lp->mergedCommand.separateComm && commStart == baseStart && highlightBaseName) {
/* If it was matched with procExe's basename, make it bold if needed */ /* If it was matched with procExe's basename, make it bold if needed */
if (commEnd > baseEnd) { if (commEnd > baseEnd) {
RichString_setAttrn(str, A_BOLD | baseAttr, baseStart, baseEnd - 1); RichString_setAttrn(str, A_BOLD | baseAttr, baseStart, baseEnd - baseStart);
RichString_setAttrn(str, A_BOLD | commAttr, baseEnd, commEnd - 1); RichString_setAttrn(str, A_BOLD | commAttr, baseEnd, commEnd - baseEnd);
} else if (commEnd < baseEnd) { } else if (commEnd < baseEnd) {
RichString_setAttrn(str, A_BOLD | commAttr, commStart, commEnd - 1); RichString_setAttrn(str, A_BOLD | commAttr, commStart, commEnd - commStart);
RichString_setAttrn(str, A_BOLD | baseAttr, commEnd, baseEnd - 1); RichString_setAttrn(str, A_BOLD | baseAttr, commEnd, baseEnd - commEnd);
} else { } else {
// Actually should be highlighted commAttr, but marked baseAttr to reduce visual noise // Actually should be highlighted commAttr, but marked baseAttr to reduce visual noise
RichString_setAttrn(str, A_BOLD | baseAttr, commStart, commEnd - 1); RichString_setAttrn(str, A_BOLD | baseAttr, commStart, commEnd - commStart);
} }
baseStart = baseEnd; baseStart = baseEnd;
} else { } else {
RichString_setAttrn(str, commAttr, commStart, commEnd - 1); RichString_setAttrn(str, commAttr, commStart, commEnd - commStart);
} }
} }
if (baseStart < baseEnd && highlightBaseName) { if (baseStart < baseEnd && highlightBaseName) {
RichString_setAttrn(str, baseAttr, baseStart, baseEnd - 1); RichString_setAttrn(str, baseAttr, baseStart, baseEnd - baseStart);
} }
if (mc->sep1) if (mc->sep1)
RichString_setAttrn(str, CRT_colors[FAILED_READ], strStart + mc->sep1, strStart + mc->sep1); RichString_setAttrn(str, CRT_colors[FAILED_READ], strStart + mc->sep1, 1);
if (mc->sep2) if (mc->sep2)
RichString_setAttrn(str, CRT_colors[FAILED_READ], strStart + mc->sep2, strStart + mc->sep2); RichString_setAttrn(str, CRT_colors[FAILED_READ], strStart + mc->sep2, 1);
} }
static void LinuxProcess_writeCommandField(const Process *this, RichString *str, char *buffer, int n, int attr) { static void LinuxProcess_writeCommandField(const Process *this, RichString *str, char *buffer, int n, int attr) {