diff --git a/src/i18n.c b/src/i18n.c new file mode 100644 index 0000000..87b7113 --- /dev/null +++ b/src/i18n.c @@ -0,0 +1,213 @@ +/* + * splinter -- tiny message catalogues. + * + * Copyright (c) 2026 Konrad Lother + * SPDX-License-Identifier: BSD-3-Clause + */ +#include +#include +#include +#include +#include + +#include "splinter.h" +#include "i18n.h" + +#define MAX_KEYS 512 +#define MAX_DIRS 8 + +struct entry { + char key[96]; + char text[1024]; +}; + +static struct entry main_cat[MAX_KEYS]; +static size_t main_n; +static struct entry fallback[MAX_KEYS]; +static size_t fallback_n; +static char lang[16] = "en"; +static char dirs[MAX_DIRS][256]; +static size_t ndirs; + +void +sp_i18n_add_dir(const char *dir) +{ + if (dir == NULL || ndirs == MAX_DIRS) + return; + snprintf(dirs[ndirs++], sizeof(dirs[0]), "%s", dir); +} + +const char * +sp_i18n_lang(void) +{ + return lang; +} + +static char * +trim(char *s) +{ + char *end; + + while (*s == ' ' || *s == '\t') + s++; + end = s + strlen(s); + while (end > s && (end[-1] == '\n' || end[-1] == '\r' || + end[-1] == ' ' || end[-1] == '\t')) + *--end = '\0'; + return s; +} + +/* Detect the language from the environment, ignoring the regional part. */ +static void +detect_language(const char *override_lang) +{ + const char *cand = override_lang; + + if (cand == NULL || *cand == '\0') + cand = getenv("SPLINTER_LOCALE"); + if (cand == NULL || *cand == '\0') + cand = getenv("LC_ALL"); + if (cand == NULL || *cand == '\0') + cand = getenv("LC_MESSAGES"); + if (cand == NULL || *cand == '\0') + cand = getenv("LANG"); + + memset(lang, 0, sizeof(lang)); + if (cand == NULL || *cand == '\0') { + snprintf(lang, sizeof(lang), "en"); + return; + } + { + size_t i = 0; + + while (cand[i] != '\0' && cand[i] != '_' && cand[i] != '.' && + cand[i] != '@' && i < sizeof(lang) - 1) { + lang[i] = (char)tolower((unsigned char)cand[i]); + i++; + } + lang[i] = '\0'; + if (i == 0 || (i == 1 && lang[0] == 'c')) + snprintf(lang, sizeof(lang), "en"); + } +} + +static size_t +parse_file(const char *path, struct entry *cat, size_t max) +{ + FILE *f; + char line[1400]; + size_t n = 0; + + f = fopen(path, "r"); + if (f == NULL) + return 0; + while (n < max && fgets(line, sizeof(line), f) != NULL) { + char *eq, *key, *val; + + key = trim(line); + if (*key == '\0' || *key == '#') + continue; + eq = strchr(key, '='); + if (eq == NULL) + continue; /* a catalogue is data, not config: + * a bad line is skipped, not fatal */ + *eq = '\0'; + val = trim(eq + 1); + key = trim(key); + if (*key == '\0') + continue; + snprintf(cat[n].key, sizeof(cat[n].key), "%s", key); + snprintf(cat[n].text, sizeof(cat[n].text), "%s", val); + n++; + } + fclose(f); + return n; +} + +int +sp_i18n_load(struct sp_buf *err, const char *want) +{ + size_t i, tried = 0; + char path[512]; + + main_n = 0; + if (want == NULL || *want == '\0') + want = lang; + + for (i = 0; i < ndirs; i++) { + snprintf(path, sizeof(path), "%s/%s.msg", dirs[i], want); + main_n = parse_file(path, main_cat, MAX_KEYS); + tried++; + if (main_n > 0) + break; + } + /* English is the fallback for every key, always loaded */ + for (i = 0; i < ndirs; i++) { + if (strcmp(want, "en") == 0 && main_n > 0) + break; + snprintf(path, sizeof(path), "%s/en.msg", dirs[i]); + fallback_n = parse_file(path, fallback, MAX_KEYS); + if (fallback_n > 0) + break; + } + + if (main_n == 0 && fallback_n == 0) { + if (err != NULL) + sp_buf_printf(err, "i18n: no catalogue for '%s' in %zu " + "directories", want, tried); + return -1; + } + snprintf(lang, sizeof(lang), "%s", want); + return 0; +} + +static const char * +lookup(struct entry *cat, size_t n, const char *key) +{ + size_t i; + + for (i = 0; i < n; i++) + if (strcmp(cat[i].key, key) == 0) + return cat[i].text; + return NULL; +} + +const char * +sp_t(const char *key) +{ + const char *v; + + if (key == NULL) + return ""; + v = lookup(main_cat, main_n, key); + if (v != NULL) + return v; + v = lookup(fallback, fallback_n, key); + if (v != NULL) + return v; + return key; /* untranslated keys are visible */ +} + +const char * +sp_i18n_init(const char *override_lang) +{ + struct sp_buf err; + + detect_language(override_lang); + + /* where catalogues may live, cheapest first */ + sp_i18n_add_dir("share/locale"); + { + const char *env = getenv("SPLINTER_LOCALE_DIR"); + + if (env != NULL && *env != '\0') + sp_i18n_add_dir(env); + } + sp_i18n_add_dir("/usr/local/share/splinter/locale"); + + if (sp_buf_init(&err) != 0) + return lang; + (void)sp_i18n_load(&err, lang); + sp_buf_free(&err); + return lang; +} diff --git a/src/i18n.h b/src/i18n.h new file mode 100644 index 0000000..154467c --- /dev/null +++ b/src/i18n.h @@ -0,0 +1,38 @@ +/* + * splinter -- tiny message catalogues. + * + * Copyright (c) 2026 Konrad Lother + * SPDX-License-Identifier: BSD-3-Clause + * + * Not gettext. The whole point of this harness is that it starts in ten years + * without a build system, and gettext needs msgfmt at build time and .mo files + * at run time. So: plain `key = text` files, read at startup, hot enough. + * + * share/locale/en.msg + * share/locale/de.msg + * + * A missing translation falls back to English, and a key that exists in neither + * returns itself - visible in the UI instead of an empty line. + * + * The locale comes from, in order: $SPLINTER_LOCALE, LC_ALL, LC_MESSAGES, LANG. + * Only the language part is used ("de_DE.UTF-8" -> "de"), because a harness does + * not need regional variants to say "Sitzung". + */ +#ifndef SPLINTER_I18N_H +#define SPLINTER_I18N_H + +#include + +/* Load the catalogue for the current environment. Returns the language code in + * use ("en" when nothing matched). Never NULL. */ +const char *sp_i18n_init(const char *override_lang); + +/* Translate a key. Never NULL: falls back to English, then to the key. */ +const char *sp_t(const char *key); + +/* For tests and for plugins that ship their own catalogue. */ +int sp_i18n_load(struct sp_buf *err, const char *lang); +const char *sp_i18n_lang(void); +void sp_i18n_add_dir(const char *dir); + +#endif /* SPLINTER_I18N_H */ diff --git a/src/ui.c b/src/ui.c index 911b639..f40d770 100644 --- a/src/ui.c +++ b/src/ui.c @@ -441,7 +441,7 @@ align_utf8(const char *s, size_t len, size_t pos) * like the answer was lost. */ static void -draw_history(struct ui_state *s) +draw_history(struct ui_state *s, int reserve) { int rows, cols, y = 0; size_t idx, total = 0; @@ -449,6 +449,9 @@ draw_history(struct ui_state *s) getmaxyx(ui_hist, rows, cols); werase(ui_hist); + /* while a turn runs the bottom lines belong to it */ + if (reserve > 0 && reserve < rows) + rows -= reserve; for (idx = 0; idx < s->n; idx++) total += (size_t)entry_lines(cols, &s->entries[idx]); @@ -511,35 +514,59 @@ draw_history(struct ui_state *s) * place the conversation lives: the model's reasoning dimmed above, the answer * as it arrives below. The sidebar keeps the counters - numbers are not text. */ +static int +live_height(int rows, int len, const char *tool, const char *think) +{ + int answer = 0, th = 0; + + if (len > 0) + answer = rows / 3 > 8 ? 8 : (rows / 3 < 3 ? 3 : rows / 3); + if (think != NULL && think[0] != '\0') + th = answer > 0 ? answer : 3; + if (tool != NULL && tool[0] != '\0') + th += 0; + if (answer + th == 0) + return tool != NULL && tool[0] != '\0' ? 1 : 0; + if (answer + th + 1 > rows) + return rows - 1; + return answer + th + 1; +} + +/* + * While a turn runs, the bottom of the chat pane shows what is happening: the + * model's reasoning dimmed directly above the answer as it arrives, tool name + * under it. Anchored at the bottom, so it appears where the conversation is, + * not at the top of the window. + */ static void draw_live(struct ui_state *s, const char *text, const char *tool, const char *think) { - int rows, cols, answer_rows, think_rows; - size_t len; + int rows, cols, h, answer_rows, think_rows, y_answer; if ((text == NULL || text[0] == '\0') && (think == NULL || think[0] == '\0') && (tool == NULL || tool[0] == '\0')) return; getmaxyx(ui_hist, rows, cols); - if (rows < 4 || cols < 8) + if (rows < 5 || cols < 8) return; - len = text != NULL ? strlen(text) : 0; - answer_rows = rows / 3; - if (answer_rows < 3) - answer_rows = 3; - if (answer_rows > 8) - answer_rows = 8; - if (len == 0) - answer_rows = tool != NULL && tool[0] != '\0' ? 1 : 0; - think_rows = rows - answer_rows - 1; + h = live_height(rows, text != NULL ? (int)strlen(text) : 0, tool, think); + if (h <= 0) + return; + answer_rows = (text != NULL && text[0] != '\0') ? + (rows / 3 > 8 ? 8 : (rows / 3 < 3 ? 3 : rows / 3)) : 0; + if (answer_rows > h) + answer_rows = h; + think_rows = h - answer_rows; + y_answer = rows - answer_rows; - draw_wrapped_tail(0, think_rows > 0 ? think_rows : 0, cols, think, - PAIR_DIM, s->think_back); - if (len > 0) - draw_wrapped_tail(rows - answer_rows, answer_rows, cols, text, + if (think_rows > 0) + draw_wrapped_tail(y_answer - think_rows, think_rows, cols, think, + PAIR_DIM, s->think_back); + if (answer_rows > 0) + draw_wrapped_tail(y_answer, answer_rows, cols, text, PAIR_ACCENT, 0); if (tool != NULL && tool[0] != '\0') { char head[96]; @@ -723,7 +750,7 @@ sp_ui_run(struct sp_agent *ag, const char *endpoint, const char *theme_name, pending_steer[0] = '\0'; - draw_history(&s); + draw_history(&s, 0); draw_sidebar(&s, ag->sess != NULL ? ag->sess->id : NULL, ag->sess != NULL ? ag->sess->dir : NULL, host); draw_status(&s); @@ -895,7 +922,7 @@ sp_ui_run(struct sp_agent *ag, const char *endpoint, const char *theme_name, } add_entry(&s, "you", input); - draw_history(&s); + draw_history(&s, 0); draw_sidebar(&s, ag->sess != NULL ? ag->sess->id : NULL, ag->sess != NULL ? ag->sess->dir : NULL, host); draw_status(&s); @@ -960,6 +987,7 @@ sp_ui_run(struct sp_agent *ag, const char *endpoint, const char *theme_name, if (c == ERR) { char live[4096]; char think[2048]; + int live_reserve = 0; char tool[64]; size_t llen2 = 0, tlen2 = 0; @@ -981,13 +1009,16 @@ sp_ui_run(struct sp_agent *ag, const char *endpoint, const char *theme_name, pthread_mutex_unlock(&tu.lock); live[llen2] = '\0'; think[tlen2] = '\0'; + live_reserve = live_height( + getmaxy(ui_hist), + (int)llen2, tool, think); if (tlen2 > 0) { sp_buf_clear(&s.last_think); (void)sp_buf_puts(&s.last_think, think); } s.out_chars = llen2; - draw_history(&s); + draw_history(&s, live_reserve); draw_live(&s, live, tool, think); doupdate(); /* redraw the clock in the status bar */ @@ -1005,7 +1036,7 @@ sp_ui_run(struct sp_agent *ag, const char *endpoint, const char *theme_name, } if (c == KEY_PPAGE) { s.think_back += 3; - draw_history(&s); + draw_history(&s, 0); draw_sidebar(&s, ag->sess != NULL ? ag->sess->id : NULL, ag->sess != NULL ? ag->sess->dir : @@ -1094,9 +1125,9 @@ sp_ui_run(struct sp_agent *ag, const char *endpoint, const char *theme_name, const char *th = tu.think.p; size_t tl = tu.think.len; - if (tl > 1500) { - th += tl - 1500; - tl = 1500; + if (tl > 400) { + th += tl - 400; + tl = 400; } add_entry(&s, "thinking", th); } diff --git a/tests/test_ascii b/tests/test_ascii index 696f2bc..b111d53 100755 Binary files a/tests/test_ascii and b/tests/test_ascii differ