/* Unit tests for the terminal UI modules (width, term decoder, render, editor). */ #include "tui/atfile.h" #include "tui/editor.h" #include "tui/fuzzy.h" #include "tui/markdown.h" #include "tui/render.h" #include "tui/term.h" #include "tui/view.h" #include "tui/viewport.h" #include "tui/width.h" #include #include #include #include #include #include #include #define SYNC_ON "\x1b[?2026h" #define SYNC_OFF "\x1b[?2026l" #define HIDE "\x1b[?25l" #define SHOW "\x1b[?25h" static void fail(const char *what, const char *fmt, ...) { va_list ap; fprintf(stderr, "FAIL: %s: ", what); va_start(ap, fmt); vfprintf(stderr, fmt, ap); va_end(ap); fputc('\n', stderr); exit(1); } static void pass(const char *what) { printf("PASS: %s\n", what); } /* Escapes control bytes so a failure message is readable. */ static void show(const char *s, size_t n, char *out, size_t cap) { size_t o = 0; for (size_t i = 0; i < n && o + 8 < cap; i++) { unsigned char c = (unsigned char)s[i]; if (c == 0x1b) o += (size_t)snprintf(out + o, cap - o, "\\e"); else if (c == '\n') o += (size_t)snprintf(out + o, cap - o, "\\n"); else if (c == '\r') o += (size_t)snprintf(out + o, cap - o, "\\r"); else if (c < 0x20) o += (size_t)snprintf(out + o, cap - o, "\\x%02x", c); else out[o++] = (char)c; } out[o] = 0; } static void check_str(const char *what, const char *got, const char *want) { if (strcmp(got, want) != 0) { char a[1024], b[1024]; show(got, strlen(got), a, sizeof a); show(want, strlen(want), b, sizeof b); fail(what, "got \"%s\" want \"%s\"", a, b); } } static void check_int(const char *what, long got, long want) { if (got != want) fail(what, "got %ld want %ld", got, want); } /* ---- width ------------------------------------------------------------- */ static void test_width(void) { uint32_t cp; check_int("decode ascii", (long)tui_utf8_decode("a", 1, &cp), 1); check_int("decode ascii cp", cp, 'a'); check_int("decode 2-byte", (long)tui_utf8_decode("\xC3\xA9", 2, &cp), 2); check_int("decode 2-byte cp", cp, 0xE9); check_int("decode 3-byte", (long)tui_utf8_decode("\xE4\xB8\xAD", 3, &cp), 3); check_int("decode 3-byte cp", cp, 0x4E2D); check_int("decode 4-byte", (long)tui_utf8_decode("\xF0\x9F\x98\x80", 4, &cp), 4); check_int("decode 4-byte cp", cp, 0x1F600); check_int("decode truncated", (long)tui_utf8_decode("\xE4\xB8", 2, &cp), 1); check_int("decode truncated cp", cp, 0xFFFD); check_int("decode overlong", (long)tui_utf8_decode("\xC0\x80", 2, &cp), 1); check_int("decode surrogate", (long)tui_utf8_decode("\xED\xA0\x80", 3, &cp), 1); check_int("decode stray continuation", (long)tui_utf8_decode("\x80", 1, &cp), 1); check_int("decode real U+FFFD", (long)tui_utf8_decode("\xEF\xBF\xBD", 3, &cp), 3); pass("utf8 decode"); check_int("width a", tui_cp_width('a'), 1); check_int("width nul", tui_cp_width(0), 0); check_int("width esc", tui_cp_width(0x1b), 0); check_int("width C1", tui_cp_width(0x85), 0); check_int("width e-acute", tui_cp_width(0xE9), 1); check_int("width combining acute", tui_cp_width(0x301), 0); check_int("width combining 1AB0", tui_cp_width(0x1AB0), 0); check_int("width combining 1DC0", tui_cp_width(0x1DC0), 0); check_int("width combining 20D0", tui_cp_width(0x20D0), 0); check_int("width ZWSP", tui_cp_width(0x200B), 0); check_int("width ZWJ", tui_cp_width(0x200D), 0); check_int("width VS16", tui_cp_width(0xFE0F), 0); check_int("width VS supplement", tui_cp_width(0xE0100), 0); check_int("width CJK", tui_cp_width(0x4E2D), 2); check_int("width hangul", tui_cp_width(0xAC00), 2); check_int("width jamo", tui_cp_width(0x1100), 2); check_int("width fullwidth A", tui_cp_width(0xFF21), 2); check_int("width 303F narrow", tui_cp_width(0x303F), 1); check_int("width emoji", tui_cp_width(0x1F600), 2); check_int("width emoji 1F9FF", tui_cp_width(0x1F9FF), 2); check_int("width CJK ext B", tui_cp_width(0x20000), 2); check_int("width cent sign", tui_cp_width(0xA2), 1); check_int("width plain symbol", tui_cp_width(0x2603), 1); pass("code point width"); const char *s = "\x1b[31mred\x1b[0m"; check_int("width ignores sgr", (long)tui_width(s, strlen(s)), 3); s = "\x1b]8;;http://x\x1b\\link\x1b]8;;\x1b\\"; check_int("width ignores osc", (long)tui_width(s, strlen(s)), 4); s = "\x1b]0;title\a" "ab"; check_int("width ignores osc bel", (long)tui_width(s, strlen(s)), 2); check_int("width tab", (long)tui_width("a\tb", 3), 6); check_int("width cjk string", (long)tui_width("\xE4\xB8\xAD\xE6\x96\x87", 6), 4); check_int("width combining string", (long)tui_width("e\xCC\x81", 3), 1); check_int("width truncated escape", (long)tui_width("a\x1b[3", 4), 1); pass("display width"); check_int("fit all", (long)tui_fit("hello", 5, 10), 5); check_int("fit prefix", (long)tui_fit("hello", 5, 3), 3); check_int("fit wide", (long)tui_fit("\xE4\xB8\xAD\xE6\x96\x87", 6, 3), 3); check_int("fit wide exact", (long)tui_fit("\xE4\xB8\xAD\xE6\x96\x87", 6, 4), 6); s = "\x1b[31mabc"; check_int("fit keeps escape whole", (long)tui_fit(s, strlen(s), 2), 7); check_int("fit zero cols", (long)tui_fit("abc", 3, 0), 0); pass("fit"); } static void lines_eq(const char *what, const tui_lines *l, size_t n, ...) { va_list ap; va_start(ap, n); if (l->n != n) { for (size_t i = 0; i < l->n; i++) fprintf(stderr, " line %zu: \"%s\"\n", i, l->lines[i]); fail(what, "got %zu lines want %zu", l->n, n); } for (size_t i = 0; i < n; i++) { const char *want = va_arg(ap, const char *); char label[128]; snprintf(label, sizeof label, "%s line %zu", what, i); check_str(label, l->lines[i], want); } va_end(ap); } static void wrap_check(const char *what, const char *text, size_t cols, size_t n, ...) { tui_lines l = {0}; if (tui_wrap(text, strlen(text), cols, &l) != 0) fail(what, "wrap failed"); va_list ap; va_start(ap, n); if (l.n != n) fail(what, "got %zu lines want %zu", l.n, n); for (size_t i = 0; i < n; i++) { const char *want = va_arg(ap, const char *); char label[128]; snprintf(label, sizeof label, "%s line %zu", what, i); check_str(label, l.lines[i], want); if (tui_width(l.lines[i], strlen(l.lines[i])) > cols) fail(what, "line %zu wider than %zu", i, cols); } va_end(ap); tui_lines_clear(&l); } static void test_wrap(void) { tui_lines l = {0}; check_int("lines add", tui_lines_add(&l, "abc", 2), 0); check_str("lines copy", l.lines[0], "ab"); tui_lines_clear(&l); check_int("lines cleared", (long)l.n, 0); pass("lines list"); wrap_check("wrap short", "hello", 10, 1, "hello"); wrap_check("wrap empty", "", 10, 1, ""); wrap_check("wrap newline", "ab\ncd", 10, 2, "ab", "cd"); wrap_check("wrap trailing newline", "ab\n", 10, 2, "ab", ""); wrap_check("wrap words", "hello world foo", 11, 2, "hello world", "foo"); wrap_check("wrap drops spaces at break", "hello world", 6, 2, "hello", "world"); wrap_check("wrap hard break", "abcdefghij", 4, 3, "abcd", "efgh", "ij"); wrap_check("wrap long word after text", "ab cdefghij", 4, 3, "ab", "cdef", "ghij"); wrap_check("wrap exact fit", "abcd efgh", 4, 2, "abcd", "efgh"); wrap_check("wrap space at edge", "abcd efgh", 5, 2, "abcd", "efgh"); wrap_check("wrap tab", "a\tb", 10, 1, "a b"); wrap_check("wrap wide", "\xE4\xB8\xAD\xE6\x96\x87\xE5\xAD\x97", 4, 2, "\xE4\xB8\xAD\xE6\x96\x87", "\xE5\xAD\x97"); wrap_check("wrap keeps leading indent", " ab", 10, 1, " ab"); pass("wrap plain"); wrap_check("wrap sgr carries", "\x1b[31mred green blue\x1b[0m", 9, 2, "\x1b[31mred green\x1b[0m", "\x1b[31mblue\x1b[0m"); wrap_check("wrap sgr reset before break", "\x1b[1mbold\x1b[0m plain text", 10, 2, "\x1b[1mbold\x1b[0m plain", "text"); wrap_check("wrap sgr hard break", "\x1b[32mabcdef", 3, 2, "\x1b[32mabc\x1b[0m", "\x1b[32mdef"); wrap_check("wrap sgr across newline", "\x1b[4mab\ncd", 10, 2, "\x1b[4mab\x1b[0m", "\x1b[4mcd"); wrap_check("wrap sgr two styles", "\x1b[1m\x1b[31mab cd", 2, 2, "\x1b[1m\x1b[31mab\x1b[0m", "\x1b[1m\x1b[31mcd"); pass("wrap sgr"); wrap_check("wrap drops non-SGR csi", "a\x1b[2Jb\x1b[?25lc", 10, 1, "abc"); wrap_check("wrap drops osc (title) but keeps OSC 8 hyperlinks", "a\x1b]0;title\ab\x1b]8;;u\x1b\\c", 10, 1, "ab\x1b]8;;u\x1b\\c"); wrap_check("wrap closes a hyperlink at a break and reopens it", "\x1b]8;;u\x1b\\abcdef\x1b]8;;\x1b\\", 3, 2, "\x1b]8;;u\x1b\\abc\x1b]8;;\x1b\\", "\x1b]8;;u\x1b\\def\x1b]8;;\x1b\\"); wrap_check("wrap drops ESC x", "a\x1b" "7b", 10, 1, "ab"); wrap_check("wrap drops C0 controls", "a\rb\bc\ad\x01" "e\x7f", 10, 1, "abcde"); wrap_check("wrap drops C1 controls", "a\xC2\x85" "b\xC2\x9F", 10, 1, "ab"); wrap_check("wrap keeps sgr among dropped", "\x1b[1ma\x1b[Kb", 10, 1, "\x1b[1mab"); check_int("fit stops at newline", (long)tui_fit("ab\ncd", 5, 10), 2); pass("wrap sanitizes"); } /* ---- key decoder ------------------------------------------------------- */ static const char *key_name(tui_key_kind k) { static const char *names[] = { "CHAR", "ENTER", "NEWLINE", "TAB", "BACKSPACE", "DELETE", "LEFT", "RIGHT", "UP", "DOWN", "HOME", "END", "PAGEUP", "PAGEDOWN", "WORD_LEFT", "WORD_RIGHT", "DELETE_WORD", "KILL_LINE", "KILL_START", "ESC", "CTRL_C", "CTRL_D", "CTRL_L", "CTRL_O", "PASTE", "CTRL_S", "CTRL_P", "BACKTAB", "CTRL_T", "DELETE_WORD_FWD", "YANK", "YANK_POP", "UNDO", "JUMP", "JUMP_BACK", "CTRL_G", "CTRL_X", "CTRL_Z", "ALT_UP", "CTRL_P_BACK", "ALT_ENTER", }; return (size_t)k < sizeof names / sizeof names[0] ? names[k] : "?"; } /* Drains the decoder into a space-separated description. */ static void drain(tui_keys *k, char *out, size_t cap) { size_t o = 0; tui_key key; out[0] = 0; while (tui_keys_next(k, &key)) { if (o) o += (size_t)snprintf(out + o, cap - o, " "); if (key.kind == TUI_KEY_CHAR) { o += (size_t)snprintf(out + o, cap - o, "CHAR:%s", key.text); } else if (key.kind == TUI_KEY_PASTE) { char esc[512]; show(key.paste, key.paste_len, esc, sizeof esc); o += (size_t)snprintf(out + o, cap - o, "PASTE:%s", esc); free(key.paste); } else { o += (size_t)snprintf(out + o, cap - o, "%s", key_name(key.kind)); } } } static void decode_check(const char *what, const char *bytes, size_t n, const char *want) { tui_keys *k = tui_keys_new(); char got[1024]; tui_keys_feed(k, bytes, n); drain(k, got, sizeof got); check_str(what, got, want); tui_keys_free(k); } #define DECODE(what, bytes, want) decode_check(what, bytes, sizeof(bytes) - 1, want) static void test_decoder(void) { DECODE("printable", "abc", "CHAR:a CHAR:b CHAR:c"); DECODE("utf8 2-byte", "\xC3\xA9", "CHAR:\xC3\xA9"); DECODE("utf8 3-byte", "\xE4\xB8\xAD", "CHAR:\xE4\xB8\xAD"); DECODE("utf8 emoji", "\xF0\x9F\x98\x80", "CHAR:\xF0\x9F\x98\x80"); DECODE("space", " ", "CHAR: "); DECODE("invalid bytes dropped", "\xFF\x80z", "CHAR:z"); pass("decode printable"); DECODE("cr", "\r", "ENTER"); DECODE("lf", "\n", "NEWLINE"); DECODE("tab", "\t", "TAB"); DECODE("del", "\x7f", "BACKSPACE"); DECODE("ctrl-h", "\x08", "BACKSPACE"); DECODE("ctrl-a", "\x01", "HOME"); DECODE("ctrl-e", "\x05", "END"); DECODE("ctrl-c", "\x03", "CTRL_C"); DECODE("ctrl-d", "\x04", "CTRL_D"); DECODE("ctrl-k", "\x0b", "KILL_LINE"); DECODE("ctrl-l", "\x0c", "CTRL_L"); DECODE("ctrl-o", "\x0f", "CTRL_O"); DECODE("ctrl-s", "\x13", "CTRL_S"); DECODE("ctrl-p", "\x10", "CTRL_P"); DECODE("ctrl-t", "\x14", "CTRL_T"); DECODE("shift-tab (CSI Z)", "\x1b[Z", "BACKTAB"); DECODE("ctrl-u", "\x15", "KILL_START"); DECODE("ctrl-w", "\x17", "DELETE_WORD"); DECODE("other control dropped", "\x00\x1c", ""); pass("decode control keys"); DECODE("csi arrows", "\x1b[A\x1b[B\x1b[C\x1b[D", "UP DOWN RIGHT LEFT"); DECODE("ss3 arrows", "\x1bOA\x1bOB\x1bOC\x1bOD", "UP DOWN RIGHT LEFT"); DECODE("csi home end", "\x1b[H\x1b[F", "HOME END"); DECODE("ss3 home end", "\x1bOH\x1bOF", "HOME END"); DECODE("tilde home end", "\x1b[1~\x1b[4~\x1b[7~\x1b[8~", "HOME END HOME END"); DECODE("delete pgup pgdn", "\x1b[3~\x1b[5~\x1b[6~", "DELETE PAGEUP PAGEDOWN"); pass("decode navigation"); DECODE("alt-b alt-f", "\x1b" "b\x1b" "f", "WORD_LEFT WORD_RIGHT"); DECODE("alt arrows", "\x1b[1;3D\x1b[1;3C", "WORD_LEFT WORD_RIGHT"); DECODE("ctrl arrows", "\x1b[1;5D\x1b[1;5C", "WORD_LEFT WORD_RIGHT"); DECODE("shift arrow is plain", "\x1b[1;2D", "LEFT"); DECODE("alt backspace", "\x1b\x7f", "DELETE_WORD"); DECODE("alt enter", "\x1b\r", "ALT_ENTER"); DECODE("alt enter kitty", "\x1b[13;3u", "ALT_ENTER"); DECODE("alt enter modifyOtherKeys", "\x1b[27;3;13~", "ALT_ENTER"); DECODE("ctrl+alt enter kitty", "\x1b[13;7u", "ALT_ENTER"); DECODE("esc then a key", "\x1bx", "ESC CHAR:x"); DECODE("esc then a command, one read", "\x1b/s\r", "ESC CHAR:/ CHAR:s ENTER"); pass("decode word keys"); DECODE("shift-enter kitty", "\x1b[13;2u", "NEWLINE"); DECODE("shift-enter modifyOtherKeys", "\x1b[27;2;13~", "NEWLINE"); DECODE("enter kitty plain", "\x1b[13u", "ENTER"); DECODE("enter kitty mod 1", "\x1b[13;1u", "ENTER"); pass("decode shift-enter"); DECODE("unknown csi dropped", "\x1b[99;1Xa", "CHAR:a"); DECODE("unknown csi with intermediate", "\x1b[?25$pb", "CHAR:b"); DECODE("esc then csi", "\x1b\x1b[A", "ESC UP"); pass("decode unknown sequences"); /* Split sequences: a CSI cut at every byte boundary still decodes. */ { tui_keys *k = tui_keys_new(); const char seq[] = "\x1b[1;5D"; char got[128]; for (size_t i = 0; i + 1 < sizeof seq; i++) { tui_keys_feed(k, seq + i, 1); if (i + 2 < sizeof seq) { drain(k, got, sizeof got); check_str("split csi nothing early", got, ""); check_int("split csi waiting", tui_keys_waiting(k), 1); } } drain(k, got, sizeof got); check_str("split csi done", got, "WORD_LEFT"); check_int("split csi not waiting", tui_keys_waiting(k), 0); const char u[] = "\xF0\x9F"; tui_keys_feed(k, u, 2); check_int("split utf8 waiting", tui_keys_waiting(k), 1); tui_keys_feed(k, "\x98\x80", 2); drain(k, got, sizeof got); check_str("split utf8 done", got, "CHAR:\xF0\x9F\x98\x80"); tui_keys_free(k); } pass("decode split input"); { tui_keys *k = tui_keys_new(); char got[128]; tui_keys_feed(k, "\x1b", 1); drain(k, got, sizeof got); check_str("lone esc pending", got, ""); check_int("lone esc waiting", tui_keys_waiting(k), 1); tui_keys_flush(k); drain(k, got, sizeof got); check_str("lone esc flushed", got, "ESC"); check_int("lone esc done", tui_keys_waiting(k), 0); tui_keys_feed(k, "\x1b[", 2); check_int("unfinished csi waiting", tui_keys_waiting(k), 1); for (int i = 0; i < 2; i++) { tui_keys_flush(k); check_int("unfinished csi kept through flush", tui_keys_waiting(k), 1); } tui_keys_feed(k, "A", 1); drain(k, got, sizeof got); check_str("late rest completes the csi", got, "UP"); tui_keys_feed(k, "\x1b[", 2); for (int i = 0; i < 3; i++) tui_keys_flush(k); drain(k, got, sizeof got); check_str("unfinished csi dropped after 3 flushes", got, ""); check_int("unfinished csi done", tui_keys_waiting(k), 0); tui_keys_feed(k, "\xE4\xB8", 2); tui_keys_flush(k); tui_keys_feed(k, "\xAD", 1); drain(k, got, sizeof got); check_str("utf8 split across a flush", got, "CHAR:\xE4\xB8\xAD"); tui_keys_feed(k, "\x1b", 1); tui_keys_feed(k, "[A", 2); drain(k, got, sizeof got); check_str("esc then rest is a key", got, "UP"); tui_keys_free(k); } pass("lone esc"); DECODE("paste", "\x1b[200~hello\x1b[201~", "PASTE:hello"); DECODE("paste crlf", "\x1b[200~a\r\nb\rc\nd\x1b[201~", "PASTE:a\\nb\\nc\\nd"); DECODE("paste keeps escapes inside", "\x1b[200~x\x1b[Ay\x1b[201~", "PASTE:x\\e[Ay"); DECODE("paste empty", "\x1b[200~\x1b[201~", "PASTE:"); DECODE("keys around paste", "a\x1b[200~b\x1b[201~c", "CHAR:a PASTE:b CHAR:c"); { tui_keys *k = tui_keys_new(); char got[256]; const char *parts[] = {"\x1b[20", "0~line1\r", "\nline2\x1b", "[20", "1", "~z"}; for (size_t i = 0; i < sizeof parts / sizeof parts[0]; i++) { tui_keys_feed(k, parts[i], strlen(parts[i])); drain(k, got, sizeof got); if (i < 5) check_str("paste split quiet", got, ""); else check_str("paste split done", got, "PASTE:line1\\nline2 CHAR:z"); } /* a lone ESC inside a paste is not flushed away */ tui_keys_feed(k, "\x1b[200~ab\x1b", 9); check_int("paste not waiting", tui_keys_waiting(k), 0); tui_keys_flush(k); tui_keys_feed(k, "[201~", 5); drain(k, got, sizeof got); check_str("paste survives flush", got, "PASTE:ab"); tui_keys_free(k); } pass("decode bracketed paste"); } /* ---- render ------------------------------------------------------------ */ static int rfd[2]; static void render_expect(const char *what, const char *want) { char got[4096]; ssize_t n = read(rfd[0], got, sizeof got - 1); if (n < 0) n = 0; got[n] = 0; check_str(what, got, want); if (read(rfd[0], got, 1) > 0) fail(what, "extra output after the expected frame"); } static void test_render(void) { if (pipe(rfd) != 0) fail("render", "pipe"); int fl = fcntl(rfd[0], F_GETFL); fcntl(rfd[0], F_SETFL, fl | O_NONBLOCK); tui_render *r = tui_render_new(rfd[1]); const char *ab[] = {"a", "b"}; tui_render_draw(r, ab, 2, 1, 3, 24); render_expect("first draw", SYNC_ON HIDE "a\r\nb" "\r" "\x1b[4G" SHOW SYNC_OFF); const char *abc[] = {"a", "b", "c"}; tui_render_draw(r, abc, 3, 2, 0, 24); render_expect("append writes only the new line", SYNC_ON HIDE "\r\nc" "\r" SHOW SYNC_OFF); const char *aXc[] = {"a", "X", "c"}; tui_render_draw(r, aXc, 3, 2, 1, 24); render_expect("middle change", SYNC_ON HIDE "\x1b[1A" "\r\x1b[J" "X\r\nc" "\r\x1b[2G" SHOW SYNC_OFF); tui_render_draw(r, aXc, 3, 0, 0, 24); render_expect("identical lines move only the cursor", SYNC_ON HIDE "\x1b[2A" "\r" SHOW SYNC_OFF); const char *aX[] = {"a", "X"}; tui_render_draw(r, aX, 2, 1, 0, 24); render_expect("shrink", SYNC_ON HIDE "\x1b[2B" "\r\x1b[J" "\x1b[1A" "\r" SHOW SYNC_OFF); tui_render_draw(r, aXc, 3, 2, 1, 24); render_expect("regrow", SYNC_ON HIDE "\r\n" "c" "\r\x1b[2G" SHOW SYNC_OFF); const char *done[] = {"done"}; tui_render_commit(r, done, 1); render_expect("commit", SYNC_ON HIDE "\x1b[2A" "\r\x1b[J" "done\r\n" "a\r\nX\r\nc" "\r\x1b[2G" SHOW SYNC_OFF); tui_render_invalidate(r); tui_render_draw(r, aXc, 3, 2, 1, 24); render_expect("invalidate redraws everything", SYNC_ON HIDE "\x1b[2A" "\r\x1b[J" "a\r\nX\r\nc" "\r\x1b[2G" SHOW SYNC_OFF); tui_render_draw(r, aXc, 3, 2, 1, 24); render_expect("no redraw after invalidate is consumed", SYNC_ON HIDE "\r" "\x1b[2G" SHOW SYNC_OFF); const char *many[] = {"l0", "l1", "l2", "l3", "l4", "l5", "l6", "l7", "l8", "l9"}; tui_render_draw(r, many, 10, 9, 1, 4); render_expect("more lines than rows keeps the last rows-1", SYNC_ON HIDE "\x1b[2A" "\r\x1b[J" "l7\r\nl8\r\nl9" "\r\x1b[2G" SHOW SYNC_OFF); tui_render_draw(r, many, 10, -1, 0, 4); render_expect("hidden cursor stays hidden", SYNC_ON HIDE SYNC_OFF); tui_render_draw(r, aXc, 3, 2, 1, 24); render_expect("redraw after clip", SYNC_ON HIDE "\x1b[2A" "\r\x1b[J" "a\r\nX\r\nc" "\r\x1b[2G" SHOW SYNC_OFF); tui_render_invalidate(r); tui_render_commit(r, done, 1); render_expect("commit while invalid does not redraw old lines", SYNC_ON HIDE "\x1b[2A" "\r\x1b[J" "done\r\n" SHOW SYNC_OFF); tui_render_draw(r, aXc, 3, 2, 1, 24); render_expect("draw after invalid commit rewrites fully", SYNC_ON HIDE "a\r\nX\r\nc" "\r\x1b[2G" SHOW SYNC_OFF); tui_render_finish(r); render_expect("finish", "\r\n" SHOW); tui_render_free(r); /* An empty live area: commit just writes the lines. */ r = tui_render_new(rfd[1]); tui_render_commit(r, done, 1); render_expect("commit with empty live area", SYNC_ON HIDE "done\r\n" SHOW SYNC_OFF); tui_render_free(r); /* A closed fd reports an error. */ r = tui_render_new(-1); check_int("write error", tui_render_draw(r, ab, 2, 0, 0, 24), -1); tui_render_free(r); close(rfd[0]); close(rfd[1]); pass("render"); } /* ---- editor ------------------------------------------------------------ */ static int press(tui_editor *e, tui_key_kind kind) { tui_key k; memset(&k, 0, sizeof k); k.kind = kind; return tui_editor_key(e, &k); } static void type(tui_editor *e, const char *s) { while (*s) { uint32_t cp; size_t n = tui_utf8_decode(s, strlen(s), &cp); tui_key k; memset(&k, 0, sizeof k); k.kind = TUI_KEY_CHAR; memcpy(k.text, s, n); tui_editor_key(e, &k); s += n; } } static void paste_text(tui_editor *e, const char *s) { tui_key k; memset(&k, 0, sizeof k); k.kind = TUI_KEY_PASTE; k.paste = (char *)(uintptr_t)s; k.paste_len = strlen(s); tui_editor_key(e, &k); } /* The editor's text, "|" marking nothing: read it with take on a copy. */ static char *text_of(tui_editor *e) { tui_lines l = {0}; int row, col; tui_editor_render(e, 1000, &l, &row, &col); size_t cap = 1; for (size_t i = 0; i < l.n; i++) cap += strlen(l.lines[i]) + 1; char *out = calloc(cap, 1); for (size_t i = 0; i < l.n; i++) { if (i) strcat(out, "\n"); strcat(out, l.lines[i] + 2); } tui_lines_clear(&l); return out; } static void text_is(const char *what, tui_editor *e, const char *want) { char *t = text_of(e); check_str(what, t, want); free(t); } static void cursor_is(const char *what, tui_editor *e, size_t cols, int want_row, int want_col) { tui_lines l = {0}; int row, col; if (tui_editor_render(e, cols, &l, &row, &col) != 0) fail(what, "render failed"); if (row != want_row || col != want_col) fail(what, "cursor at %d,%d want %d,%d", row, col, want_row, want_col); tui_lines_clear(&l); } static void take_is(const char *what, tui_editor *e, const char *want) { char *t = tui_editor_take(e); if (!t) fail(what, "take returned NULL"); check_str(what, t, want); free(t); } static void test_editor(void) { tui_editor *e = tui_editor_new(); check_int("new is empty", tui_editor_empty(e), 1); type(e, "hello"); text_is("typing", e, "hello"); check_int("typing makes it non-empty", tui_editor_empty(e), 0); check_int("enter submits", press(e, TUI_KEY_ENTER), TUI_EDIT_SUBMIT); take_is("take returns text", e, "hello"); check_int("empty after take", tui_editor_empty(e), 1); check_int("empty enter ignored", press(e, TUI_KEY_ENTER), TUI_EDIT_NONE); type(e, " "); check_int("blank enter ignored", press(e, TUI_KEY_ENTER), TUI_EDIT_NONE); check_int("typing reports changed", press(e, TUI_KEY_NEWLINE), TUI_EDIT_CHANGED); check_int("blank multi-line enter ignored", press(e, TUI_KEY_ENTER), TUI_EDIT_NONE); check_int("unhandled key", press(e, TUI_KEY_CTRL_C), TUI_EDIT_NONE); check_int("tab not handled", press(e, TUI_KEY_TAB), TUI_EDIT_NONE); tui_editor_clear(e); pass("editor type and submit"); type(e, "ab"); press(e, TUI_KEY_NEWLINE); type(e, "cd"); text_is("newline", e, "ab\ncd"); press(e, TUI_KEY_HOME); check_int("backspace joins lines", press(e, TUI_KEY_BACKSPACE), TUI_EDIT_CHANGED); text_is("joined", e, "abcd"); cursor_is("cursor after join", e, 80, 0, 4); press(e, TUI_KEY_BACKSPACE); text_is("backspace", e, "acd"); press(e, TUI_KEY_DELETE); text_is("delete", e, "ad"); press(e, TUI_KEY_HOME); check_int("backspace at start", press(e, TUI_KEY_BACKSPACE), TUI_EDIT_NONE); press(e, TUI_KEY_END); check_int("delete at end", press(e, TUI_KEY_DELETE), TUI_EDIT_NONE); tui_editor_clear(e); type(e, "a\xC3\xA9\xE4\xB8\xAD"); press(e, TUI_KEY_BACKSPACE); text_is("backspace removes a whole UTF-8 char", e, "a\xC3\xA9"); press(e, TUI_KEY_LEFT); press(e, TUI_KEY_DELETE); text_is("delete removes a whole UTF-8 char", e, "a"); tui_editor_clear(e); pass("editor backspace and delete"); tui_editor_free(e); /* a fresh editor: no history yet */ e = tui_editor_new(); type(e, "one"); press(e, TUI_KEY_NEWLINE); type(e, "two2"); press(e, TUI_KEY_NEWLINE); type(e, "x"); check_int("up moves", press(e, TUI_KEY_UP), TUI_EDIT_CHANGED); check_int("up again", press(e, TUI_KEY_UP), TUI_EDIT_CHANGED); check_int("up on first line without history", press(e, TUI_KEY_UP), TUI_EDIT_NONE); cursor_is("up keeps the column (clamped)", e, 80, 0, 3); press(e, TUI_KEY_END); press(e, TUI_KEY_DOWN); cursor_is("down into a longer line", e, 80, 1, 5); press(e, TUI_KEY_DOWN); cursor_is("down into a shorter line", e, 80, 2, 3); press(e, TUI_KEY_UP); cursor_is("goal column is remembered", e, 80, 1, 5); for (int i = 0; i < 4; i++) press(e, TUI_KEY_LEFT); cursor_is("left crosses a line break", e, 80, 0, 5); press(e, TUI_KEY_RIGHT); cursor_is("right crosses a line break", e, 80, 1, 2); tui_editor_clear(e); pass("editor vertical movement"); type(e, "first"); take_is("take first", e, "first"); type(e, "second"); take_is("take second", e, "second"); type(e, "draft"); check_int("up into history", press(e, TUI_KEY_UP), TUI_EDIT_CHANGED); text_is("newest entry", e, "second"); press(e, TUI_KEY_UP); text_is("older entry", e, "first"); check_int("up past the oldest", press(e, TUI_KEY_UP), TUI_EDIT_NONE); text_is("still the oldest", e, "first"); press(e, TUI_KEY_DOWN); text_is("back to newer", e, "second"); press(e, TUI_KEY_DOWN); text_is("unsent text restored", e, "draft"); check_int("down past the newest", press(e, TUI_KEY_DOWN), TUI_EDIT_NONE); tui_editor_clear(e); type(e, "second"); take_is("no repeat of last entry", e, "second"); press(e, TUI_KEY_UP); press(e, TUI_KEY_UP); text_is("history has no duplicate", e, "first"); tui_editor_clear(e); for (int i = 0; i < 210; i++) { char b[16]; snprintf(b, sizeof b, "m%d", i); type(e, b); take_is("fill history", e, b); } for (int i = 0; i < 300; i++) press(e, TUI_KEY_UP); text_is("history capped at 200", e, "m10"); tui_editor_clear(e); pass("editor history"); type(e, "foo bar baz"); press(e, TUI_KEY_WORD_LEFT); cursor_is("word left", e, 80, 0, 11); press(e, TUI_KEY_WORD_LEFT); cursor_is("word left again", e, 80, 0, 6); press(e, TUI_KEY_WORD_RIGHT); cursor_is("word right", e, 80, 0, 9); press(e, TUI_KEY_DELETE_WORD); text_is("delete word", e, "foo baz"); press(e, TUI_KEY_HOME); press(e, TUI_KEY_WORD_RIGHT); cursor_is("word right from start", e, 80, 0, 5); press(e, TUI_KEY_KILL_LINE); text_is("kill line", e, "foo"); tui_editor_set(e, "abc\ndef"); press(e, TUI_KEY_UP); press(e, TUI_KEY_END); press(e, TUI_KEY_KILL_LINE); text_is("kill line at end joins next", e, "abcdef"); press(e, TUI_KEY_RIGHT); press(e, TUI_KEY_KILL_START); text_is("kill start", e, "ef"); tui_editor_set(e, "ab\ncd"); press(e, TUI_KEY_HOME); press(e, TUI_KEY_KILL_START); text_is("kill start at line start joins previous", e, "abcd"); pass("editor word moves and kills"); tui_editor_clear(e); paste_text(e, "short\npaste"); text_is("small paste literal", e, "short\npaste"); tui_editor_clear(e); check_int("empty after clear", tui_editor_empty(e), 1); char big[256] = ""; for (int i = 1; i <= 20; i++) { char l[16]; snprintf(l, sizeof l, "line%d%s", i, i < 20 ? "\n" : ""); strcat(big, l); } type(e, "x "); paste_text(e, big); text_is("big paste collapsed", e, "x [paste #1 +20 lines]"); type(e, " y"); check_int("marker plus text can submit", press(e, TUI_KEY_ENTER), TUI_EDIT_SUBMIT); char *taken = tui_editor_take(e); char want[320]; snprintf(want, sizeof want, "x %s y", big); check_str("take expands marker", taken, want); free(taken); paste_text(e, big); text_is("numbering restarts after submit", e, "[paste #1 +20 lines]"); paste_text(e, big); text_is("second paste", e, "[paste #1 +20 lines][paste #2 +20 lines]"); check_int("backspace on marker", press(e, TUI_KEY_BACKSPACE), TUI_EDIT_CHANGED); text_is("marker removed whole", e, "[paste #1 +20 lines]"); press(e, TUI_KEY_HOME); press(e, TUI_KEY_DELETE); text_is("delete removes a whole marker", e, ""); check_int("paste table cleared on removal still safe", tui_editor_empty(e), 1); /* cursor moves jump over a marker whole */ tui_editor_clear(e); type(e, "ab"); paste_text(e, big); type(e, "cd"); for (int i = 0; i < 3; i++) press(e, TUI_KEY_LEFT); cursor_is("left from after marker lands before it", e, 80, 0, 4); press(e, TUI_KEY_RIGHT); cursor_is("right from before marker lands after it", e, 80, 0, 2 + 2 + 20); press(e, TUI_KEY_LEFT); type(e, "X"); press(e, TUI_KEY_WORD_RIGHT); cursor_is("word right does not stop inside the marker", e, 80, 0, 2 + 3 + 20); press(e, TUI_KEY_WORD_LEFT); cursor_is("word left does not stop inside the marker", e, 80, 0, 2 + 3); press(e, TUI_KEY_RIGHT); press(e, TUI_KEY_RIGHT); type(e, "Y"); taken = tui_editor_take(e); snprintf(want, sizeof want, "abX%scYd", big); check_str("typing beside a marker keeps it expandable", taken, want); free(taken); tui_editor_clear(e); paste_text(e, big); type(e, "\nabcdefghijklmnopqrstuvwxyz"); press(e, TUI_KEY_UP); press(e, TUI_KEY_END); press(e, TUI_KEY_DOWN); press(e, TUI_KEY_UP); press(e, TUI_KEY_LEFT); type(e, "Q"); taken = tui_editor_take(e); snprintf(want, sizeof want, "Q%s\nabcdefghijklmnopqrstuvwxyz", big); check_str("up/down never land inside a marker", taken, want); free(taken); tui_editor_clear(e); char *chars = malloc(1201); memset(chars, 'z', 1200); chars[1200] = 0; paste_text(e, chars); text_is("long single line collapsed ", e, "[paste #1 1200 chars]"); taken = tui_editor_take(e); check_str("long paste expands", taken, chars); free(taken); free(chars); paste_text(e, "[paste #1 +20 lines]"); take_is("typed lookalike marker is literal", e, "[paste #1 +20 lines]"); tui_editor_clear(e); pass("editor paste markers"); tui_editor_clear(e); tui_editor_set(e, "hello"); cursor_is("set puts cursor at end", e, 80, 0, 7); tui_lines l = {0}; int row, col; check_int("render ok", tui_editor_render(e, 80, &l, &row, &col), 0); lines_eq("render one line", &l, 1, "> hello"); tui_lines_clear(&l); tui_editor_set(e, "aaa bbb ccc"); check_int("render wrapped", tui_editor_render(e, 10, &l, &row, &col), 0); lines_eq("wrapped lines", &l, 2, "> aaa bbb", " ccc"); check_int("wrapped cursor row", row, 1); check_int("wrapped cursor col", col, 5); tui_lines_clear(&l); tui_editor_set(e, "ab\ncd"); tui_editor_render(e, 80, &l, &row, &col); lines_eq("render multi-line prefixes", &l, 2, "> ab", " cd"); tui_lines_clear(&l); tui_editor_set(e, "\xE4\xB8\xAD\xE6\x96\x87x"); cursor_is("cursor after wide chars", e, 80, 0, 7); press(e, TUI_KEY_LEFT); cursor_is("cursor between wide and narrow", e, 80, 0, 6); press(e, TUI_KEY_LEFT); cursor_is("cursor between wide chars", e, 80, 0, 4); tui_editor_set(e, "\xE4\xB8\xAD\xE6\x96\x87\xE5\xAD\x97\xE5\xAD\x97"); tui_editor_render(e, 9, &l, &row, &col); /* wrap width 6: three wide chars a row */ lines_eq("wide chars wrap", &l, 2, "> \xE4\xB8\xAD\xE6\x96\x87\xE5\xAD\x97", " \xE5\xAD\x97"); check_int("wide wrap cursor row", row, 1); check_int("wide wrap cursor col", col, 4); tui_lines_clear(&l); tui_editor_set(e, "abcdefgh"); tui_editor_render(e, 8, &l, &row, &col); /* wrap width 5 */ lines_eq("hard wrap in editor", &l, 2, "> abcde", " fgh"); check_int("cursor at end after hard wrap row", row, 1); check_int("cursor at end after hard wrap col", col, 5); tui_lines_clear(&l); tui_editor_set(e, "abcde"); cursor_is("cursor at end of a full line stays on screen", e, 8, 0, 7); tui_editor_set(e, "ab\ncd"); press(e, TUI_KEY_LEFT); check_str("text accessor", tui_editor_text(e), "ab\ncd"); check_int("cursor accessor", (long)tui_editor_cursor(e), 4); tui_editor_clear(e); check_str("text accessor empty", tui_editor_text(e), ""); check_int("cursor accessor empty", (long)tui_editor_cursor(e), 0); paste_text(e, big); check_str("text accessor shows the marker", tui_editor_text(e), "[paste #1 +20 lines]"); check_int("cursor after marker", (long)tui_editor_cursor(e), 20); tui_editor_set(e, "ab\n"); cursor_is("cursor on trailing empty line", e, 80, 1, 2); tui_editor_set(e, ""); cursor_is("cursor in empty editor", e, 80, 0, 2); tui_editor_free(e); pass("editor render"); } static void near(const char *what, double got, double want) { if (got < want - 0.001 || got > want + 0.001) fail(what, "got %f want %f", got, want); } static void order_is(const char *what, const char *query, const char *const *items, size_t n, size_t want_n, ...) { size_t order[16]; size_t got = tui_fuzzy_filter(query, items, n, order); va_list ap; va_start(ap, want_n); if (got != want_n) fail(what, "got %zu matches want %zu", got, want_n); for (size_t i = 0; i < want_n; i++) { size_t w = va_arg(ap, size_t); if (order[i] != w) fail(what, "rank %zu is item %zu want %zu", i, order[i], w); } va_end(ap); } static void test_fuzzy(void) { double s; check_int("match yes", tui_fuzzy_match("son", "claude-sonnet-5-5", &s), 1); near("score at a word boundary", s, -22.6); /* -10+.7, -5+.8, -10+.9 */ check_int("match no", tui_fuzzy_match("son", "gpt-5-nano", &s), 0); check_int("empty query matches", tui_fuzzy_match("", "abc", &s), 1); near("empty query scores 0", s, 0); check_int("query longer than text", tui_fuzzy_match("abcd", "abc", &s), 0); check_int("case folded", tui_fuzzy_match("SON", "Claude-Sonnet", &s), 1); near("exact match bonus", (tui_fuzzy_match("sonnet", "sonnet", &s), s), -213.5); check_int("digits and letters swapped", tui_fuzzy_match("nano5", "gpt-5-nano", &s), 1); near("swapped match costs 5", s, -39.6); /* "5nano" scores -44.6 there */ near("first character at index 0 also counts as consecutive (as in Pi)", (tui_fuzzy_match("son", "sonnet", &s), s), -39.7); pass("fuzzy match"); const char *models[] = {"gpt-5-nano", "claude-sonnet-5-5", "sonnet", "deepseek/deepseek-v4-flash", "xsonx"}; order_is("son ranking", "son", models, 5, 3, (size_t)2, (size_t)1, (size_t)4); const char *anth[] = {"anthropic/claude-haiku-4-5", "claude-haiku", "anthropic/claude-sonnet", "anth-haiku"}; order_is("multi-token query", "anth haiku", anth, 4, 2, (size_t)3, (size_t)0); order_is("slash separates tokens", "anth/haiku", anth, 4, 2, (size_t)3, (size_t)0); order_is("no match", "zzz", models, 5, 0); order_is("empty query keeps order", "", models, 5, 5, (size_t)0, (size_t)1, (size_t)2, (size_t)3, (size_t)4); order_is("blank query keeps order", " \t ", models, 5, 5, (size_t)0, (size_t)1, (size_t)2, (size_t)3, (size_t)4); const char *ties[] = {"axb", "azb", "a-b"}; order_is("ties keep input order", "ab", ties, 3, 3, (size_t)2, (size_t)0, (size_t)1); pass("fuzzy filter"); } /* ---- view: tool blocks, diffs, footer ---------------------------------- */ /* SGR sequences removed, for comparing text. */ static void plain(const char *s, char *out, size_t cap) { size_t o = 0; for (; *s && o + 1 < cap; s++) { if (*s == 0x1b && s[1] == '[') { s += 2; while (*s && !(*s >= '@' && *s <= '~')) s++; if (!*s) break; } else { out[o++] = *s; } } out[o] = '\0'; } static void plain_line_is(const char *what, const tui_lines *l, size_t i, const char *want) { char p[512]; if (i >= l->n) fail(what, "no line %zu (%zu lines)", i, l->n); plain(l->lines[i], p, sizeof p); check_str(what, p, want); } static void test_view(void) { char buf[512]; char *d; tui_lines l = {NULL, 0, 0}; tui_fmt_duration(4200, buf, sizeof buf); check_str("duration seconds", buf, "4.2s"); tui_fmt_duration(0, buf, sizeof buf); check_str("duration zero", buf, "0.0s"); tui_fmt_duration(65000, buf, sizeof buf); check_str("duration minutes", buf, "1m 5s"); tui_fmt_duration(3723000, buf, sizeof buf); check_str("duration hours", buf, "1h 2m 3s"); tui_fmt_tokens(999, buf, sizeof buf); check_str("tokens small", buf, "999"); tui_fmt_tokens(1500, buf, sizeof buf); check_str("tokens k", buf, "1.5k"); tui_fmt_tokens(15400, buf, sizeof buf); check_str("tokens rounded k", buf, "15k"); tui_fmt_tokens(2500000, buf, sizeof buf); check_str("tokens M", buf, "2.5M"); tui_fmt_size(12, buf, sizeof buf); check_str("size bytes", buf, "12B"); tui_fmt_size(1536, buf, sizeof buf); check_str("size KB", buf, "1.5KB"); pass("view: durations, tokens, sizes"); d = tui_diff_text("a\nb\nc\nd\ne\n", "a\nb\nX\nY\nd\ne\n"); check_str("diff", d, " a\n b\n-c\n+X\n+Y\n d\n e"); free(d); d = tui_diff_text("one\ntwo", "one\ntwo"); check_str("equal texts: no diff", d, ""); free(d); d = tui_diff_text("x", "y"); check_str("diff of one line", d, "-x\n+y"); free(d); d = tui_diff_text("", "new\n"); check_str("diff from nothing", d, "+new"); free(d); pass("view: edit diff"); { tui_tool t = {"edit", "t.txt", "-x\n+y", "Successfully replaced 1 block(s) in t.txt.", TUI_TOOL_OK, 4200}; tui_tool_lines(&t, 0, 40, &l); plain_line_is("edit header", &l, 0, "\xe2\x9c\x93 edit t.txt"); plain_line_is("diff removed", &l, 1, " -x"); plain_line_is("diff added", &l, 2, " +y"); plain_line_is("took", &l, 3, " Took 4.2s"); check_int("edit success hides the result text", (long)l.n, 4); if (!strstr(l.lines[1], "\x1b[31m") || !strstr(l.lines[2], "\x1b[32m")) fail("diff colours", "removed %s added %s", l.lines[1], l.lines[2]); tui_lines_clear(&l); } { tui_tool t = {"bash", "$ seq 1 12", NULL, "1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n11\n12\n", TUI_TOOL_OK, 300}; tui_tool_lines(&t, 0, 60, &l); plain_line_is("bash header", &l, 0, "\xe2\x9c\x93 $ seq 1 12"); plain_line_is("bash hint", &l, 1, " ... (7 earlier lines, ctrl+o to expand)"); plain_line_is("bash keeps the last lines", &l, 2, " 8"); plain_line_is("bash last", &l, 6, " 12"); plain_line_is("bash took", &l, 7, " Took 0.3s"); tui_lines_clear(&l); tui_tool_lines(&t, 1, 60, &l); check_int("expanded shows everything", (long)l.n, 1 + 12 + 1); plain_line_is("expanded first", &l, 1, " 1"); tui_lines_clear(&l); } { tui_tool t = {"read", "f.c", NULL, "1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n11\n12\n13\n", TUI_TOOL_OK, -1}; tui_tool_lines(&t, 0, 60, &l); plain_line_is("read keeps the first lines", &l, 1, " 1"); plain_line_is("read hint", &l, 11, " ... (3 more lines, ctrl+o to expand)"); check_int("no duration, no line", (long)l.n, 12); tui_lines_clear(&l); } { tui_tool t = {"bash", "$ false", NULL, "boom", TUI_TOOL_ERROR, 10}; tui_tool_lines(&t, 0, 60, &l); plain_line_is("error header", &l, 0, "\xe2\x9c\x97 $ false"); if (!strstr(l.lines[0], "\x1b[31m") || !strstr(l.lines[1], "\x1b[31m")) fail("error colour", "%s | %s", l.lines[0], l.lines[1]); tui_tool_lines(&(tui_tool){"bash", "$ sleep 5", NULL, NULL, TUI_TOOL_PENDING, 1200}, 0, 60, &l); plain_line_is("pending header", &l, 3, "\xe2\x97\x8f $ sleep 5"); plain_line_is("pending elapsed", &l, 4, " Elapsed 1.2s"); tui_lines_clear(&l); } pass("view: tool blocks"); { tui_footer f = {1500, 200, 300, 40, 140000, 200000, "m", "high", 0, 0}; tui_footer_stats(&f, 60, buf, sizeof buf); { char p[512]; plain(buf, p, sizeof p); check_str("footer", p, "\xe2\x86\x91" "1.5k \xe2\x86\x93" "200 R300 W40 70.0%/200k m \xe2\x80\xa2 high"); if (strstr(buf, "\x1b[33m") || strstr(buf, "\x1b[31m")) fail("footer at 70%", "coloured: %s", buf); } f.cost = 0.0123; tui_footer_stats(&f, 60, buf, sizeof buf); { char p[512]; plain(buf, p, sizeof p); if (!strstr(p, "W40 $0.012 70.0%/200k")) fail("footer cost", "not after the cache counts: %s", p); } f.cost = 0; f.context = 140001; tui_footer_stats(&f, 60, buf, sizeof buf); if (!strstr(buf, "\x1b[33m70.0%/200k")) fail("footer past 70%", "not yellow: %s", buf); f.context = 190000; f.level = "off"; f.in = f.out = f.cache_read = f.cache_write = 0; tui_footer_stats(&f, 60, buf, sizeof buf); if (!strstr(buf, "\x1b[31m95.0%/200k")) fail("footer past 90%", "not red: %s", buf); { char p[512]; plain(buf, p, sizeof p); check_str("footer: only the percentage, thinking off", p, "95.0%/200k m \xe2\x80\xa2 thinking off"); } f.level = NULL; tui_footer_stats(&f, 12, buf, sizeof buf); { char p[512]; plain(buf, p, sizeof p); if (tui_width(p, strlen(p)) > 12) fail("footer fits", "%zu columns: %s", tui_width(p, strlen(p)), p); } } tui_footer_where("/home/u/proj", "/home/u", "main", "work", buf, sizeof buf); check_str("where", buf, "~/proj (main) \xe2\x80\xa2 work"); tui_footer_where("/srv/x", "/home/u", NULL, "", buf, sizeof buf); check_str("where without branch or name", buf, "/srv/x"); tui_footer_where("/home/user2", "/home/user", NULL, "", buf, sizeof buf); check_str("home is a whole path component", buf, "/home/user2"); pass("view: footer"); { char dir[] = "/tmp/pic-view.XXXXXX", p[600]; FILE *f; if (!mkdtemp(dir)) fail("git branch", "mkdtemp"); snprintf(p, sizeof p, "%s/.git", dir); mkdir(p, 0700); snprintf(p, sizeof p, "%s/sub", dir); mkdir(p, 0700); snprintf(p, sizeof p, "%s/.git/HEAD", dir); f = fopen(p, "w"); fputs("ref: refs/heads/feature/x\n", f); fclose(f); snprintf(p, sizeof p, "%s/sub", dir); check_int("branch found from a subdirectory", tui_git_branch(p, buf, sizeof buf), 1); check_str("branch name", buf, "feature/x"); snprintf(p, sizeof p, "%s/.git/HEAD", dir); f = fopen(p, "w"); fputs("0123456789abcdef0123456789abcdef01234567\n", f); fclose(f); check_int("detached", tui_git_branch(dir, buf, sizeof buf), 1); check_str("detached name", buf, "detached"); snprintf(p, sizeof p, "%s/.git/HEAD", dir); unlink(p); snprintf(p, sizeof p, "%s/.git", dir); rmdir(p); snprintf(p, sizeof p, "%s/sub", dir); rmdir(p); rmdir(dir); } pass("view: git branch"); tui_retry_status(2, 5, 8000, 1500, buf, sizeof buf); check_str("retry countdown", buf, "Retrying (2/5) in 7s\xe2\x80\xa6 (esc to cancel)"); tui_retry_status(2, 5, 8000, 9000, buf, sizeof buf); check_str("retry countdown ends", buf, "Retrying (2/5) in 0s\xe2\x80\xa6 (esc to cancel)"); tui_retry_status(3, 3, 0, 0, buf, sizeof buf); check_str("retry without a delay", buf, "Retrying (3/3)\xe2\x80\xa6 (esc to cancel)"); pass("view: retry status"); { tui_editor *e = tui_editor_new(); tui_editor_history_add(e, "first"); tui_editor_history_add(e, "second"); tui_editor_history_add(e, "second"); check_int("up into seeded history", press(e, TUI_KEY_UP), TUI_EDIT_CHANGED); text_is("newest seeded entry", e, "second"); press(e, TUI_KEY_UP); text_is("older seeded entry", e, "first"); press(e, TUI_KEY_DOWN); press(e, TUI_KEY_DOWN); text_is("back to the unsent text", e, ""); tui_editor_free(e); } pass("editor: seeded history"); } /* ---- round 2: keys, kill ring, undo, markdown, bash blocks ---------------- */ static void keys_are(const char *what, const char *bytes, size_t n, const tui_key_kind *want, size_t nwant) { tui_keys *k = tui_keys_new(); tui_key key; size_t got = 0; tui_keys_feed(k, bytes, n); while (tui_keys_next(k, &key)) { if (got < nwant && key.kind != want[got]) fail(what, "key %zu is %d want %d", got, (int)key.kind, (int)want[got]); got++; } if (got != nwant) fail(what, "%zu keys want %zu", got, nwant); tui_keys_free(k); } static void test_keys2(void) { tui_key_kind one; struct { const char *bytes; tui_key_kind kind; const char *what; } t[] = { {"\x19", TUI_KEY_YANK, "ctrl+y"}, {"\x1b" "y", TUI_KEY_YANK_POP, "alt+y"}, {"\x1b" "d", TUI_KEY_DELETE_WORD_FWD, "alt+d"}, {"\x1b[3;3~", TUI_KEY_DELETE_WORD_FWD, "alt+delete"}, {"\x1f", TUI_KEY_UNDO, "ctrl+- (0x1f)"}, {"\x1b[45;5u", TUI_KEY_UNDO, "ctrl+- (kitty)"}, {"\x1d", TUI_KEY_JUMP, "ctrl+]"}, {"\x1b\x1d", TUI_KEY_JUMP_BACK, "ctrl+alt+]"}, {"\x07", TUI_KEY_CTRL_G, "ctrl+g"}, {"\x18", TUI_KEY_CTRL_X, "ctrl+x"}, {"\x1a", TUI_KEY_CTRL_Z, "ctrl+z"}, {"\x1b[1;3A", TUI_KEY_ALT_UP, "alt+up"}, {"\x1b[A", TUI_KEY_UP, "plain up"}, {"\x1b[112;6u", TUI_KEY_CTRL_P_BACK, "shift+ctrl+p (kitty)"}, {"\x1b[27;6;112~", TUI_KEY_CTRL_P_BACK, "shift+ctrl+p (modifyOtherKeys)"}, {"\x02", TUI_KEY_LEFT, "ctrl+b"}, {"\x06", TUI_KEY_RIGHT, "ctrl+f"}, }; for (size_t i = 0; i < sizeof t / sizeof t[0]; i++) { one = t[i].kind; keys_are(t[i].what, t[i].bytes, strlen(t[i].bytes), &one, 1); } pass("term: Pi's editor and app keys decode"); } static void test_editor2(void) { tui_editor *e = tui_editor_new(); type(e, "alpha beta gamma"); press(e, TUI_KEY_DELETE_WORD); text_is("ctrl+w kills the word", e, "alpha beta "); press(e, TUI_KEY_DELETE_WORD); text_is("ctrl+w again", e, "alpha "); press(e, TUI_KEY_YANK); text_is("yank restores both words (consecutive kills merged, prepended)", e, "alpha beta gamma"); tui_editor_free(e); e = tui_editor_new(); type(e, "one two"); press(e, TUI_KEY_KILL_START); text_is("ctrl+u kills to the line start", e, ""); type(e, "x"); press(e, TUI_KEY_YANK); text_is("yank inserts the killed text", e, "xone two"); press(e, TUI_KEY_YANK_POP); text_is("yank-pop with one entry does nothing", e, "xone two"); tui_editor_free(e); e = tui_editor_new(); type(e, "first"); press(e, TUI_KEY_KILL_START); type(e, "second"); press(e, TUI_KEY_KILL_START); press(e, TUI_KEY_YANK); text_is("yank gives the newest kill", e, "second"); press(e, TUI_KEY_YANK_POP); text_is("yank-pop swaps in the older kill", e, "first"); press(e, TUI_KEY_YANK_POP); text_is("yank-pop cycles back", e, "second"); type(e, "!"); press(e, TUI_KEY_YANK_POP); text_is("yank-pop only works right after a yank", e, "second!"); tui_editor_free(e); e = tui_editor_new(); type(e, "ab cd ef"); press(e, TUI_KEY_HOME); press(e, TUI_KEY_DELETE_WORD_FWD); text_is("alt+d kills the next word", e, " cd ef"); press(e, TUI_KEY_DELETE_WORD_FWD); press(e, TUI_KEY_YANK); text_is("alt+d kills append: yank returns both", e, "ab cd ef"); tui_editor_free(e); e = tui_editor_new(); type(e, "hello world"); press(e, TUI_KEY_UNDO); text_is("undo removes the last typed word", e, "hello"); press(e, TUI_KEY_UNDO); text_is("undo removes the first word", e, ""); press(e, TUI_KEY_UNDO); text_is("undo on an empty stack does nothing", e, ""); type(e, "keep"); press(e, TUI_KEY_KILL_START); text_is("killed", e, ""); press(e, TUI_KEY_UNDO); text_is("undo restores a kill", e, "keep"); cursor_is("undo restores the cursor", e, 40, 0, 6); tui_editor_free(e); e = tui_editor_new(); type(e, "a.b.c d"); press(e, TUI_KEY_HOME); press(e, TUI_KEY_JUMP); type(e, "."); cursor_is("ctrl+] then . jumps to the next .", e, 40, 0, 3); press(e, TUI_KEY_JUMP); type(e, "d"); cursor_is("jumps to d", e, 40, 0, 8); press(e, TUI_KEY_JUMP_BACK); type(e, "a"); cursor_is("ctrl+alt+] jumps back to a", e, 40, 0, 2); press(e, TUI_KEY_JUMP); press(e, TUI_KEY_JUMP); type(e, "z"); text_is("pressing the jump key again cancels it: z is typed", e, "za.b.c d"); tui_editor_free(e); pass("editor: kill ring, yank, yank-pop, alt+d, undo, jump to character"); } /* The lines joined with '\n', escape sequences removed. */ static char *plain_lines(const tui_lines *l) { size_t cap = 1, n = 0; char *out; for (size_t i = 0; i < l->n; i++) cap += strlen(l->lines[i]) + 1; out = malloc(cap); for (size_t i = 0; i < l->n; i++) { const char *p = l->lines[i]; while (*p) { if (*p == 0x1b) { p++; if (*p == '[') { p++; while (*p && !(*p >= 0x40 && *p <= 0x7e)) p++; p += *p != 0; } else if (*p == ']') { while (*p && *p != 0x07 && !(*p == 0x1b && p[1] == '\\')) p++; p += *p == 0x1b ? 2 : *p ? 1 : 0; } } else { out[n++] = *p++; } } out[n++] = '\n'; } if (n) n--; out[n] = 0; return out; } static char *md_plain(const char *text, size_t cols, int links) { tui_md_opts o = {NULL, links, 0}; tui_lines l = {0}; char *p; if (tui_markdown(text, strlen(text), cols, &o, &l) != 0) fail("markdown", "render failed"); p = plain_lines(&l); tui_lines_clear(&l); return p; } static void md_is(const char *what, const char *text, size_t cols, const char *want) { char *got = md_plain(text, cols, 0); check_str(what, got, want); free(got); } static void md_has(const char *what, const char *text, size_t cols, int links, const char *needle) { tui_md_opts o = {NULL, links, 0}; tui_lines l = {0}; size_t total = 0; char *all; tui_markdown(text, strlen(text), cols, &o, &l); for (size_t i = 0; i < l.n; i++) total += strlen(l.lines[i]) + 1; all = calloc(total + 1, 1); for (size_t i = 0; i < l.n; i++) { strcat(all, l.lines[i]); strcat(all, "\n"); } if (!strstr(all, needle)) { char shown[2048]; show(all, strlen(all), shown, sizeof shown); fail(what, "no \"%s\" in \"%s\"", needle, shown); } free(all); tui_lines_clear(&l); } static void test_markdown(void) { md_is("paragraph and heading", "# Title\n\nSome text.", 40, "Title\n\nSome text."); md_is("h3 keeps its hashes", "### Deep", 40, "### Deep"); md_is("soft line breaks stay", "a\nb", 40, "a\nb"); md_is("bullets are dashes (Pi)", "- one\n- two\n - nested\n- three", 40, "- one\n- two\n - nested\n- three"); md_is("numbered lists count from their start", "3. c\n4. d", 40, "3. c\n4. d"); md_is("a long item wraps under its text", "1. aaa bbb ccc ddd", 12, "1. aaa bbb\n ccc ddd"); md_is("a loose list has blank lines", "- a\n\n- b", 40, "- a\n\n- b"); md_is("task items", "- [x] done\n- [ ] todo", 40, "- [x] done\n- [ ] todo"); md_is("quote", "> quoted *text*\n> more", 40, "\xe2\x94\x82 quoted text\n\xe2\x94\x82 more"); md_is("nested block in a quote", "> - a\n> - b", 40, "\xe2\x94\x82 - a\n\xe2\x94\x82 - b"); { tui_lines l = {0}; tui_md_opts o = {NULL, 0, 0}; size_t worst = 0; tui_markdown("2) a very long item", 19, 3, &o, &l); for (size_t i = 0; i < l.n; i++) { size_t w = tui_width(l.lines[i], strlen(l.lines[i])); worst = w > worst ? w : worst; } if (worst > 3) fail("markdown: lines always fit", "a %zu-column line in 3", worst); tui_lines_clear(&l); } md_is("rule", "a\n\n---\n\nb", 10, "a\n\n\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\n\nb"); md_is("code fence shows its language", "```c\nint x;\n```", 40, "```c\n int x;\n```"); md_is("code fence without a language, followed by text", "```\nx\n```\ntext", 40, "```\n x\n```\n\ntext"); md_is("an unterminated fence (streaming)", "```py\nprint(1)", 40, "```py\n print(1)\n```"); md_is("a partial closing fence is not code", "```py\nx\n``", 40, "```py\n x\n```"); md_has("highlighted C block: keyword coloured", "```c\nreturn 1;\n```", 40, 0, "\x1b[38;2;138;149;224mreturn"); md_is("highlighted C block: text unchanged", "```c\nreturn 1; // done\n```", 40, "```c\n return 1; // done\n```"); md_has("unknown language stays plain", "```zzz\nreturn 1;\n```", 40, 0, "\x1b[36mreturn 1;"); md_has("no language stays plain", "```\nreturn 1;\n```", 40, 0, "\x1b[36mreturn 1;"); { tui_md_opts o = {NULL, 0, 0}; tui_lines l = {0}; const char *src = "```c\n// aaaa bbbb cccc dddd eeee ffff gggg hhhh\n```"; size_t colored = 0; tui_markdown(src, strlen(src), 20, &o, &l); for (size_t i = 1; i + 1 < l.n; i++) { if (tui_width(l.lines[i], strlen(l.lines[i])) > 20) fail("highlighted wrap", "line %zu is wider than 20 columns", i); if (strstr(l.lines[i], "\x1b[38;2;168;170;184m")) colored++; } if (l.n < 5 || colored != l.n - 2) fail("highlighted wrap", "%zu lines, %zu carrying the comment colour", l.n, colored); tui_lines_clear(&l); } md_is("table", "| a | bb |\n|---|---|\n| 1 | 2 |\n| 33 | 4 |", 40, "\xe2\x94\x8c\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\xac\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x90\n" "\xe2\x94\x82 a \xe2\x94\x82 bb \xe2\x94\x82\n" "\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\xbc\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\xa4\n" "\xe2\x94\x82 1 \xe2\x94\x82 2 \xe2\x94\x82\n" "\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\xbc\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\xa4\n" "\xe2\x94\x82 33 \xe2\x94\x82 4 \xe2\x94\x82\n" "\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\xb4\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x98"); pass("markdown: blocks (headings, lists, quotes, rules, fences, tables)"); md_is("inline markers are consumed", "a **b** *c* ~~d~~ `e` f", 40, "a b c d e f"); md_is("intraword underscores stay", "snake_case_name and 2 * 3 * 4", 40, "snake_case_name and 2 * 3 * 4"); md_is("backslash escapes", "\\*not italic\\*", 40, "*not italic*"); md_is("unmatched markers are literal", "a *b", 40, "a *b"); md_is("a link shows its url when the text differs", "[docs](https://x.dev/a)", 60, "docs (https://x.dev/a)"); md_is("a bare url is not repeated", "see https://x.dev/a.", 60, "see https://x.dev/a."); md_is("an autolink", "", 60, "https://x.dev"); md_has("bold is SGR 1", "**b**", 40, 0, "\x1b[1m" "b"); md_has("italic is SGR 3", "*i*", 40, 0, "\x1b[3m" "i"); md_has("inline code is cyan", "`c`", 40, 0, "\x1b[36m" "c"); md_has("a link is blue and underlined", "[t](http://u)", 40, 0, "\x1b[34m" "\x1b[4m"); md_has("a link becomes an OSC 8 hyperlink", "[docs](https://x.dev/a)", 60, 1, "\x1b]8;;https://x.dev/a\x1b\\"); { char *with = md_plain("[docs](https://x.dev/a)", 60, 1); check_str("OSC 8: the url is not printed", with, "docs"); free(with); } { tui_md_opts o = {NULL, 1, 0}; tui_lines l = {0}; tui_markdown("[aaa bbb ccc](https://x.dev/a)", 30, 8, &o, &l); check_int("a hyperlink wrapped over two lines", (long)l.n, 2); if (!strstr(l.lines[0], "\x1b]8;;\x1b\\") || strncmp(l.lines[1], "\x1b[", 2) != 0 || !strstr(l.lines[1], "\x1b]8;;https://x.dev/a")) fail("hyperlink across a wrap", "line 0 closes it and line 1 reopens it"); tui_lines_clear(&l); } pass("markdown: italics, links (OSC 8), code spans, escapes"); check_int("block len: open paragraph", (long)tui_markdown_block_len("abc\ndef\n"), 0); check_int("block len: finished paragraph", (long)tui_markdown_block_len("abc\n\ndef"), 5); check_int("block len: blank lines inside a fence do not end it", (long)tui_markdown_block_len("```\na\n\nb\n```\n\nx"), 14); check_int("block len: leading blank lines", (long)tui_markdown_block_len("\n\nabc\n\nx"), 7); { int fence = 0; tui_lines l = {0}; tui_markdown_line("```sh", 6, &fence, 40, NULL, &l); check_int("line: fence opens", fence, 1); tui_markdown_line("ls -l", 5, &fence, 40, NULL, &l); tui_markdown_line("```", 3, &fence, 40, NULL, &l); check_int("line: fence closes", fence, 0); { char *p = plain_lines(&l); check_str("line: rendered as code", p, "```sh\n ls -l\n```"); free(p); } tui_lines_clear(&l); } pass("markdown: streaming block boundaries"); { tui_lines l = {0}; char *p; tui_user_message_lines("hello **you**", 20, 0, &l); check_int("user message: blank row, text, blank row", (long)l.n, 3); p = plain_lines(&l); check_str("user message text is padded to the width", p, " \n hello you \n "); free(p); if (strncmp(l.lines[1], "\x1b[48;", 5) != 0) fail("user message", "rows start with a background colour"); tui_lines_clear(&l); tui_user_message_lines("1) a\n2) b", 20, 0, &l); p = plain_lines(&l); if (!strstr(p, "1) a") || !strstr(p, "2) b")) fail("user message keeps its list markers", "%s", p); free(p); tui_lines_clear(&l); } pass("markdown: user messages on a background"); } static void test_view2(void) { tui_lines l = {0}; char *p; tui_bash b = {"echo hi", "hi\n", TUI_BASH_DONE, 0, 0}; tui_bash_lines(&b, 0, 20, &l); p = plain_lines(&l); check_str("bash block: rule, command, output, rule", p, "\n\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\n$ echo hi\nhi\n\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80"); free(p); tui_lines_clear(&l); { char out[4096] = ""; for (int i = 1; i <= 30; i++) snprintf(out + strlen(out), sizeof out - strlen(out), "line%d\n", i); b = (tui_bash){"seq", out, TUI_BASH_DONE, 3, 0}; tui_bash_lines(&b, 0, 30, &l); p = plain_lines(&l); if (strstr(p, "line10\n") || !strstr(p, "line11\n") || !strstr(p, "line30") || !strstr(p, "... 10 more lines (ctrl+o to expand)") || !strstr(p, "(exit 3)")) fail("bash block: the last 20 lines, a hint, the exit code", "%s", p); free(p); tui_lines_clear(&l); tui_bash_lines(&b, 1, 30, &l); p = plain_lines(&l); if (!strstr(p, "line1\n") || strstr(p, "more lines") || !strstr(p, "(ctrl+o to collapse)")) fail("bash block expanded", "%s", p); free(p); tui_lines_clear(&l); } b = (tui_bash){"sleep 9", "\x1b[31mred\x1b[0m\r\nx\r", TUI_BASH_RUNNING, 0, 1}; tui_bash_lines(&b, 0, 40, &l); p = plain_lines(&l); if (!strstr(p, "red\nx") || strstr(p, "[31m") || !strstr(p, "Running... (esc to cancel)")) fail("bash block running: output cleaned, status shown", "%s", p); if (!strstr(l.lines[1], "\x1b[2m")) fail("bash block: !! is dim", "%s", l.lines[1]); free(p); tui_lines_clear(&l); b = (tui_bash){"x", "", TUI_BASH_CANCELLED, 0, 0}; tui_bash_lines(&b, 0, 40, &l); p = plain_lines(&l); if (!strstr(p, "(cancelled)")) fail("bash block cancelled", "%s", p); free(p); tui_lines_clear(&l); pass("view: user bash blocks"); { char *c = tui_bash_context("ls", "a\nb\n", 0, 0); check_str("bash context", c, "Ran `ls`\n```\na\nb\n```"); free(c); c = tui_bash_context("false", "", 0, 1); check_str("bash context: no output, exit code", c, "Ran `false`\n(no output)\n\nCommand exited with code 1"); free(c); c = tui_bash_context("sleep 9", "\x1b[1mx\x1b[0m", 1, 130); check_str("bash context: cancelled, escapes removed", c, "Ran `sleep 9`\n```\nx\n```\n\n(command cancelled)"); free(c); { char big[20000]; size_t n = 0; for (int i = 1; i <= 3000; i++) n += (size_t)snprintf(big + n, sizeof big - n, "l%d\n", i); c = tui_bash_context("seq", big, 0, 0); if (strstr(c, "l1\n") || !strstr(c, "l3000\n```") || !strstr(c, "[Output truncated]")) fail("bash context: the last 2000 lines", "%.80s", c); free(c); } } pass("view: bash context for the model"); { char buf[512]; const char *items[] = {"zeta", "alpha", "mid"}; tui_startup_hints(buf, sizeof buf); p = (char *)"esc interrupt \xc2\xb7 ctrl+c/ctrl+d clear/exit \xc2\xb7 / commands \xc2\xb7 ! bash \xc2\xb7 ctrl+o more"; { tui_lines one = {0}; char *got; tui_lines_add(&one, buf, strlen(buf)); got = plain_lines(&one); check_str("startup key hints", got, p); free(got); tui_lines_clear(&one); } tui_loaded_section("Skills", items, 3, 1, 40, &l); p = plain_lines(&l); check_str("loaded section sorted", p, "[Skills]\n alpha, mid, zeta"); free(p); tui_lines_clear(&l); tui_loaded_section("Context", items, 3, 0, 40, &l); p = plain_lines(&l); check_str("loaded section keeps load order", p, "[Context]\n zeta, alpha, mid"); free(p); tui_lines_clear(&l); check_int("an empty section prints nothing", tui_loaded_section("X", items, 0, 1, 40, &l), 0); check_int("and adds no lines", (long)l.n, 0); } { char t[256]; tui_title("/home/me/work/proj/", NULL, t, sizeof t); check_str("terminal title", t, "\x1b]0;pic - proj\x07"); tui_title("/home/me/work/proj", "my\x1b chat", t, sizeof t); check_str("terminal title with a name, controls dropped", t, "\x1b]0;pic - my chat - proj\x07"); tui_title("/", NULL, t, sizeof t); check_str("terminal title at the root", t, "\x1b]0;pic - /\x07"); } pass("view: startup hints, loaded sections, terminal title"); } static void touch(const char *dir, const char *rel) { char p[512]; int fd; snprintf(p, sizeof p, "%s/%s", dir, rel); fd = open(p, O_WRONLY | O_CREAT, 0600); if (fd < 0) fail("atfile setup", "%s", p); close(fd); } static void put(const char *dir, const char *rel, const char *text) { char p[512]; FILE *f; snprintf(p, sizeof p, "%s/%s", dir, rel); if (!(f = fopen(p, "w"))) fail("atfile setup", "%s", p); fputs(text, f); fclose(f); } static int offers(const char *root, const char *query, const char *value) { atfile_item it[64]; size_t n = atfile_candidates(root, query, 0, it, 64); for (size_t i = 0; i < n; i++) if (strcmp(it[i].value, value) == 0) return 1; return 0; } static void test_atfile_ignore(void) { char root[] = "/tmp/pic-atign.XXXXXX", p[512]; if (!mkdtemp(root)) fail("atfile ignore", "mkdtemp"); snprintf(p, sizeof p, "%s/sub", root), mkdir(p, 0700); snprintf(p, sizeof p, "%s/gi", root), mkdir(p, 0700); snprintf(p, sizeof p, "%s/zi", root), mkdir(p, 0700); snprintf(p, sizeof p, "%s/.fossil-settings", root), mkdir(p, 0700); snprintf(p, sizeof p, "%s/.zit-settings", root), mkdir(p, 0700); snprintf(p, sizeof p, "%s/.zit", root), mkdir(p, 0700); snprintf(p, sizeof p, "%s/sub/deep", root), mkdir(p, 0700); for (const char *f = "keepme.txt gitgone.txt zitgone.txt fslgone.txt quoted one.txt comma1.txt comma2.txt wsp.txt " "settgone.txt zsettgone.txt sub/keepme.txt sub/subgone.txt sub/deep/subgone.txt sub/rel.o " "gi/inner.txt zi/inner.txt .zit/x .fslckout _FOSSIL_ .fossil-settings/ignore-glob " ".zit-settings/ignore-glob"; *f;) { char name[128]; size_t k = strcspn(f, " "); snprintf(name, sizeof name, "%.*s", (int)k, f); if (strcmp(name, ".fossil-settings/ignore-glob") && strcmp(name, ".zit-settings/ignore-glob")) touch(root, name); f += k + (f[k] != 0); } put(root, ".gitignore", "gitgone.txt\ngi/\n"); put(root, ".zitignore", "zitgone.txt\nzi/\n"); put(root, ".fslignore", "fslgone.txt \"quoted one.txt\",comma1.txt\ncomma2.txt\twsp.txt"); put(root, ".fossil-settings/ignore-glob", "settgone.txt\n"); put(root, ".zit-settings/ignore-glob", "zsettgone.txt"); put(root, "sub/.fslignore", "subgone.txt *.o"); /* the "quoted one.txt" pattern names a file that does not exist: assert it again below */ touch(root, "quoted one.txt"); if (!offers(root, "keepme", "@keepme.txt") || !offers(root, "keepme", "@sub/keepme.txt")) fail("atfile ignore", "kept files missing"); if (offers(root, "gitgone", "@gitgone.txt") || offers(root, "inner", "@gi/inner.txt") || offers(root, "gi", "@gi/")) fail("atfile ignore", ".gitignore not applied"); if (offers(root, "zitgone", "@zitgone.txt") || offers(root, "inner", "@zi/inner.txt")) fail("atfile ignore", ".zitignore not applied"); if (offers(root, "fslgone", "@fslgone.txt") || offers(root, "quoted", "@\"quoted one.txt\"") || offers(root, "comma1", "@comma1.txt") || offers(root, "comma2", "@comma2.txt") || offers(root, "wsp", "@wsp.txt")) fail("atfile ignore", ".fslignore (quotes, commas, white space) not applied"); if (!offers(root, "one", "@one.txt")) fail("atfile ignore", "a quoted pattern must not split at its space"); if (offers(root, "subgone", "@sub/subgone.txt") || offers(root, "rel", "@sub/rel.o")) fail("atfile ignore", "sub/.fslignore not applied under sub/"); if (!offers(root, "subgone", "@sub/deep/subgone.txt")) /* Fossil globs match the whole relative path */ fail("atfile ignore", "a bare glob must not match in nested folders"); if (offers(root, "settgone", "@settgone.txt") || offers(root, "zsettgone", "@zsettgone.txt")) fail("atfile ignore", "settings ignore-glob not applied at the root"); if (offers(root, "", "@.zit/") || offers(root, "", "@.fossil-settings/") || offers(root, "", "@.zit-settings/") || offers(root, "fslckout", "@.fslckout") || offers(root, "FOSSIL", "@_FOSSIL_") || offers(root, "x", "@.zit/x")) fail("atfile ignore", "version-control metadata offered"); snprintf(p, sizeof p, "rm -rf '%s'", root); if (system(p) != 0) fail("atfile ignore cleanup", "rm"); pass("@file completion honours .gitignore, .zitignore, .fslignore, ignore-glob settings"); } static void test_atfile(void) { char root[] = "/tmp/pic-atfile.XXXXXX", p[512], q[256]; atfile_item it[8]; size_t start, n; int quoted; const char *txt = "see @src/ma and more"; tui_editor *e = tui_editor_new(); if (!mkdtemp(root)) fail("atfile", "mkdtemp"); snprintf(p, sizeof p, "%s/src", root), mkdir(p, 0700); snprintf(p, sizeof p, "%s/src/deep", root), mkdir(p, 0700); snprintf(p, sizeof p, "%s/.git", root), mkdir(p, 0700); snprintf(p, sizeof p, "%s/node_modules", root), mkdir(p, 0700); touch(root, "src/main.c"); touch(root, "src/deep/main.h"); touch(root, "README.md"); touch(root, "a b.txt"); touch(root, ".git/config"); touch(root, "node_modules/x.js"); if (!atfile_token(txt, 11, &start, q, sizeof q, "ed) || start != 4 || strcmp(q, "src/ma") != 0 || quoted) fail("atfile token", "start %zu query '%s'", start, q); if (atfile_token("mail a@b", 8, &start, q, sizeof q, "ed) || atfile_token("plain", 5, &start, q, sizeof q, "ed)) fail("atfile token", "a mid-word @ or no @ must not open the menu"); if (!atfile_token("go @\"my f", 10, &start, q, sizeof q, "ed) || !quoted || start != 3 || strcmp(q, "my f") != 0) fail("atfile token", "quoted: %zu '%s'", start, q); if (!atfile_token("(@a", 3, &start, q, sizeof q, "ed) || start != 1 || strcmp(q, "a") != 0) fail("atfile token", "wrapper: %zu '%s'", start, q); n = atfile_candidates(root, "", 0, it, 8); if (n != 6 || strcmp(it[0].value, "@src/") != 0 || !it[0].is_dir || strcmp(it[1].value, "@\"a b.txt\"") != 0 || strcmp(it[2].value, "@README.md") != 0 || strcmp(it[3].value, "@src/deep/") != 0 || strcmp(it[4].value, "@src/main.c") != 0 || strcmp(it[5].value, "@src/deep/main.h") != 0) fail("atfile empty query", "n %zu: %s %s %s", n, it[0].value, n > 1 ? it[1].value : "", n > 2 ? it[2].value : ""); for (size_t i = 0; i < n; i++) if (strstr(it[i].value, ".git") || strstr(it[i].value, "node_modules")) fail("atfile skip", "%s", it[i].value); n = atfile_candidates(root, "main", 0, it, 8); if (n != 2 || strcmp(it[0].value, "@src/main.c") != 0 || strcmp(it[1].value, "@src/deep/main.h") != 0) fail("atfile filter", "n %zu first %s", n, n ? it[0].value : ""); n = atfile_candidates(root, "mnc", 0, it, 8); if (n != 1 || strcmp(it[0].value, "@src/main.c") != 0) fail("atfile fuzzy", "n %zu", n); n = atfile_candidates(root, "src/de", 0, it, 8); if (n != 1 || strcmp(it[0].value, "@src/deep/") != 0 || !it[0].is_dir) fail("atfile scoped", "n %zu %s", n, n ? it[0].value : ""); n = atfile_candidates(root, "src/deep/", 0, it, 8); if (n != 1 || strcmp(it[0].value, "@src/deep/main.h") != 0) fail("atfile scoped folder", "n %zu", n); if (atfile_candidates(root, "nope/", 0, it, 8) != 0 || atfile_candidates(root, "zzzz", 0, it, 8) != 0) fail("atfile none", "expected nothing"); pass("@file token, candidates, filtering, skips"); tui_editor_set(e, "see @sr and more"); tui_editor_replace(e, 4, 7, "@src/", 5); text_is("replace", e, "see @src/ and more"); if (tui_editor_cursor(e) != 9) fail("replace cursor", "%zu", tui_editor_cursor(e)); pass("editor replace"); tui_editor_free(e); snprintf(p, sizeof p, "rm -rf '%s'", root); if (system(p) != 0) fail("atfile cleanup", "rm"); } /* ---- the fullscreen viewport ------------------------------------------ */ static void scroll_is(const char *what, const tui_scroll *s, size_t top, int follow) { if (s->top != top || s->follow != follow) fail(what, "top %zu follow %d, want top %zu follow %d", s->top, s->follow, top, follow); } static const char *vp_lines[] = {"hello world", "\x1b[1mbold\x1b[0m ", "xyz"}; static const char *vp_line_at(void *ctx, size_t i) { (void)ctx; return i < 3 ? vp_lines[i] : ""; } static void test_viewport(void) { tui_scroll s; tui_scrollbar g; tui_wheel w; tui_selection sel; tui_point a, b; size_t c0, c1; char *t; /* follow: new content keeps the end in view */ tui_scroll_init(&s); tui_scroll_layout(&s, 10, 4); scroll_is("follow layout", &s, 6, 1); tui_scroll_layout(&s, 12, 4); scroll_is("follow grows", &s, 8, 1); /* scrolling up stops following; growth keeps the top */ if (!tui_scroll_by(&s, -3)) fail("scroll up", "did not move"); scroll_is("scroll up", &s, 5, 0); tui_scroll_layout(&s, 20, 4); scroll_is("no follow grows", &s, 5, 0); /* reaching the end follows again */ (void)tui_scroll_by(&s, 100); scroll_is("scroll to end", &s, 16, 1); tui_scroll_layout(&s, 25, 4); scroll_is("follow again", &s, 21, 1); if (tui_scroll_by(&s, 1)) fail("scroll past end", "moved"); (void)tui_scroll_by(&s, -100); scroll_is("scroll past start", &s, 0, 0); /* a taller viewport clamps the top; content that fits follows */ (void)tui_scroll_by(&s, 10); tui_scroll_layout(&s, 25, 20); scroll_is("taller clamps", &s, 5, 1); tui_scroll_layout(&s, 3, 20); scroll_is("fits", &s, 0, 1); tui_scroll_layout(&s, 50, 10); tui_scroll_start(&s); scroll_is("start", &s, 0, 0); tui_scroll_end(&s); scroll_is("end", &s, 40, 1); /* PageUp/PageDown: the height less 4 lines of overlap, at least 1 */ if (tui_scroll_page(&s, -1) != -6 || tui_scroll_page(&s, 1) != 6) fail("page", "%ld %ld", tui_scroll_page(&s, -1), tui_scroll_page(&s, 1)); tui_scroll_layout(&s, 50, 3); if (tui_scroll_page(&s, -1) != -1) fail("page short", "%ld", tui_scroll_page(&s, -1)); pass("viewport scrolling follows the end, PageUp/PageDown overlap 4 lines"); /* Pi's scrollbar geometry */ tui_scroll_init(&s); tui_scroll_layout(&s, 100, 10); s.top = 0, s.follow = 0; if (!tui_scrollbar_geometry(&s, &g) || g.thumb_top != 0 || g.thumb_height != 2) fail("bar top", "%zu+%zu", g.thumb_top, g.thumb_height); s.top = 45; if (!tui_scrollbar_geometry(&s, &g) || g.thumb_top != 4 || g.thumb_height != 2) fail("bar middle", "%zu+%zu", g.thumb_top, g.thumb_height); s.top = 90; if (!tui_scrollbar_geometry(&s, &g) || g.thumb_top != 8) fail("bar end", "%zu+%zu", g.thumb_top, g.thumb_height); tui_scroll_layout(&s, 20, 10); s.top = 0; if (!tui_scrollbar_geometry(&s, &g) || g.thumb_height != 5 || g.thumb_top != 0) fail("bar half", "%zu+%zu", g.thumb_top, g.thumb_height); tui_scroll_layout(&s, 4, 10); if (!tui_scrollbar_geometry(&s, &g) || g.thumb_height != 10) fail("bar fits", "%zu+%zu", g.thumb_top, g.thumb_height); tui_scroll_layout(&s, 4, 0); if (tui_scrollbar_geometry(&s, &g)) fail("bar no rows", "drawn"); if (tui_scrollbar_mode("always") != TUI_BAR_ALWAYS || tui_scrollbar_mode("hidden") != TUI_BAR_HIDDEN || tui_scrollbar_mode("auto") != TUI_BAR_AUTO || tui_scrollbar_mode(NULL) != TUI_BAR_AUTO || tui_scrollbar_mode("bogus") != TUI_BAR_AUTO) fail("bar mode", "parse"); pass("viewport scrollbar geometry"); /* the wheel: fixed counts, auto without acceleration, auto with it */ tui_wheel_init(&w, 3, 1); if (tui_wheel_next(&w, 1, 0) != 3) fail("wheel fixed", "not 3"); tui_wheel_init(&w, 0, 0); if (tui_wheel_next(&w, 1, 0) != 1 || tui_wheel_next(&w, 1, 10) != 1) fail("wheel auto macOS", "not 1"); tui_wheel_init(&w, 0, 1); if (tui_wheel_next(&w, 1, 1000) != 1) fail("wheel first", "not 1"); if (tui_wheel_next(&w, 1, 1020) != 5) fail("wheel fast", "20 ms apart is not 5"); if (tui_wheel_next(&w, 1, 1022) != 1) fail("wheel burst", "2 ms apart is not 1"); if (tui_wheel_next(&w, -1, 1030) != 1) fail("wheel reverse", "a new direction is not 1"); if (tui_wheel_next(&w, -1, 1035) != 6) fail("wheel max", "5 ms apart is not 6"); if (tui_wheel_next(&w, -1, 2000) != 1) fail("wheel pause", "a new gesture is not 1"); tui_wheel_init(&w, 0, 1); (void)tui_wheel_next(&w, 1, 0); if (tui_wheel_next(&w, 1, 100) != 1 || tui_wheel_next(&w, 1, 150) != 1 || tui_wheel_next(&w, 1, 200) != 1 || tui_wheel_next(&w, 1, 250) != 2) fail("wheel carry", "the fraction is not carried"); if (tui_wheel_lines("auto") != 0 || tui_wheel_lines(NULL) != 0 || tui_wheel_lines("3") != 3 || tui_wheel_lines("0") != 1 || tui_wheel_lines("500") != 100 || tui_wheel_lines("x") != 0) fail("wheel lines", "parse"); pass("viewport wheel lines follow Pi's accelerator"); /* selection: ordered bounds, columns per line, the text */ memset(&sel, 0, sizeof sel); sel.on = 1; sel.anchor = (tui_point){2, 1}; sel.focus = sel.anchor; if (tui_selection_bounds(&sel, &a, &b)) fail("selection click", "a click selected"); sel.focus = (tui_point){0, 6}; if (!tui_selection_bounds(&sel, &a, &b) || a.line != 0 || a.col != 6 || b.line != 2 || b.col != 1) fail("selection bounds", "%zu:%zu-%zu:%zu", a.line, a.col, b.line, b.col); if (!tui_selection_cols(&sel, 0, 11, &c0, &c1) || c0 != 6 || c1 != 11) fail("selection first", "%zu-%zu", c0, c1); if (!tui_selection_cols(&sel, 1, 6, &c0, &c1) || c0 != 0 || c1 != 6) fail("selection middle", "%zu-%zu", c0, c1); if (!tui_selection_cols(&sel, 2, 3, &c0, &c1) || c0 != 0 || c1 != 2) fail("selection last", "%zu-%zu", c0, c1); if (tui_selection_cols(&sel, 3, 3, &c0, &c1)) fail("selection after", "selected"); t = tui_selection_text(&sel, vp_line_at, NULL); check_str("selection text", t ? t : "(null)", "world\nbold\nxy"); free(t); sel.on = 0; if (tui_selection_text(&sel, vp_line_at, NULL)) fail("selection off", "text"); pass("viewport selection bounds and text"); t = tui_line_plain("a\xe7\x95\x8c" "b", 1, 3); check_str("plain wide", t, "\xe7\x95\x8c"); free(t); t = tui_line_plain("a\xe7\x95\x8c" "b", 2, 4); check_str("plain wide start", t, "b"); free(t); t = tui_line_plain("\x1b]8;;http://x\x07link\x1b]8;;\x07!", 0, 100); check_str("plain osc", t, "link!"); free(t); t = tui_line_highlight("ab\x1b[1mcd\x1b[0mef", 1, 5); check_str("highlight", t, "a\x1b[7mb\x1b[1m\x1b[7mcd\x1b[0m\x1b[7me\x1b[27mf"); free(t); t = tui_line_highlight("abc", 1, 9); check_str("highlight to end", t, "a\x1b[7mbc\x1b[27m"); free(t); t = tui_line_bar("abcdef", 5, "|"); check_str("bar cut", t, "abcd\x1b[0m|"); free(t); t = tui_line_bar("ab", 5, "|"); check_str("bar pad", t, "ab\x1b[0m |"); free(t); { tui_lines l = {NULL, 0, 0}; if (tui_line_layout("hello", 10, &l) != 0 || l.n != 1) fail("layout fits", "%zu", l.n); check_str("layout fits", l.lines[0], "hello"); tui_lines_clear(&l); (void)tui_line_layout("\x1b[44mabc \x1b[0m", 5, &l); if (l.n != 1) fail("layout padded", "%zu lines", l.n); check_str("layout padded", l.lines[0], "\x1b[44mabc \x1b[0m"); tui_lines_clear(&l); (void)tui_line_layout("hello world", 5, &l); if (l.n != 2) fail("layout wraps", "%zu lines", l.n); check_str("layout wraps 1", l.lines[0], "hello"); check_str("layout wraps 2", l.lines[1], "world"); tui_lines_clear(&l); } pass("viewport line plain text, highlight, scrollbar cell, layout"); } static void mouse_is(const char *what, const char *bytes, int mouse, int button, int x, int y, int alt) { tui_keys *k = tui_keys_new(); tui_key key; tui_keys_feed(k, bytes, strlen(bytes)); if (!tui_keys_next(k, &key) || key.kind != TUI_KEY_MOUSE) fail(what, "not a mouse event"); if (key.mouse != mouse || key.button != button || key.mx != x || key.my != y || key.alt != alt) fail(what, "got %d %d %d,%d alt %d", key.mouse, key.button, key.mx, key.my, key.alt); if (tui_keys_next(k, &key)) fail(what, "more than one key"); tui_keys_free(k); } static void test_keys_fullscreen(void) { tui_key_kind one = TUI_KEY_CTRL_HOME; keys_are("ctrl+home", "\x1b[1;5H", 7, &one, 1); one = TUI_KEY_CTRL_END; keys_are("ctrl+end", "\x1b[1;5F", 7, &one, 1); one = TUI_KEY_HOME; keys_are("plain home", "\x1b[H", 3, &one, 1); mouse_is("left press", "\x1b[<0;10;5M", TUI_MOUSE_PRESS, 0, 9, 4, 0); mouse_is("left drag", "\x1b[<32;12;6M", TUI_MOUSE_DRAG, 0, 11, 5, 0); mouse_is("left release", "\x1b[<0;12;6m", TUI_MOUSE_RELEASE, 0, 11, 5, 0); mouse_is("wheel up", "\x1b[<64;1;1M", TUI_MOUSE_WHEEL, -1, 0, 0, 0); mouse_is("wheel down", "\x1b[<65;3;2M", TUI_MOUSE_WHEEL, 1, 2, 1, 0); mouse_is("alt wheel", "\x1b[<72;3;2M", TUI_MOUSE_WHEEL, -1, 2, 1, 1); mouse_is("right press", "\x1b[<2;3;2M", TUI_MOUSE_PRESS, 2, 2, 1, 0); pass("term: ctrl+home/end and SGR mouse reports decode"); } /* ---- render: fullscreen --------------------------------------------- */ #define ROW(n, s) "\x1b[" #n ";1H" s "\x1b[0m\x1b[K" static char fs_got[65536]; /* Everything written since the last call. */ static const char *fs_read(void) { size_t o = 0; ssize_t n; while (o < sizeof fs_got - 1 && (n = read(rfd[0], fs_got + o, sizeof fs_got - 1 - o)) > 0) o += (size_t)n; fs_got[o] = 0; return fs_got; } static void fs_has(const char *what, const char *got, const char *want) { char a[2048], b[512]; if (!strstr(got, want)) { show(got, strlen(got), a, sizeof a); show(want, strlen(want), b, sizeof b); fail(what, "no \"%s\" in \"%s\"", b, a); } } static void fs_lacks(const char *what, const char *got, const char *want) { char a[2048]; if (strstr(got, want)) { show(got, strlen(got), a, sizeof a); fail(what, "unexpected \"%s\" in \"%s\"", want, a); } } static void test_render_fullscreen(void) { const char *t123[] = {"t1", "t2", "t3"}, *dock[] = {"ed", "ft"}, *body[] = {"b1", "ed", "ft"}; const char *got; char *sel; if (pipe(rfd) != 0) fail("render fullscreen", "pipe"); fcntl(rfd[0], F_SETFL, fcntl(rfd[0], F_GETFL) | O_NONBLOCK); tui_render *r = tui_render_new(rfd[1]); /* Lines committed before fullscreen are part of the transcript. */ tui_render_commit(r, t123, 1); (void)fs_read(); tui_render_options(r, TUI_BAR_HIDDEN, 1); check_int("fullscreen on", tui_render_fullscreen(r, 1, 40, 4), 0); check_int("is fullscreen", tui_render_is_fullscreen(r), 1); got = fs_read(); fs_has("enter", got, "\x1b[?1049h"); fs_has("mouse on", got, "\x1b[?1006h"); tui_render_commit(r, t123 + 1, 2); check_str("commit draws nothing", fs_read(), ""); /* the viewport (2 rows) shows the end; the dock is pinned below */ tui_render_draw_split(r, dock, 2, 0, 0, 2, 4); check_str("first frame", fs_read(), SYNC_ON HIDE "\x1b[2J" ROW(1, "t2") ROW(2, "t3") ROW(3, "ed") ROW(4, "ft") "\x1b[3;3H" SHOW SYNC_OFF); tui_render_draw_split(r, dock, 2, 0, 0, 2, 4); check_str("same frame", fs_read(), SYNC_ON HIDE "\x1b[3;3H" SHOW SYNC_OFF); /* live lines before split end the transcript */ tui_render_draw_split(r, body, 3, 1, 1, 0, 4); check_str("live line", fs_read(), SYNC_ON HIDE ROW(1, "t3") ROW(2, "b1") "\x1b[3;1H" SHOW SYNC_OFF); /* PageUp: Pi's height - 4, at least a line; the view stops following */ check_int("page up", tui_render_page(r, -1), 1); tui_render_draw_split(r, body, 3, 1, -1, 0, 4); got = fs_read(); fs_has("page up row 1", got, ROW(1, "t2")); fs_has("jump indicator", got, "Jump to latest message"); fs_lacks("cursor hidden", got, SHOW); /* a commit does not move a view that was scrolled up */ tui_render_commit(r, t123, 1); tui_render_draw_split(r, body, 3, 1, -1, 0, 4); check_str("scrolled view stays", fs_read(), SYNC_ON HIDE SYNC_OFF); check_int("to the top", tui_render_edge(r, 0), 1); tui_render_draw_split(r, body, 3, 1, -1, 0, 4); fs_has("top row", fs_read(), ROW(1, "t1")); check_int("to the end", tui_render_edge(r, 1), 1); tui_render_draw_split(r, body, 3, 1, -1, 0, 4); got = fs_read(); fs_has("end rows 2", got, ROW(2, "b1")); check_int("scroll past end", tui_render_scroll(r, 5), 0); check_int("scroll up", tui_render_scroll(r, -1), 1); check_int("scroll down", tui_render_scroll(r, 1), 1); /* selection: press, drag, release; the text and the highlight */ check_int("press", tui_render_mouse(r, TUI_MOUSE_PRESS, 0, 0), 0); check_int("drag", tui_render_mouse(r, TUI_MOUSE_DRAG, 0, 1), 0); check_int("release", tui_render_mouse(r, TUI_MOUSE_RELEASE, 1, 1), 1); sel = tui_render_selection(r); check_str("selection", sel ? sel : "(null)", "t1\nb1"); free(sel); tui_render_draw_split(r, body, 3, 1, -1, 0, 4); fs_has("highlight", fs_read(), "\x1b[7mt1"); check_int("click in the dock", tui_render_mouse(r, TUI_MOUSE_PRESS, 0, 3), 0); check_int("click release", tui_render_mouse(r, TUI_MOUSE_RELEASE, 0, 3), 0); if (tui_render_selection(r)) fail("click clears", "still selected"); /* the scrollbar: always, in the last column */ tui_render_options(r, TUI_BAR_ALWAYS, 1); tui_render_draw_split(r, body, 3, 1, -1, 0, 4); fs_has("scrollbar", fs_read(), "\xe2\x94\x83"); /* the thumb */ /* resize: wide lines are laid out again */ tui_render_options(r, TUI_BAR_HIDDEN, 1); { const char *wide[] = {"hello world"}; tui_render_commit(r, wide, 1); } tui_render_size(r, 5, 6); tui_render_invalidate(r); tui_render_draw_split(r, dock, 2, 0, -1, 0, 6); got = fs_read(); fs_has("resize clears", got, "\x1b[2J"); fs_has("resize wraps 1", got, ROW(3, "hello")); fs_has("resize wraps 2", got, ROW(4, "world")); /* pause and resume keep the transcript, print nothing */ check_int("pause", tui_render_pause(r), 0); got = fs_read(); fs_has("pause leaves", got, "\x1b[?1049l"); fs_lacks("pause prints nothing", got, "t1"); check_int("resume", tui_render_resume(r), 0); fs_has("resume enters", fs_read(), "\x1b[?1049h"); tui_render_draw_split(r, dock, 2, 0, -1, 0, 6); fs_has("resume redraws", fs_read(), ROW(3, "hello")); /* finish: leave, then the transcript and the dock on the normal screen */ tui_render_size(r, 40, 6); check_int("finish", tui_render_finish(r), 0); got = fs_read(); fs_has("finish leaves", got, "\x1b[?1049l"); #define PR(s) "\r\x1b[2K" s "\x1b[0m\r\n" fs_has("finish prints", got, PR("t1") PR("t2") PR("t3") PR("t1") PR("hello world") PR("ed") PR("ft")); if (strstr(got, "\x1b[?1049l") > strstr(got, PR("t1"))) fail("finish order", "printed before leaving"); check_int("finished", tui_render_is_fullscreen(r), 0); tui_render_free(r); /* resume-hint: finish only leaves */ r = tui_render_new(rfd[1]); tui_render_commit(r, t123, 3); tui_render_options(r, TUI_BAR_AUTO, 0); (void)tui_render_fullscreen(r, 1, 40, 4); tui_render_draw_split(r, dock, 2, 0, -1, 0, 4); (void)fs_read(); (void)tui_render_finish(r); got = fs_read(); fs_has("hint leaves", got, "\x1b[?1049l"); fs_lacks("hint prints nothing", got, "t1"); tui_render_free(r); /* switching off prints the transcript; regular drawing goes on */ r = tui_render_new(rfd[1]); tui_render_commit(r, t123, 3); (void)tui_render_fullscreen(r, 1, 40, 4); tui_render_draw_split(r, dock, 2, 0, -1, 0, 4); (void)fs_read(); check_int("switch off", tui_render_fullscreen(r, 0, 40, 4), 0); got = fs_read(); fs_has("switch off leaves", got, "\x1b[?1049l"); fs_has("switch off prints", got, PR("t1") PR("t2") PR("t3")); fs_lacks("switch off: no dock", got, "ed"); tui_render_draw_split(r, dock, 2, 0, 1, 1, 24); check_str("regular after switch", fs_read(), SYNC_ON HIDE "ed\r\nft" "\r" "\x1b[2G" SHOW SYNC_OFF); check_int("regular scroll is a no-op", tui_render_scroll(r, -1), 0); tui_render_free(r); tui_term_restore(); close(rfd[0]); close(rfd[1]); pass("render: fullscreen viewport, dock, scrolling, selection, resize, exit output"); } int main(void) { test_width(); test_wrap(); test_decoder(); test_render(); test_fuzzy(); test_atfile(); test_atfile_ignore(); test_editor(); test_view(); test_keys2(); test_editor2(); test_markdown(); test_view2(); test_viewport(); test_keys_fullscreen(); test_render_fullscreen(); return 0; }