aboutsummaryrefslogtreecommitdiff
path: root/clients/tui.c
diff options
context:
space:
mode:
authorAnders Betts <anders.betts@gmail.com>2026-09-20 00:05:51 +0200
committerAnders Betts <anders.betts@gmail.com>2026-09-20 00:05:51 +0200
commita61cfeefb0599e74fc9189b189b16dd0b6d36f0f (patch)
tree83e0934e190744605e91e69cd032e91c85eecf57 /clients/tui.c
parent1f2d7a8190876e3d1c541251e079a11a2cc637f2 (diff)
downloadbokf-0.1.44.tar.gz
bokf-0.1.44.zip
tui: remember the focused row in forms and the bokslut hubv0.1.44
Diffstat (limited to 'clients/tui.c')
-rw-r--r--clients/tui.c71
1 files changed, 52 insertions, 19 deletions
diff --git a/clients/tui.c b/clients/tui.c
index a5b0cd2..c52538c 100644
--- a/clients/tui.c
+++ b/clients/tui.c
@@ -1346,6 +1346,36 @@ static int act_selectable(const struct tui_form_action *acts, int i, int na)
return acts && i >= 0 && i < na && acts[i].enabled != -1;
}
+int tui_form_focus_clamp(int focus, int nf,
+ const struct tui_form_action *acts, int na)
+{
+ if (nf < 0)
+ nf = 0;
+ if (na < 0)
+ na = 0;
+ int n = nf + na;
+ if (n <= 0)
+ return -1;
+ int pos = focus;
+ if (pos < 0)
+ pos = 0;
+ if (pos >= n)
+ pos = n - 1;
+ for (int i = 0; i < n; i++) {
+ if (pos < nf || act_selectable(acts, pos - nf, na))
+ return pos;
+ pos = (pos + 1) % n;
+ }
+ return -1;
+}
+
+int tui_form_focus_store(int *focus, int sel, int ret)
+{
+ if (focus)
+ *focus = sel;
+ return ret;
+}
+
int tui_form_act_step(int focus, int nf, const struct tui_form_action *acts,
int na, int dir)
{
@@ -1440,13 +1470,14 @@ static void form_draw_actions(const struct tui_form_action *acts, int na,
static int form_run(const char *title, struct tui_form_field *f, int nf,
int can_edit, const struct tui_form_action *acts, int na,
- const char *hint, int (*key)(void *ud, int ch), void *ud)
+ const char *hint, int (*key)(void *ud, int ch), void *ud,
+ int *focus)
{
- int sel = nf > 0 ? 0 : tui_form_act_first(0, acts, na);
+ int sel = tui_form_focus_clamp(focus ? *focus : -1, nf, acts, na);
for (;;) {
int ntot = nf + (na > 0 ? na : 0);
if (ntot <= 0)
- return TUI_FORM_BACK;
+ return tui_form_focus_store(focus, sel, TUI_FORM_BACK);
if (sel < 0)
sel = tui_form_act_first(nf, acts, na);
if (sel >= ntot)
@@ -1523,15 +1554,15 @@ static int form_run(const char *title, struct tui_form_field *f, int nf,
}
if (nxt == TUI_FORM_REFRESH || nxt == TUI_FORM_SUBMIT ||
nxt == TUI_FORM_BACK)
- return nxt;
+ return tui_form_focus_store(focus, sel, nxt);
if (key) {
int hk = key(ud, ch);
if (hk == TUI_HOOK_BACK)
- return TUI_FORM_BACK;
+ return tui_form_focus_store(focus, sel, TUI_FORM_BACK);
if (hk == TUI_HOOK_REFRESH)
- return TUI_FORM_REFRESH;
+ return tui_form_focus_store(focus, sel, TUI_FORM_REFRESH);
if (hk == TUI_HOOK_SUBMIT)
- return TUI_FORM_SUBMIT;
+ return tui_form_focus_store(focus, sel, TUI_FORM_SUBMIT);
if (hk != TUI_HOOK_NONE)
continue;
}
@@ -1550,7 +1581,8 @@ static int form_run(const char *title, struct tui_form_field *f, int nf,
}
if (act->enabled == -1)
continue;
- return TUI_FORM_ACTION + (sel - nf);
+ return tui_form_focus_store(focus, sel,
+ TUI_FORM_ACTION + (sel - nf));
}
if (!can_edit) {
tui_message(title, "Endast behöriga kan ändra.");
@@ -1558,49 +1590,50 @@ static int form_run(const char *title, struct tui_form_field *f, int nf,
}
struct tui_form_field *fl = &f[sel];
if (fl->kind == TUI_F_ACTION)
- return sel;
+ return tui_form_focus_store(focus, sel, sel);
char label[192];
snprintf(label, sizeof label, "%s: ", fl->label);
if (fl->kind == TUI_F_TEXT) {
if (fl->value &&
tui_prompt_into(fl->value, fl->cap, label, fl->value,
fl->mask))
- return sel;
+ return tui_form_focus_store(focus, sel, sel);
} else if (fl->kind == TUI_F_DATE) {
if (fl->value && tui_date_prompt_into(fl->value, fl->cap, label,
fl->value))
- return sel;
+ return tui_form_focus_store(focus, sel, sel);
} else if (fl->kind == TUI_F_AMOUNT) {
if (fl->ore && tui_amount_prompt_into(fl->ore, label, *fl->ore))
- return sel;
+ return tui_form_focus_store(focus, sel, sel);
} else if (fl->kind == TUI_F_CHOICE && fl->choice) {
int c = tui_choice_prompt(label, fl->choices, fl->nchoices,
*fl->choice);
if (c >= 0) {
*fl->choice = c;
- return sel;
+ return tui_form_focus_store(focus, sel, sel);
}
}
}
}
int tui_form_run(const char *title, struct tui_form_field *f, int n,
- int can_edit)
+ int can_edit, int *focus)
{
- return form_run(title, f, n, can_edit, NULL, 0, NULL, NULL, NULL);
+ return form_run(title, f, n, can_edit, NULL, 0, NULL, NULL, NULL, focus);
}
int tui_form_run_hook(const char *title, struct tui_form_field *f, int n,
- int can_edit, int (*key)(void *ud, int ch), void *ud)
+ int can_edit, int (*key)(void *ud, int ch), void *ud,
+ int *focus)
{
- return form_run(title, f, n, can_edit, NULL, 0, NULL, key, ud);
+ return form_run(title, f, n, can_edit, NULL, 0, NULL, key, ud, focus);
}
int tui_form_run_actions(const char *title, struct tui_form_field *f, int nf,
int can_edit, const struct tui_form_action *acts,
- int na, const char *hint)
+ int na, const char *hint, int *focus)
{
- return form_run(title, f, nf, can_edit, acts, na, hint, NULL, NULL);
+ return form_run(title, f, nf, can_edit, acts, na, hint, NULL, NULL, focus);
}
/* ------------------------------------------------------------------ */