summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorChristian Göttsche <cgzones@googlemail.com>2020-11-23 20:12:11 +0100
committercgzones <cgzones@googlemail.com>2020-11-25 20:45:54 +0100
commitadf918520976a5f06181c1c05392a0da6e4bbaa5 (patch)
tree69caa800c12c7d576f4dbbaa41c3099dd315718c
parentc038326a709f2e62c1c458c4c59dd71e5490fc1c (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
-rw-r--r--Meter.c25
-rw-r--r--RichString.c9
-rw-r--r--RichString.h2
3 files changed, 28 insertions, 8 deletions
diff --git a/Meter.c b/Meter.c
index cf8ccc52..2f02f0ad 100644
--- a/Meter.c
+++ b/Meter.c
@@ -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]);
}
diff --git a/RichString.c b/RichString.c
index 904b44b6..1d2a3fee 100644
--- a/RichString.c
+++ b/RichString.c
@@ -119,6 +119,15 @@ void RichString_prune(RichString* this) {
this->chptr = this->chstr;
}
+void RichString_appendChr(RichString* this, char c, int count) {
+ int from = this->chlen;
+ int newLen = from + count;
+ RichString_setLen(this, newLen);
+ for (int i = from; i < newLen; i++) {
+ RichString_setChar(this, i, c);
+ }
+}
+
void RichString_setAttr(RichString* this, int attrs) {
RichString_setAttrn(this, attrs, 0, this->chlen - 1);
}
diff --git a/RichString.h b/RichString.h
index 12b09540..262befc5 100644
--- a/RichString.h
+++ b/RichString.h
@@ -50,6 +50,8 @@ void RichString_prune(RichString* this);
void RichString_setAttr(RichString* this, int attrs);
+void RichString_appendChr(RichString* this, char c, int count);
+
void RichString_append(RichString* this, int attrs, const char* data);
void RichString_appendn(RichString* this, int attrs, const char* data, int len);

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