Skip to content

Commit 5542a99

Browse files
committed
Compute terminal widths for Bar and Graph Meter captions
1 parent 62b39cd commit 5542a99

File tree

1 file changed

+18
-10
lines changed

1 file changed

+18
-10
lines changed

Meter.c

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -82,17 +82,21 @@ static void BarMeterMode_draw(Meter* this, int x, int y, int w) {
8282
assert(w <= INT_MAX - x);
8383

8484
// Draw the caption
85-
int captionLen = 3;
85+
const int captionWidth = 3;
8686
const char* caption = Meter_getCaption(this);
87-
if (w >= captionLen) {
87+
if (w >= captionWidth) {
8888
attrset(CRT_colors[METER_TEXT]);
89-
mvaddnstr(y, x, caption, captionLen);
89+
90+
const char* ptr = caption;
91+
int nCols = String_mbswidth(&ptr, 256, captionWidth);
92+
int len = (int)(ptr - caption);
93+
mvprintw(y, x, "%-*.*s", len + captionWidth - nCols, len, caption);
9094
}
91-
w -= captionLen;
95+
w -= captionWidth;
9296

9397
// Draw the bar borders
9498
if (w >= 1) {
95-
x += captionLen;
99+
x += captionWidth;
96100
attrset(CRT_colors[BAR_BORDER]);
97101
mvaddch(y, x, '[');
98102
w--;
@@ -207,13 +211,17 @@ static void GraphMeterMode_draw(Meter* this, int x, int y, int w) {
207211
assert(w <= INT_MAX - x);
208212

209213
// Draw the caption
210-
const int captionLen = 3;
214+
const int captionWidth = 3;
211215
const char* caption = Meter_getCaption(this);
212-
if (w >= captionLen) {
216+
if (w >= captionWidth) {
213217
attrset(CRT_colors[METER_TEXT]);
214-
mvaddnstr(y, x, caption, captionLen);
218+
219+
const char* ptr = caption;
220+
int nCols = String_mbswidth(&ptr, 256, captionWidth);
221+
int len = (int)(ptr - caption);
222+
mvprintw(y, x, "%-*.*s", len + captionWidth - nCols, len, caption);
215223
}
216-
w -= captionLen;
224+
w -= captionWidth;
217225

218226
assert(this->h >= 1);
219227
int h = this->h;
@@ -254,7 +262,7 @@ static void GraphMeterMode_draw(Meter* this, int x, int y, int w) {
254262
if (w < 1) {
255263
goto end;
256264
}
257-
x += captionLen;
265+
x += captionWidth;
258266

259267
// Graph drawing style (character set, etc.)
260268
const char* const* GraphMeterMode_dots;

0 commit comments

Comments
 (0)