diff options
author | Christian Göttsche <cgzones@googlemail.com> | 2020-11-23 20:12:11 +0100 |
---|---|---|
committer | cgzones <cgzones@googlemail.com> | 2020-11-25 20:45:54 +0100 |
commit | adf918520976a5f06181c1c05392a0da6e4bbaa5 (patch) | |
tree | 69caa800c12c7d576f4dbbaa41c3099dd315718c /Meter.c | |
parent | c038326a709f2e62c1c458c4c59dd71e5490fc1c (diff) |
Fully support non-ascii characters in Meter-Bar
Currently the code does not handle multi-byte characters, so length-
computations take the raw count of C characters and not the to displayed
size into account.
An example is the degree sign for temperatures.
Closes: #329
Diffstat (limited to 'Meter.c')
-rw-r--r-- | Meter.c | 25 |
1 files changed, 17 insertions, 8 deletions
@@ -196,11 +196,18 @@ static void BarMeterMode_draw(Meter* this, int x, int y, int w) { attrset(CRT_colors[RESET_COLOR]); return; } - char bar[w + 1]; - int blockSizes[10]; + // 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 = MAXIMUM(w - textLen, 0); + + RichString_begin(bar); + RichString_appendChr(&bar, ' ', padding); + RichString_append(&bar, 0, buffer); + assert(RichString_sizeVal(bar) >= w); - xSnprintf(bar, w + 1, "%*.*s", w, w, buffer); + int blockSizes[10]; // First draw in the bar[] buffer... int offset = 0; @@ -216,11 +223,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 (bar[j] == ' ') { + if (RichString_getCharVal(bar, j) == ' ') { if (CRT_colorScheme == COLORSCHEME_MONOCHROME) { - bar[j] = BarMeterMode_characters[i]; + RichString_setChar(&bar, j, BarMeterMode_characters[i]); } else { - bar[j] = '|'; + RichString_setChar(&bar, j, '|'); } } offset = nextOffset; @@ -230,15 +237,17 @@ static void BarMeterMode_draw(Meter* this, int x, int y, int w) { offset = 0; for (uint8_t i = 0; i < this->curItems; i++) { attrset(CRT_colors[Meter_attributes(this)[i]]); - mvaddnstr(y, x + offset, bar + offset, blockSizes[i]); + RichString_printoffnVal(bar, y, x + offset, offset, blockSizes[i]); offset += blockSizes[i]; offset = CLAMP(offset, 0, w); } if (offset < w) { attrset(CRT_colors[BAR_SHADOW]); - mvaddnstr(y, x + offset, bar + offset, w - offset); + RichString_printoffnVal(bar, y, x + offset, offset, w - offset); } + RichString_end(bar); + move(y, x + w + 1); attrset(CRT_colors[RESET_COLOR]); } |