ui: slash commands run off the screen thread, sysinfo one row per metric
A command that talks to the network (/connect, /model probe, a plugin command, a commands/ program) ran on the UI thread: the prompt froze until it returned. ui_command_run() now runs be->input on its own thread while the screen keeps reading keys. One next line can be queued and is processed when the command ends; Ctrl-C clears the edit, Ctrl-D quits. The worker never touches curses, and the backend is not polled while it runs. Widget text may now have continuation lines without "name:": they are rendered under the widget above. sysinfo uses that for one metric per row instead of wrapping "RAM ... CPU ..." into the same paragraph. A blank row stays between widgets, not between their rows. The Ctrl-C UI test looks for the active steer prompt; "working" is replaced by it on the first 100 ms tick. make check: all tests passed.
This commit is contained in:
1 parent
67b4d5d070
commit
be3d0ee415
6 files changed
+190
-11
No files matched your search
@@ -160,7 +160,7 @@ tick(struct sp_host *h, const char *name, void *ctx)
|
||||
gauge(sb, sizeof(sb), swap);
|
||||
gauge(db, sizeof(db), disk);
|
||||
snprintf(line, sizeof(line),
|
||||
"RAM %.0f%% %s CPU %.0f%% %s SWAP %.0f%% %s DISK %.0f%% %s",
|
||||
"RAM %.0f%% %s\nCPU %.0f%% %s\nSWAP %.0f%% %s\nDISK %.0f%% %s",
|
||||
ram, rb, cpu, cb, swap, sb, disk, db);
|
||||
return sp_host_widget_text(h, name, line);
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
ui.ready = bereit. Enter sendet, Ctrl-D beendet.
|
||||
ui.more_lines = ... %zu Zeilen mehr
|
||||
ui.working = laeuft...
|
||||
ui.command_running = Kommando laeuft...
|
||||
ui.reconnected = wieder verbunden
|
||||
ui.reconnecting = Verbindung weg - verbinde neu ...
|
||||
ui.no_sessions = noch keine Sitzungen
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
ui.ready = ready. Enter sends, Ctrl-D quits.
|
||||
ui.more_lines = ... %zu more lines
|
||||
ui.working = working...
|
||||
ui.command_running = running command...
|
||||
ui.reconnected = reconnected
|
||||
ui.reconnecting = connection lost - reconnecting ...
|
||||
ui.no_sessions = no sessions yet
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
ui.ready = pronto. Enter envia, Ctrl-D sai.
|
||||
ui.more_lines = ... %zu linhas a mais
|
||||
ui.working = trabalhando...
|
||||
ui.command_running = executando comando...
|
||||
ui.reconnected = reconectado
|
||||
ui.reconnecting = conexao perdida - reconectando ...
|
||||
ui.no_sessions = nenhuma sessao ainda
|
||||
|
||||
+185
-9
@@ -1184,6 +1184,7 @@ draw_sidebar(struct ui_state *s, const char *id, const char *dir,
|
||||
/* what the plugins want to show: the goal, the plan, tasks ... */
|
||||
if (host != NULL && sp_host_widget_count(host) > 0) {
|
||||
char snap[1024], *line, *save = NULL;
|
||||
char cont_name[48] = "";
|
||||
|
||||
sp_host_widget_snapshot(host, snap, sizeof(snap));
|
||||
for (line = strtok_r(snap, "\n", &save); line != NULL;
|
||||
@@ -1192,8 +1193,16 @@ draw_sidebar(struct ui_state *s, const char *id, const char *dir,
|
||||
char *text;
|
||||
size_t nl;
|
||||
|
||||
if (colon == NULL)
|
||||
/*
|
||||
* A line without "name:" continues the widget above
|
||||
* it. sysinfo uses that for one metric per row.
|
||||
*/
|
||||
if (colon == NULL) {
|
||||
if (cont_name[0] != '\0' && *line != '\0' &&
|
||||
y < getmaxy(ui_side) - 2)
|
||||
side_text(&y, cols, line, 1);
|
||||
continue;
|
||||
}
|
||||
nl = (size_t)(colon - line);
|
||||
if (nl >= sizeof(name))
|
||||
continue;
|
||||
@@ -1210,11 +1219,16 @@ draw_sidebar(struct ui_state *s, const char *id, const char *dir,
|
||||
}
|
||||
if (*text == '\0' || y >= getmaxy(ui_side) - 2)
|
||||
continue;
|
||||
/* one blank row between widgets, not between their rows */
|
||||
if (cont_name[0] != '\0' && y < getmaxy(ui_side) - 2)
|
||||
y++;
|
||||
snprintf(key, sizeof(key), "widget.%s", name);
|
||||
sidebar_heading(&y, cols, sp_td(key, name));
|
||||
side_text(&y, cols, text, 4);
|
||||
y++;
|
||||
snprintf(cont_name, sizeof(cont_name), "%s", name);
|
||||
}
|
||||
if (cont_name[0] != '\0' && y < getmaxy(ui_side) - 2)
|
||||
y++;
|
||||
}
|
||||
|
||||
/* the last turn, in one line - two where the column is narrow */
|
||||
@@ -1930,6 +1944,163 @@ seg_show(struct ui_state *s, struct seg *g, const char *role, const char *text)
|
||||
}
|
||||
|
||||
/* what the screen keeps of a running turn between two ticks */
|
||||
/*
|
||||
* A slash command may talk to a network endpoint (/connect, /model probe, a
|
||||
* plugin command). Running it on the screen thread froze the prompt until it
|
||||
* returned, which is exactly when the operator wants to type the next line.
|
||||
* The command runs on its own thread; the screen keeps reading keys and can
|
||||
* queue one line for after it.
|
||||
*/
|
||||
struct cmd_job {
|
||||
const struct ui_backend *be;
|
||||
char line[1024];
|
||||
struct sp_buf out;
|
||||
int kind;
|
||||
int done;
|
||||
pthread_mutex_t lock;
|
||||
};
|
||||
|
||||
static void *
|
||||
command_thread(void *arg)
|
||||
{
|
||||
struct cmd_job *j = arg;
|
||||
|
||||
j->kind = j->be->input(j->be->ctx, j->line, &j->out);
|
||||
pthread_mutex_lock(&j->lock);
|
||||
j->done = 1;
|
||||
pthread_mutex_unlock(&j->lock);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static int
|
||||
command_done(struct cmd_job *j)
|
||||
{
|
||||
int done;
|
||||
|
||||
pthread_mutex_lock(&j->lock);
|
||||
done = j->done;
|
||||
pthread_mutex_unlock(&j->lock);
|
||||
return done;
|
||||
}
|
||||
|
||||
static void
|
||||
draw_command_line(const char *text, size_t len)
|
||||
{
|
||||
int w = getmaxx(ui_input);
|
||||
|
||||
werase(ui_input);
|
||||
mvwaddnstr(ui_input, 0, 0, sp_td("ui.command_running",
|
||||
"running command... "), w - 1);
|
||||
if (text != NULL && len > 0)
|
||||
ui_add_text(ui_input, text, w - 1);
|
||||
wrefresh(ui_input);
|
||||
}
|
||||
|
||||
static int
|
||||
ui_command_run(struct ui_state *s, const char *line, struct sp_buf *reply,
|
||||
char *queued, size_t qcap)
|
||||
{
|
||||
struct cmd_job j;
|
||||
pthread_t th;
|
||||
int threaded;
|
||||
char edit[1024];
|
||||
size_t elen = 0;
|
||||
|
||||
memset(&j, 0, sizeof(j));
|
||||
j.be = s->be;
|
||||
snprintf(j.line, sizeof(j.line), "%s", line);
|
||||
if (sp_buf_init(&j.out) != 0)
|
||||
return s->be->input(s->be->ctx, line, reply);
|
||||
pthread_mutex_init(&j.lock, NULL);
|
||||
threaded = pthread_create(&th, NULL, command_thread, &j) == 0;
|
||||
if (!threaded) {
|
||||
/* no thread is no reason to lose the command */
|
||||
j.kind = s->be->input(s->be->ctx, line, &j.out);
|
||||
j.done = 1;
|
||||
}
|
||||
edit[0] = '\0';
|
||||
if (threaded) {
|
||||
wtimeout(ui_input, 100);
|
||||
for (;;) {
|
||||
int c;
|
||||
|
||||
if (command_done(&j))
|
||||
break;
|
||||
c = wgetch(ui_input);
|
||||
if (s->quit)
|
||||
break;
|
||||
if (c == ERR) {
|
||||
draw_command_line(queued[0] != '\0' ? queued :
|
||||
edit, queued[0] != '\0' ? strlen(queued) :
|
||||
elen);
|
||||
continue;
|
||||
}
|
||||
if (c == 3) {
|
||||
if (queued[0] != '\0') {
|
||||
queued[0] = '\0';
|
||||
} else {
|
||||
edit[0] = '\0';
|
||||
elen = 0;
|
||||
}
|
||||
draw_command_line(queued[0] != '\0' ? queued :
|
||||
edit, queued[0] != '\0' ? strlen(queued) :
|
||||
elen);
|
||||
continue;
|
||||
}
|
||||
if (c == 4) {
|
||||
s->quit = 1;
|
||||
break;
|
||||
}
|
||||
if (c == KEY_RESIZE) {
|
||||
relayout(100);
|
||||
wtimeout(ui_input, 100);
|
||||
draw_command_line(queued[0] != '\0' ? queued :
|
||||
edit, queued[0] != '\0' ? strlen(queued) :
|
||||
elen);
|
||||
continue;
|
||||
}
|
||||
if (c == '\n' || c == '\r' || c == KEY_ENTER) {
|
||||
if (queued[0] == '\0' && elen > 0 &&
|
||||
elen + 1 < qcap) {
|
||||
memcpy(queued, edit, elen + 1);
|
||||
queued[elen] = '\0';
|
||||
edit[0] = '\0';
|
||||
elen = 0;
|
||||
}
|
||||
draw_command_line(queued[0] != '\0' ? queued :
|
||||
edit, queued[0] != '\0' ? strlen(queued) :
|
||||
elen);
|
||||
continue;
|
||||
}
|
||||
if (c == KEY_BACKSPACE || c == 127 || c == 8) {
|
||||
if (queued[0] != '\0') {
|
||||
/* a queued line is not edited here */
|
||||
} else if (elen > 0) {
|
||||
edit[--elen] = '\0';
|
||||
}
|
||||
draw_command_line(queued[0] != '\0' ? queued :
|
||||
edit, queued[0] != '\0' ? strlen(queued) :
|
||||
elen);
|
||||
continue;
|
||||
}
|
||||
if (c >= 32 && c < 256 && queued[0] == '\0' &&
|
||||
elen + 1 < sizeof(edit)) {
|
||||
edit[elen++] = (char)c;
|
||||
edit[elen] = '\0';
|
||||
draw_command_line(edit, elen);
|
||||
}
|
||||
}
|
||||
wtimeout(ui_input, -1);
|
||||
pthread_join(th, NULL);
|
||||
}
|
||||
sp_buf_clear(reply);
|
||||
if (j.out.p != NULL && j.out.len > 0)
|
||||
(void)sp_buf_add(reply, j.out.p, j.out.len);
|
||||
sp_buf_free(&j.out);
|
||||
pthread_mutex_destroy(&j.lock);
|
||||
return j.kind;
|
||||
}
|
||||
|
||||
struct turn_view {
|
||||
struct timespec t0;
|
||||
struct seg think, ans; /* the reasoning, the answer */
|
||||
@@ -2454,7 +2625,7 @@ int
|
||||
sp_ui_run(const struct ui_backend *be, const char *theme_name)
|
||||
{
|
||||
struct ui_state s;
|
||||
char input[1024];
|
||||
char input[1024], queued[1024] = "";
|
||||
|
||||
if (ui_start(&s, be, theme_name) != 0) {
|
||||
be->close(be->ctx);
|
||||
@@ -2474,11 +2645,16 @@ sp_ui_run(const struct ui_backend *be, const char *theme_name)
|
||||
wnoutrefresh(ui_input);
|
||||
doupdate();
|
||||
|
||||
ui_read_line(&s, be, input, sizeof(input));
|
||||
if (s.quit)
|
||||
break;
|
||||
if (input[0] == '\0')
|
||||
continue;
|
||||
if (queued[0] != '\0') {
|
||||
snprintf(input, sizeof(input), "%s", queued);
|
||||
queued[0] = '\0';
|
||||
} else {
|
||||
ui_read_line(&s, be, input, sizeof(input));
|
||||
if (s.quit)
|
||||
break;
|
||||
if (input[0] == '\0')
|
||||
continue;
|
||||
}
|
||||
if (sp_buf_init(&reply) != 0)
|
||||
break;
|
||||
if (strcmp(input, "/sessions") == 0) {
|
||||
@@ -2529,7 +2705,7 @@ sp_ui_run(const struct ui_backend *be, const char *theme_name)
|
||||
}
|
||||
continue;
|
||||
}
|
||||
kind = be->input(be->ctx, input, &reply);
|
||||
kind = ui_command_run(&s, input, &reply, queued, sizeof(queued));
|
||||
if (kind == SP_INPUT_HANDLED) {
|
||||
/*
|
||||
* The engine queued what the command did (a shell's
|
||||
|
||||
+1
-1
@@ -164,7 +164,7 @@ tmux new-session -d -s $S -x 110 -y 30 "HOME=$D/home SPLINTER_LOCALE=en \
|
||||
check shows 'Enter sends'
|
||||
sleep 1
|
||||
typed 'tu was'; key Enter
|
||||
check shows 'working'
|
||||
check shows 'steer>'
|
||||
sleep 0.5
|
||||
key C-c
|
||||
check shows 'cancelled'
|
||||
|
||||
Reference in new issue
Block a user