diff options
| author | Anders Betts <anders.betts@gmail.com> | 2026-09-20 10:09:41 +0200 |
|---|---|---|
| committer | Anders Betts <anders.betts@gmail.com> | 2026-09-20 10:09:41 +0200 |
| commit | 5097941fa96ce0c309e5f1af034993781a70fc66 (patch) | |
| tree | ff46ced172d475046994a6c23b0cae20b693e12f | |
| parent | fed4952b5e9e6bc6f9f6ca4a30570b0b898ee953 (diff) | |
tui: align list rows beyond nine itemsv0.1.47
| -rw-r--r-- | clients/tui.c | 15 | ||||
| -rw-r--r-- | clients/tui.h | 1 | ||||
| -rwxr-xr-x | scripts/tui-golden.py | 6 | ||||
| -rw-r--r-- | tests/test_tui.c | 13 |
4 files changed, 29 insertions, 6 deletions
diff --git a/clients/tui.c b/clients/tui.c index c52538c..06740d3 100644 --- a/clients/tui.c +++ b/clients/tui.c @@ -191,6 +191,14 @@ int tui_disp_width_n(const char *s, size_t n) return w; } +int tui_num_width(int n) +{ + int w = 1; + for (int tmp = n; tmp >= 10; tmp /= 10) + w++; + return w; +} + void tui_le_init(struct tui_ledit *e, char *buf, size_t cap) { e->buf = buf; @@ -1020,8 +1028,7 @@ int tui_select_list_hook(const char *title, char **items, int n, int start, if (n > 0 && v.sel >= n) v.sel = n - 1; v.view = LINES - 4; - for (int tmp = n; tmp >= 10; tmp /= 10) - v.numw++; + v.numw = tui_num_width(n); snprintf(v.gotolabel, sizeof v.gotolabel, "Gå till rad: "); for (;;) { if (cursor) @@ -1078,9 +1085,7 @@ int tui_menu(const char *title, const char *const *items, int n, v.sel = (cursor && *cursor >= 0 && *cursor < n) ? *cursor : 0; if (!flags[v.sel]) v.sel = nav_snap(&v, v.sel, 1); - v.numw = 1; - for (int tmp = nsel; tmp >= 10; tmp /= 10) - v.numw++; + v.numw = tui_num_width(nsel); if (v.numw < 2) v.numw = 2; v.view = (LINES - 4) / 2; diff --git a/clients/tui.h b/clients/tui.h index 7250c0b..fcab5b7 100644 --- a/clients/tui.h +++ b/clients/tui.h @@ -61,6 +61,7 @@ void tui_keys_setup(void); /* --- text measure and formatting --- */ int tui_disp_width(const char *s); int tui_disp_width_n(const char *s, size_t n); +int tui_num_width(int n); void tui_pad_field(char *buf, size_t cap, int width); void tui_pad_label(char *buf, size_t n, const char *text, int width); void tui_pad_hdr(char *buf, size_t n, int width, const char *text); diff --git a/scripts/tui-golden.py b/scripts/tui-golden.py index f214f40..589b693 100755 --- a/scripts/tui-golden.py +++ b/scripts/tui-golden.py @@ -82,7 +82,11 @@ SCENARIOS = [ { "name": "momsregler", "screen": "rules", - "expect": ["Momsregler", "3000-3019"], + "expect": [ + "Momsregler", + " 1. 05 range 3000-3019", + "10. 20 range 4510-4529", + ], }, ] diff --git a/tests/test_tui.c b/tests/test_tui.c index a024e27..56880fa 100644 --- a/tests/test_tui.c +++ b/tests/test_tui.c @@ -32,6 +32,18 @@ static void test_disp_width(void) CHECK(tui_disp_width_n("abc", 2) == 2); } +static void test_num_width(void) +{ + CHECK(tui_num_width(0) == 1); + CHECK(tui_num_width(1) == 1); + CHECK(tui_num_width(9) == 1); + CHECK(tui_num_width(10) == 2); + CHECK(tui_num_width(16) == 2); + CHECK(tui_num_width(99) == 2); + CHECK(tui_num_width(100) == 3); + CHECK(tui_num_width(1000) == 4); +} + static void test_formats(void) { char buf[64]; @@ -667,6 +679,7 @@ static void test_rt_footer(void) int main(void) { test_disp_width(); + test_num_width(); test_formats(); test_parse_kr(); test_ledit(); |
