mdview: drop printf from status rendering

Replace status-line printf formatting with compact manual decimal rendering
(u16/u8 right-aligned helpers + wrchar/put_str_attr). This removes runtime
printf usage from mdview.c and keeps fixed status columns without stale digits.

Co-Authored-By: Oz <oz-agent@warp.dev>
This commit is contained in:
2026-06-07 19:19:13 +03:00
parent 982af12710
commit 8abc1d6c16
+59 -15
View File
@@ -186,6 +186,40 @@ static void put_str(uint8_t x, uint8_t y, const char *s)
puts(s); puts(s);
} }
/* Convert uint16 to decimal text without printf.
* Returns number of produced digits in out[] (1..5). */
static uint8_t u16_to_dec(uint16_t v, char *out)
{
char tmp[5];
uint8_t n = 0;
if (v == 0) {
out[0] = '0';
return 1;
}
while (v > 0 && n < 5) {
tmp[n++] = (char)('0' + (v % 10));
v /= 10;
}
for (uint8_t i = 0; i < n; i++) out[i] = tmp[(uint8_t)(n - 1 - i)];
return n;
}
/* Draw uint16 in decimal, right-aligned to `width` cells, using attr. */
static void put_u16_right_attr(uint8_t x, uint8_t y, uint16_t v, uint8_t width, uint8_t attr)
{
char buf[5];
uint8_t len = u16_to_dec(v, buf);
uint8_t pad = (len < width) ? (uint8_t)(width - len) : 0;
while (pad-- && x < SCREEN_W) wrchar(x++, y, ' ', attr);
for (uint8_t i = 0; i < len && x < SCREEN_W; i++) wrchar(x++, y, buf[i], attr);
}
/* Draw uint8 in decimal, right-aligned to `width` cells, using attr. */
static void put_u8_right_attr(uint8_t x, uint8_t y, uint8_t v, uint8_t width, uint8_t attr)
{
put_u16_right_attr(x, y, (uint16_t)v, width, attr);
}
static void fill_row(uint8_t y, uint8_t attr) static void fill_row(uint8_t y, uint8_t attr)
{ {
for (uint8_t c = 0; c < SCREEN_W; c++) { for (uint8_t c = 0; c < SCREEN_W; c++) {
@@ -1480,33 +1514,43 @@ static uint8_t calc_pct(void)
return pct; return pct;
} }
static void render_full_status(void) /* Render dynamic counters on the status bar without printf.
* Rewrites cols 45..79 fully to avoid stale digits when widths shrink. */
static void render_status_numbers(void)
{ {
uint16_t last = top_line + VIEW_H; uint16_t last = top_line + VIEW_H;
if (last > n_lines) last = n_lines; if (last > n_lines) last = n_lines;
uint8_t pct = calc_pct(); uint8_t pct = calc_pct();
for (uint8_t c = 45; c < SCREEN_W; c++) wrchar(c, 0, ' ', ATTR_BAR);
wrchar(45, 0, 0xB3, ATTR_BAR); /* │ */
wrchar(47, 0, 'L', ATTR_BAR);
put_u16_right_attr(49, 0, (uint16_t)(top_line + 1), 5, ATTR_BAR);
wrchar(54, 0, '-', ATTR_BAR);
put_u16_right_attr(55, 0, last, 5, ATTR_BAR);
put_str_attr(60, 0, " / ", ATTR_BAR);
put_u16_right_attr(63, 0, n_lines, 5, ATTR_BAR);
wrchar(70, 0, 0xB3, ATTR_BAR); /* │ */
put_u8_right_attr(73, 0, pct, 3, ATTR_BAR);
wrchar(76, 0, '%', ATTR_BAR);
}
static void render_full_status(void)
{
fill_row(0, ATTR_BAR); fill_row(0, ATTR_BAR);
gotoxy(1, 0);
/* "MDVIEW", then col 7 = space, col 8 = spinner slot, col 9 = space, /* "MDVIEW", then col 7 = space, col 8 = spinner slot, col 9 = space,
* col 10+ = filename. Spinner_tick draws over col 8 in ATTR_BAR. */ * col 10+ = filename. Spinner_tick draws over col 8 in ATTR_BAR. */
printf("MDVIEW %s", filename); put_str_attr(1, 0, "MDVIEW", ATTR_BAR);
gotoxy(45, 0); put_str_attr(10, 0, filename, ATTR_BAR);
printf("\xB3 L %5u-%u / %u", (top_line + 1), last, n_lines); render_status_numbers();
gotoxy(70, 0);
printf("\xB3 %3u%%", pct);
} }
static void render_updated_status(void) static void render_updated_status(void)
{ {
uint16_t last = top_line + VIEW_H; render_status_numbers();
if (last > n_lines) last = n_lines;
uint8_t pct = calc_pct();
gotoxy(45, 0);
printf("\xB3 L %5u-%u / %u", (top_line + 1), last, n_lines);
gotoxy(70, 0);
printf("\xB3 %3u%%", pct);
} }
static void render_menu(void) static void render_menu(void)