From 3bb731c645d1bcde6ecf6ca23e44eb6655da8726 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20G=C3=B6ttsche?= Date: Sun, 10 Jan 2021 11:14:02 +0100 Subject: RichString_setAttrn: refactor to take a length instead of a stop index Fixes: #459 --- Meter.c | 4 ++-- Process.c | 12 ++++++------ RichString.c | 22 +++++++++------------- RichString.h | 2 +- linux/LinuxProcess.c | 18 +++++++++--------- 5 files changed, 27 insertions(+), 31 deletions(-) diff --git a/Meter.c b/Meter.c index 714fef25..2fe949ef 100644 --- a/Meter.c +++ b/Meter.c @@ -254,13 +254,13 @@ static void BarMeterMode_draw(Meter* this, int x, int y, int w) { offset = 0; for (uint8_t i = 0; i < this->curItems; 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)); offset += blockSizes[i]; offset = CLAMP(offset, 0, 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); } diff --git a/Process.c b/Process.c index 1e2d53c0..9524dfbe 100644 --- a/Process.c +++ b/Process.c @@ -183,7 +183,8 @@ void Process_fillStarttimeBuffer(Process* this) { } 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; 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] == '/') { basename = i + 1; } else if (comm[i] == ':') { - finish = i + 1; + len = i + 1; break; } } - if (!finish) { + if (len == 0) { if (this->settings->showProgramPath) { start += basename; } else { comm += basename; } - finish = this->basenameOffset - basename; + len = this->basenameOffset - basename; } - finish += start - 1; } RichString_appendWide(str, attr, comm); if (this->settings->highlightBaseName) { - RichString_setAttrn(str, baseattr, start, finish); + RichString_setAttrn(str, baseattr, start, len); } } diff --git a/RichString.c b/RichString.c index 3e3d3703..9b69e98c 100644 --- a/RichString.c +++ b/RichString.c @@ -73,12 +73,10 @@ static inline int RichString_writeFromAscii(RichString* this, int attrs, const c return len; } -inline void RichString_setAttrn(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 = attrs; - ch++; +inline void RichString_setAttrn(RichString* this, int attrs, int start, int charcount) { + int end = CLAMP(start + charcount, 0, this->chlen); + for (int i = start; i < end; i++) { + this->chptr[i].attr = attrs; } } @@ -110,12 +108,10 @@ static inline int RichString_writeFromAscii(RichString* this, int attrs, const c return RichString_writeFromWide(this, attrs, data_c, from, len); } -void RichString_setAttrn(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++; +void RichString_setAttrn(RichString* this, int attrs, int start, int charcount) { + int end = CLAMP(start + charcount, 0, this->chlen); + for (int i = start; i < end; i++) { + this->chptr[i] = (this->chptr[i] & 0xff) | attrs; } } @@ -148,7 +144,7 @@ void RichString_appendChr(RichString* this, char c, int count) { } 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) { diff --git a/RichString.h b/RichString.h index 1cae1cea..4145b0d0 100644 --- a/RichString.h +++ b/RichString.h @@ -42,7 +42,7 @@ typedef struct RichString_ { int highlightAttr; } 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); diff --git a/linux/LinuxProcess.c b/linux/LinuxProcess.c index 10017bd1..11bc2d80 100644 --- a/linux/LinuxProcess.c +++ b/linux/LinuxProcess.c @@ -528,30 +528,30 @@ static void LinuxProcess_writeCommand(const Process* this, int attr, int baseAtt if (!lp->mergedCommand.separateComm && commStart == baseStart && highlightBaseName) { /* If it was matched with procExe's basename, make it bold if needed */ if (commEnd > baseEnd) { - RichString_setAttrn(str, A_BOLD | baseAttr, baseStart, baseEnd - 1); - RichString_setAttrn(str, A_BOLD | commAttr, baseEnd, commEnd - 1); + RichString_setAttrn(str, A_BOLD | baseAttr, baseStart, baseEnd - baseStart); + RichString_setAttrn(str, A_BOLD | commAttr, baseEnd, commEnd - baseEnd); } else if (commEnd < baseEnd) { - RichString_setAttrn(str, A_BOLD | commAttr, commStart, commEnd - 1); - RichString_setAttrn(str, A_BOLD | baseAttr, commEnd, baseEnd - 1); + RichString_setAttrn(str, A_BOLD | commAttr, commStart, commEnd - commStart); + RichString_setAttrn(str, A_BOLD | baseAttr, commEnd, baseEnd - commEnd); } else { // 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; } else { - RichString_setAttrn(str, commAttr, commStart, commEnd - 1); + RichString_setAttrn(str, commAttr, commStart, commEnd - commStart); } } if (baseStart < baseEnd && highlightBaseName) { - RichString_setAttrn(str, baseAttr, baseStart, baseEnd - 1); + RichString_setAttrn(str, baseAttr, baseStart, baseEnd - baseStart); } 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) - 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) { -- cgit v1.2.3