From 74b28849c4ee7234aa6018667ff6c8c9a6b41273 Mon Sep 17 00:00:00 2001 From: Explorer09 Date: Tue, 10 Oct 2023 20:17:48 +0800 Subject: Let GraphMeterMode_draw() return early when the meter is too narrow... ...to draw even a single record of the graph. This makes the code more robust by checking (w <= 0) explicitly. Also avoids unnecessary reallocations of the GraphData buffer. Signed-off-by: Kang-Che Sung --- Meter.c | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) (limited to 'Meter.c') diff --git a/Meter.c b/Meter.c index 5b2784dc..df3412b0 100644 --- a/Meter.c +++ b/Meter.c @@ -289,10 +289,18 @@ static const char* const GraphMeterMode_dotsAscii[] = { }; static void GraphMeterMode_draw(Meter* this, int x, int y, int w) { + const char* caption = Meter_getCaption(this); + attrset(CRT_colors[METER_TEXT]); + const int captionLen = 3; + mvaddnstr(y, x, caption, captionLen); + x += captionLen; + w -= captionLen; + if (w <= 0) + return; + const Machine* host = this->host; GraphData* data = &this->drawData; - assert(w > 0); if ((size_t)w * 2 > data->nValues) { size_t oldNValues = data->nValues; data->nValues = MAXIMUM(oldNValues + (oldNValues / 2), (size_t)w * 2); @@ -315,13 +323,6 @@ 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, caption, captionLen); - x += captionLen; - w -= captionLen; - if (!timercmp(&host->realtime, &(data->time), <)) { int globalDelay = this->host->settings->delay; struct timeval delay = { .tv_sec = globalDelay / 10, .tv_usec = (globalDelay % 10) * 100000L }; -- cgit v1.2.3