aboutsummaryrefslogtreecommitdiffstats
path: root/Meter.c
diff options
context:
space:
mode:
authorDaniel Lange <DLange@git.local>2021-01-11 20:43:27 +0100
committerDaniel Lange <DLange@git.local>2021-01-11 20:43:27 +0100
commitc55320e9e2a8916e911bcd39ab37b79e3a7d03b2 (patch)
treed6be9a09fdf7d6dc155de3429a70697ee2bb43b0 /Meter.c
parent65357c8c46154de4e4eca14075bfe5523bb5fc14 (diff)
downloaddebian_htop-c55320e9e2a8916e911bcd39ab37b79e3a7d03b2.tar.gz
debian_htop-c55320e9e2a8916e911bcd39ab37b79e3a7d03b2.tar.bz2
debian_htop-c55320e9e2a8916e911bcd39ab37b79e3a7d03b2.zip
New upstream version 3.0.5upstream/3.0.5
Diffstat (limited to 'Meter.c')
-rw-r--r--Meter.c66
1 files changed, 43 insertions, 23 deletions
diff --git a/Meter.c b/Meter.c
index 945911c..2fe949e 100644
--- a/Meter.c
+++ b/Meter.c
@@ -39,6 +39,7 @@ Meter* Meter_new(const struct ProcessList_* pl, int param, const MeterClass* typ
this->param = param;
this->pl = pl;
this->curItems = type->maxItems;
+ this->curAttributes = NULL;
this->values = type->maxItems ? xCalloc(type->maxItems, sizeof(double)) : NULL;
this->total = type->total;
this->caption = xStrdup(type->caption);
@@ -100,7 +101,7 @@ static inline void Meter_displayBuffer(const Meter* this, const char* buffer, Ri
if (Object_displayFn(this)) {
Object_display(this, out);
} else {
- RichString_write(out, CRT_colors[Meter_attributes(this)[0]], buffer);
+ RichString_writeWide(out, CRT_colors[Meter_attributes(this)[0]], buffer);
}
}
@@ -156,16 +157,20 @@ ListItem* Meter_toListItem(Meter* this, bool moving) {
static void TextMeterMode_draw(Meter* this, int x, int y, int w) {
char buffer[METER_BUFFER_LEN];
Meter_updateValues(this, buffer, sizeof(buffer));
- (void) w;
attrset(CRT_colors[METER_TEXT]);
- mvaddstr(y, x, this->caption);
+ mvaddnstr(y, x, this->caption, w - 1);
+ attrset(CRT_colors[RESET_COLOR]);
+
int captionLen = strlen(this->caption);
x += captionLen;
- attrset(CRT_colors[RESET_COLOR]);
+ w -= captionLen;
+ if (w <= 0)
+ return;
+
RichString_begin(out);
Meter_displayBuffer(this, buffer, &out);
- RichString_printVal(out, y, x);
+ RichString_printoffnVal(out, y, x, 0, w - 1);
RichString_end(out);
}
@@ -185,7 +190,7 @@ static void BarMeterMode_draw(Meter* this, int x, int y, int w) {
w -= captionLen;
attrset(CRT_colors[BAR_BORDER]);
mvaddch(y, x, '[');
- mvaddch(y, x + w, ']');
+ mvaddch(y, x + MAXIMUM(w, 0), ']');
attrset(CRT_colors[RESET_COLOR]);
w--;
@@ -195,14 +200,29 @@ static void BarMeterMode_draw(Meter* this, int x, int y, int w) {
return;
// The text in the bar is right aligned;
- // calculate needed padding and generate leading spaces
- const int textLen = mbstowcs(NULL, buffer, 0);
- const int padding = CLAMP(w - textLen, 0, w);
-
+ // Pad with maximal spaces and then calculate needed starting position offset
RichString_begin(bar);
- RichString_appendChr(&bar, ' ', padding);
- RichString_append(&bar, 0, buffer);
- assert(RichString_sizeVal(bar) >= w);
+ RichString_appendChr(&bar, ' ', w);
+ RichString_appendWide(&bar, 0, buffer);
+ int startPos = RichString_sizeVal(bar) - w;
+ if (startPos > w) {
+ // Text is too large for bar
+ // Truncate meter text at a space character
+ for (int pos = 2 * w; pos > w; pos--) {
+ if (RichString_getCharVal(bar, pos) == ' ') {
+ while (pos > w && RichString_getCharVal(bar, pos - 1) == ' ')
+ pos--;
+ startPos = pos - w;
+ break;
+ }
+ }
+
+ // If still to large, print the start not the end
+ startPos = MINIMUM(startPos, w);
+ }
+ assert(startPos >= 0);
+ assert(startPos <= w);
+ assert(startPos + w <= RichString_sizeVal(bar));
int blockSizes[10];
@@ -220,11 +240,11 @@ static void BarMeterMode_draw(Meter* this, int x, int y, int w) {
// (Control against invalid values)
nextOffset = CLAMP(nextOffset, 0, w);
for (int j = offset; j < nextOffset; j++)
- if (RichString_getCharVal(bar, j) == ' ') {
+ if (RichString_getCharVal(bar, startPos + j) == ' ') {
if (CRT_colorScheme == COLORSCHEME_MONOCHROME) {
- RichString_setChar(&bar, j, BarMeterMode_characters[i]);
+ RichString_setChar(&bar, startPos + j, BarMeterMode_characters[i]);
} else {
- RichString_setChar(&bar, j, '|');
+ RichString_setChar(&bar, startPos + j, '|');
}
}
offset = nextOffset;
@@ -233,14 +253,15 @@ static void BarMeterMode_draw(Meter* this, int x, int y, int w) {
// ...then print the buffer.
offset = 0;
for (uint8_t i = 0; i < this->curItems; i++) {
- RichString_setAttrn(&bar, CRT_colors[Meter_attributes(this)[i]], offset, offset + blockSizes[i] - 1);
- RichString_printoffnVal(bar, y, x + offset, offset, blockSizes[i]);
+ int attr = this->curAttributes ? this->curAttributes[i] : Meter_attributes(this)[i];
+ 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], offset, w - 1);
- RichString_printoffnVal(bar, y, x + offset, offset, w - offset);
+ RichString_setAttrn(&bar, CRT_colors[BAR_SHADOW], startPos + offset, w - offset);
+ RichString_printoffnVal(bar, y, x + offset, startPos + offset, w - offset);
}
RichString_end(bar);
@@ -271,9 +292,6 @@ static const char* const GraphMeterMode_dotsAscii[] = {
/*20*/":", /*21*/":", /*22*/":"
};
-static const char* const* GraphMeterMode_dots;
-static int GraphMeterMode_pixPerRow;
-
static void GraphMeterMode_draw(Meter* this, int x, int y, int w) {
if (!this->drawData) {
@@ -282,6 +300,8 @@ static void GraphMeterMode_draw(Meter* this, int x, int y, int w) {
GraphData* data = this->drawData;
const int nValues = METER_BUFFER_LEN;
+ const char* const* GraphMeterMode_dots;
+ int GraphMeterMode_pixPerRow;
#ifdef HAVE_LIBNCURSESW
if (CRT_utf8) {
GraphMeterMode_dots = GraphMeterMode_dotsUtf8;

© 2014-2024 Faster IT GmbH | imprint | privacy policy