From 01f5b892785014db30db4d6b4b7c23e042c7ffe4 Mon Sep 17 00:00:00 2001 From: Nathan Scott Date: Wed, 7 Jul 2021 16:57:03 +1000 Subject: Pretty-print values in the PCP DynamicMeter code Several improvements to the way values are displayed in the PCP platform DynamicMeter implementation: - handle the initial 'caption' setting as with regular meters, this required a new meter callback because we no longer have just a single meter caption for the DynamicMeter case - if no label is provided for a metric in a configuration file use the short form metric name as a fallback - honour the suffix setting in the configuration file - convert metric values to the canonical units for htop (kbyte and seconds), and use Meter_humanUnit when it makes sense to do so. Also improves the handling of fatal string error messages in a couple of places, thanks to BenBE for the review feedback. --- Meter.c | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) (limited to 'Meter.c') diff --git a/Meter.c b/Meter.c index 0052773c..5e3a00d0 100644 --- a/Meter.c +++ b/Meter.c @@ -153,11 +153,12 @@ ListItem* Meter_toListItem(const Meter* this, bool moving) { /* ---------- TextMeterMode ---------- */ static void TextMeterMode_draw(Meter* this, int x, int y, int w) { + const char* caption = Meter_getCaption(this); attrset(CRT_colors[METER_TEXT]); - mvaddnstr(y, x, this->caption, w - 1); + mvaddnstr(y, x, caption, w - 1); attrset(CRT_colors[RESET_COLOR]); - int captionLen = strlen(this->caption); + int captionLen = strlen(caption); x += captionLen; w -= captionLen; if (w <= 0) @@ -174,10 +175,11 @@ static void TextMeterMode_draw(Meter* this, int x, int y, int w) { static const char BarMeterMode_characters[] = "|#*@$%&."; static void BarMeterMode_draw(Meter* this, int x, int y, int w) { + const char* caption = Meter_getCaption(this); w -= 2; attrset(CRT_colors[METER_TEXT]); int captionLen = 3; - mvaddnstr(y, x, this->caption, captionLen); + mvaddnstr(y, x, caption, captionLen); x += captionLen; w -= captionLen; attrset(CRT_colors[BAR_BORDER]); @@ -306,9 +308,10 @@ static void GraphMeterMode_draw(Meter* this, int x, int y, int w) { GraphMeterMode_pixPerRow = PIXPERROW_ASCII; } + const char* caption = Meter_getCaption(this); attrset(CRT_colors[METER_TEXT]); int captionLen = 3; - mvaddnstr(y, x, this->caption, captionLen); + mvaddnstr(y, x, caption, captionLen); x += captionLen; w -= captionLen; @@ -393,8 +396,9 @@ static void LEDMeterMode_draw(Meter* this, int x, int y, ATTR_UNUSED int w) { #endif y + 2; attrset(CRT_colors[LED_COLOR]); - mvaddstr(yText, x, this->caption); - int xx = x + strlen(this->caption); + const char* caption = Meter_getCaption(this); + mvaddstr(yText, x, caption); + int xx = x + strlen(caption); int len = RichString_sizeVal(out); for (int i = 0; i < len; i++) { int c = RichString_getCharVal(out, i); -- cgit v1.2.3