diff options
author | Explorer09 <explorer09@gmail.com> | 2023-11-06 12:36:24 +0800 |
---|---|---|
committer | Explorer09 <explorer09@gmail.com> | 2023-11-10 04:36:13 +0800 |
commit | b57adcde0ab1bd4c01e7ab98aaec921aa77fcbe0 (patch) | |
tree | e7ec21c6cf722f8390272bb80c0176b341bb544d /Meter.c | |
parent | a64d1ea6c02923289a36119f162d90fff1e4997d (diff) |
Permit zero 'total' in meters; remove assertion
The `Meter.total` value may be 0 on BatteryMeter, SwapMeter,
linux/HugePageMeter, linux/ZramMeter and others. In case of a zero
total value, print an empty bar in bar meter mode.
Also change GraphMeterMode_draw() so that it no longer writes back the
`total` value if it is zero.
Signed-off-by: Kang-Che Sung <explorer09@gmail.com>
Diffstat (limited to 'Meter.c')
-rw-r--r-- | Meter.c | 10 |
1 files changed, 4 insertions, 6 deletions
@@ -226,8 +226,7 @@ static void BarMeterMode_draw(Meter* this, int x, int y, int w) { int offset = 0; for (uint8_t i = 0; i < this->curItems; i++) { double value = this->values[i]; - if (isPositive(value)) { - assert(this->total > 0.0); + if (isPositive(value) && this->total > 0.0) { value = MINIMUM(value, this->total); blockSizes[i] = ceil((value / this->total) * w); } else { @@ -348,10 +347,9 @@ static void GraphMeterMode_draw(Meter* this, int x, int y, int w) { size_t i = nValues - (size_t)w * 2; for (int col = 0; i < nValues - 1; i += 2, col++) { int pix = GraphMeterMode_pixPerRow * GRAPH_HEIGHT; - if (this->total < 1) - this->total = 1; - int v1 = CLAMP((int) lround(data->values[i] / this->total * pix), 1, pix); - int v2 = CLAMP((int) lround(data->values[i + 1] / this->total * pix), 1, pix); + double total = MAXIMUM(this->total, 1); + int v1 = CLAMP((int) lround(data->values[i] / total * pix), 1, pix); + int v2 = CLAMP((int) lround(data->values[i + 1] / total * pix), 1, pix); int colorIdx = GRAPH_1; for (int line = 0; line < GRAPH_HEIGHT; line++) { |