@sixb/cli 0.1.21 → 0.1.23

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 (129) hide show
  1. package/README.md +18 -3
  2. package/dist/commands/api.d.ts.map +1 -1
  3. package/dist/commands/app.d.ts.map +1 -1
  4. package/dist/commands/build.d.ts.map +1 -1
  5. package/dist/commands/check.d.ts.map +1 -1
  6. package/dist/commands/db-migrate.d.ts.map +1 -1
  7. package/dist/commands/deploy.d.ts.map +1 -1
  8. package/dist/commands/desktop.d.ts +15 -0
  9. package/dist/commands/desktop.d.ts.map +1 -0
  10. package/dist/commands/dev-runner.d.ts +4 -0
  11. package/dist/commands/dev-runner.d.ts.map +1 -1
  12. package/dist/commands/dev.d.ts.map +1 -1
  13. package/dist/commands/init.d.ts.map +1 -1
  14. package/dist/commands/lake-check.d.ts.map +1 -1
  15. package/dist/commands/lake-cleanup.d.ts.map +1 -1
  16. package/dist/commands/login.d.ts.map +1 -1
  17. package/dist/commands/logout.d.ts.map +1 -1
  18. package/dist/commands/orchestrator.d.ts.map +1 -1
  19. package/dist/commands/profile.d.ts.map +1 -1
  20. package/dist/commands/rules.d.ts.map +1 -1
  21. package/dist/commands/scheduler.d.ts.map +1 -1
  22. package/dist/commands/service-account.d.ts.map +1 -1
  23. package/dist/commands/status.d.ts.map +1 -1
  24. package/dist/commands/token.d.ts.map +1 -1
  25. package/dist/commands/typegen.d.ts.map +1 -1
  26. package/dist/commands/worker-group.d.ts +2 -0
  27. package/dist/commands/worker-group.d.ts.map +1 -1
  28. package/dist/commands/worker.d.ts +2 -0
  29. package/dist/commands/worker.d.ts.map +1 -1
  30. package/dist/lib/command-line.d.ts +22 -2
  31. package/dist/lib/command-line.d.ts.map +1 -1
  32. package/dist/lib/deploy-release.d.ts +3 -1
  33. package/dist/lib/deploy-release.d.ts.map +1 -1
  34. package/dist/lib/desktop.d.ts +26 -0
  35. package/dist/lib/desktop.d.ts.map +1 -0
  36. package/dist/lib/dev-messages.d.ts +43 -0
  37. package/dist/lib/dev-messages.d.ts.map +1 -0
  38. package/dist/lib/dev-supervisor.d.ts +27 -0
  39. package/dist/lib/dev-supervisor.d.ts.map +1 -1
  40. package/dist/lib/open-url.d.ts +3 -0
  41. package/dist/lib/open-url.d.ts.map +1 -0
  42. package/dist/lib/role-lifecycle.d.ts.map +1 -1
  43. package/dist/lib/runtime.d.ts +2 -0
  44. package/dist/lib/runtime.d.ts.map +1 -1
  45. package/dist/lib/worker-concurrency.d.ts +11 -0
  46. package/dist/lib/worker-concurrency.d.ts.map +1 -1
  47. package/dist/lib/worker-registry.d.ts +2 -0
  48. package/dist/lib/worker-registry.d.ts.map +1 -1
  49. package/dist/ui/deploy-screens.d.ts +48 -0
  50. package/dist/ui/deploy-screens.d.ts.map +1 -0
  51. package/dist/ui/index.d.ts +28 -244
  52. package/dist/ui/index.d.ts.map +1 -1
  53. package/dist/ui/layout.d.ts +65 -0
  54. package/dist/ui/layout.d.ts.map +1 -0
  55. package/dist/ui/live/ansi.d.ts +8 -0
  56. package/dist/ui/live/ansi.d.ts.map +1 -0
  57. package/dist/ui/live/can-run-live.d.ts +6 -0
  58. package/dist/ui/live/can-run-live.d.ts.map +1 -0
  59. package/dist/ui/live/components.d.ts +17 -0
  60. package/dist/ui/live/components.d.ts.map +1 -0
  61. package/dist/ui/live/deploy.d.ts +22 -0
  62. package/dist/ui/live/deploy.d.ts.map +1 -0
  63. package/dist/ui/live/dev.d.ts +47 -0
  64. package/dist/ui/live/dev.d.ts.map +1 -0
  65. package/dist/ui/live/prompt.d.ts +30 -0
  66. package/dist/ui/live/prompt.d.ts.map +1 -0
  67. package/dist/ui/live/session.d.ts +27 -0
  68. package/dist/ui/live/session.d.ts.map +1 -0
  69. package/dist/ui/screens.d.ts +136 -0
  70. package/dist/ui/screens.d.ts.map +1 -0
  71. package/dist/ui/theme.d.ts +58 -0
  72. package/dist/ui/theme.d.ts.map +1 -0
  73. package/package.json +19 -18
  74. package/src/commands/{api.tsx → api.ts} +18 -27
  75. package/src/commands/{app.tsx → app.ts} +14 -18
  76. package/src/commands/build.ts +70 -0
  77. package/src/commands/{check.tsx → check.ts} +27 -18
  78. package/src/commands/db-migrate.ts +32 -0
  79. package/src/commands/{deploy.tsx → deploy.ts} +109 -102
  80. package/src/commands/desktop.ts +71 -0
  81. package/src/commands/{dev-runner.tsx → dev-runner.ts} +67 -33
  82. package/src/commands/{init.tsx → init.ts} +19 -8
  83. package/src/commands/lake-check.ts +36 -0
  84. package/src/commands/{lake-cleanup.tsx → lake-cleanup.ts} +17 -12
  85. package/src/commands/{login.tsx → login.ts} +36 -30
  86. package/src/commands/{logout.tsx → logout.ts} +5 -5
  87. package/src/commands/{orchestrator.tsx → orchestrator.ts} +15 -28
  88. package/src/commands/{profile.tsx → profile.ts} +38 -19
  89. package/src/commands/{rules.tsx → rules.ts} +15 -24
  90. package/src/commands/{scheduler.tsx → scheduler.ts} +14 -27
  91. package/src/commands/{service-account.tsx → service-account.ts} +37 -39
  92. package/src/commands/{status.tsx → status.ts} +8 -8
  93. package/src/commands/{token.tsx → token.ts} +19 -20
  94. package/src/commands/typegen.ts +18 -0
  95. package/src/commands/{worker-group.tsx → worker-group.ts} +40 -29
  96. package/src/commands/{worker.tsx → worker.ts} +30 -22
  97. package/src/index.tsx +22 -1
  98. package/src/lib/command-line.ts +108 -57
  99. package/src/lib/deploy-release.ts +17 -1
  100. package/src/lib/desktop.ts +52 -0
  101. package/src/lib/dev-messages.ts +44 -0
  102. package/src/lib/dev-supervisor.ts +132 -12
  103. package/src/lib/open-url.ts +15 -0
  104. package/src/lib/role-lifecycle.ts +2 -3
  105. package/src/lib/runtime.ts +4 -0
  106. package/src/lib/worker-concurrency.ts +31 -0
  107. package/src/lib/worker-registry.ts +4 -0
  108. package/src/ui/README.md +54 -0
  109. package/src/ui/deploy-screens.ts +360 -0
  110. package/src/ui/index.ts +158 -0
  111. package/src/ui/layout.ts +245 -0
  112. package/src/ui/live/ansi.ts +103 -0
  113. package/src/ui/live/can-run-live.ts +9 -0
  114. package/src/ui/live/components.tsx +81 -0
  115. package/src/ui/live/deploy.tsx +170 -0
  116. package/src/ui/live/dev.tsx +522 -0
  117. package/src/ui/live/prompt.tsx +205 -0
  118. package/src/ui/live/session.ts +103 -0
  119. package/src/ui/screens.ts +490 -0
  120. package/src/ui/theme.ts +122 -0
  121. package/dist/lib/role-view.d.ts +0 -21
  122. package/dist/lib/role-view.d.ts.map +0 -1
  123. package/src/commands/build.tsx +0 -65
  124. package/src/commands/db-migrate.tsx +0 -30
  125. package/src/commands/lake-check.tsx +0 -33
  126. package/src/commands/typegen.tsx +0 -18
  127. package/src/lib/role-view.ts +0 -49
  128. package/src/ui/index.tsx +0 -1398
  129. /package/src/commands/{dev.tsx → dev.ts} +0 -0
