opencode-gh-search 0.0.0-stage → 0.1.0

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/src/tui.tsx ADDED
@@ -0,0 +1,186 @@
1
+ /** @jsxImportSource @opentui/solid */
2
+ import { createSignal, For, onCleanup, Show } from "solid-js"
3
+ import { Plugin, usePlugin } from "@opencode/plugin/tui"
4
+ import { useTerminalDimensions } from "@opentui/solid"
5
+ import { KINDS, empty, highlight, meta, mine, status, placeholder, scope, type Item, type Kind } from "./rpc.js"
6
+ import { createSearch } from "./search.js"
7
+
8
+ const BATCH = 100
9
+ // Terminal lines the dialog needs besides the rows: its margins and frame, the header, the search box and gaps.
10
+ const CHROME = 16
11
+ // Rows shown at most; more makes the dialog taller than the host places it.
12
+ const MAX_ROWS = 12
13
+ // GitHub's Primer colours for the theme's light or dark mode: state text and review pill backgrounds
14
+ // (`emphasis`, under white text). The theme has no matching tokens.
15
+ const GITHUB = {
16
+ light: {
17
+ state: { open: "#1a7f37", draft: "#59636e", merged: "#8250df", closed: "#d1242f", issueClosed: "#8250df" },
18
+ pill: { attention: "#9a6700", danger: "#cf222e", success: "#1f883d", neutral: "#59636e" },
19
+ },
20
+ dark: {
21
+ state: { open: "#3fb950", draft: "#9198a1", merged: "#ab7df8", closed: "#f85149", issueClosed: "#ab7df8" },
22
+ pill: { attention: "#9e6a03", danger: "#da3633", success: "#238636", neutral: "#656c76" },
23
+ },
24
+ }
25
+ const PILL_TEXT = "#ffffff"
26
+ // Stands in for the Desktop stack octicon; a plain BMP character, so any terminal font has it.
27
+ const STACK = "≡"
28
+
29
+ function Search(props: { kind: Kind; directory: string; initial: string; onPick: (item: Item) => void }) {
30
+ const ctx = usePlugin()
31
+ const theme = ctx.theme
32
+ const github = () => GITHUB[ctx.themeMode]
33
+ const stateColor = (item: Item) =>
34
+ github().state[item.kind === "issue" && item.state === "closed" ? "issueClosed" : (item.state as "open")]
35
+ const kind = KINDS[props.kind]
36
+ const s = createSearch({
37
+ solid: { createSignal, onCleanup },
38
+ client: ctx.client,
39
+ kind: props.kind,
40
+ directory: props.directory,
41
+ initial: props.initial,
42
+ batch: BATCH,
43
+ })
44
+ const label = (item: Item) => kind.states[item.state] ?? item.state
45
+ const width = Math.max(...Object.values(kind.states).map((label) => label.length))
46
+ const terminal = useTerminalDimensions()
47
+ // As many two-line rows as fit the terminal, up to MAX_ROWS.
48
+ const fit = () => Math.max(3, Math.min(MAX_ROWS, Math.floor((terminal().height - CHROME) / 2)))
49
+
50
+ // OpenCode's own dialog navigation commands, so the user's configured keys apply (default up/ctrl+p,
51
+ // down/ctrl+n, pageup, pagedown). Like the host's dialogs, they live in the `modal` input mode.
52
+ ctx.keymap.layer(() => ({
53
+ mode: "modal",
54
+ priority: 10,
55
+ commands: [
56
+ { id: "dialog.select.prev", run: () => s.move(-1, fit()) },
57
+ { id: "dialog.select.next", run: () => s.move(1, fit()) },
58
+ { id: "dialog.select.page_up", run: () => s.move(-fit(), fit()) },
59
+ { id: "dialog.select.page_down", run: () => s.move(fit(), fit()) },
60
+ ],
61
+ }))
62
+
63
+ const start = () => Math.max(0, Math.min(s.selected() - Math.floor(fit() / 2), s.rows().length - fit()))
64
+ const visible = () => s.rows().slice(start(), start() + fit())
65
+ const message = () => empty(kind.plural, { error: s.error(), searching: s.searching(), indexing: s.indexing() })
66
+
67
+ return (
68
+ <box flexDirection="column" paddingLeft={2} paddingRight={2} paddingBottom={1} gap={1}>
69
+ <box flexDirection="row" justifyContent="space-between">
70
+ <text fg={theme.text.base}>
71
+ <b>{kind.title}</b>
72
+ </text>
73
+ <text fg={theme.text.muted}>
74
+ {scope(s.repos(), s.error())}
75
+ {s.indexing() ? ` · indexing ${kind.plural}…` : ""}
76
+ </text>
77
+ </box>
78
+ <input
79
+ focused
80
+ value={s.text()}
81
+ placeholder={placeholder(props.kind)}
82
+ onInput={s.input}
83
+ onSubmit={() => {
84
+ const item = s.current()
85
+ if (item) props.onPick(item)
86
+ }}
87
+ />
88
+ <box flexDirection="column" height={fit() * 2}>
89
+ <Show when={s.rows().length > 0} fallback={<text fg={theme.text.muted}>{message()}</text>}>
90
+ <For each={visible()}>
91
+ {(item, index) => {
92
+ const active = () => start() + index() === s.selected()
93
+ const fg = () => (active() ? theme.text.action.primary.state({ selected: true }) : theme.text.base)
94
+ const weak = () => (active() ? fg() : theme.text.muted)
95
+ return (
96
+ <box
97
+ flexDirection="row"
98
+ gap={2}
99
+ height={2}
100
+ backgroundColor={active() ? theme.background.action.primary.state({ selected: true }) : undefined}
101
+ onMouseDown={() => props.onPick(item)}
102
+ >
103
+ <text fg={active() ? fg() : stateColor(item)} flexShrink={0}>
104
+ {label(item).padEnd(width)}
105
+ </text>
106
+ <box flexDirection="column" flexGrow={1}>
107
+ <box flexDirection="row" gap={2}>
108
+ <text fg={fg()} wrapMode="none" flexShrink={1} flexGrow={1}>
109
+ <For each={highlight(item.title, s.text(), props.kind)}>
110
+ {(part) => (part.hit ? <b>{part.text}</b> : part.text)}
111
+ </For>
112
+ </text>
113
+ <Show when={status(item, s.viewer())}>
114
+ {(st) => (
115
+ <text fg={PILL_TEXT} bg={github().pill[st().tone || "neutral"]} wrapMode="none" flexShrink={0}>
116
+ {` ${st().label} `}
117
+ </text>
118
+ )}
119
+ </Show>
120
+ </box>
121
+ <box flexDirection="row">
122
+ <text fg={weak()} wrapMode="none" flexShrink={1}>
123
+ {meta(item, s.many()).before}
124
+ <Show when={mine(item, s.viewer())} fallback={meta(item, s.many()).author}>
125
+ <b>{meta(item, s.many()).author}</b>
126
+ </Show>
127
+ {meta(item, s.many()).after}
128
+ </text>
129
+ <Show when={item.stack}>
130
+ {(stack) => (
131
+ <text fg={weak()} wrapMode="none" flexShrink={0}>
132
+ {` · ${STACK} ${stack().position}/${stack().size}`}
133
+ </text>
134
+ )}
135
+ </Show>
136
+ </box>
137
+ </box>
138
+ </box>
139
+ )
140
+ }}
141
+ </For>
142
+ </Show>
143
+ </box>
144
+ </box>
145
+ )
146
+ }
147
+
148
+ export default Plugin.define({
149
+ id: "gh-search",
150
+ setup(ctx) {
151
+ // The TUI has no prompt API for plugins; the dialog returns focus to the prompt, so type into it directly.
152
+ function insert(text: string, noun: string) {
153
+ const focused = ctx.renderer.currentFocusedRenderable as { insertText?: (text: string) => void } | null
154
+ if (typeof focused?.insertText === "function") {
155
+ focused.insertText(text)
156
+ ctx.renderer.requestRender()
157
+ return
158
+ }
159
+ ctx.renderer.copyToClipboardOSC52(text)
160
+ ctx.ui.toast.show({ message: `Copied ${noun} to the clipboard`, variant: "info" })
161
+ }
162
+
163
+ function open(kind: Kind, input: string) {
164
+ const directory = (ctx.location ?? ctx.data.location.default()).directory
165
+ const onPick = (item: Item) => {
166
+ ctx.ui.dialog.clear()
167
+ setTimeout(() => insert(`${item.url} `, KINDS[kind].noun), 0)
168
+ }
169
+ ctx.ui.dialog.show(() => <Search kind={kind} directory={directory} initial={input.trim()} onPick={onPick} />)
170
+ ctx.ui.dialog.set({ size: "xlarge" })
171
+ }
172
+
173
+ ctx.keymap.layer(() => ({
174
+ mode: "global",
175
+ commands: (Object.keys(KINDS) as Kind[]).map((kind) => ({
176
+ id: `gh-search.${kind}`,
177
+ title: `Attach a GitHub ${KINDS[kind].noun}`,
178
+ description: `/${KINDS[kind].command} [@repo] [query]`,
179
+ group: "GitHub",
180
+ palette: true,
181
+ slash: { name: KINDS[kind].command, arguments: true },
182
+ run: (input?: string) => open(kind, input ?? ""),
183
+ })),
184
+ }))
185
+ },
186
+ })