Skip to content
pi-c Documentation
Login

Documentation

/* The settings panel's lines and keys (panel.c). A view with no storage: it
 * takes the settings list as get_settings answers it, a width and its own
 * small state (the selected row, the filter, an open field), and gives lines
 * and a cursor; a key gives what the caller is to do (save a value, close,
 * open a picker). The caller saves, re-reads and decides where the lines go:
 * a box over the transcript, the rows above the footer, or one line. */
#ifndef PIC_TUI_PANEL_H
#define PIC_TUI_PANEL_H

#include <stddef.h>

#include "term.h"
#include "width.h"

/* A row of get_settings (docs/reference/rpc-commands.md), as its fields are named there. */
typedef struct {
    char *id, *key, *group, *name, *kind, *unit, *def, *def_label, *description, *applies;
    char *stored; /* NULL when unset */
    char *value;  /* the value in effect */
    int valid;    /* the stored value is allowed */
    char **values, **labels; /* a choice's, nvalues of each */
    size_t nvalues;
    long long min, max; /* a number's range */
} tui_setting;

typedef struct {
    tui_setting *s;
    size_t n;
    long long version; /* the change counter the answer carries */
} tui_settings;

/* get_settings' data into OUT (emptied first): 0; 1 when it says unchanged (OUT is left as it was, its
 * version too); -1 when it is not such an answer. */
int tui_settings_parse(const char *json, tui_settings *out);
void tui_settings_free(tui_settings *l);
const tui_setting *tui_settings_get(const tui_settings *l, const char *id);
/* How the row's value is shown: a choice's label, a number with its unit, a worked-out default's label.
 * Into out (cap bytes). */
void tui_setting_shown(const tui_setting *s, char *out, size_t cap);

typedef struct {
    char sel[64];      /* the selected row's id ("": the first) */
    char filter[128];
    int field;         /* a field is open on the selected row */
    char text[1100];   /* what is typed in it */
    size_t cur;        /* the cursor in text, in bytes */
    char note[300];    /* a line under the description: an error, or a change made elsewhere */
    int note_err;
    size_t top;        /* the first list row shown */
    size_t page;       /* list rows last drawn (PageUp, PageDown, a click) */
    int form;          /* TUI_PANEL_* last drawn */
    /* Another list in the same panel (/auth): its title, a line in the filter's place (no filtering,
     * no cursor), its keys line. NULL: the settings panel's. Its rows are kind "info": the value as
     * given (red when not valid), and under the description the row's own keys (applies). */
    const char *title, *head, *hint;
} tui_panel;

/* Where the lines go: a bordered box, the rows above the footer, or a line and its keys. */
enum { TUI_PANEL_BOX, TUI_PANEL_DOCK, TUI_PANEL_LINE };

/* A box's list rows are its height less this: borders, filter, separator, description. */
#define TUI_PANEL_CHROME 7

/* The frame the settings panel and the model picker share, around CONTENT (lines WIDTH - 4 columns wide in
 * a box, WIDTH - 1 docked). BOX: ┌─ TITLE ── COUNT ─┐, the lines in borders with ├──┤ before line SEP
 * (0: none), └─ HINTS ──┘ (a plain rule without hints). DOCK: TITLE and COUNT, the lines (all but the first
 * one column in), then HINTS when there are any. 0 or -1. */
int tui_panel_frame(int form, const char *title, const char *count, const tui_lines *content, size_t sep,
                    const char *hints, size_t width, tui_lines *out);

/* Which of N list items show in LIST rows with item SEL (-1: none) selected: from *first, *k of them, and
 * whether the first and last rows say how many more there are (*up, *down). *TOP is where the window was
 * and is kept; the selection keeps a row of margin. */
void tui_panel_scroll(size_t *top, size_t n, long sel, size_t list, size_t *first, size_t *k, int *up, int *down);

/* The rows of the list for the current filter: group headings and settings with no filter, the matching
 * settings best first with one. */
size_t tui_panel_list_len(const tui_panel *p, const tui_settings *l);

/* The panel's lines, appended to OUT. BOX: WIDTH columns wide (at least 24), LIST rows of list, so
 * LIST + TUI_PANEL_CHROME lines. DOCK: WIDTH is the window's, a title, the filter, LIST rows, the
 * description and the keys. LINE: the selected setting and its value, then the keys (LIST is not used).
 * The cursor (in the filter, or the open field) at line *crow, column *ccol of them. 0 or -1. */
int tui_panel_lines(tui_panel *p, const tui_settings *l, int form, size_t width, size_t list, tui_lines *out, int *crow,
                    int *ccol);

/* What a key asks of the caller. */
enum {
    TUI_PANEL_NONE,  /* nothing (the panel's own state may have changed: draw again) */
    TUI_PANEL_CLOSE,
    TUI_PANEL_SET,   /* save value for id */
    TUI_PANEL_RESET, /* back to the default: remove id's value */
    TUI_PANEL_PICK   /* open the picker of id's row */
};
typedef struct {
    int what;
    char id[64];
    char value[1100];
} tui_panel_act;

/* A key as decoded (before the keybindings), mouse reports aside: the list's keys, the filter's, an open
 * field's. A field stays open after TUI_PANEL_SET: the caller closes it (field = 0) once the value is
 * saved, or leaves it open with a note. 0, with ACT filled. */
int tui_panel_key(tui_panel *p, const tui_settings *l, const tui_key *k, tui_panel_act *act);

/* A click on line LINE of the lines tui_panel_lines last gave: selects the setting drawn there (and leaves a
 * field). 1 when it did. */
int tui_panel_click(tui_panel *p, const tui_settings *l, size_t line);

/* Selects row ID (no-op when the list has no such row) and closes any field. */
void tui_panel_select(tui_panel *p, const tui_settings *l, const char *id);

#endif