@@ -0,0 +1,245 @@
1
+ import { glyph, paint, palette, tone } from "./theme"
2
+
3
+ /**
4
+ * Printed screens are strings: built here, written once, kept in the scrollback, and plain text
5
+ * when piped. Every screen sits two columns in and between blank lines.
6
+ */
7
+ const GUTTER = " "
8
+
9
+ export type Block = string | null | undefined | false
10
+
11
+ /** Joins blocks with a blank line between them, indented, with breathing room above and below. */
12
+ export function screen(...blocks: Block[]): string {
13
+ const body = blocks
14
+ .filter((block): block is string => typeof block === "string" && block.length > 0)
15
+ .join("\n\n")
16
+ return `\n${indent(body)}\n\n`
17
+ }
18
+
19
+ /** Joins blocks with no blank line between them. */
20
+ export function stack(...blocks: Block[]): string {
21
+ return blocks.filter((block): block is string => typeof block === "string").join("\n")
22
+ }
23
+
24
+ function indent(text: string, prefix = GUTTER): string {
25
+ return text
26
+ .split("\n")
27
+ .map((line) => (line.length > 0 ? `${prefix}${line}` : line))
28
+ .join("\n")
29
+ }
30
+
31
+ /**
32
+ * Columns the output can use, less the gutter. Piped output never wraps: a log line stays one
33
+ * line, so it still greps.
34
+ */
35
+ export function contentWidth(): number {
36
+ if (!process.stdout.isTTY) return Number.MAX_SAFE_INTEGER
37
+ return Math.max(40, Math.min(process.stdout.columns ?? 80, MAX_COLUMNS) - GUTTER.length * 2)
38
+ }
39
+
40
+ /** Past this, a wide terminal only spreads a screen out: the eye loses the end of the line. */
41
+ export const MAX_COLUMNS = 104
42
+
43
+ export function visibleWidth(text: string): number {
44
+ return Bun.stringWidth(text)
45
+ }
46
+
47
+ export function pad(text: string, width: number): string {
48
+ return text + " ".repeat(Math.max(0, width - visibleWidth(text)))
49
+ }
50
+
51
+ /** Breaks plain text into lines no wider than `width`, at spaces where it can. */
52
+ export function wrap(text: string, width: number): string[] {
53
+ const lines: string[] = []
54
+ for (const paragraph of text.split("\n")) {
55
+ let line = ""
56
+ for (const word of paragraph.split(" ")) {
57
+ if (line === "") {
58
+ line = word
59
+ } else if (visibleWidth(line) + 1 + visibleWidth(word) <= width) {
60
+ line += ` ${word}`
61
+ } else {
62
+ lines.push(line)
63
+ line = word
64
+ }
65
+ while (visibleWidth(line) > width) {
66
+ lines.push(line.slice(0, width))
67
+ line = line.slice(width)
68
+ }
69
+ }
70
+ lines.push(line)
71
+ }
72
+ return lines
73
+ }
74
+
75
+ /** Wraps plain text and styles each line, so color never spans a line break. */
76
+ export function paragraph(text: string, style: (line: string) => string = (line) => line): string {
77
+ return wrap(text, contentWidth()).map(style).join("\n")
78
+ }
79
+
80
+ let commandPath = ""
81
+
82
+ /** Names the command that is running, for screens that open with it. Set once, at startup. */
83
+ export function setCommandPath(path: readonly string[]): void {
84
+ commandPath = path.join(" ")
85
+ }
86
+
87
+ /**
88
+ * The line that opens every screen: the mark, then what ran, then where, then how it stands.
89
+ * Without a command, the one that is running.
90
+ *
91
+ * ▰ sixb check northline
92
+ * ▰ sixb worker-group northline ● running
93
+ */
94
+ export function header(command?: string, context?: string, state?: string): string {
95
+ const name = command ?? commandPath
96
+ const title = name ? `${tone.strong("sixb")} ${name}` : tone.strong("sixb")
97
+ return `${tone.blue(glyph.mark)} ${title}${context ? ` ${tone.muted(context)}` : ""}${state ? ` ${state}` : ""}`
98
+ }
99
+
100
+ export type Status = "ok" | "warn" | "fail" | "live" | "idle"
101
+
102
+ function statusGlyph(status: Status): string {
103
+ switch (status) {
104
+ case "ok":
105
+ return tone.ok(glyph.ok)
106
+ case "warn":
107
+ return tone.warn(glyph.warn)
108
+ case "fail":
109
+ return tone.fail(glyph.fail)
110
+ case "live":
111
+ return tone.blue(glyph.live)
112
+ case "idle":
113
+ return tone.faint(glyph.idle)
114
+ }
115
+ }
116
+
117
+ /** A result's headline: a status mark, a sentence, a quieter aside, and where it landed. */
118
+ export function headline(status: Status, text: string, detail?: string, target?: string): string {
119
+ const styled =
120
+ status === "fail" ? paint(text, { color: palette.red, bold: true }) : tone.strong(text)
121
+ return `${statusGlyph(status)} ${styled}${detail ? tone.muted(` ${glyph.dot} ${detail}`) : ""}${target ? tone.faint(` ${glyph.arrow} ${target}`) : ""}`
122
+ }
123
+
124
+ /** What to do about a problem, in prose: led by an arrow, never colored like a command. */
125
+ export function remedy(text: string): string {
126
+ return `${tone.faint(glyph.arrow)} ${text}`
127
+ }
128
+
129
+ export interface Row {
130
+ readonly label: string
131
+ readonly value: string
132
+ readonly status?: Status
133
+ /** A dimmer line under the value, such as a path. */
134
+ readonly note?: string
135
+ /** What to do about it, under the value, led by an arrow. */
136
+ readonly remedy?: string
137
+ }
138
+
139
+ /**
140
+ * Labels in a column and values beside them. A value wraps under itself, never under the label.
141
+ *
142
+ * ✓ Storage SqliteStorage · schema current
143
+ * Project 11 object types
144
+ */
145
+ export function rows(items: readonly Row[]): string {
146
+ const hasStatus = items.some((item) => item.status)
147
+ const labelWidth = Math.max(...items.map((item) => visibleWidth(item.label))) + 2
148
+ const lead = hasStatus ? 2 : 0
149
+ const valueWidth = Math.max(20, contentWidth() - lead - labelWidth)
150
+ const hanging = " ".repeat(lead + labelWidth)
151
+
152
+ return items
153
+ .map((item) => {
154
+ const mark = hasStatus ? `${item.status ? statusGlyph(item.status) : " "} ` : ""
155
+ const label = tone.muted(pad(item.label, labelWidth))
156
+ const valueLines = isUrl(item.value)
157
+ ? [tone.url(item.value)]
158
+ : wrap(item.value, valueWidth).map((line) =>
159
+ item.status === "fail" ? tone.fail(line) : line
160
+ )
161
+ const noteLines = item.note ? wrap(item.note, valueWidth).map(tone.muted) : []
162
+ const remedyLines = item.remedy
163
+ ? wrap(item.remedy, valueWidth - 2).map((line, index) =>
164
+ index === 0 ? remedy(line) : ` ${line}`
165
+ )
166
+ : []
167
+ const [first = "", ...rest] = [...valueLines, ...noteLines, ...remedyLines]
168
+ return [`${mark}${label}${first}`, ...rest.map((line) => `${hanging}${line}`)].join("\n")
169
+ })
170
+ .join("\n")
171
+ }
172
+
173
+ /** A quiet heading over a group of lines: the content carries the weight, not the label. */
174
+ export function section(title: string, body: string): string {
175
+ return `${tone.muted(title)}\n${body}`
176
+ }
177
+
178
+ /** Columns sized to their widest cell, the header in quiet type, no rules. */
179
+ export function table(headers: readonly string[], cells: readonly (readonly string[])[]): string {
180
+ const widths = headers.map((header, index) =>
181
+ Math.max(visibleWidth(header), ...cells.map((row) => visibleWidth(row[index] ?? "")))
182
+ )
183
+ const line = (values: readonly string[], style: (text: string, index: number) => string) =>
184
+ values
185
+ .map((value, index) =>
186
+ index === values.length - 1
187
+ ? style(value, index)
188
+ : style(pad(value, widths[index] ?? 0), index)
189
+ )
190
+ .join(" ")
191
+ .trimEnd()
192
+ return [
193
+ line(headers, (text) => tone.faint(text)),
194
+ ...cells.map((row) => line(row, (text) => text)),
195
+ ].join("\n")
196
+ }
197
+
198
+ /** Warnings, each wrapped under its own mark. */
199
+ export function warnings(items: readonly string[]): string | null {
200
+ if (items.length === 0) return null
201
+ return items.map((item) => marked(tone.warn(glyph.warn), item, tone.muted)).join("\n")
202
+ }
203
+
204
+ /** A line led by a one-cell mark, wrapped so the text keeps its column. */
205
+ export function marked(
206
+ mark: string,
207
+ text: string,
208
+ style: (line: string) => string = (line) => line
209
+ ): string {
210
+ const [first = "", ...rest] = wrap(text, contentWidth() - 2)
211
+ return [`${mark} ${style(first)}`, ...rest.map((line) => ` ${style(line)}`)].join("\n")
212
+ }
213
+
214
+ /** Command lines to run, as the next thing to do. */
215
+ export function commands(lines: readonly string[]): string {
216
+ return lines.map((line) => `${tone.faint("$")} ${tone.blue(line)}`).join("\n")
217
+ }
218
+
219
+ /** Verbatim output, such as a build log, set off by a rule. The last 40 lines are enough. */
220
+ export function quoted(lines: readonly string[]): string {
221
+ const shown = lines.slice(-40)
222
+ const width = contentWidth() - 2
223
+ const body = shown.flatMap((line) =>
224
+ wrap(Bun.stripANSI(line), width).map((part) => `${tone.faint(glyph.rule)} ${tone.muted(part)}`)
225
+ )
226
+ if (shown.length < lines.length) {
227
+ body.unshift(tone.faint(`${glyph.rule} … ${lines.length - shown.length} earlier lines`))
228
+ }
229
+ return body.join("\n")
230
+ }
231
+
232
+ /** Key hints at the foot of a long-running screen: `ctrl+c stop`. */
233
+ export function hints(items: readonly (readonly [string, string])[]): string {
234
+ return items
235
+ .map(([key, label]) => `${tone.strong(key)} ${tone.muted(label)}`)
236
+ .join(tone.faint(` ${glyph.dot} `))
237
+ }
238
+
239
+ function isUrl(value: string): boolean {
240
+ return /^(https?|wss?):\/\/\S+$/.test(value)
241
+ }
242
+
243
+ export function plural(count: number, one: string, many = `${one}s`): string {
244
+ return `${count} ${count === 1 ? one : many}`
245
+ }
@@ -0,0 +1,103 @@
1
+ import { createTextAttributes, RGBA, StyledText, type TextChunk } from "@opentui/core"
2
+
3
+ const NAMED = [
4
+ "#1c1f24",
5
+ "#ff453a",
6
+ "#30d158",
7
+ "#ff9f0a",
8
+ "#0a84ff",
9
+ "#bf5af2",
10
+ "#64d2ff",
11
+ "#d7dae0",
12
+ "#8b919a",
13
+ "#ff6961",
14
+ "#4be07a",
15
+ "#ffb340",
16
+ "#409cff",
17
+ "#da8fff",
18
+ "#8ae0ff",
19
+ "#ffffff",
20
+ ]
21
+
22
+ function cubeColor(index: number): string {
23
+ if (index < 16) return NAMED[index] ?? "#ffffff"
24
+ if (index < 232) {
25
+ const steps = [0, 95, 135, 175, 215, 255]
26
+ const value = index - 16
27
+ const channel = (n: number) => (steps[n] ?? 0).toString(16).padStart(2, "0")
28
+ return `#${channel(Math.floor(value / 36))}${channel(Math.floor(value / 6) % 6)}${channel(value % 6)}`
29
+ }
30
+ const level = (8 + (index - 232) * 10).toString(16).padStart(2, "0")
31
+ return `#${level}${level}${level}`
32
+ }
33
+
34
+ interface Style {
35
+ fg?: string
36
+ bold?: boolean
37
+ dim?: boolean
38
+ underline?: boolean
39
+ }
40
+
41
+ /**
42
+ * Turns text styled with SGR escapes (what the printed screens and a child process write) into
43
+ * OpenTUI styled text, so it keeps its colors in the scrollback above a live footer. Every other
44
+ * escape is dropped: OpenTUI would show it as text.
45
+ */
46
+ export function ansiToStyledText(text: string, defaultFg: string): StyledText {
47
+ const chunks: TextChunk[] = []
48
+ let style: Style = {}
49
+ const push = (value: string) => {
50
+ if (!value) return
51
+ chunks.push({
52
+ __isChunk: true,
53
+ text: value,
54
+ fg: RGBA.fromHex(style.fg ?? defaultFg),
55
+ attributes: createTextAttributes({
56
+ bold: style.bold ?? false,
57
+ dim: style.dim ?? false,
58
+ underline: style.underline ?? false,
59
+ }),
60
+ })
61
+ }
62
+
63
+ const pattern =
64
+ /\x1b\[([0-9;:]*)m|\x1b\[[0-9;?]*[A-Za-z]|\x1b\][^\x07\x1b]*(?:\x07|\x1b\\)|([^\x1b]+)/g
65
+ for (const match of text.matchAll(pattern)) {
66
+ if (match[2] !== undefined) {
67
+ push(match[2])
68
+ } else if (match[1] !== undefined) {
69
+ style = applySgr(style, match[1])
70
+ }
71
+ }
72
+ return new StyledText(chunks)
73
+ }
74
+
75
+ function applySgr(current: Style, sequence: string): Style {
76
+ const params = sequence === "" ? [0] : sequence.split(/[;:]/).map((part) => Number(part) || 0)
77
+ let style = { ...current }
78
+ for (let index = 0; index < params.length; index++) {
79
+ const code = params[index] ?? 0
80
+ if (code === 0) style = {}
81
+ else if (code === 1) style.bold = true
82
+ else if (code === 2) style.dim = true
83
+ else if (code === 4) style.underline = true
84
+ else if (code === 22) {
85
+ style.bold = false
86
+ style.dim = false
87
+ } else if (code === 24) style.underline = false
88
+ else if (code >= 30 && code <= 37) style.fg = NAMED[code - 30]
89
+ else if (code >= 90 && code <= 97) style.fg = NAMED[code - 90 + 8]
90
+ else if (code === 39) delete style.fg
91
+ else if (code === 38 && params[index + 1] === 5) {
92
+ style.fg = cubeColor(params[index + 2] ?? 0)
93
+ index += 2
94
+ } else if (code === 38 && params[index + 1] === 2) {
95
+ const hex = (n: number | undefined) => (n ?? 0).toString(16).padStart(2, "0")
96
+ style.fg = `#${hex(params[index + 2])}${hex(params[index + 3])}${hex(params[index + 4])}`
97
+ index += 4
98
+ } else if (code === 48 && (params[index + 1] === 5 || params[index + 1] === 2)) {
99
+ index += params[index + 1] === 5 ? 2 : 4
100
+ }
101
+ }
102
+ return style
103
+ }
@@ -0,0 +1,9 @@
1
+ /**
2
+ * A live screen needs both ends of a terminal; a pipe, a CI log, or a dumb terminal gets lines.
3
+ * Kept apart from `./session` so asking does not load the renderer.
4
+ */
5
+ export function canRunLive(): boolean {
6
+ return Boolean(
7
+ process.stdin.isTTY && process.stdout.isTTY && !process.env.CI && process.env.TERM !== "dumb"
8
+ )
9
+ }
@@ -0,0 +1,81 @@
1
+ /** @jsxImportSource @opentui/react */
2
+ import { createTextAttributes } from "@opentui/core"
3
+ import { useEffect, useState } from "react"
4
+ import { glyph, SPINNER_FRAMES } from "../theme"
5
+ import type { LiveColors } from "./session"
6
+
7
+ export const BOLD = createTextAttributes({ bold: true })
8
+
9
+ export function useSpinner(active = true): string {
10
+ const [frame, setFrame] = useState(0)
11
+ useEffect(() => {
12
+ if (!active) return
13
+ const timer = setInterval(() => setFrame((value) => value + 1), 80)
14
+ return () => clearInterval(timer)
15
+ }, [active])
16
+ return SPINNER_FRAMES[frame % SPINNER_FRAMES.length] ?? glyph.mark
17
+ }
18
+
19
+ /** Re-renders every `ms`, for clocks such as an uptime. */
20
+ export function useTick(ms: number): number {
21
+ const [now, setNow] = useState(Date.now())
22
+ useEffect(() => {
23
+ const timer = setInterval(() => setNow(Date.now()), ms)
24
+ return () => clearInterval(timer)
25
+ }, [ms])
26
+ return now
27
+ }
28
+
29
+ /** The mosaic as progress: tiles fill palest blue first. */
30
+ export function Tiles({
31
+ colors,
32
+ filled,
33
+ total,
34
+ }: {
35
+ readonly colors: LiveColors
36
+ readonly filled: number
37
+ readonly total: number
38
+ }) {
39
+ return (
40
+ <>
41
+ {Array.from({ length: total }, (_, index) => {
42
+ const step = Math.min(
43
+ colors.water.length - 1,
44
+ Math.floor((index / Math.max(1, total - 1)) * (colors.water.length - 1))
45
+ )
46
+ return index < filled ? (
47
+ <span key={index} fg={colors.water[step]}>
48
+ {glyph.mark}
49
+ </span>
50
+ ) : (
51
+ <span key={index} fg={colors.faint}>
52
+ {glyph.tile}
53
+ </span>
54
+ )
55
+ })}
56
+ </>
57
+ )
58
+ }
59
+
60
+ /** Key hints: `o open app · r restart · q quit`. */
61
+ export function Hints({
62
+ colors,
63
+ items,
64
+ }: {
65
+ readonly colors: LiveColors
66
+ readonly items: readonly (readonly [string, string])[]
67
+ }) {
68
+ return (
69
+ <text>
70
+ {items.map(([key, label], index) => (
71
+ <span key={key}>
72
+ {index > 0 ? <span fg={colors.faint}>{` ${glyph.dot} `}</span> : null}
73
+ <span fg={colors.text} attributes={BOLD}>
74
+ {key}
75
+ </span>
76
+ <span fg={colors.muted}>{` ${label}`}</span>
77
+ </span>
78
+ ))}
79
+ </text>
80
+ )
81
+ }
@@ -0,0 +1,170 @@
1
+ /** @jsxImportSource @opentui/react */
2
+ import type { KeyEvent } from "@opentui/core"
3
+ import { useKeyboard, useTerminalDimensions } from "@opentui/react"
4
+ import { useSyncExternalStore } from "react"
5
+ import { header } from "../layout"
6
+ import { glyph, tone } from "../theme"
7
+ import { BOLD, Tiles, useSpinner, useTick } from "./components"
8
+ import { type LiveColors, startFooter } from "./session"
9
+
10
+ export interface DeployProgressHeader {
11
+ readonly name: string
12
+ readonly location: string
13
+ readonly commit: string
14
+ readonly ref: string
15
+ readonly dirty: boolean
16
+ }
17
+
18
+ interface ProgressState {
19
+ readonly labels: readonly string[]
20
+ readonly done: number
21
+ readonly current: number | null
22
+ readonly startedAt: number
23
+ /** When the deploy itself began, for its running total. */
24
+ readonly deployStartedAt: number
25
+ readonly output: readonly string[]
26
+ }
27
+
28
+ export interface LiveDeployProgress {
29
+ steps(labels: readonly string[]): void
30
+ running(index: number): void
31
+ done(index: number, seconds: string): void
32
+ output(line: string): void
33
+ /** Marks the step that failed, then leaves `report` (the error screen) in the scrollback. */
34
+ failed(index: number | null, seconds: string | null, report: string): Promise<void>
35
+ finished(report: string): Promise<void>
36
+ }
37
+
38
+ const OUTPUT_ROWS = 6
39
+
40
+ /**
41
+ * A deploy in a terminal. Finished steps land in the scrollback as they finish; the footer holds
42
+ * the step running now, the mosaic filling toward done, and that step's latest output.
43
+ */
44
+ export async function startLiveDeploy(release: DeployProgressHeader): Promise<LiveDeployProgress> {
45
+ const footer = await startFooter({ rows: OUTPUT_ROWS + 2 })
46
+ let state: ProgressState = {
47
+ labels: [],
48
+ done: 0,
49
+ current: null,
50
+ startedAt: Date.now(),
51
+ deployStartedAt: Date.now(),
52
+ output: [],
53
+ }
54
+ const listeners = new Set<() => void>()
55
+ const set = (next: Partial<ProgressState>) => {
56
+ state = { ...state, ...next }
57
+ for (const listener of listeners) listener()
58
+ }
59
+ const store = {
60
+ subscribe(listener: () => void) {
61
+ listeners.add(listener)
62
+ return () => listeners.delete(listener)
63
+ },
64
+ get: () => state,
65
+ }
66
+
67
+ footer.print(
68
+ [
69
+ "",
70
+ ` ${header("deploy", `${release.name} ${glyph.arrow} ${release.location}`)}`,
71
+ ` ${tone.muted(`${release.commit.slice(0, 12)} ${glyph.dot} ${release.ref}`)}`,
72
+ ...(release.dirty
73
+ ? [` ${tone.warn(`${glyph.warn} Uncommitted changes are not deployed.`)}`]
74
+ : []),
75
+ // A space, not "": the scrollback drops trailing empty lines.
76
+ " ",
77
+ ].join("\n")
78
+ )
79
+ // Raw mode turns ctrl+c into a key. It still ends the command, once the terminal is given back.
80
+ const interrupt = () => void footer.stop().then(() => process.exit(130))
81
+ footer.render(<DeployFooter store={store} colors={footer.colors} onInterrupt={interrupt} />)
82
+
83
+ return {
84
+ steps(labels) {
85
+ set({ labels: [...labels] })
86
+ },
87
+ running(index) {
88
+ set({ current: index, startedAt: Date.now(), output: [] })
89
+ },
90
+ done(index, seconds) {
91
+ // Steps line up, and so do their times, right-aligned in a column.
92
+ footer.print(` ${tone.ok(glyph.ok)} ${stepLine(state.labels, index, seconds)}`)
93
+ set({ done: index + 1 })
94
+ },
95
+ output(line) {
96
+ set({ output: [...state.output, line].slice(-OUTPUT_ROWS) })
97
+ },
98
+ async failed(index, seconds, report) {
99
+ if (index !== null) {
100
+ footer.print(
101
+ ` ${tone.fail(glyph.fail)} ${stepLine(state.labels, index, seconds, tone.fail)}`
102
+ )
103
+ }
104
+ footer.print(report)
105
+ await footer.stop()
106
+ },
107
+ async finished(report) {
108
+ footer.print(report)
109
+ await footer.stop()
110
+ },
111
+ }
112
+ }
113
+
114
+ /** A finished step: its label padded to the longest one, then its time, right-aligned. */
115
+ function stepLine(
116
+ labels: readonly string[],
117
+ index: number,
118
+ seconds: string | null,
119
+ style: (text: string) => string = (text) => text
120
+ ): string {
121
+ const width = Math.max(0, ...labels.map((label) => label.length)) + 2
122
+ const label = labels[index] ?? ""
123
+ return `${style(label.padEnd(width))}${seconds ? tone.faint(`${seconds}s`.padStart(6)) : ""}`
124
+ }
125
+
126
+ function DeployFooter({
127
+ store,
128
+ colors,
129
+ onInterrupt,
130
+ }: {
131
+ readonly store: { subscribe(listener: () => void): () => void; get(): ProgressState }
132
+ readonly colors: LiveColors
133
+ readonly onInterrupt: () => void
134
+ }) {
135
+ const state = useSyncExternalStore(store.subscribe, store.get)
136
+ const spinner = useSpinner()
137
+ const now = useTick(100)
138
+ const { width } = useTerminalDimensions()
139
+
140
+ useKeyboard((key: KeyEvent) => {
141
+ if (key.ctrl && key.name === "c") onInterrupt()
142
+ })
143
+
144
+ const label = state.current === null ? "Connecting" : (state.labels[state.current] ?? "")
145
+ const elapsed = ((now - state.startedAt) / 1000).toFixed(1)
146
+ const total = ((now - state.deployStartedAt) / 1000).toFixed(1)
147
+ const lineWidth = Math.max(10, width - 8)
148
+ return (
149
+ <box flexDirection="column" paddingLeft={2}>
150
+ <text>
151
+ <Tiles colors={colors} filled={state.done} total={Math.max(1, state.labels.length)} />
152
+ <span fg={colors.muted}>{` ${state.done}/${state.labels.length || "?"} `}</span>
153
+ <span fg={colors.blue}>{spinner}</span>
154
+ <span fg={colors.text} attributes={BOLD}>{` ${label}`}</span>
155
+ <span fg={colors.faint}>{` ${elapsed}s`}</span>
156
+ <span fg={colors.faint}>{` ${glyph.dot} ${total}s total`}</span>
157
+ </text>
158
+ <box flexDirection="column" paddingLeft={2} paddingTop={1} height={OUTPUT_ROWS}>
159
+ {state.output.map((line, index) => (
160
+ <text key={`${index}:${line}`}>
161
+ <span fg={colors.faint}>{`${glyph.rule} `}</span>
162
+ <span fg={colors.muted}>
163
+ {line.length > lineWidth ? `${line.slice(0, lineWidth - 1)}…` : line}
164
+ </span>
165
+ </text>
166
+ ))}
167
+ </box>
168
+ </box>
169
+ )
170
+ }