@sixb/cli 0.1.25 → 0.1.26
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.
- package/README.md +5 -5
- package/dist/commands/agent.d.ts +16 -0
- package/dist/commands/agent.d.ts.map +1 -0
- package/dist/commands/dev-runner.d.ts +0 -1
- package/dist/commands/dev-runner.d.ts.map +1 -1
- package/dist/commands/worker-group.d.ts +0 -1
- package/dist/commands/worker-group.d.ts.map +1 -1
- package/dist/commands/worker.d.ts +0 -1
- package/dist/commands/worker.d.ts.map +1 -1
- package/dist/lib/command-line.d.ts +16 -6
- package/dist/lib/command-line.d.ts.map +1 -1
- package/dist/lib/deploy-release.d.ts.map +1 -1
- package/dist/lib/github-ci.d.ts.map +1 -1
- package/dist/lib/runtime.d.ts +0 -1
- package/dist/lib/runtime.d.ts.map +1 -1
- package/dist/lib/worker-registry.d.ts +0 -1
- package/dist/lib/worker-registry.d.ts.map +1 -1
- package/dist/tui/app.d.ts +19 -0
- package/dist/tui/app.d.ts.map +1 -0
- package/dist/tui/brand.d.ts +7 -0
- package/dist/tui/brand.d.ts.map +1 -0
- package/dist/tui/computer.d.ts +3 -0
- package/dist/tui/computer.d.ts.map +1 -0
- package/dist/tui/connection.d.ts +45 -0
- package/dist/tui/connection.d.ts.map +1 -0
- package/dist/tui/folders.d.ts +7 -0
- package/dist/tui/folders.d.ts.map +1 -0
- package/dist/tui/format.d.ts +33 -0
- package/dist/tui/format.d.ts.map +1 -0
- package/dist/tui/index.d.ts +12 -0
- package/dist/tui/index.d.ts.map +1 -0
- package/dist/tui/palette.d.ts +25 -0
- package/dist/tui/palette.d.ts.map +1 -0
- package/dist/tui/parts.d.ts +66 -0
- package/dist/tui/parts.d.ts.map +1 -0
- package/dist/tui/runner.d.ts +52 -0
- package/dist/tui/runner.d.ts.map +1 -0
- package/dist/tui/settings.d.ts +44 -0
- package/dist/tui/settings.d.ts.map +1 -0
- package/dist/tui/slash.d.ts +14 -0
- package/dist/tui/slash.d.ts.map +1 -0
- package/dist/tui/theme.d.ts +39 -0
- package/dist/tui/theme.d.ts.map +1 -0
- package/dist/tui/transcript.d.ts +52 -0
- package/dist/tui/transcript.d.ts.map +1 -0
- package/dist/tui/ui.d.ts +35 -0
- package/dist/tui/ui.d.ts.map +1 -0
- package/package.json +17 -15
- package/src/commands/agent.ts +337 -0
- package/src/commands/dev-runner.ts +0 -4
- package/src/commands/worker-group.ts +0 -4
- package/src/commands/worker.ts +0 -4
- package/src/index.tsx +57 -5
- package/src/lib/command-line.ts +52 -15
- package/src/lib/deploy-release.ts +0 -11
- package/src/lib/github-ci.ts +3 -1
- package/src/lib/runtime.ts +0 -2
- package/src/lib/worker-registry.ts +0 -2
- package/src/tui/app.tsx +1198 -0
- package/src/tui/brand.tsx +51 -0
- package/src/tui/computer.ts +54 -0
- package/src/tui/connection.ts +149 -0
- package/src/tui/folders.ts +41 -0
- package/src/tui/format.ts +260 -0
- package/src/tui/index.tsx +353 -0
- package/src/tui/palette.tsx +183 -0
- package/src/tui/parts.ts +254 -0
- package/src/tui/runner.ts +173 -0
- package/src/tui/settings.ts +127 -0
- package/src/tui/slash.ts +32 -0
- package/src/tui/theme.ts +111 -0
- package/src/tui/transcript.tsx +555 -0
- package/src/tui/ui.tsx +148 -0
- package/dist/lib/agent-turn-timeout.d.ts +0 -8
- package/dist/lib/agent-turn-timeout.d.ts.map +0 -1
- package/src/lib/agent-turn-timeout.ts +0 -46
|
@@ -0,0 +1,353 @@
|
|
|
1
|
+
/** @jsxImportSource @opentui/react */
|
|
2
|
+
import { appendFileSync } from "node:fs"
|
|
3
|
+
import { tmpdir } from "node:os"
|
|
4
|
+
import { join } from "node:path"
|
|
5
|
+
import { format } from "node:util"
|
|
6
|
+
import { createCliRenderer, type KeyEvent, RGBA, SyntaxStyle } from "@opentui/core"
|
|
7
|
+
import { createRoot, useKeyboard } from "@opentui/react"
|
|
8
|
+
import { useCallback, useEffect, useState } from "react"
|
|
9
|
+
import { Workspace } from "./app"
|
|
10
|
+
import { Logo } from "./brand"
|
|
11
|
+
import { thisComputerId } from "./computer"
|
|
12
|
+
import {
|
|
13
|
+
type Connection,
|
|
14
|
+
connect,
|
|
15
|
+
type PendingSignIn,
|
|
16
|
+
type SignedOut,
|
|
17
|
+
startSignIn,
|
|
18
|
+
} from "./connection"
|
|
19
|
+
import { LocalRunner, stopOnHangUp } from "./runner"
|
|
20
|
+
import { loadSettings, type Settings } from "./settings"
|
|
21
|
+
import { type TuiColors, tuiColors } from "./theme"
|
|
22
|
+
import { BOLD, Composer, useSpinner } from "./ui"
|
|
23
|
+
|
|
24
|
+
/** How long quitting waits for a turn running here to record its end. */
|
|
25
|
+
const STOP_WAIT_MS = 10_000
|
|
26
|
+
|
|
27
|
+
/** Where the app writes what would otherwise draw over it: its runner's notes and errors. */
|
|
28
|
+
export const TUI_LOG = join(tmpdir(), "sixb-tui.log")
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* `sixb` with no command: the full-screen app. It connects as the CLI's current profile and opens
|
|
32
|
+
* on a new conversation that runs in the folder it was started in.
|
|
33
|
+
*/
|
|
34
|
+
export async function runTui(options: {
|
|
35
|
+
readonly version: string
|
|
36
|
+
readonly apiUrl?: string
|
|
37
|
+
readonly profile?: string
|
|
38
|
+
}): Promise<void> {
|
|
39
|
+
// Read before the screen is taken, so that a failure reads as any command's.
|
|
40
|
+
const computerId = await thisComputerId()
|
|
41
|
+
const restoreConsole = captureConsole()
|
|
42
|
+
const renderer = await createCliRenderer({
|
|
43
|
+
screenMode: "alternate-screen",
|
|
44
|
+
exitOnCtrlC: false,
|
|
45
|
+
useMouse: true,
|
|
46
|
+
targetFps: 60,
|
|
47
|
+
})
|
|
48
|
+
// The terminal says whether it is light or dark; SIXB_THEME decides where it can't.
|
|
49
|
+
const forced = process.env.SIXB_THEME
|
|
50
|
+
const mode =
|
|
51
|
+
forced === "light" || forced === "dark"
|
|
52
|
+
? forced
|
|
53
|
+
: await renderer.waitForThemeMode(150).catch(() => null)
|
|
54
|
+
// The terminal's own colors, where it says them: the app paints over neither.
|
|
55
|
+
const reported = forced
|
|
56
|
+
? null
|
|
57
|
+
: await renderer
|
|
58
|
+
.getPalette({ timeout: 200, size: 16 })
|
|
59
|
+
.then(({ defaultBackground, defaultForeground }) =>
|
|
60
|
+
defaultBackground && defaultForeground
|
|
61
|
+
? { background: defaultBackground, foreground: defaultForeground }
|
|
62
|
+
: null
|
|
63
|
+
)
|
|
64
|
+
.catch(() => null)
|
|
65
|
+
const colors = tuiColors(mode, reported)
|
|
66
|
+
renderer.setBackgroundColor("transparent")
|
|
67
|
+
const syntax = createSyntax(colors, renderer)
|
|
68
|
+
const root = createRoot(renderer)
|
|
69
|
+
const settings = await loadSettings()
|
|
70
|
+
const folder = process.cwd()
|
|
71
|
+
let runner: LocalRunner | null = null
|
|
72
|
+
// The renderer gives its screen back on these signals; sixb then ends its turns and exits.
|
|
73
|
+
const unhook = stopOnHangUp(
|
|
74
|
+
() => runner,
|
|
75
|
+
(signal) => process.exit(signal === "SIGHUP" ? 129 : 143)
|
|
76
|
+
)
|
|
77
|
+
|
|
78
|
+
try {
|
|
79
|
+
await new Promise<void>((resolve) => {
|
|
80
|
+
root.render(
|
|
81
|
+
<App
|
|
82
|
+
colors={colors}
|
|
83
|
+
syntax={syntax}
|
|
84
|
+
version={options.version}
|
|
85
|
+
settings={settings}
|
|
86
|
+
computerId={computerId}
|
|
87
|
+
connectWith={(apiUrl) => connect(apiUrl ? { ...options, apiUrl } : options)}
|
|
88
|
+
runnerFor={(connection) => {
|
|
89
|
+
runner ??= new LocalRunner(connection, folder, (error) => console.error(error))
|
|
90
|
+
return runner
|
|
91
|
+
}}
|
|
92
|
+
onExit={resolve}
|
|
93
|
+
/>
|
|
94
|
+
)
|
|
95
|
+
})
|
|
96
|
+
} finally {
|
|
97
|
+
unhook()
|
|
98
|
+
root.unmount()
|
|
99
|
+
renderer.destroy()
|
|
100
|
+
await renderer.closed
|
|
101
|
+
syntax.destroy()
|
|
102
|
+
restoreConsole()
|
|
103
|
+
// A turn running here ends interrupted, its end recorded, once the terminal is back; an API
|
|
104
|
+
// that stops answering gets a bounded wait, and ctrl+c ends it.
|
|
105
|
+
const running = runner as LocalRunner | null
|
|
106
|
+
if (running?.busy) {
|
|
107
|
+
process.stderr.write(" Stopping the turn running here…\n")
|
|
108
|
+
process.once("SIGINT", () => process.exit(130))
|
|
109
|
+
await Promise.race([running.stop(), Bun.sleep(STOP_WAIT_MS)])
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
function App(props: {
|
|
115
|
+
readonly colors: TuiColors
|
|
116
|
+
readonly syntax: SyntaxStyle
|
|
117
|
+
readonly version: string
|
|
118
|
+
readonly settings: Settings
|
|
119
|
+
readonly computerId: string
|
|
120
|
+
readonly connectWith: (apiUrl?: string) => Promise<Connection | SignedOut>
|
|
121
|
+
readonly runnerFor: (connection: Connection) => LocalRunner
|
|
122
|
+
readonly onExit: () => void
|
|
123
|
+
}) {
|
|
124
|
+
const [state, setState] = useState<Connection | SignedOut | null>(null)
|
|
125
|
+
const { connectWith } = props
|
|
126
|
+
const reconnect = useCallback(
|
|
127
|
+
(apiUrl?: string) => {
|
|
128
|
+
setState(null)
|
|
129
|
+
void connectWith(apiUrl).then(setState)
|
|
130
|
+
},
|
|
131
|
+
[connectWith]
|
|
132
|
+
)
|
|
133
|
+
useEffect(() => reconnect(), [reconnect])
|
|
134
|
+
|
|
135
|
+
if (state === null) return <Splash colors={props.colors} onExit={props.onExit} />
|
|
136
|
+
if ("reason" in state) {
|
|
137
|
+
return (
|
|
138
|
+
<Connect
|
|
139
|
+
colors={props.colors}
|
|
140
|
+
signedOut={state}
|
|
141
|
+
onConnect={reconnect}
|
|
142
|
+
onExit={props.onExit}
|
|
143
|
+
/>
|
|
144
|
+
)
|
|
145
|
+
}
|
|
146
|
+
return (
|
|
147
|
+
<Workspace
|
|
148
|
+
connection={state}
|
|
149
|
+
runner={props.runnerFor(state)}
|
|
150
|
+
computerId={props.computerId}
|
|
151
|
+
colors={props.colors}
|
|
152
|
+
syntax={props.syntax}
|
|
153
|
+
version={props.version}
|
|
154
|
+
initialSettings={props.settings}
|
|
155
|
+
onExit={props.onExit}
|
|
156
|
+
/>
|
|
157
|
+
)
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
function Splash({ colors, onExit }: { readonly colors: TuiColors; readonly onExit: () => void }) {
|
|
161
|
+
const spinner = useSpinner(true)
|
|
162
|
+
useKeyboard((key: KeyEvent) => {
|
|
163
|
+
if (key.ctrl && key.name === "c") onExit()
|
|
164
|
+
})
|
|
165
|
+
return (
|
|
166
|
+
<box
|
|
167
|
+
flexGrow={1}
|
|
168
|
+
alignItems="center"
|
|
169
|
+
justifyContent="center"
|
|
170
|
+
flexDirection="column"
|
|
171
|
+
backgroundColor={colors.background}
|
|
172
|
+
>
|
|
173
|
+
<Logo colors={colors} />
|
|
174
|
+
<box height={2} />
|
|
175
|
+
<text>
|
|
176
|
+
<span fg={colors.blue}>{`${spinner} `}</span>
|
|
177
|
+
<span fg={colors.muted}>Connecting</span>
|
|
178
|
+
</text>
|
|
179
|
+
</box>
|
|
180
|
+
)
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/** Signed out, or the project is unreachable: sign in with the browser, as `sixb login` does. */
|
|
184
|
+
function Connect({
|
|
185
|
+
colors,
|
|
186
|
+
signedOut,
|
|
187
|
+
onConnect,
|
|
188
|
+
onExit,
|
|
189
|
+
}: {
|
|
190
|
+
readonly colors: TuiColors
|
|
191
|
+
readonly signedOut: SignedOut
|
|
192
|
+
/** Connect again, to `apiUrl` when the person gave another address. */
|
|
193
|
+
readonly onConnect: (apiUrl?: string) => void
|
|
194
|
+
readonly onExit: () => void
|
|
195
|
+
}) {
|
|
196
|
+
const [pending, setPending] = useState<PendingSignIn | null>(null)
|
|
197
|
+
const [error, setError] = useState<string | null>(null)
|
|
198
|
+
const spinner = useSpinner(pending !== null)
|
|
199
|
+
const unreachable = signedOut.reason === "unreachable"
|
|
200
|
+
|
|
201
|
+
const signIn = async (): Promise<void> => {
|
|
202
|
+
setError(null)
|
|
203
|
+
try {
|
|
204
|
+
const started = await startSignIn(signedOut.apiUrl)
|
|
205
|
+
setPending(started)
|
|
206
|
+
await Bun.$`open ${started.url}`.quiet().nothrow()
|
|
207
|
+
await started.complete(AbortSignal.timeout(10 * 60_000))
|
|
208
|
+
onConnect()
|
|
209
|
+
} catch (caught) {
|
|
210
|
+
setPending(null)
|
|
211
|
+
setError(caught instanceof Error ? caught.message : String(caught))
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
useKeyboard((key: KeyEvent) => {
|
|
216
|
+
if (key.ctrl && key.name === "c") return onExit()
|
|
217
|
+
if (key.name === "escape" && pending) return setPending(null)
|
|
218
|
+
if (key.name === "return" && !unreachable && !pending) void signIn()
|
|
219
|
+
})
|
|
220
|
+
|
|
221
|
+
const headline = unreachable
|
|
222
|
+
? `Sixb isn't answering at ${signedOut.apiUrl}`
|
|
223
|
+
: signedOut.reason === "session-ended"
|
|
224
|
+
? "Your sign-in has ended"
|
|
225
|
+
: `Sign in to ${new URL(signedOut.apiUrl).host}`
|
|
226
|
+
return (
|
|
227
|
+
<box
|
|
228
|
+
flexGrow={1}
|
|
229
|
+
alignItems="center"
|
|
230
|
+
justifyContent="center"
|
|
231
|
+
flexDirection="column"
|
|
232
|
+
backgroundColor={colors.background}
|
|
233
|
+
>
|
|
234
|
+
<Logo colors={colors} />
|
|
235
|
+
<box height={2} />
|
|
236
|
+
<text fg={colors.text} attributes={BOLD}>
|
|
237
|
+
{headline}
|
|
238
|
+
</text>
|
|
239
|
+
<box height={1} />
|
|
240
|
+
<box width={64} flexDirection="column" alignItems="center">
|
|
241
|
+
{pending ? (
|
|
242
|
+
<>
|
|
243
|
+
<text>
|
|
244
|
+
<span fg={colors.muted}>Confirm this code in your browser </span>
|
|
245
|
+
<span fg={colors.blue} attributes={BOLD}>
|
|
246
|
+
{pending.userCode}
|
|
247
|
+
</span>
|
|
248
|
+
</text>
|
|
249
|
+
<box height={1} />
|
|
250
|
+
<text>
|
|
251
|
+
<span fg={colors.blue}>{`${spinner} `}</span>
|
|
252
|
+
<span fg={colors.muted}>Waiting for you to approve it</span>
|
|
253
|
+
<span fg={colors.faint}>{" esc cancel"}</span>
|
|
254
|
+
</text>
|
|
255
|
+
</>
|
|
256
|
+
) : unreachable ? (
|
|
257
|
+
<>
|
|
258
|
+
<text fg={colors.muted}>Start it, or connect to another address.</text>
|
|
259
|
+
<box height={1} />
|
|
260
|
+
<box width={64}>
|
|
261
|
+
<Composer
|
|
262
|
+
colors={colors}
|
|
263
|
+
focused
|
|
264
|
+
allowEmpty
|
|
265
|
+
placeholder={signedOut.apiUrl}
|
|
266
|
+
status={[{ text: "Enter to connect", color: colors.muted }]}
|
|
267
|
+
onSubmit={(text) => {
|
|
268
|
+
onConnect(text || signedOut.apiUrl)
|
|
269
|
+
return true
|
|
270
|
+
}}
|
|
271
|
+
/>
|
|
272
|
+
</box>
|
|
273
|
+
</>
|
|
274
|
+
) : (
|
|
275
|
+
<text>
|
|
276
|
+
<span fg={colors.text}>enter</span>
|
|
277
|
+
<span fg={colors.muted}> sign in with your browser </span>
|
|
278
|
+
<span fg={colors.text}>ctrl+c</span>
|
|
279
|
+
<span fg={colors.muted}> quit</span>
|
|
280
|
+
</text>
|
|
281
|
+
)}
|
|
282
|
+
{error ? (
|
|
283
|
+
<box paddingTop={1}>
|
|
284
|
+
<text fg={colors.red}>{error}</text>
|
|
285
|
+
</box>
|
|
286
|
+
) : unreachable && signedOut.message ? (
|
|
287
|
+
<box paddingTop={1}>
|
|
288
|
+
<text fg={colors.faint}>{signedOut.message}</text>
|
|
289
|
+
</box>
|
|
290
|
+
) : null}
|
|
291
|
+
</box>
|
|
292
|
+
</box>
|
|
293
|
+
)
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
/** Markdown and code in the app's colors. */
|
|
297
|
+
function createSyntax(
|
|
298
|
+
colors: TuiColors,
|
|
299
|
+
renderer: { readonly nativeScene?: unknown }
|
|
300
|
+
): SyntaxStyle {
|
|
301
|
+
const hex = (value: string) => RGBA.fromHex(value)
|
|
302
|
+
return SyntaxStyle.fromStyles(
|
|
303
|
+
{
|
|
304
|
+
default: { fg: hex(colors.text) },
|
|
305
|
+
"markup.heading": { fg: hex(colors.text), bold: true },
|
|
306
|
+
"markup.strong": { fg: hex(colors.text), bold: true },
|
|
307
|
+
"markup.italic": { fg: hex(colors.text), italic: true },
|
|
308
|
+
"markup.strikethrough": { fg: hex(colors.muted) },
|
|
309
|
+
"markup.quote": { fg: hex(colors.muted), italic: true },
|
|
310
|
+
"markup.list": { fg: hex(colors.blue) },
|
|
311
|
+
"markup.link": { fg: hex(colors.blue), underline: true },
|
|
312
|
+
"markup.link.label": { fg: hex(colors.blue) },
|
|
313
|
+
"markup.link.url": { fg: hex(colors.faint) },
|
|
314
|
+
"markup.raw": { fg: hex(colors.code) },
|
|
315
|
+
"markup.raw.block": { fg: hex(colors.text) },
|
|
316
|
+
keyword: { fg: hex(colors.blue) },
|
|
317
|
+
string: { fg: hex(colors.green) },
|
|
318
|
+
number: { fg: hex(colors.amber) },
|
|
319
|
+
comment: { fg: hex(colors.faint), italic: true },
|
|
320
|
+
function: { fg: hex(colors.water[0] ?? colors.blue) },
|
|
321
|
+
type: { fg: hex(colors.water[1] ?? colors.blue) },
|
|
322
|
+
punctuation: { fg: hex(colors.muted) },
|
|
323
|
+
},
|
|
324
|
+
renderer.nativeScene as Parameters<typeof SyntaxStyle.fromStyles>[1]
|
|
325
|
+
)
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
/** While the app owns the screen, what code logs goes to a file instead of over it. */
|
|
329
|
+
function captureConsole(): () => void {
|
|
330
|
+
const methods = ["log", "info", "warn", "error", "debug"] as const
|
|
331
|
+
const originals = methods.map((method) => console[method])
|
|
332
|
+
for (const method of methods) {
|
|
333
|
+
console[method] = (...args: unknown[]) => {
|
|
334
|
+
try {
|
|
335
|
+
appendFileSync(TUI_LOG, `${new Date().toISOString()} ${method} ${format(...args)}\n`)
|
|
336
|
+
} catch {}
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
// Warnings and anything else written to stderr would draw over the screen too.
|
|
340
|
+
const writeError = process.stderr.write.bind(process.stderr)
|
|
341
|
+
process.stderr.write = ((chunk: string | Uint8Array) => {
|
|
342
|
+
try {
|
|
343
|
+
appendFileSync(TUI_LOG, typeof chunk === "string" ? chunk : Buffer.from(chunk))
|
|
344
|
+
} catch {}
|
|
345
|
+
return true
|
|
346
|
+
}) as typeof process.stderr.write
|
|
347
|
+
return () => {
|
|
348
|
+
methods.forEach((method, index) => {
|
|
349
|
+
console[method] = originals[index] as (typeof console)[typeof method]
|
|
350
|
+
})
|
|
351
|
+
process.stderr.write = writeError
|
|
352
|
+
}
|
|
353
|
+
}
|
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
/** @jsxImportSource @opentui/react */
|
|
2
|
+
import { type KeyEvent, RGBA } from "@opentui/core"
|
|
3
|
+
import { useKeyboard, useTerminalDimensions } from "@opentui/react"
|
|
4
|
+
import { useState } from "react"
|
|
5
|
+
import type { TuiColors } from "./theme"
|
|
6
|
+
import { BOLD } from "./ui"
|
|
7
|
+
|
|
8
|
+
export interface PaletteItem {
|
|
9
|
+
readonly id: string
|
|
10
|
+
readonly label: string
|
|
11
|
+
/** Quiet text after the label: a model's tier, a conversation's age. */
|
|
12
|
+
readonly detail?: string
|
|
13
|
+
/** A shortcut that does the same, shown on the right. */
|
|
14
|
+
readonly keys?: string
|
|
15
|
+
readonly group?: string
|
|
16
|
+
/** The current choice, marked. */
|
|
17
|
+
readonly current?: boolean
|
|
18
|
+
run(): void
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* A list to pick from, filtered as you type: commands, conversations, models. Opens centered over
|
|
23
|
+
* the screen; esc closes it.
|
|
24
|
+
*/
|
|
25
|
+
export function Palette({
|
|
26
|
+
colors,
|
|
27
|
+
title,
|
|
28
|
+
items,
|
|
29
|
+
placeholder,
|
|
30
|
+
onClose,
|
|
31
|
+
}: {
|
|
32
|
+
readonly colors: TuiColors
|
|
33
|
+
readonly title: string
|
|
34
|
+
readonly items: readonly PaletteItem[]
|
|
35
|
+
readonly placeholder: string
|
|
36
|
+
readonly onClose: () => void
|
|
37
|
+
}) {
|
|
38
|
+
const { width, height } = useTerminalDimensions()
|
|
39
|
+
const [query, setQuery] = useState("")
|
|
40
|
+
const [selected, setSelected] = useState(() =>
|
|
41
|
+
Math.max(
|
|
42
|
+
0,
|
|
43
|
+
items.findIndex((item) => item.current)
|
|
44
|
+
)
|
|
45
|
+
)
|
|
46
|
+
const words = query.toLowerCase().split(/\s+/).filter(Boolean)
|
|
47
|
+
const matches = items.filter((item) => {
|
|
48
|
+
const haystack = `${item.label} ${item.detail ?? ""} ${item.group ?? ""}`.toLowerCase()
|
|
49
|
+
return words.every((word) => haystack.includes(word))
|
|
50
|
+
})
|
|
51
|
+
const index = Math.min(selected, Math.max(0, matches.length - 1))
|
|
52
|
+
const boxWidth = Math.min(72, width - 8)
|
|
53
|
+
const rows = Math.max(4, Math.min(matches.length, height - 14, 14))
|
|
54
|
+
const first = Math.min(Math.max(0, index - rows + 1), Math.max(0, matches.length - rows))
|
|
55
|
+
|
|
56
|
+
useKeyboard((key: KeyEvent) => {
|
|
57
|
+
if (key.name === "escape" || (key.ctrl && key.name === "c")) return onClose()
|
|
58
|
+
if (key.name === "up" || (key.ctrl && key.name === "p")) {
|
|
59
|
+
return setSelected((value) => Math.max(0, Math.min(value, matches.length - 1) - 1))
|
|
60
|
+
}
|
|
61
|
+
if (key.name === "down" || (key.ctrl && key.name === "n")) {
|
|
62
|
+
return setSelected((value) => Math.min(matches.length - 1, value + 1))
|
|
63
|
+
}
|
|
64
|
+
if (key.name === "return") {
|
|
65
|
+
const item = matches[index]
|
|
66
|
+
if (item) {
|
|
67
|
+
onClose()
|
|
68
|
+
item.run()
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
})
|
|
72
|
+
|
|
73
|
+
// Each row on one line: the label keeps its room first, then its detail gets what is left.
|
|
74
|
+
const visible = matches.slice(first, first + rows)
|
|
75
|
+
const rowsWithHeaders = visible.map((item, offset) => ({
|
|
76
|
+
item,
|
|
77
|
+
active: first + offset === index,
|
|
78
|
+
header:
|
|
79
|
+
item.group && item.group !== (offset === 0 ? undefined : visible[offset - 1]?.group)
|
|
80
|
+
? item.group
|
|
81
|
+
: undefined,
|
|
82
|
+
}))
|
|
83
|
+
const headerRows = rowsWithHeaders.reduce(
|
|
84
|
+
(sum, row, offset) => sum + (row.header ? (offset === 0 ? 1 : 2) : 0),
|
|
85
|
+
0
|
|
86
|
+
)
|
|
87
|
+
const listRows = matches.length === 0 ? 1 : visible.length + headerRows
|
|
88
|
+
const inner = boxWidth - 6
|
|
89
|
+
return (
|
|
90
|
+
<>
|
|
91
|
+
<box
|
|
92
|
+
position="absolute"
|
|
93
|
+
left={0}
|
|
94
|
+
top={0}
|
|
95
|
+
width={width}
|
|
96
|
+
height={height}
|
|
97
|
+
backgroundColor={RGBA.fromValues(0, 0, 0, 0.45)}
|
|
98
|
+
zIndex={9}
|
|
99
|
+
/>
|
|
100
|
+
<box
|
|
101
|
+
position="absolute"
|
|
102
|
+
left={Math.floor((width - boxWidth) / 2)}
|
|
103
|
+
top={Math.max(2, Math.floor(height / 5))}
|
|
104
|
+
width={boxWidth}
|
|
105
|
+
height={listRows + 6}
|
|
106
|
+
backgroundColor={colors.surface}
|
|
107
|
+
flexDirection="column"
|
|
108
|
+
paddingTop={1}
|
|
109
|
+
paddingBottom={1}
|
|
110
|
+
zIndex={10}
|
|
111
|
+
>
|
|
112
|
+
<box flexDirection="row" justifyContent="space-between" paddingLeft={3} paddingRight={3}>
|
|
113
|
+
<text fg={colors.text} attributes={BOLD}>
|
|
114
|
+
{title}
|
|
115
|
+
</text>
|
|
116
|
+
<text fg={colors.faint}>esc</text>
|
|
117
|
+
</box>
|
|
118
|
+
<box paddingLeft={3} paddingRight={3} paddingTop={1} paddingBottom={1}>
|
|
119
|
+
<input
|
|
120
|
+
focused
|
|
121
|
+
placeholder={placeholder}
|
|
122
|
+
textColor={colors.text}
|
|
123
|
+
focusedTextColor={colors.text}
|
|
124
|
+
placeholderColor={colors.faint}
|
|
125
|
+
backgroundColor={colors.surface}
|
|
126
|
+
focusedBackgroundColor={colors.surface}
|
|
127
|
+
onInput={(value: string) => {
|
|
128
|
+
setQuery(value)
|
|
129
|
+
setSelected(0)
|
|
130
|
+
}}
|
|
131
|
+
/>
|
|
132
|
+
</box>
|
|
133
|
+
{matches.length === 0 ? (
|
|
134
|
+
<box paddingLeft={3}>
|
|
135
|
+
<text fg={colors.muted}>Nothing matches.</text>
|
|
136
|
+
</box>
|
|
137
|
+
) : null}
|
|
138
|
+
{rowsWithHeaders.map(({ item, active, header }, offset) => {
|
|
139
|
+
const keys = item.keys ? item.keys.length + 2 : 0
|
|
140
|
+
const mark = item.current ? 3 : 0
|
|
141
|
+
const detailRoom = item.detail ? Math.min(item.detail.length + 2, 26) : 0
|
|
142
|
+
const label = fit(item.label, inner - keys - mark - detailRoom)
|
|
143
|
+
const detail = item.detail ? fit(item.detail, inner - keys - mark - label.length - 2) : ""
|
|
144
|
+
return (
|
|
145
|
+
<box key={item.id} flexDirection="column">
|
|
146
|
+
{header ? (
|
|
147
|
+
<box paddingLeft={3} paddingTop={offset === 0 ? 0 : 1}>
|
|
148
|
+
<text fg={colors.blue}>{header}</text>
|
|
149
|
+
</box>
|
|
150
|
+
) : null}
|
|
151
|
+
<box
|
|
152
|
+
flexDirection="row"
|
|
153
|
+
justifyContent="space-between"
|
|
154
|
+
backgroundColor={active ? colors.raised : colors.surface}
|
|
155
|
+
paddingLeft={3}
|
|
156
|
+
paddingRight={3}
|
|
157
|
+
>
|
|
158
|
+
<text>
|
|
159
|
+
<span fg={colors.text} attributes={active ? BOLD : 0}>
|
|
160
|
+
{label}
|
|
161
|
+
</span>
|
|
162
|
+
{item.current ? <span fg={colors.blue}>{" ●"}</span> : null}
|
|
163
|
+
{detail && item.keys ? <span fg={colors.faint}>{` ${detail}`}</span> : null}
|
|
164
|
+
</text>
|
|
165
|
+
{item.keys ? (
|
|
166
|
+
<text fg={colors.faint}>{item.keys}</text>
|
|
167
|
+
) : detail ? (
|
|
168
|
+
<text fg={colors.faint}>{detail}</text>
|
|
169
|
+
) : null}
|
|
170
|
+
</box>
|
|
171
|
+
</box>
|
|
172
|
+
)
|
|
173
|
+
})}
|
|
174
|
+
</box>
|
|
175
|
+
</>
|
|
176
|
+
)
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/** Text cut to `room` columns, with an ellipsis; nothing when there is no room. */
|
|
180
|
+
function fit(text: string, room: number): string {
|
|
181
|
+
if (room < 4) return ""
|
|
182
|
+
return text.length > room ? `${text.slice(0, room - 1)}…` : text
|
|
183
|
+
}
|