prompt: the input line has a cursor (Ctrl-A/E, Left/Right, Delete)
The prompt only appended: Ctrl-A, Ctrl-E, Left and Right fell through the printable-key path and vanished, and fixing the first word of a sentence meant retyping it. One struct editline now holds buf/cap/len/cur (bytes, always on a UTF-8 boundary) and both prompts edit with the same keys. Two bugs surfaced with it: - draw_prompt_cur guarded the caret with 'caret > 0', so caret == 0 (the start of the line) was drawn as the end of the text. - the swarm block reset ed.cur to ed.len on every loop tick, which undid every cursor move right after the key that made it. The steer line of a running turn gets the same editor and a window that follows its caret, instead of cutting the text at the right edge.
This commit is contained in:
1 parent
ca81d297c2
commit
5ad5768e6a
3 files changed
+440
-70
No files matched your search
@@ -443,11 +443,22 @@ observation, whichever comes first, and says how many lines are left out, so an
|
||||
`ls -la` or a 300 KB JSON dump does not bury the conversation; the log has all
|
||||
of it.
|
||||
|
||||
## Input history
|
||||
## The input line
|
||||
|
||||
Up and Down walk through what was typed before, like a shell. The list is kept
|
||||
in `<session_dir>/history`, so the next session starts with it.
|
||||
|
||||
The prompt is a line editor, not an append buffer: Ctrl-A and Home go to the
|
||||
start, Ctrl-E and End to the end, Left/Right and Ctrl-B/Ctrl-F move by a
|
||||
character (a whole UTF-8 sequence, never a byte), Backspace and Delete remove
|
||||
before and at the caret, Ctrl-K cuts to the end, Ctrl-U clears the line and
|
||||
Ctrl-W removes the word before the caret. Ctrl-L repaints, Ctrl-C clears the
|
||||
line, Ctrl-D quits, Tab completes a `/command` or switches the agent, and
|
||||
Shift+Enter inserts a line break. The `steer>` line of a running turn takes the
|
||||
same keys and scrolls with its caret, so a long sentence stays readable while
|
||||
you type into it. Both prompts used to append every key, which made fixing the
|
||||
first word of a sentence mean retyping it.
|
||||
|
||||
## Steering while a turn runs
|
||||
|
||||
The turn runs on its own thread, so the keyboard stays live. Every tool call is
|
||||
|
||||
+411
-69
@@ -2009,20 +2009,225 @@ ui_getkey(WINDOW *w, int restore_ms)
|
||||
return r;
|
||||
}
|
||||
|
||||
/* ---- the input line: bytes, length, cursor -----------------------------
|
||||
*
|
||||
* One line of text is `buf` (NUL-terminated) plus `len` bytes in it and `cur`:
|
||||
* where the next character goes, counted in BYTES but always on a UTF-8
|
||||
* boundary. Bytes are the only unit the rest of the program uses (the session
|
||||
* log, the buffer capacity), and keeping the cursor in the same unit means no
|
||||
* second index to keep in sync; the helpers below move it by whole sequences.
|
||||
*
|
||||
* Both prompts use this: the ordinary one and the "steer>" line a running turn
|
||||
* shows. They look different but must behave the same, and they used to not
|
||||
* behave at all - every key appended, so Ctrl-A and Left did nothing.
|
||||
*/
|
||||
struct editline {
|
||||
char *buf;
|
||||
size_t cap;
|
||||
size_t len;
|
||||
size_t cur;
|
||||
};
|
||||
|
||||
/* the start of the character that ends at `i` */
|
||||
static size_t
|
||||
el_prev(const struct editline *e, size_t i)
|
||||
{
|
||||
if (i == 0)
|
||||
return 0;
|
||||
i--;
|
||||
while (i > 0 && ((unsigned char)e->buf[i] & 0xc0) == 0x80)
|
||||
i--;
|
||||
return i;
|
||||
}
|
||||
|
||||
/* the start of the character after the one at `i` */
|
||||
static size_t
|
||||
el_next(const struct editline *e, size_t i)
|
||||
{
|
||||
if (i >= e->len)
|
||||
return e->len;
|
||||
i++;
|
||||
while (i < e->len && ((unsigned char)e->buf[i] & 0xc0) == 0x80)
|
||||
i++;
|
||||
return i;
|
||||
}
|
||||
|
||||
static void
|
||||
el_home(struct editline *e)
|
||||
{
|
||||
e->cur = 0;
|
||||
}
|
||||
|
||||
static void
|
||||
el_end(struct editline *e)
|
||||
{
|
||||
e->cur = e->len;
|
||||
}
|
||||
|
||||
static void
|
||||
el_left(struct editline *e)
|
||||
{
|
||||
e->cur = el_prev(e, e->cur);
|
||||
}
|
||||
|
||||
static void
|
||||
el_right(struct editline *e)
|
||||
{
|
||||
e->cur = el_next(e, e->cur);
|
||||
}
|
||||
|
||||
/* insert one character (one or more bytes) at the cursor */
|
||||
static void
|
||||
el_insert(struct editline *e, const char *s, size_t n)
|
||||
{
|
||||
if (s == NULL || n == 0 || e->len + n + 1 > e->cap)
|
||||
return;
|
||||
memmove(e->buf + e->cur + n, e->buf + e->cur, e->len - e->cur + 1);
|
||||
memcpy(e->buf + e->cur, s, n);
|
||||
e->len += n;
|
||||
e->cur += n;
|
||||
}
|
||||
|
||||
/* the character before the cursor goes; returns 1 when something changed */
|
||||
static int
|
||||
el_backspace(struct editline *e)
|
||||
{
|
||||
size_t from, to;
|
||||
|
||||
if (e->cur == 0)
|
||||
return 0;
|
||||
from = e->cur;
|
||||
to = el_prev(e, e->cur);
|
||||
memmove(e->buf + to, e->buf + from, e->len - from + 1);
|
||||
e->len -= from - to;
|
||||
e->cur = to;
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* the character AT the cursor goes (Delete) */
|
||||
static int
|
||||
el_delete(struct editline *e)
|
||||
{
|
||||
size_t from, to;
|
||||
|
||||
if (e->cur >= e->len)
|
||||
return 0;
|
||||
from = e->cur;
|
||||
to = el_next(e, e->cur);
|
||||
memmove(e->buf + from, e->buf + to, e->len - to + 1);
|
||||
e->len -= to - from;
|
||||
return 1;
|
||||
}
|
||||
|
||||
static void
|
||||
el_clear(struct editline *e)
|
||||
{
|
||||
e->len = 0;
|
||||
e->cur = 0;
|
||||
if (e->buf != NULL && e->cap > 0)
|
||||
e->buf[0] = '\0';
|
||||
}
|
||||
|
||||
static void
|
||||
el_set(struct editline *e, const char *s)
|
||||
{
|
||||
size_t n = s != NULL ? strlen(s) : 0;
|
||||
|
||||
if (n + 1 > e->cap)
|
||||
n = e->cap > 0 ? e->cap - 1 : 0;
|
||||
if (n > 0 && e->buf != NULL)
|
||||
memcpy(e->buf, s, n);
|
||||
if (e->buf != NULL && e->cap > 0)
|
||||
e->buf[n] = '\0';
|
||||
e->len = n;
|
||||
e->cur = n;
|
||||
}
|
||||
|
||||
/*
|
||||
* The editing keys, in one place because both prompts take them. Returns 1 when
|
||||
* the key was one of ours (and the caller must repaint), 0 when it was not.
|
||||
* Ctrl-A/Ctrl-E are the readline pair the operator expects; Ctrl-U clears like
|
||||
* Ctrl-C, Ctrl-K cuts to the end, Ctrl-W deletes the word before the cursor.
|
||||
*/
|
||||
static int
|
||||
el_key(struct editline *e, int c)
|
||||
{
|
||||
switch (c) {
|
||||
case 1: /* Ctrl-A */
|
||||
el_home(e);
|
||||
return 1;
|
||||
case 5: /* Ctrl-E */
|
||||
el_end(e);
|
||||
return 1;
|
||||
case 2: /* Ctrl-B */
|
||||
el_left(e);
|
||||
return 1;
|
||||
case 6: /* Ctrl-F */
|
||||
el_right(e);
|
||||
return 1;
|
||||
case KEY_LEFT:
|
||||
el_left(e);
|
||||
return 1;
|
||||
case KEY_RIGHT:
|
||||
el_right(e);
|
||||
return 1;
|
||||
case KEY_HOME:
|
||||
el_home(e);
|
||||
return 1;
|
||||
case KEY_END:
|
||||
el_end(e);
|
||||
return 1;
|
||||
case KEY_BACKSPACE:
|
||||
case 127:
|
||||
case 8:
|
||||
return el_backspace(e) || 1;
|
||||
case KEY_DC:
|
||||
return el_delete(e) || 1;
|
||||
case 11: /* Ctrl-K: to the end */
|
||||
e->len = e->cur;
|
||||
e->buf[e->len] = '\0';
|
||||
return 1;
|
||||
case 21: /* Ctrl-U: the whole line */
|
||||
el_clear(e);
|
||||
return 1;
|
||||
case 23: { /* Ctrl-W: the word before the cursor */
|
||||
size_t to = e->cur;
|
||||
|
||||
while (to > 0 && e->buf[to - 1] == ' ')
|
||||
to--;
|
||||
while (to > 0 && e->buf[to - 1] != ' ')
|
||||
to = el_prev(e, to);
|
||||
if (to < e->cur) {
|
||||
memmove(e->buf + to, e->buf + e->cur,
|
||||
e->len - e->cur + 1);
|
||||
e->len -= e->cur - to;
|
||||
e->cur = to;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* The prompt box with what has been typed so far. It wraps at the window
|
||||
* width, grows to five rows and then keeps the line with the cursor visible;
|
||||
* the last column stays empty (writing the bottom-right cell of a window that
|
||||
* width, grows to five rows and then keeps the row with the cursor visible; the
|
||||
* last column stays empty (writing the bottom-right cell of a window that
|
||||
* cannot scroll is an error in this curses port).
|
||||
*
|
||||
* `caret` is the cursor position in bytes into `input`. The window scrolls
|
||||
* vertically to the caret's row; the horizontal case does not exist because the
|
||||
* text wraps instead of running off.
|
||||
*/
|
||||
static void
|
||||
draw_prompt(struct ui_state *s, const char *input)
|
||||
draw_prompt_cur(struct ui_state *s, const char *input, size_t caret)
|
||||
{
|
||||
const char *mark = prompt_mark();
|
||||
char *ascii;
|
||||
size_t cap, i;
|
||||
char *ascii, *pre;
|
||||
size_t cap, i, clen;
|
||||
int cols, marklen, row, used, total, cur_row, cur_used;
|
||||
int prefix, room, start, drawn = -1;
|
||||
int prefix, room, start, drawn = -1, have_cur = 0;
|
||||
|
||||
cols = getmaxx(ui_input) - 1;
|
||||
if (cols < 8)
|
||||
@@ -2036,13 +2241,42 @@ draw_prompt(struct ui_state *s, const char *input)
|
||||
ascii[0] = '\0';
|
||||
else if (sp_ui_ascii(input, ascii, cap) != 0)
|
||||
snprintf(ascii, cap, "%s", input);
|
||||
/*
|
||||
* The caret lives in the input, but the screen shows the transliterated
|
||||
* text: a character can become several bytes ("ü" -> "ue"). Translating
|
||||
* the prefix up to the caret gives the column, without a second table
|
||||
* that could disagree with the transliterator.
|
||||
*/
|
||||
clen = strlen(ascii);
|
||||
/*
|
||||
* caret == 0 is the start of the line, not "no caret": the guard must
|
||||
* not be caret > 0, or Ctrl-A draws the cursor at the end of the text
|
||||
* while the editor's cursor sits at the beginning.
|
||||
*/
|
||||
if (input != NULL && caret <= strlen(input)) {
|
||||
pre = malloc(cap);
|
||||
if (pre != NULL) {
|
||||
snprintf(pre, cap, "%.*s", (int)caret, input);
|
||||
if (sp_ui_ascii(pre, pre, cap) != 0)
|
||||
snprintf(pre, cap, "%.*s", (int)caret, input);
|
||||
clen = strlen(pre);
|
||||
free(pre);
|
||||
}
|
||||
}
|
||||
|
||||
/* first pass: how many rows, and where the cursor is */
|
||||
row = 0;
|
||||
used = 0;
|
||||
total = 1;
|
||||
prefix = marklen;
|
||||
cur_row = 0;
|
||||
cur_used = 0;
|
||||
for (i = 0; ascii[i] != '\0'; i++) {
|
||||
if (!have_cur && i == clen) {
|
||||
cur_row = row;
|
||||
cur_used = used;
|
||||
have_cur = 1;
|
||||
}
|
||||
if (ascii[i] == '\n') {
|
||||
row++;
|
||||
used = 0;
|
||||
@@ -2063,8 +2297,10 @@ draw_prompt(struct ui_state *s, const char *input)
|
||||
}
|
||||
used++;
|
||||
}
|
||||
cur_row = row;
|
||||
cur_used = used;
|
||||
if (!have_cur) {
|
||||
cur_row = row;
|
||||
cur_used = used;
|
||||
}
|
||||
if (total < 1)
|
||||
total = 1;
|
||||
{
|
||||
@@ -2081,7 +2317,21 @@ draw_prompt(struct ui_state *s, const char *input)
|
||||
}
|
||||
}
|
||||
werase(ui_input);
|
||||
start = total > ui_input_h ? total - ui_input_h : 0;
|
||||
/*
|
||||
* The window follows the CURSOR, not the end of the text: with Ctrl-A
|
||||
* the operator is at the top of a long line and must see it. Keep the
|
||||
* caret row inside [start, start + ui_input_h).
|
||||
*/
|
||||
if (total <= ui_input_h)
|
||||
start = 0;
|
||||
else if (cur_row < ui_input_h)
|
||||
start = 0;
|
||||
else
|
||||
start = cur_row - ui_input_h + 1;
|
||||
if (start > total - ui_input_h)
|
||||
start = total - ui_input_h;
|
||||
if (start < 0)
|
||||
start = 0;
|
||||
row = 0;
|
||||
used = 0;
|
||||
prefix = marklen;
|
||||
@@ -2138,6 +2388,16 @@ draw_prompt(struct ui_state *s, const char *input)
|
||||
free(ascii);
|
||||
}
|
||||
|
||||
/*
|
||||
* The prompt with the caret at the end - the shape every caller that has no
|
||||
* cursor of its own wants (a repaint after a widget ticked, a restored line).
|
||||
*/
|
||||
static void
|
||||
draw_prompt(struct ui_state *s, const char *input)
|
||||
{
|
||||
draw_prompt_cur(s, input, input != NULL ? strlen(input) : 0);
|
||||
}
|
||||
|
||||
/* ---- /model: a picker, not a printed list ------------------------------ */
|
||||
|
||||
struct pick_list {
|
||||
@@ -2386,6 +2646,11 @@ model_picker(const struct ui_backend *be, char *sel, size_t cap)
|
||||
* Read one line at the prompt. Enter ends it; Ctrl-D sets s->quit. While no
|
||||
* key comes, the read times out every 500 ms and repaints sidebar and status
|
||||
* line: a widget that ticks is useless if the screen only refreshes on a key.
|
||||
*
|
||||
* Editing keys (Ctrl-A/E, Left/Right, Home/End, Backspace/Delete, Ctrl-K/U/W)
|
||||
* come from el_key(): a prompt that cannot move its cursor is a prompt you
|
||||
* retype. `input` is the caller's buffer and cap its size; what comes back is
|
||||
* exactly that buffer, NUL-terminated at `len`.
|
||||
*/
|
||||
static void
|
||||
ui_read_line(struct ui_state *s, const struct ui_backend *be, char *input,
|
||||
@@ -2393,10 +2658,14 @@ ui_read_line(struct ui_state *s, const struct ui_backend *be, char *input,
|
||||
{
|
||||
struct sp_host *host = be->host(be->ctx);
|
||||
struct swarm *sw = be->swarm(be->ctx);
|
||||
size_t len = 0;
|
||||
struct editline ed;
|
||||
int ch;
|
||||
int locked = (sw != NULL && sw->role == SWARM_SLAVE && sw->locked);
|
||||
|
||||
ed.buf = input;
|
||||
ed.cap = cap;
|
||||
ed.len = 0;
|
||||
ed.cur = 0;
|
||||
input[0] = '\0';
|
||||
if (locked) {
|
||||
werase(ui_input);
|
||||
@@ -2414,6 +2683,12 @@ ui_read_line(struct ui_state *s, const struct ui_backend *be, char *input,
|
||||
if (sw != NULL) {
|
||||
swarm_poll(sw);
|
||||
locked = (sw->role == SWARM_SLAVE && sw->locked);
|
||||
/*
|
||||
* The inbox hands over a whole line and returns; the
|
||||
* editor is not touched otherwise. Re-syncing len/cur
|
||||
* from `input` here would undo every cursor move the
|
||||
* editor just made (Ctrl-A, Left, Home).
|
||||
*/
|
||||
if (swarm_take_inbox(sw, input, cap)) {
|
||||
wtimeout(ui_input, -1);
|
||||
return;
|
||||
@@ -2448,7 +2723,7 @@ ui_read_line(struct ui_state *s, const struct ui_backend *be, char *input,
|
||||
mvwaddstr(ui_input, 0, 0, sp_td("ui.swarm_locked", "[swarm locked] "));
|
||||
wnoutrefresh(ui_input);
|
||||
} else {
|
||||
draw_prompt(s, input);
|
||||
draw_prompt_cur(s, input, ed.cur);
|
||||
wnoutrefresh(ui_input);
|
||||
}
|
||||
doupdate();
|
||||
@@ -2456,10 +2731,9 @@ ui_read_line(struct ui_state *s, const struct ui_backend *be, char *input,
|
||||
}
|
||||
if (ch == UK_NEWLINE) {
|
||||
/* Shift+Enter (Alt+Enter): a line break, not a send */
|
||||
if (!locked && len + 1 < cap) {
|
||||
input[len++] = '\n';
|
||||
input[len] = '\0';
|
||||
draw_prompt(s, input);
|
||||
if (!locked) {
|
||||
el_insert(&ed, "\n", 1);
|
||||
draw_prompt_cur(s, input, ed.cur);
|
||||
wrefresh(ui_input);
|
||||
}
|
||||
continue;
|
||||
@@ -2478,7 +2752,7 @@ ui_read_line(struct ui_state *s, const struct ui_backend *be, char *input,
|
||||
werase(ui_input);
|
||||
mvwaddstr(ui_input, 0, 0, sp_td("ui.swarm_locked", "[swarm locked] "));
|
||||
} else
|
||||
draw_prompt(s, input);
|
||||
draw_prompt_cur(s, input, ed.cur);
|
||||
wnoutrefresh(ui_input);
|
||||
doupdate();
|
||||
continue;
|
||||
@@ -2506,15 +2780,14 @@ ui_read_line(struct ui_state *s, const struct ui_backend *be, char *input,
|
||||
werase(ui_input);
|
||||
mvwaddstr(ui_input, 0, 0, sp_td("ui.swarm_locked", "[swarm locked] "));
|
||||
} else
|
||||
draw_prompt(s, input);
|
||||
draw_prompt_cur(s, input, ed.cur);
|
||||
wnoutrefresh(ui_input);
|
||||
doupdate();
|
||||
continue;
|
||||
}
|
||||
if (ch == 3) {
|
||||
if (locked) continue;
|
||||
len = 0;
|
||||
input[0] = '\0';
|
||||
el_clear(&ed);
|
||||
draw_prompt(s, input);
|
||||
wrefresh(ui_input);
|
||||
continue;
|
||||
@@ -2531,12 +2804,9 @@ ui_read_line(struct ui_state *s, const struct ui_backend *be, char *input,
|
||||
else if (ch == KEY_DOWN)
|
||||
s->hist_pos = (int)s->nhist;
|
||||
if (s->hist_pos >= (int)s->nhist || s->hist_pos < 0)
|
||||
len = 0;
|
||||
else {
|
||||
snprintf(input, cap, "%s", s->hist[s->hist_pos]);
|
||||
len = strlen(input);
|
||||
}
|
||||
input[len] = '\0';
|
||||
el_clear(&ed);
|
||||
else
|
||||
el_set(&ed, s->hist[s->hist_pos]);
|
||||
draw_prompt(s, input);
|
||||
wrefresh(ui_input);
|
||||
continue;
|
||||
@@ -2553,7 +2823,7 @@ ui_read_line(struct ui_state *s, const struct ui_backend *be, char *input,
|
||||
werase(ui_input);
|
||||
mvwaddstr(ui_input, 0, 0, sp_td("ui.swarm_locked", "[swarm locked] "));
|
||||
} else
|
||||
draw_prompt(s, input);
|
||||
draw_prompt_cur(s, input, ed.cur);
|
||||
wnoutrefresh(ui_input);
|
||||
doupdate();
|
||||
continue;
|
||||
@@ -2569,22 +2839,22 @@ ui_read_line(struct ui_state *s, const struct ui_backend *be, char *input,
|
||||
werase(ui_input);
|
||||
mvwaddstr(ui_input, 0, 0, sp_td("ui.swarm_locked", "[swarm locked] "));
|
||||
} else
|
||||
draw_prompt(s, input);
|
||||
draw_prompt_cur(s, input, ed.cur);
|
||||
wnoutrefresh(ui_input);
|
||||
doupdate();
|
||||
continue;
|
||||
}
|
||||
if (ch == KEY_BACKSPACE || ch == 127 || ch == 8) {
|
||||
if (locked) continue;
|
||||
if (len > 0) {
|
||||
/* drop a whole UTF-8 sequence, not half of one */
|
||||
do
|
||||
len--;
|
||||
while (len > 0 && ((unsigned char)input[len] & 0xc0) == 0x80);
|
||||
input[len] = '\0';
|
||||
draw_prompt(s, input);
|
||||
wrefresh(ui_input);
|
||||
}
|
||||
if (locked)
|
||||
continue;
|
||||
/*
|
||||
* The editing keys come before the agent switch and the command
|
||||
* completion, because a printable key is not the only thing a
|
||||
* prompt has to understand. KEY_LEFT, HOME and friends used to
|
||||
* fall through to the append path and vanish.
|
||||
*/
|
||||
if (el_key(&ed, ch)) {
|
||||
draw_prompt_cur(s, input, ed.cur);
|
||||
wrefresh(ui_input);
|
||||
continue;
|
||||
}
|
||||
/*
|
||||
@@ -2596,7 +2866,7 @@ ui_read_line(struct ui_state *s, const struct ui_backend *be, char *input,
|
||||
int n = be->agents(be->ctx);
|
||||
int cur = be->agent_cur(be->ctx);
|
||||
|
||||
if (locked || n < 2)
|
||||
if (n < 2)
|
||||
continue;
|
||||
/*
|
||||
* A turn still running (another client's, a swarm's)
|
||||
@@ -2609,7 +2879,7 @@ ui_read_line(struct ui_state *s, const struct ui_backend *be, char *input,
|
||||
"it is done"));
|
||||
draw_history(s);
|
||||
s->painted = s->gen;
|
||||
draw_prompt(s, input);
|
||||
draw_prompt_cur(s, input, ed.cur);
|
||||
wnoutrefresh(ui_input);
|
||||
doupdate();
|
||||
continue;
|
||||
@@ -2621,7 +2891,7 @@ ui_read_line(struct ui_state *s, const struct ui_backend *be, char *input,
|
||||
draw_history(s);
|
||||
draw_sidebar(s, SESS_ID(be), SESS_DIR(be), host);
|
||||
draw_statusbar(s);
|
||||
draw_prompt(s, input);
|
||||
draw_prompt_cur(s, input, ed.cur);
|
||||
wnoutrefresh(ui_input);
|
||||
doupdate();
|
||||
continue;
|
||||
@@ -2630,7 +2900,6 @@ ui_read_line(struct ui_state *s, const struct ui_backend *be, char *input,
|
||||
* Slash completion: Tab shows available /commands.
|
||||
*/
|
||||
if (ch == '\t' && input[0] == '/' && strchr(input, ' ') == NULL) {
|
||||
if (locked) continue;
|
||||
{
|
||||
char matches[32][64];
|
||||
int nm = be->command_names(be->ctx, matches,
|
||||
@@ -2649,8 +2918,10 @@ ui_read_line(struct ui_state *s, const struct ui_backend *be, char *input,
|
||||
if (strncmp(matches[i2], prefix, plen) == 0)
|
||||
m[k++] = matches[i2];
|
||||
if (k == 1) {
|
||||
snprintf(input, cap, "/%s ", m[0]);
|
||||
len = strlen(input);
|
||||
char one[96];
|
||||
|
||||
snprintf(one, sizeof(one), "/%s ", m[0]);
|
||||
el_set(&ed, one);
|
||||
} else if (k > 1) {
|
||||
size_t c = strlen(m[0]);
|
||||
for (i2 = 1; i2 < k; i2++) {
|
||||
@@ -2659,8 +2930,11 @@ ui_read_line(struct ui_state *s, const struct ui_backend *be, char *input,
|
||||
c = j;
|
||||
}
|
||||
if (c > plen) {
|
||||
snprintf(input, cap, "/%.*s", (int)c, m[0]);
|
||||
len = strlen(input);
|
||||
char one[96];
|
||||
|
||||
snprintf(one, sizeof(one),
|
||||
"/%.*s", (int)c, m[0]);
|
||||
el_set(&ed, one);
|
||||
}
|
||||
{
|
||||
char hint[512]; size_t off = 0;
|
||||
@@ -2685,19 +2959,17 @@ ui_read_line(struct ui_state *s, const struct ui_backend *be, char *input,
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if (locked)
|
||||
continue;
|
||||
if (ch >= 32 && ch < 256 && len + 1 < cap) {
|
||||
input[len++] = (char)ch;
|
||||
input[len] = '\0';
|
||||
draw_prompt(s, input);
|
||||
if (ch >= 32 && ch < 256) {
|
||||
char one = (char)ch;
|
||||
|
||||
el_insert(&ed, &one, 1);
|
||||
draw_prompt_cur(s, input, ed.cur);
|
||||
wrefresh(ui_input);
|
||||
}
|
||||
}
|
||||
wtimeout(ui_input, -1);
|
||||
}
|
||||
|
||||
|
||||
static void
|
||||
seg_show(struct ui_state *s, struct seg *g, const char *role, const char *text)
|
||||
{
|
||||
@@ -2903,6 +3175,7 @@ struct turn_view {
|
||||
struct sp_buf live; /* the step's answer so far */
|
||||
char line[1024]; /* steering text being typed */
|
||||
size_t llen;
|
||||
size_t lcur; /* the caret in line, in bytes */
|
||||
int rc;
|
||||
int got_end;
|
||||
};
|
||||
@@ -3102,8 +3375,9 @@ drain_events(struct ui_state *s, struct turn_view *v)
|
||||
static void
|
||||
turn_draw_input(struct ui_state *s, struct turn_view *v)
|
||||
{
|
||||
int w, used;
|
||||
char steer[32];
|
||||
char steer[32], *ascii, *pre;
|
||||
size_t cap, clen;
|
||||
int w, marklen, cols, total, caret, start_off, shown;
|
||||
|
||||
(void)s;
|
||||
werase(ui_input);
|
||||
@@ -3121,10 +3395,58 @@ turn_draw_input(struct ui_state *s, struct turn_view *v)
|
||||
}
|
||||
}
|
||||
mvwaddnstr(ui_input, 0, 0, steer, w);
|
||||
used = (int)strlen(steer);
|
||||
if (used < w)
|
||||
ui_add_text(ui_input, v->line, w - used - 1);
|
||||
marklen = (int)strlen(steer);
|
||||
if (marklen >= w) {
|
||||
wclrtoeol(ui_input);
|
||||
wnoutrefresh(ui_input);
|
||||
doupdate();
|
||||
return;
|
||||
}
|
||||
cols = w - marklen;
|
||||
/*
|
||||
* The line is transliterated for the screen, so a caret in bytes has
|
||||
* to be translated too: "ü" is two bytes and two cells here.
|
||||
*/
|
||||
cap = strlen(v->line) * 4 + 16;
|
||||
ascii = malloc(cap);
|
||||
pre = malloc(cap);
|
||||
if (ascii == NULL || pre == NULL) {
|
||||
free(ascii);
|
||||
free(pre);
|
||||
wclrtoeol(ui_input);
|
||||
wnoutrefresh(ui_input);
|
||||
doupdate();
|
||||
return;
|
||||
}
|
||||
if (sp_ui_ascii(v->line, ascii, cap) != 0)
|
||||
snprintf(ascii, cap, "%s", v->line);
|
||||
snprintf(pre, cap, "%.*s", (int)v->lcur, v->line);
|
||||
if (sp_ui_ascii(pre, pre, cap) != 0)
|
||||
snprintf(pre, cap, "%.*s", (int)v->lcur, v->line);
|
||||
caret = (int)strlen(pre);
|
||||
total = (int)strlen(ascii);
|
||||
/*
|
||||
* A long line scrolls so that the caret stays on screen: a steering
|
||||
* line that runs out of the window right while you type into it hides
|
||||
* the one character you are looking at.
|
||||
*/
|
||||
start_off = 0;
|
||||
if (caret >= cols)
|
||||
start_off = caret - cols + 1;
|
||||
if (start_off > total)
|
||||
start_off = total;
|
||||
shown = total - start_off;
|
||||
if (shown > cols)
|
||||
shown = cols;
|
||||
if (shown > 0)
|
||||
mvwaddnstr(ui_input, 0, marklen, ascii + start_off, shown);
|
||||
wclrtoeol(ui_input);
|
||||
clen = (size_t)caret - (size_t)start_off;
|
||||
if (clen > (size_t)cols - 1)
|
||||
clen = (size_t)cols - 1;
|
||||
wmove(ui_input, 0, marklen + (int)clen);
|
||||
free(ascii);
|
||||
free(pre);
|
||||
wnoutrefresh(ui_input);
|
||||
doupdate();
|
||||
}
|
||||
@@ -3335,6 +3657,7 @@ turn_key(struct ui_state *s, struct turn_view *v, int c)
|
||||
if (v->line[0] == '/' || v->line[0] == '!') {
|
||||
snprintf(line, sizeof(line), "%s", v->line);
|
||||
v->llen = 0;
|
||||
v->lcur = 0;
|
||||
v->line[0] = '\0';
|
||||
turn_run_command(s, line);
|
||||
turn_draw_input(s, v);
|
||||
@@ -3352,22 +3675,41 @@ turn_key(struct ui_state *s, struct turn_view *v, int c)
|
||||
add_entry(s, "error",
|
||||
sp_t("ui.steer_failed"));
|
||||
v->llen = 0;
|
||||
v->lcur = 0;
|
||||
v->line[0] = '\0';
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
if ((c == KEY_BACKSPACE || c == 127 || c == 8) && v->llen > 0) {
|
||||
/* a whole UTF-8 sequence, not a byte */
|
||||
do
|
||||
v->llen--;
|
||||
while (v->llen > 0 &&
|
||||
((unsigned char)v->line[v->llen] & 0xc0) == 0x80);
|
||||
v->line[v->llen] = '\0';
|
||||
return 0;
|
||||
/*
|
||||
* The steering line is edited with the same keys as the prompt: Ctrl-A
|
||||
* and Ctrl-E, Left/Right, Home/End, Delete, Ctrl-K/U/W. Words are
|
||||
* retyped often enough while a turn runs; a line that only appends
|
||||
* costs the whole sentence.
|
||||
*/
|
||||
{
|
||||
struct editline ed;
|
||||
|
||||
ed.buf = v->line;
|
||||
ed.cap = sizeof(v->line);
|
||||
ed.len = v->llen;
|
||||
ed.cur = v->lcur;
|
||||
if (el_key(&ed, c)) {
|
||||
v->llen = ed.len;
|
||||
v->lcur = ed.cur;
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
if (c >= 32 && c < 256 && c != 127 && v->llen < sizeof(v->line) - 2) {
|
||||
v->line[v->llen++] = (char)c;
|
||||
v->line[v->llen] = '\0';
|
||||
if (c >= 32 && c < 256 && c != 127) {
|
||||
struct editline ed;
|
||||
char one = (char)c;
|
||||
|
||||
ed.buf = v->line;
|
||||
ed.cap = sizeof(v->line);
|
||||
ed.len = v->llen;
|
||||
ed.cur = v->lcur;
|
||||
el_insert(&ed, &one, 1);
|
||||
v->llen = ed.len;
|
||||
v->lcur = ed.cur;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -147,6 +147,13 @@ key BSpace BSpace
|
||||
check shows '^build> ha *$' # the backspaces arrived
|
||||
key C-l; sleep 1
|
||||
check shows '^build> ha *$'
|
||||
# Ctrl-A and Ctrl-E move the caret, not only the text: a prompt that only
|
||||
# appends makes you retype a sentence to fix its first word. The keys are
|
||||
# checked mid-text, where appending shows up as a wrong string.
|
||||
key C-a; typed 'x'; check shows '^build> xha *$'
|
||||
key C-e; typed 'y'; check shows '^build> xhay *$'
|
||||
key C-a C-f; key BSpace; check shows '^build> hay *$'
|
||||
key C-a C-k; check shows '^build> *$'
|
||||
typed 'zeig eine tabelle'; key Enter
|
||||
check shows '| a | b |'
|
||||
check shows '|---|----|'
|
||||
@@ -189,6 +196,16 @@ check shows 'steer>'
|
||||
# the elapsed seconds are the first meter in the status bar, not part of
|
||||
# the prompt; the prompt line is only "steer> " and what was typed
|
||||
check shows 's | ctx'
|
||||
# the steering line takes the same editing keys as the prompt (Ctrl-A/E,
|
||||
# Left, Delete); it used to append every key, so a typo mid-sentence meant
|
||||
# cancelling the turn to fix it
|
||||
typed 'eins zwei'
|
||||
key C-a; typed 'A'
|
||||
check shows 'steer> Aeins zwei'
|
||||
key C-e; typed 'Z'
|
||||
check shows 'steer> Aeins zweiZ'
|
||||
key C-a C-e C-u
|
||||
check shows '^steer> *$'
|
||||
sleep 0.5
|
||||
key C-c
|
||||
check shows 'cancelled'
|
||||
|
||||
Reference in new issue
Block a user