Skip to content

Commit d439eb1

Browse files
committed
Compute terminal widths for Bar and Graph Meter captions
1 parent 13ec188 commit d439eb1

1 file changed

Lines changed: 18 additions & 10 deletions

File tree

Meter.c

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

9393
// Draw the caption
94-
int captionLen = 3;
94+
const int captionWidth = 3;
9595
const char* caption = Meter_getCaption(this);
96-
if (w >= captionLen) {
96+
if (w >= captionWidth) {
9797
attrset(CRT_colors[METER_TEXT]);
98-
mvaddnstr(y, x, caption, captionLen);
98+
99+
const char* ptr = caption;
100+
int nCols = String_mbswidth(&ptr, 256, captionWidth);
101+
int len = (int)(ptr - caption);
102+
mvprintw(y, x, "%-*.*s", len + captionWidth - nCols, len, caption);
99103
}
100-
w -= captionLen;
104+
w -= captionWidth;
101105

102106
// Draw the bar borders
103107
if (w >= 1) {
104-
x += captionLen;
108+
x += captionWidth;
105109
attrset(CRT_colors[BAR_BORDER]);
106110
mvaddch(y, x, '[');
107111
w--;
@@ -222,13 +226,17 @@ static void GraphMeterMode_draw(Meter* this, int x, int y, int w) {
222226
assert(w <= INT_MAX - x);
223227

224228
// Draw the caption
225-
const int captionLen = 3;
229+
const int captionWidth = 3;
226230
const char* caption = Meter_getCaption(this);
227-
if (w >= captionLen) {
231+
if (w >= captionWidth) {
228232
attrset(CRT_colors[METER_TEXT]);
229-
mvaddnstr(y, x, caption, captionLen);
233+
234+
const char* ptr = caption;
235+
int nCols = String_mbswidth(&ptr, 256, captionWidth);
236+
int len = (int)(ptr - caption);
237+
mvprintw(y, x, "%-*.*s", len + captionWidth - nCols, len, caption);
230238
}
231-
w -= captionLen;
239+
w -= captionWidth;
232240

233241
// Prepare parameters for drawing
234242
assert(this->h >= 1);
@@ -274,7 +282,7 @@ static void GraphMeterMode_draw(Meter* this, int x, int y, int w) {
274282
if (w < 1) {
275283
goto end;
276284
}
277-
x += captionLen;
285+
x += captionWidth;
278286

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

0 commit comments

Comments
 (0)