@operad-io/tui 0.0.0-stage → 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (52) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +68 -2
  3. package/package.json +27 -4
  4. package/src/actions.ts +997 -0
  5. package/src/app.ts +1665 -0
  6. package/src/args.ts +198 -0
  7. package/src/autostart.ts +302 -0
  8. package/src/client.ts +478 -0
  9. package/src/discover.ts +90 -0
  10. package/src/events.ts +345 -0
  11. package/src/index.ts +11 -0
  12. package/src/layout.ts +260 -0
  13. package/src/live.ts +113 -0
  14. package/src/markdown.ts +85 -0
  15. package/src/recent.ts +40 -0
  16. package/src/sanitize.ts +18 -0
  17. package/src/state.ts +3200 -0
  18. package/src/theme.ts +525 -0
  19. package/src/themes.ts +60 -0
  20. package/src/view/index.ts +1204 -0
  21. package/src/wrap.ts +156 -0
  22. package/theme.schema.json +197 -0
  23. package/themes/LICENSE-catppuccin +21 -0
  24. package/themes/LICENSE-dracula +21 -0
  25. package/themes/LICENSE-github +21 -0
  26. package/themes/LICENSE-kanagawa +21 -0
  27. package/themes/LICENSE-nord +21 -0
  28. package/themes/LICENSE-one +20 -0
  29. package/themes/LICENSE-rose-pine +21 -0
  30. package/themes/LICENSE-solarized +20 -0
  31. package/themes/LICENSE-tokyo-night +22 -0
  32. package/themes/README.md +81 -0
  33. package/themes/catppuccin-frappe.json +19 -0
  34. package/themes/catppuccin-latte.json +19 -0
  35. package/themes/catppuccin-macchiato.json +19 -0
  36. package/themes/catppuccin-mocha.json +19 -0
  37. package/themes/dracula.json +19 -0
  38. package/themes/github-dark.json +19 -0
  39. package/themes/github-light.json +19 -0
  40. package/themes/kanagawa-dragon.json +19 -0
  41. package/themes/kanagawa-lotus.json +19 -0
  42. package/themes/kanagawa-wave.json +19 -0
  43. package/themes/nord.json +19 -0
  44. package/themes/one-dark.json +19 -0
  45. package/themes/one-light.json +19 -0
  46. package/themes/rose-pine-dawn.json +19 -0
  47. package/themes/rose-pine-main.json +19 -0
  48. package/themes/solarized-dark.json +19 -0
  49. package/themes/solarized-light.json +19 -0
  50. package/themes/tokyo-night-day.json +19 -0
  51. package/themes/tokyo-night-night.json +19 -0
  52. package/tui.schema.json +24 -0
