opencode-change-history 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/CHANGELOG.md +32 -0
- package/CHANGELOG.zh-CN.md +31 -0
- package/LICENSE +21 -0
- package/README.md +103 -2
- package/README.zh-CN.md +104 -0
- package/package.json +70 -4
- package/src/core/diff.ts +54 -0
- package/src/core/history.ts +86 -0
- package/src/core/parser.ts +260 -0
- package/src/core/patch-input.ts +45 -0
- package/src/core/paths.ts +47 -0
- package/src/core/types.ts +52 -0
- package/src/session-history.ts +180 -0
- package/src/tui.tsx +90 -0
- package/src/ui/changes-sidebar.tsx +177 -0
- package/src/ui/changes.tsx +300 -0
- package/src/ui/diff-view.tsx +141 -0
- package/src/ui/explorer.tsx +268 -0
- package/src/ui/file-tree.ts +113 -0
- package/src/ui/history-nav.tsx +216 -0
- package/src/ui/layout.ts +90 -0
- package/src/ui/presentation.tsx +320 -0
|
@@ -0,0 +1,320 @@
|
|
|
1
|
+
import { MouseButton, RGBA, type MouseEvent, type ScrollBoxRenderable } from "@opentui/core"
|
|
2
|
+
import type { Plugin } from "@opencode/plugin/tui"
|
|
3
|
+
import { createEffect, createMemo, createSignal, For, on, Show } from "solid-js"
|
|
4
|
+
import { relativeLabel } from "../core/paths"
|
|
5
|
+
import type { History, RecordedChange, Statistics } from "../core/types"
|
|
6
|
+
import type { HistoryState } from "../session-history"
|
|
7
|
+
|
|
8
|
+
export type Context = Plugin.Context
|
|
9
|
+
export type HistoryProps = { context: Context; state: HistoryState }
|
|
10
|
+
export type PaneProps = { context: Context; focused: boolean; focus: () => void }
|
|
11
|
+
type CountsProps = Pick<Statistics,
|
|
12
|
+
"additions" | "deletions" | "estimatedAdditions" | "unknownAdditions" | "unknownDeletions">
|
|
13
|
+
type CountFormat = { hideZero?: boolean; showUnknown?: boolean; width?: number }
|
|
14
|
+
export type CountColumns = { added: number; removed: number }
|
|
15
|
+
|
|
16
|
+
export const visible = (text: string) => text.replace(/[\x00-\x08\x0b\x0c\x0e-\x1f\x7f-\x9f]/g, "�")
|
|
17
|
+
export const inline = (text: string) => visible(text).replace(/[\r\n\t]/g, " ")
|
|
18
|
+
export const pathLabel = (path: string, directory: string) => inline(relativeLabel(path, directory))
|
|
19
|
+
export const plural = (count: number, noun: string) => `${count} ${noun}${count === 1 ? "" : "s"}`
|
|
20
|
+
export const hasHunks = (change: RecordedChange) => change.diffQuality === "recorded" && /^@@ /m.test(change.diff ?? "")
|
|
21
|
+
|
|
22
|
+
export function leftClick(action: () => void) {
|
|
23
|
+
return (event: MouseEvent) => {
|
|
24
|
+
if (event.button === MouseButton.LEFT) action()
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** Mouse-over state for a clickable element, matching the official TUI's hover handling. */
|
|
29
|
+
export function createHover() {
|
|
30
|
+
const [hovered, setHovered] = createSignal(false)
|
|
31
|
+
return {
|
|
32
|
+
hovered,
|
|
33
|
+
onMouseOver: () => setHovered(true),
|
|
34
|
+
onMouseOut: () => setHovered(false),
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** Official inline-link hover color; falls back for partial test and preview themes. */
|
|
39
|
+
export function hoverColor(theme: Context["theme"]) {
|
|
40
|
+
return theme.text.action?.secondary?.hovered ?? theme.text.base
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** Official row/button hover surface; partial themes simply skip the band. */
|
|
44
|
+
export function hoverBackground(theme: Context["theme"]) {
|
|
45
|
+
return theme.background.raised?.high
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export function tint(surface: RGBA, foreground: RGBA, amount: number) {
|
|
49
|
+
const overlay = foreground.a * amount
|
|
50
|
+
const base = surface.a * (1 - overlay)
|
|
51
|
+
const alpha = base + overlay
|
|
52
|
+
if (!alpha) return surface
|
|
53
|
+
return RGBA.fromValues(
|
|
54
|
+
(surface.r * base + foreground.r * overlay) / alpha,
|
|
55
|
+
(surface.g * base + foreground.g * overlay) / alpha,
|
|
56
|
+
(surface.b * base + foreground.b * overlay) / alpha,
|
|
57
|
+
alpha,
|
|
58
|
+
)
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
const graphemes = new Intl.Segmenter(undefined, { granularity: "grapheme" })
|
|
62
|
+
|
|
63
|
+
function takeStart(text: string, width: number) {
|
|
64
|
+
let used = 0
|
|
65
|
+
let result = ""
|
|
66
|
+
for (const { segment } of graphemes.segment(text)) {
|
|
67
|
+
const cells = Bun.stringWidth(segment)
|
|
68
|
+
if (used + cells > width) break
|
|
69
|
+
result += segment
|
|
70
|
+
used += cells
|
|
71
|
+
}
|
|
72
|
+
return result
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
function takeEnd(text: string, width: number) {
|
|
76
|
+
const result: string[] = []
|
|
77
|
+
let used = 0
|
|
78
|
+
for (const { segment } of Array.from(graphemes.segment(text)).reverse()) {
|
|
79
|
+
const cells = Bun.stringWidth(segment)
|
|
80
|
+
if (used + cells > width) break
|
|
81
|
+
result.push(segment)
|
|
82
|
+
used += cells
|
|
83
|
+
}
|
|
84
|
+
return result.reverse().join("")
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/** Fit the path into terminal cells, spending the available width on the filename first. */
|
|
88
|
+
export function pathTail(path: string, width: number): string {
|
|
89
|
+
if (Bun.stringWidth(path) <= width) return path
|
|
90
|
+
if (width < 2) return width === 1 ? "…" : ""
|
|
91
|
+
const suffix = takeEnd(path, width - 1)
|
|
92
|
+
const separator = suffix.indexOf("/")
|
|
93
|
+
return `…${separator < 0 || separator === suffix.length - 1 ? suffix : suffix.slice(separator)}`
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
function extensionOf(text: string) {
|
|
97
|
+
const separator = Math.max(text.lastIndexOf("/"), text.lastIndexOf("\\"))
|
|
98
|
+
const dot = text.lastIndexOf(".")
|
|
99
|
+
return dot > separator + 1 && dot < text.length - 1 ? text.slice(dot) : ""
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Fit a name into terminal cells while keeping both ends readable. The suffix is
|
|
104
|
+
* sized to keep the extension (and a little of the stem) visible.
|
|
105
|
+
*/
|
|
106
|
+
export function middleEllipsis(text: string, width: number): string {
|
|
107
|
+
const available = Math.max(0, Math.floor(width))
|
|
108
|
+
const cells = Bun.stringWidth(text)
|
|
109
|
+
if (cells <= available) return text
|
|
110
|
+
if (available <= 0) return ""
|
|
111
|
+
if (available === 1) return "…"
|
|
112
|
+
const extension = Bun.stringWidth(extensionOf(text))
|
|
113
|
+
const tailWidth = Math.min(available - 2, Math.max(Math.floor(available * 0.4), extension))
|
|
114
|
+
const tail = takeEnd(text, tailWidth)
|
|
115
|
+
const head = takeStart(text, available - 1 - Bun.stringWidth(tail))
|
|
116
|
+
return `${head}…${tail}`
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
// Keep the last successful view mounted during a reload.
|
|
120
|
+
export function useHistory(state: () => HistoryState, keepOnError = false) {
|
|
121
|
+
return createMemo<Extract<HistoryState, { status: "ready" }> | undefined>((previous) => {
|
|
122
|
+
const current = state()
|
|
123
|
+
if (current.status === "loading" || (keepOnError && current.status === "error")) return previous
|
|
124
|
+
return current.status === "ready" ? current : undefined
|
|
125
|
+
})
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
// A live update can insert or move rows above the viewport. Keep the first
|
|
129
|
+
// surviving visible row in place while OpenTUI lays out the updated content.
|
|
130
|
+
export function keepScrollPosition(ids: () => readonly string[], getScroll: () => ScrollBoxRenderable | undefined) {
|
|
131
|
+
createEffect(on(ids, (_current, previous) => {
|
|
132
|
+
const scroll = getScroll()
|
|
133
|
+
if (!scroll?.scrollTop || !previous) return
|
|
134
|
+
const top = scroll.viewport.screenY
|
|
135
|
+
const bottom = top + scroll.viewport.height
|
|
136
|
+
const children = new Map(scroll.getChildren().map((child) => [child.id, child]))
|
|
137
|
+
for (const id of previous) {
|
|
138
|
+
const child = children.get(id)
|
|
139
|
+
if (!child || child.y >= bottom || child.y + child.height <= top) continue
|
|
140
|
+
scroll.afterLayout(scroll.anchor(child))
|
|
141
|
+
break
|
|
142
|
+
}
|
|
143
|
+
}))
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
const compactCounts = [3, 2, 1].map((maximumSignificantDigits) =>
|
|
147
|
+
new Intl.NumberFormat("en", { notation: "compact", maximumSignificantDigits }))
|
|
148
|
+
|
|
149
|
+
function fitCount(label: string, width: number) {
|
|
150
|
+
if (width < 1) return ""
|
|
151
|
+
if (label.length <= width) return label
|
|
152
|
+
// Keep the sign and the unknown marker while replacing the value with a compact form.
|
|
153
|
+
const sign = label[0] === "?" ? "" : label[0] ?? ""
|
|
154
|
+
const rest = label.slice(sign.length)
|
|
155
|
+
const estimated = rest.startsWith("~")
|
|
156
|
+
const suffix = rest.endsWith("?") ? "?" : ""
|
|
157
|
+
const value = Number(rest.slice(estimated ? 1 : 0, suffix ? -1 : undefined))
|
|
158
|
+
const budget = width - sign.length - suffix.length
|
|
159
|
+
if (budget < 1) return suffix || "…"
|
|
160
|
+
if (!Number.isFinite(value)) return suffix || "…"
|
|
161
|
+
// Compact and scientific forms are rounded, so they are marked approximate as well.
|
|
162
|
+
for (const formatter of compactCounts) {
|
|
163
|
+
const compact = formatter.format(value)
|
|
164
|
+
if (`~${compact}`.length <= budget) return `${sign}~${compact}${suffix}`
|
|
165
|
+
}
|
|
166
|
+
for (const precision of [2, 1, 0]) {
|
|
167
|
+
const scientific = value.toExponential(precision).replace("e+", "e")
|
|
168
|
+
if (`~${scientific}`.length <= budget) return `${sign}~${scientific}${suffix}`
|
|
169
|
+
}
|
|
170
|
+
return `${sign}…${suffix}`
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
export function countLabels(stats: CountsProps & Pick<Statistics, "operations">, format: CountFormat = {}) {
|
|
174
|
+
function label(sign: string, count: number, unknown: number, estimated = 0) {
|
|
175
|
+
// A submitted write preview supplies a magnitude but never a recorded line count.
|
|
176
|
+
const approximate = estimated > 0
|
|
177
|
+
if (format.showUnknown && unknown > 0 && !approximate) return count > 0 ? `${sign}${count}?` : "?"
|
|
178
|
+
if (stats.operations <= unknown && !approximate) return ""
|
|
179
|
+
if (format.hideZero && count === 0 && !approximate) return ""
|
|
180
|
+
return approximate ? `${sign}~${count + estimated}` : `${sign}${count}`
|
|
181
|
+
}
|
|
182
|
+
const added = label("+", stats.additions, stats.unknownAdditions, stats.estimatedAdditions)
|
|
183
|
+
const removed = label("-", stats.deletions, stats.unknownDeletions)
|
|
184
|
+
if (format.width === undefined || countsWidth({ added, removed }) <= format.width) return { added, removed }
|
|
185
|
+
const cells = format.width - Number(!!added && !!removed)
|
|
186
|
+
const left = Math.min(added.length, Math.max(Math.floor(cells / 2), cells - removed.length))
|
|
187
|
+
return { added: fitCount(added, left), removed: fitCount(removed, cells - left) }
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
export function countColumns(stats: Iterable<Statistics>, format?: CountFormat): CountColumns {
|
|
191
|
+
const columns = { added: 0, removed: 0 }
|
|
192
|
+
for (const entry of stats) {
|
|
193
|
+
const counts = countLabels(entry, format)
|
|
194
|
+
columns.added = Math.max(columns.added, counts.added.length)
|
|
195
|
+
columns.removed = Math.max(columns.removed, counts.removed.length)
|
|
196
|
+
}
|
|
197
|
+
return columns
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
export function countsWidth(labels: CountColumns | { added: string; removed: string }) {
|
|
201
|
+
const cells = (value: number | string) => typeof value === "string" ? value.length : value
|
|
202
|
+
return cells(labels.added) + cells(labels.removed) + Number(!!labels.added && !!labels.removed)
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
export function Counts(props: {
|
|
206
|
+
context: Context
|
|
207
|
+
stats: CountsProps & Pick<Statistics, "operations">
|
|
208
|
+
columns?: CountColumns
|
|
209
|
+
format?: CountFormat
|
|
210
|
+
selectable?: boolean
|
|
211
|
+
}) {
|
|
212
|
+
const labels = createMemo(() => countLabels(props.stats, props.format))
|
|
213
|
+
return (
|
|
214
|
+
<box flexDirection="row" gap={1} flexShrink={0}>
|
|
215
|
+
<Show when={props.columns?.added || labels().added}>
|
|
216
|
+
<text fg={props.context.theme.diff.highlight.added} width={props.columns?.added} selectable={props.selectable ?? true}
|
|
217
|
+
wrapMode="none" textAlign="right">{labels().added}</text>
|
|
218
|
+
</Show>
|
|
219
|
+
<Show when={props.columns?.removed || labels().removed}>
|
|
220
|
+
<text fg={props.context.theme.diff.highlight.removed} width={props.columns?.removed} selectable={props.selectable ?? true}
|
|
221
|
+
wrapMode="none" textAlign="right">{labels().removed}</text>
|
|
222
|
+
</Show>
|
|
223
|
+
</box>
|
|
224
|
+
)
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
export function Totals(props: { context: Context; stats: Statistics }) {
|
|
228
|
+
return (
|
|
229
|
+
<box flexDirection="row" flexWrap="wrap" justifyContent="space-between" columnGap={1} minHeight={1} overflow="hidden">
|
|
230
|
+
<Counts context={props.context} stats={props.stats} format={{ hideZero: true, showUnknown: true }} />
|
|
231
|
+
<text wrapMode="none" truncate fg={props.context.theme.text.muted}>{plural(props.stats.operations, "edit")}</text>
|
|
232
|
+
</box>
|
|
233
|
+
)
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
export function Summary(props: { context: Context; stats: Statistics; files?: number; width: number }) {
|
|
237
|
+
const theme = () => props.context.theme
|
|
238
|
+
const countsFormat = { hideZero: true } as const
|
|
239
|
+
const layout = createMemo(() => {
|
|
240
|
+
const { files, stats, width } = props
|
|
241
|
+
const counts = countsWidth(countLabels(stats, countsFormat))
|
|
242
|
+
const caption = `${files === undefined ? "" : `${plural(files, "file")} · `}${plural(stats.operations, "edit")}`
|
|
243
|
+
const partial = stats.incomplete ? `Partial · ${stats.incomplete} incomplete` : ""
|
|
244
|
+
const short = stats.incomplete ? `Partial ${stats.incomplete}` : ""
|
|
245
|
+
const needed = caption.length + (counts ? counts + 2 : 0)
|
|
246
|
+
if (needed + (partial ? partial.length + 2 : 0) <= width) return { caption, status: partial, counts, gap: 2 }
|
|
247
|
+
if (needed + (short ? short.length + 2 : 0) <= width) return { caption, status: short, counts, gap: 2 }
|
|
248
|
+
const label = caption.length + (short ? short.length + 1 : 0) <= width
|
|
249
|
+
? caption : `${files === undefined ? "" : `${files}f `}${stats.operations}e`
|
|
250
|
+
return { caption: label, status: short, counts: 0, gap: 1, below: counts <= width ? counts : 0 }
|
|
251
|
+
})
|
|
252
|
+
return (
|
|
253
|
+
<box flexShrink={0} overflow="hidden">
|
|
254
|
+
<box height={1} flexShrink={0} flexDirection="row" gap={layout().gap} overflow="hidden">
|
|
255
|
+
<text wrapMode="none" truncate minWidth={0} flexShrink={1} fg={theme().text.muted}>{layout().caption}</text>
|
|
256
|
+
<Show when={layout().counts}>
|
|
257
|
+
<Counts context={props.context} stats={props.stats} format={countsFormat} />
|
|
258
|
+
</Show>
|
|
259
|
+
<Show when={layout().status}>
|
|
260
|
+
{(label) => <text wrapMode="none" truncate minWidth={0} flexShrink={1}
|
|
261
|
+
fg={theme().text.feedback.warning.base}>{label()}</text>}
|
|
262
|
+
</Show>
|
|
263
|
+
</box>
|
|
264
|
+
<Show when={layout().below}>
|
|
265
|
+
<Counts context={props.context} stats={props.stats} format={countsFormat} />
|
|
266
|
+
</Show>
|
|
267
|
+
</box>
|
|
268
|
+
)
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
export function HistoryStatus(props: HistoryProps & { history?: History; compact?: boolean }) {
|
|
272
|
+
const theme = () => props.context.theme
|
|
273
|
+
const staged = () => props.history?.staged ? "Staged undo hides later edits" : ""
|
|
274
|
+
const status = () => {
|
|
275
|
+
if (props.state.status === "error") return inline(props.state.message)
|
|
276
|
+
if (props.state.status === "loading" && !props.history) return "Loading changes…"
|
|
277
|
+
if (props.history?.files.length === 0) return "No recorded file changes."
|
|
278
|
+
}
|
|
279
|
+
const message = () => props.compact ? [status(), staged()].filter(Boolean).join(" · ") : status()
|
|
280
|
+
return (
|
|
281
|
+
<>
|
|
282
|
+
<Show when={message()}>
|
|
283
|
+
{(value) => <text wrapMode={props.compact ? "none" : "word"} truncate={props.compact}
|
|
284
|
+
fg={props.state.status === "error" ? theme().text.feedback.error.base : theme().text.muted}>
|
|
285
|
+
{value()}
|
|
286
|
+
</text>}
|
|
287
|
+
</Show>
|
|
288
|
+
<Show when={!props.compact && props.history?.incomplete}>
|
|
289
|
+
{(count) => <text fg={theme().text.muted}>{`Partial · ${count()} incomplete`}</text>}
|
|
290
|
+
</Show>
|
|
291
|
+
<Show when={!props.compact && staged()}>
|
|
292
|
+
{(value) => <text fg={theme().text.muted}>{value()}</text>}
|
|
293
|
+
</Show>
|
|
294
|
+
</>
|
|
295
|
+
)
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
export type Hint = readonly [key: string, label: string, action?: () => void]
|
|
299
|
+
|
|
300
|
+
export function KeyHints(props: { context: Context; items: readonly Hint[]; singleLine?: boolean }) {
|
|
301
|
+
const theme = () => props.context.theme
|
|
302
|
+
return (
|
|
303
|
+
<box flexDirection="row" flexWrap={props.singleLine ? "no-wrap" : "wrap"}
|
|
304
|
+
height={props.singleLine ? 1 : undefined} columnGap={2} flexShrink={0} overflow="hidden">
|
|
305
|
+
<For each={props.items}>
|
|
306
|
+
{([key, label, action]) => {
|
|
307
|
+
const hover = createHover()
|
|
308
|
+
return (
|
|
309
|
+
<text wrapMode="none" selectable={false} flexShrink={0}
|
|
310
|
+
onMouseOver={action && hover.onMouseOver} onMouseOut={action && hover.onMouseOut}
|
|
311
|
+
onMouseUp={action && leftClick(action)}
|
|
312
|
+
fg={action && hover.hovered() ? hoverColor(theme()) : theme().text.muted}>
|
|
313
|
+
<span style={{ fg: theme().text.base }}>{key}</span>{label ? ` ${label}` : ""}
|
|
314
|
+
</text>
|
|
315
|
+
)
|
|
316
|
+
}}
|
|
317
|
+
</For>
|
|
318
|
+
</box>
|
|
319
|
+
)
|
|
320
|
+
}
|