splinter: a status line that plugins can write
New host API for it: sp_host_add_status() takes a named renderer, and sp_host_status_text() joins the segments with ' | '. The numbers a plugin shows come from the harness through struct sp_meter - context used and window, session tokens, cache hits, tokens per second, running tasks. A plugin that counted for itself would drift from the harness within one turn. plugins/status renders clock, ctx with a 24 column bar, cache, tok/s, tokens in and out, cost and task count. Prices are configuration, not knowledge: SPLINTER_CTX_MAX=32768 SPLINTER_PRICE_IN=0.3 SPLINTER_PRICE_OUT=1.2 Without prices the cost segment says nothing instead of inventing a number. Also fixed: the status.so recipe compiled kids.c, so the plugin that loaded was the kid mode with a different name - the reason seven status registrations produced an empty line. Known open: the segments compose (probe: 'clock: 17:28:14 | ctx 0/32.8k [........................] 0% | 0 in / 0 out | $0.0000') but the bar still draws only the title - not yet isolated.
This commit is contained in:
1 parent
a8667a8ccc
commit
3f65dfdc4c
6 files changed
+320
-1
No files matched your search
@@ -56,6 +56,9 @@ struct sp_host {
|
||||
size_t ncompact;
|
||||
const struct sp_gate *gate[MAX_COMPACTORS];
|
||||
size_t ngate;
|
||||
const struct sp_status *status[MAX_COMPACTORS];
|
||||
size_t nstatus;
|
||||
struct sp_meter meter;
|
||||
struct {
|
||||
char name[32];
|
||||
struct sp_transport tr;
|
||||
@@ -174,6 +177,57 @@ sp_host_add_compactor(struct sp_host *h, const struct sp_compactor *c)
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
sp_host_add_status(struct sp_host *h, const struct sp_status *s)
|
||||
{
|
||||
if (h == NULL || s == NULL || s->render == NULL || s->name == NULL)
|
||||
return -1;
|
||||
if (h->nstatus == MAX_COMPACTORS)
|
||||
return -1;
|
||||
h->status[h->nstatus++] = s;
|
||||
return 0;
|
||||
}
|
||||
|
||||
const struct sp_meter *
|
||||
sp_host_meter(struct sp_host *h)
|
||||
{
|
||||
static const struct sp_meter empty;
|
||||
|
||||
return h != NULL ? &h->meter : ∅
|
||||
}
|
||||
|
||||
void
|
||||
sp_host_set_meter(struct sp_host *h, const struct sp_meter *m)
|
||||
{
|
||||
if (h != NULL && m != NULL)
|
||||
h->meter = *m;
|
||||
}
|
||||
|
||||
size_t
|
||||
sp_host_status_text(struct sp_host *h, char *buf, size_t len)
|
||||
{
|
||||
size_t i, used = 0;
|
||||
|
||||
if (h == NULL || buf == NULL || len == 0)
|
||||
return 0;
|
||||
buf[0] = '\0';
|
||||
for (i = 0; i < h->nstatus; i++) {
|
||||
char piece[256];
|
||||
int n;
|
||||
|
||||
piece[0] = '\0';
|
||||
h->status[i]->render(h, piece, sizeof(piece));
|
||||
if (piece[0] == '\0')
|
||||
continue;
|
||||
n = snprintf(buf + used, len - used, "%s%s",
|
||||
used > 0 ? " | " : "", piece);
|
||||
if (n < 0 || (size_t)n >= len - used)
|
||||
break;
|
||||
used += (size_t)n;
|
||||
}
|
||||
return used;
|
||||
}
|
||||
|
||||
int
|
||||
sp_host_add_gate(struct sp_host *h, const struct sp_gate *g)
|
||||
{
|
||||
|
||||
@@ -755,6 +755,32 @@ draw_sidebar(struct ui_state *s, const char *id, const char *dir,
|
||||
wnoutrefresh(ui_side);
|
||||
}
|
||||
|
||||
/*
|
||||
* Feed the meter the status plugins read. The harness counts; a plugin must not
|
||||
* count for itself or the two numbers drift apart.
|
||||
*/
|
||||
static struct sp_host *g_status_host;
|
||||
|
||||
static void
|
||||
update_meter(struct ui_state *s, struct sp_host *host)
|
||||
{
|
||||
struct sp_meter m;
|
||||
const char *ctxmax = getenv("SPLINTER_CTX_MAX");
|
||||
|
||||
if (host == NULL)
|
||||
return;
|
||||
m = *sp_host_meter(host);
|
||||
m.ctx_used = s->tokens_in;
|
||||
m.ctx_max = ctxmax != NULL && *ctxmax != '\0' ?
|
||||
(size_t)strtoul(ctxmax, NULL, 10) : m.ctx_max;
|
||||
m.tok_in = s->tokens_in;
|
||||
m.tok_out = s->tokens_out;
|
||||
if (s->last_ms > 0 && s->tokens_out > 0)
|
||||
m.tps = (long)((double)s->tokens_out * 1000.0 /
|
||||
(double)s->last_ms);
|
||||
sp_host_set_meter(host, &m);
|
||||
}
|
||||
|
||||
static void
|
||||
draw_status(struct ui_state *s)
|
||||
{
|
||||
@@ -764,6 +790,20 @@ draw_status(struct ui_state *s)
|
||||
wattron(ui_status, COLOR_PAIR(PAIR_STATUS));
|
||||
mvwhline(ui_status, 0, 0, ' ', cols);
|
||||
mvwprintw(ui_status, 0, 1, sp_t("status.title"), SP_VERSION);
|
||||
if (g_status_host != NULL) {
|
||||
char seg[512];
|
||||
size_t n;
|
||||
|
||||
update_meter(s, g_status_host);
|
||||
n = sp_host_status_text(g_status_host, seg, sizeof(seg));
|
||||
if (n > 0) {
|
||||
int at = 1 + (int)strlen("splinter") + 1 +
|
||||
(int)strlen(SP_VERSION) + 2;
|
||||
|
||||
if (at + (int)n < cols)
|
||||
mvwaddnstr(ui_status, 0, at, seg, cols - at);
|
||||
}
|
||||
}
|
||||
{
|
||||
char right[96];
|
||||
|
||||
@@ -928,6 +968,7 @@ sp_ui_run(struct sp_agent *ag, const char *endpoint, const char *theme_name,
|
||||
memset(&s, 0, sizeof(s));
|
||||
sp_buf_init(&s.last_think);
|
||||
s.endpoint = endpoint;
|
||||
g_status_host = host; /* the status line is drawn per tick */
|
||||
if (sp_buf_init(&answer) != 0 || sp_buf_init(&err) != 0)
|
||||
return 1;
|
||||
if (sp_theme_load(theme_name, &s.theme, &err) != 0) {
|
||||
|
||||
Reference in new issue
Block a user