package/src/live.ts ADDED
@@ -0,0 +1,113 @@
1
+ /**
2
+ * Live updates, apart from any screen: which fetches an event asks for, the
3
+ * clock that spaces them, and how a failed request is read as a state of the
4
+ * connection.
5
+ */
6
+
7
+ import { TuiError, TuiUnreachableError } from "./client.ts";
8
+ import { sameObject, type TuiConnection } from "./state.ts";
9
+
10
+ /** At most one batch of refetches per this many milliseconds. */
11
+ export const LIVE_DEBOUNCE_MS = 250;
12
+
13
+ /** What the shell needs from time: one timer that can be cancelled. */
14
+ export interface TuiClock {
15
+ /** Calls `run` once after `ms`; the returned function cancels it. */
16
+ after(run: () => void, ms: number): () => void;
17
+ }
18
+
19
+ /** The real clock. */
20
+ export const systemClock: TuiClock = {
21
+ after(run, ms) {
22
+ const timer = setTimeout(run, ms);
23
+ return () => clearTimeout(timer);
24
+ },
25
+ };
26
+
27
+ /** What to fetch again after one or more events. */
28
+ export interface Refetch {
29
+ /** Every open list and the open object (a `reset`). */
30
+ readonly all: boolean;
31
+ /** Pending and Running; they refetch on any event. */
32
+ readonly lists: boolean;
33
+ /** Ids whose row is fetched again in the open lists. */
34
+ readonly rows: readonly string[];
35
+ /** The open object, its row and its live sections. */
36
+ readonly object: boolean;
37
+ }
38
+
39
+ /** Nothing to fetch. */
40
+ export const NO_REFETCH: Refetch = { all: false, lists: false, rows: [], object: false };
41
+
42
+ /** What an event is read against. */
43
+ export interface LiveEvent {
44
+ readonly kind: "change" | "reset";
45
+ readonly id?: string | undefined;
46
+ }
47
+
48
+ /** The targets one event asks for, given the object that is open. */
49
+ export function refetchTargets(
50
+ event: LiveEvent,
51
+ openId: string | undefined,
52
+ ): Refetch {
53
+ if (event.kind === "reset") {
54
+ return { all: true, lists: true, rows: [], object: openId !== undefined };
55
+ }
56
+ const id = event.id;
57
+ if (id === undefined) return { ...NO_REFETCH, lists: true };
58
+ return {
59
+ all: false,
60
+ lists: true,
61
+ rows: [id],
62
+ object: openId !== undefined && sameObject(openId, id),
63
+ };
64
+ }
65
+
66
+ /** Two sets of targets as one. */
67
+ export function mergeRefetch(a: Refetch, b: Refetch): Refetch {
68
+ return {
69
+ all: a.all || b.all,
70
+ lists: a.lists || b.lists,
71
+ rows: [...new Set([...a.rows, ...b.rows])],
72
+ object: a.object || b.object,
73
+ };
74
+ }
75
+
76
+ /**
77
+ * Collects targets and hands them over once per window. The first push opens
78
+ * the window; pushes inside it join the same batch.
79
+ */
80
+ export class LiveBatcher {
81
+ private batch: Refetch = NO_REFETCH;
82
+ private cancel: (() => void) | undefined;
83
+
84
+ constructor(
85
+ private readonly clock: TuiClock,
86
+ private readonly flush: (targets: Refetch) => void,
87
+ private readonly windowMs = LIVE_DEBOUNCE_MS,
88
+ ) {}
89
+
90
+ push(targets: Refetch): void {
91
+ this.batch = mergeRefetch(this.batch, targets);
92
+ if (this.cancel !== undefined) return;
93
+ this.cancel = this.clock.after(() => {
94
+ const ready = this.batch;
95
+ this.batch = NO_REFETCH;
96
+ this.cancel = undefined;
97
+ this.flush(ready);
98
+ }, this.windowMs);
99
+ }
100
+
101
+ /** Drops the batch and the timer. */
102
+ stop(): void {
103
+ this.cancel?.();
104
+ this.cancel = undefined;
105
+ this.batch = NO_REFETCH;
106
+ }
107
+ }
108
+
109
+ /** What a failed request says about the server; undefined says nothing. */
110
+ export function connectionOf(error: unknown): TuiConnection | undefined {
111
+ if (error instanceof TuiUnreachableError) return "unreachable";
112
+ return error instanceof TuiError && error.status === 401 ? "unauthorized" : undefined;
113
+ }
@@ -0,0 +1,85 @@
1
+ /**
2
+ * The TUI's Markdown drawing: the engine renders the lines, `layout.ts` cleans
3
+ * and wraps them, and this turns a laid-out row into OpenTUI chunks in the
4
+ * theme's colours. It owns no layout and decides no colours beyond the palette
5
+ * it is given.
6
+ */
7
+
8
+ import type { TextChunk } from "@opentui/core";
9
+ import { toRgba, type TuiPalette } from "./theme.ts";
10
+ import {
11
+ layoutMarkdown,
12
+ type LayoutLine,
13
+ type LayoutRole,
14
+ } from "./layout.ts";
15
+
16
+ /** The colours Markdown needs; the view's palette has them. */
17
+ export type MarkdownPalette = Pick<TuiPalette, "accent" | "run" | "error" | "add" | "remove" | "weak" | "text" | "code">;
18
+
19
+ /** One drawn line: its chunks, its plain text and whether it is fenced code. */
20
+ export interface TuiMarkdownLine {
21
+ readonly text: string;
22
+ readonly code: boolean;
23
+ readonly chunks: readonly TextChunk[];
24
+ }
25
+
26
+ function colourFor(
27
+ role: LayoutRole,
28
+ palette: MarkdownPalette,
29
+ ): string {
30
+ switch (role) {
31
+ case "add":
32
+ return palette.add;
33
+ case "remove":
34
+ return palette.remove;
35
+ case "accent":
36
+ return palette.accent;
37
+ case "weak":
38
+ return palette.weak;
39
+ default:
40
+ return palette.text;
41
+ }
42
+ }
43
+
44
+ /** One laid-out row in the palette's colours; `weak` draws every run in the weak colour. */
45
+ export function chunksOfLine(
46
+ line: LayoutLine,
47
+ palette: MarkdownPalette,
48
+ weak = false,
49
+ ): TextChunk[] {
50
+ return line.runs.map((run) => {
51
+ const colour = weak ? palette.weak : colourFor(run.style.role, palette);
52
+ return {
53
+ __isChunk: true,
54
+ text: run.text,
55
+ fg: toRgba(colour),
56
+ ...(line.code ? { bg: toRgba(palette.code) } : {}),
57
+ ...(run.style.attributes === 0 ? {} : { attributes: run.style.attributes }),
58
+ };
59
+ });
60
+ }
61
+
62
+ /** Styles laid-out rows for the theme. */
63
+ export function styleLines(
64
+ lines: readonly LayoutLine[],
65
+ palette: MarkdownPalette,
66
+ ): readonly TuiMarkdownLine[] {
67
+ return lines.map((line) => ({
68
+ text: line.text,
69
+ code: line.code,
70
+ chunks: chunksOfLine(line, palette),
71
+ }));
72
+ }
73
+
74
+ /**
75
+ * Every row the object pane draws for one section, styled for the theme and
76
+ * wrapped to `width` when the pane knows it. The reducer counts these same
77
+ * wrapped rows, so its scroll and the drawn content cannot drift apart.
78
+ */
79
+ export function markdownLines(
80
+ markdown: string,
81
+ palette: MarkdownPalette,
82
+ width?: number | undefined,
83
+ ): readonly TuiMarkdownLine[] {
84
+ return styleLines(layoutMarkdown(markdown, width), palette);
85
+ }
package/src/recent.ts ADDED
@@ -0,0 +1,40 @@
1
+ /**
2
+ * The TUI's own state file, `<home>/tui-state.json`: the palette entries used
3
+ * last. It is the TUI's, not the server's; a missing or broken file is empty.
4
+ */
5
+
6
+ import { mkdirSync, readFileSync, renameSync, writeFileSync } from "node:fs";
7
+ import { join } from "node:path";
8
+ import { RECENT_ENTRIES } from "./actions.ts";
9
+
10
+ export function stateFile(home: string): string {
11
+ return join(home, "tui-state.json");
12
+ }
13
+
14
+ /** The remembered palette entries, the latest first. */
15
+ export function loadRecent(home: string): readonly string[] {
16
+ try {
17
+ const parsed: unknown = JSON.parse(readFileSync(stateFile(home), "utf8"));
18
+ if (typeof parsed !== "object" || parsed === null) return [];
19
+ const list: unknown = (parsed as { recent?: unknown }).recent;
20
+ if (!Array.isArray(list)) return [];
21
+ return list
22
+ .filter((item): item is string => typeof item === "string")
23
+ .slice(0, RECENT_ENTRIES);
24
+ } catch {
25
+ return [];
26
+ }
27
+ }
28
+
29
+ /** Writes the list; a failure is ignored, since losing it costs only a convenience. */
30
+ export function saveRecent(home: string, recent: readonly string[]): void {
31
+ try {
32
+ mkdirSync(home, { recursive: true });
33
+ const file = stateFile(home);
34
+ const temporary = `${file}.${process.pid}.tmp`;
35
+ writeFileSync(temporary, `${JSON.stringify({ recent }, null, 2)}\n`);
36
+ renameSync(temporary, file);
37
+ } catch {
38
+ // Nothing to do: the next change writes again.
39
+ }
40
+ }
@@ -0,0 +1,18 @@
1
+ /**
2
+ * Everything a server sends is text to draw, never a command to the terminal.
3
+ * One function turns the control characters of such text into U+FFFD, so an
4
+ * escape sequence in a log line or a title cannot move the cursor, change a
5
+ * colour, write the clipboard or fake the screen's own styles.
6
+ */
7
+
8
+ // C0 controls except \t (0x09) and \n (0x0a), DEL, and the C1 controls. ESC,
9
+ // BEL, \r and the 8-bit CSI/OSC introducers are all in here.
10
+ const CONTROL = /[\x00-\x08\x0b-\x1f\x7f-\x9f]/g;
11
+
12
+ /** The replacement a control character gets: visible, one column wide. */
13
+ export const CONTROL_REPLACEMENT = "�";
14
+
15
+ /** `text` with every control character replaced; `\n` and `\t` are kept. */
16
+ export function sanitize(text: string): string {
17
+ return text.replace(CONTROL, CONTROL_REPLACEMENT);
18
+ }