@eliya-oss/agent-diff 0.1.0-alpha.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/.agents/adr/README.md +33 -0
- package/.agents/prd/README.md +74 -0
- package/CHANGELOG.md +58 -0
- package/README.md +239 -0
- package/bin/adiff.js +2 -0
- package/package.json +54 -0
- package/skills/adiff/SKILL.md +135 -0
- package/src/cli/catalog.ts +178 -0
- package/src/cli/commands.ts +335 -0
- package/src/cli/error.ts +44 -0
- package/src/cli/index.ts +24 -0
- package/src/cli/parse.ts +31 -0
- package/src/cli/report.ts +96 -0
- package/src/cli/story.ts +223 -0
- package/src/domain/narrative/coverage.ts +54 -0
- package/src/domain/narrative/index.ts +4 -0
- package/src/domain/narrative/model.ts +46 -0
- package/src/domain/narrative/version.ts +63 -0
- package/src/domain/patch/anchor.ts +40 -0
- package/src/domain/patch/index.ts +7 -0
- package/src/domain/patch/model.ts +54 -0
- package/src/domain/patch/parse.ts +106 -0
- package/src/domain/patch/render.ts +48 -0
- package/src/domain/patch/sticky.ts +39 -0
- package/src/domain/review/index.ts +4 -0
- package/src/domain/review/lifecycle.ts +45 -0
- package/src/domain/review/model.ts +23 -0
- package/src/domain/review/vouch.ts +15 -0
- package/src/main.ts +186 -0
- package/src/service/git/error.ts +16 -0
- package/src/service/git/index.ts +3 -0
- package/src/service/git/model.ts +13 -0
- package/src/service/git/run.ts +27 -0
- package/src/service/git/service.ts +133 -0
- package/src/service/store/error.ts +11 -0
- package/src/service/store/index.ts +5 -0
- package/src/service/store/model.ts +33 -0
- package/src/service/store/paths.ts +25 -0
- package/src/service/store/service.ts +154 -0
- package/src/testing/agent-surface.test.ts +92 -0
- package/src/testing/batch.test.ts +93 -0
- package/src/testing/branch-state.test.ts +99 -0
- package/src/testing/chrome-tone.test.ts +57 -0
- package/src/testing/comment-block.test.ts +84 -0
- package/src/testing/comment-walk.test.ts +96 -0
- package/src/testing/compose-box.test.ts +97 -0
- package/src/testing/compose.test.ts +67 -0
- package/src/testing/context.test.ts +77 -0
- package/src/testing/cursor.test.ts +49 -0
- package/src/testing/delivery.test.ts +94 -0
- package/src/testing/domains/agent/driver.ts +50 -0
- package/src/testing/domains/agent/index.ts +2 -0
- package/src/testing/domains/app/driver.ts +168 -0
- package/src/testing/domains/app/index.ts +2 -0
- package/src/testing/domains/branch/driver.ts +51 -0
- package/src/testing/domains/branch/index.ts +3 -0
- package/src/testing/domains/branch/model.ts +39 -0
- package/src/testing/domains/screen/driver.ts +241 -0
- package/src/testing/domains/screen/index.ts +2 -0
- package/src/testing/driver.ts +31 -0
- package/src/testing/escape.test.ts +56 -0
- package/src/testing/focus.test.ts +104 -0
- package/src/testing/footer-chips.test.ts +65 -0
- package/src/testing/footer.test.ts +64 -0
- package/src/testing/gaps.test.ts +47 -0
- package/src/testing/highlight.test.ts +49 -0
- package/src/testing/home-screen.test.ts +51 -0
- package/src/testing/index.ts +4 -0
- package/src/testing/inline-comments.test.ts +131 -0
- package/src/testing/marks.test.ts +49 -0
- package/src/testing/mouse.test.ts +87 -0
- package/src/testing/navigate.test.ts +73 -0
- package/src/testing/palette.test.ts +68 -0
- package/src/testing/pending.test.ts +70 -0
- package/src/testing/pin-align.test.ts +37 -0
- package/src/testing/progress.test.ts +88 -0
- package/src/testing/report.test.ts +83 -0
- package/src/testing/resume.test.ts +39 -0
- package/src/testing/review.test.ts +181 -0
- package/src/testing/roundtrip.test.ts +70 -0
- package/src/testing/scale.test.ts +55 -0
- package/src/testing/scroll-feel.test.ts +81 -0
- package/src/testing/sent-comments.test.ts +106 -0
- package/src/testing/state.ts +55 -0
- package/src/testing/sticky-removed.test.ts +67 -0
- package/src/testing/sticky.test.ts +90 -0
- package/src/testing/story-rail.test.ts +122 -0
- package/src/testing/story.test.ts +183 -0
- package/src/testing/stress.test.ts +74 -0
- package/src/testing/tree-collapse.test.ts +108 -0
- package/src/testing/tree-rows.test.ts +66 -0
- package/src/testing/tree.test.ts +91 -0
- package/src/testing/tui.test.ts +73 -0
- package/src/testing/typing.test.ts +65 -0
- package/src/testing/vouch-next.test.ts +65 -0
- package/src/testing/vouch-tui.test.ts +77 -0
- package/src/testing/wash.test.ts +32 -0
- package/src/testing/width.test.ts +62 -0
- package/src/tui/app.ts +467 -0
- package/src/tui/command.ts +390 -0
- package/src/tui/diffview.ts +333 -0
- package/src/tui/index.ts +9 -0
- package/src/tui/marks.ts +102 -0
- package/src/tui/match.ts +34 -0
- package/src/tui/model.ts +310 -0
- package/src/tui/reduce.ts +305 -0
- package/src/tui/render.ts +897 -0
- package/src/tui/report.ts +74 -0
- package/src/tui/session.ts +32 -0
- package/src/tui/theme.ts +61 -0
- package/src/tui/tree.ts +149 -0
package/src/tui/index.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export { App, launch, runOn, runTui } from "./app.ts"
|
|
2
|
+
export { actionFor, commands, commandsFor, hintsFor, listableFor } from "./command.ts"
|
|
3
|
+
export { searchCommands } from "./match.ts"
|
|
4
|
+
export type { Action, Command } from "./command.ts"
|
|
5
|
+
export { initialState } from "./model.ts"
|
|
6
|
+
export type { AppOptions } from "./app.ts"
|
|
7
|
+
export type { Screen as ScreenName, TuiState } from "./model.ts"
|
|
8
|
+
export { Screen } from "./render.ts"
|
|
9
|
+
export { palette } from "./theme.ts"
|
package/src/tui/marks.ts
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
export type MarkSet = {
|
|
2
|
+
readonly comment: string
|
|
3
|
+
readonly staged: string
|
|
4
|
+
readonly sent: string
|
|
5
|
+
readonly rule: string
|
|
6
|
+
readonly cursor: string
|
|
7
|
+
readonly tally: string
|
|
8
|
+
readonly reviewed: string
|
|
9
|
+
readonly folder: string
|
|
10
|
+
readonly folderOpen: string
|
|
11
|
+
readonly file: string
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
const sets: Readonly<Record<string, MarkSet>> = {
|
|
15
|
+
ring: {
|
|
16
|
+
comment: "○",
|
|
17
|
+
staged: "○",
|
|
18
|
+
sent: "✓",
|
|
19
|
+
rule: "│",
|
|
20
|
+
cursor: "▎",
|
|
21
|
+
tally: "•",
|
|
22
|
+
reviewed: "✓",
|
|
23
|
+
folder: "",
|
|
24
|
+
folderOpen: "",
|
|
25
|
+
file: "",
|
|
26
|
+
},
|
|
27
|
+
bubble: {
|
|
28
|
+
comment: "◗",
|
|
29
|
+
staged: "◗",
|
|
30
|
+
sent: "◖",
|
|
31
|
+
rule: "┃",
|
|
32
|
+
cursor: "▌",
|
|
33
|
+
tally: "◗",
|
|
34
|
+
reviewed: "✓",
|
|
35
|
+
folder: "",
|
|
36
|
+
folderOpen: "",
|
|
37
|
+
file: "",
|
|
38
|
+
},
|
|
39
|
+
quote: {
|
|
40
|
+
comment: "❞",
|
|
41
|
+
staged: "❝",
|
|
42
|
+
sent: "✓",
|
|
43
|
+
rule: "┊",
|
|
44
|
+
cursor: "▎",
|
|
45
|
+
tally: "❞",
|
|
46
|
+
reviewed: "✓",
|
|
47
|
+
folder: "",
|
|
48
|
+
folderOpen: "",
|
|
49
|
+
file: "",
|
|
50
|
+
},
|
|
51
|
+
diamond: {
|
|
52
|
+
comment: "◆",
|
|
53
|
+
staged: "◇",
|
|
54
|
+
sent: "◈",
|
|
55
|
+
rule: "╎",
|
|
56
|
+
cursor: "▎",
|
|
57
|
+
tally: "◆",
|
|
58
|
+
reviewed: "✓",
|
|
59
|
+
folder: "",
|
|
60
|
+
folderOpen: "",
|
|
61
|
+
file: "",
|
|
62
|
+
},
|
|
63
|
+
dot: {
|
|
64
|
+
comment: "●",
|
|
65
|
+
staged: "●",
|
|
66
|
+
sent: "✓",
|
|
67
|
+
rule: "▏",
|
|
68
|
+
cursor: "▎",
|
|
69
|
+
tally: "•",
|
|
70
|
+
reviewed: "✓",
|
|
71
|
+
folder: "",
|
|
72
|
+
folderOpen: "",
|
|
73
|
+
file: "",
|
|
74
|
+
},
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export const markSetNames: ReadonlyArray<string> = Object.keys(sets)
|
|
78
|
+
|
|
79
|
+
const ring: MarkSet = {
|
|
80
|
+
folder: "",
|
|
81
|
+
folderOpen: "",
|
|
82
|
+
file: "",
|
|
83
|
+
comment: "○",
|
|
84
|
+
staged: "○",
|
|
85
|
+
sent: "✓",
|
|
86
|
+
rule: "│",
|
|
87
|
+
cursor: "▎",
|
|
88
|
+
tally: "•",
|
|
89
|
+
reviewed: "✓",
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
const plain: MarkSet = { ...ring, folder: "▸", folderOpen: "▾", file: "·" }
|
|
93
|
+
|
|
94
|
+
let chosen: MarkSet = ring
|
|
95
|
+
|
|
96
|
+
export const useMarks = (name: string): void => {
|
|
97
|
+
chosen = name === "plain" ? plain : (sets[name] ?? ring)
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
export const marks = (): MarkSet => chosen
|
|
101
|
+
|
|
102
|
+
useMarks(process.env["ADIFF_MARKS"] ?? "ring")
|
package/src/tui/match.ts
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { listableFor, type Command } from "./command.ts"
|
|
2
|
+
import type { Screen } from "./model.ts"
|
|
3
|
+
|
|
4
|
+
const subsequence = (haystack: string, needle: string): boolean => {
|
|
5
|
+
let index = 0
|
|
6
|
+
for (const character of haystack) {
|
|
7
|
+
if (character === needle[index]) index += 1
|
|
8
|
+
if (index === needle.length) return true
|
|
9
|
+
}
|
|
10
|
+
return needle.length === 0
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
const label = (entry: Command): string => `${entry.category} ${entry.title}`.toLowerCase()
|
|
14
|
+
|
|
15
|
+
const matches = (entry: Command, query: string): boolean =>
|
|
16
|
+
query
|
|
17
|
+
.toLowerCase()
|
|
18
|
+
.split(/\s+/)
|
|
19
|
+
.filter((word) => word.length > 0)
|
|
20
|
+
.every((word) => subsequence(label(entry), word))
|
|
21
|
+
|
|
22
|
+
const closeness = (entry: Command, query: string): number => {
|
|
23
|
+
const wanted = query.trim().toLowerCase()
|
|
24
|
+
if (wanted.length === 0) return 2
|
|
25
|
+
const title = entry.title.toLowerCase()
|
|
26
|
+
if (title.startsWith(wanted)) return 0
|
|
27
|
+
if (title.includes(wanted)) return 1
|
|
28
|
+
return label(entry).includes(wanted) ? 2 : 3
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export const searchCommands = (screen: Screen, query: string): ReadonlyArray<Command> =>
|
|
32
|
+
listableFor(screen)
|
|
33
|
+
.filter((entry) => matches(entry, query))
|
|
34
|
+
.toSorted((left, right) => closeness(left, query) - closeness(right, query))
|
package/src/tui/model.ts
ADDED
|
@@ -0,0 +1,310 @@
|
|
|
1
|
+
import { Option } from "effect"
|
|
2
|
+
|
|
3
|
+
export type StagedComment = {
|
|
4
|
+
readonly file: string
|
|
5
|
+
readonly side: "old" | "new"
|
|
6
|
+
readonly start: number
|
|
7
|
+
readonly end: number
|
|
8
|
+
readonly body: string
|
|
9
|
+
}
|
|
10
|
+
import type { Patch } from "../domain/patch/index.ts"
|
|
11
|
+
import { buildTree, crowdedDirectories, flattenTree, type Tree, type TreeRow } from "./tree.ts"
|
|
12
|
+
import type { BranchSummary, StoryStep } from "../cli/index.ts"
|
|
13
|
+
|
|
14
|
+
export type Screen = "branches" | "review" | "compose" | "palette" | "pending" | "report"
|
|
15
|
+
|
|
16
|
+
export type TuiState = {
|
|
17
|
+
readonly screen: Screen
|
|
18
|
+
readonly branches: ReadonlyArray<BranchSummary>
|
|
19
|
+
readonly branchIndex: number
|
|
20
|
+
readonly patches: ReadonlyArray<Patch>
|
|
21
|
+
readonly patchIndex: number
|
|
22
|
+
readonly cursor: number
|
|
23
|
+
readonly anchorRow: number
|
|
24
|
+
readonly selecting: boolean
|
|
25
|
+
readonly draft: string
|
|
26
|
+
readonly notice: string
|
|
27
|
+
readonly query: string
|
|
28
|
+
readonly paletteIndex: number
|
|
29
|
+
readonly returnTo: Screen
|
|
30
|
+
readonly closed: ReadonlyArray<string>
|
|
31
|
+
readonly vouched: ReadonlyArray<string>
|
|
32
|
+
readonly staged: number
|
|
33
|
+
readonly pending: ReadonlyArray<StagedComment>
|
|
34
|
+
readonly sent: ReadonlyArray<StagedComment>
|
|
35
|
+
readonly pendingIndex: number
|
|
36
|
+
readonly viewport: number
|
|
37
|
+
readonly context: number
|
|
38
|
+
readonly top: number
|
|
39
|
+
readonly source: ReadonlyArray<string>
|
|
40
|
+
readonly focus: "tree" | "diff"
|
|
41
|
+
readonly navOpen: boolean
|
|
42
|
+
readonly steps: ReadonlyArray<StoryStep>
|
|
43
|
+
readonly stepIndex: number
|
|
44
|
+
readonly rail: "tree" | "steps"
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export const initialState = (branches: ReadonlyArray<BranchSummary>): TuiState => ({
|
|
48
|
+
screen: "branches",
|
|
49
|
+
branches,
|
|
50
|
+
branchIndex: 0,
|
|
51
|
+
patches: [],
|
|
52
|
+
patchIndex: 0,
|
|
53
|
+
cursor: 0,
|
|
54
|
+
anchorRow: 0,
|
|
55
|
+
selecting: false,
|
|
56
|
+
draft: "",
|
|
57
|
+
notice: "",
|
|
58
|
+
query: "",
|
|
59
|
+
paletteIndex: 0,
|
|
60
|
+
returnTo: "branches",
|
|
61
|
+
closed: [],
|
|
62
|
+
vouched: [],
|
|
63
|
+
staged: 0,
|
|
64
|
+
pending: [],
|
|
65
|
+
sent: [],
|
|
66
|
+
pendingIndex: 0,
|
|
67
|
+
viewport: 20,
|
|
68
|
+
context: 3,
|
|
69
|
+
top: 0,
|
|
70
|
+
source: [],
|
|
71
|
+
focus: "diff",
|
|
72
|
+
navOpen: true,
|
|
73
|
+
steps: [],
|
|
74
|
+
stepIndex: 0,
|
|
75
|
+
rail: "tree",
|
|
76
|
+
})
|
|
77
|
+
|
|
78
|
+
export const onSteps = (state: TuiState): boolean =>
|
|
79
|
+
state.rail === "steps" && state.steps.length > 0
|
|
80
|
+
|
|
81
|
+
export const selectedStep = (state: TuiState): StoryStep | undefined => state.steps[state.stepIndex]
|
|
82
|
+
|
|
83
|
+
export const stepFiles = (state: TuiState, stepIndex: number): ReadonlyArray<number> => {
|
|
84
|
+
const step = state.steps[stepIndex]
|
|
85
|
+
if (step === undefined) return []
|
|
86
|
+
return step.files.flatMap((path) => {
|
|
87
|
+
const at = state.patches.findIndex((patch) => patch.path === path)
|
|
88
|
+
return at === -1 ? [] : [at]
|
|
89
|
+
})
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
export const stepHolding = (state: TuiState, fileIndex: number): number => {
|
|
93
|
+
const path = state.patches[fileIndex]?.path
|
|
94
|
+
const at = state.steps.findIndex((step) => step.files.includes(path ?? ""))
|
|
95
|
+
return at === -1 ? state.stepIndex : at
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
export const stepWindow = (
|
|
99
|
+
state: TuiState,
|
|
100
|
+
height: number,
|
|
101
|
+
): { readonly first: number; readonly titles: ReadonlyArray<string>; readonly more: number } => {
|
|
102
|
+
const titles = state.steps.map((step) => step.title)
|
|
103
|
+
if (titles.length <= height) return { first: 0, titles, more: 0 }
|
|
104
|
+
const start = Math.max(0, Math.min(titles.length - height, state.stepIndex - Math.floor(height / 2)))
|
|
105
|
+
return { first: start, titles: titles.slice(start, start + height), more: titles.length - height }
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
export const selectedBranch = (state: TuiState): BranchSummary | undefined =>
|
|
109
|
+
state.branches[state.branchIndex]
|
|
110
|
+
|
|
111
|
+
export const selectedPatch = (state: TuiState): Patch | undefined => state.patches[state.patchIndex]
|
|
112
|
+
|
|
113
|
+
export const treeOf = (state: TuiState): Tree =>
|
|
114
|
+
buildTree(state.patches.map((patch) => patch.path))
|
|
115
|
+
|
|
116
|
+
export const CROWDED = 8
|
|
117
|
+
|
|
118
|
+
export const treeRows = (state: TuiState): ReadonlyArray<TreeRow> => {
|
|
119
|
+
const tree = treeOf(state)
|
|
120
|
+
return flattenTree(tree, state.closed)
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
export const crowdedOf = (patches: TuiState["patches"]): ReadonlyArray<string> =>
|
|
124
|
+
crowdedDirectories(buildTree(patches.map((patch) => patch.path)), CROWDED)
|
|
125
|
+
|
|
126
|
+
export const directoryOfFile = (state: TuiState, fileIndex: number): string | undefined => {
|
|
127
|
+
const patch = state.patches[fileIndex]
|
|
128
|
+
if (patch === undefined) return undefined
|
|
129
|
+
const segments = patch.path.split("/")
|
|
130
|
+
return segments.length < 2 ? undefined : segments.slice(0, -1).join("/")
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
export const isReviewed = (state: TuiState, fileIndex: number): boolean => {
|
|
134
|
+
const patch = state.patches[fileIndex]
|
|
135
|
+
return patch !== undefined && state.vouched.includes(patch.path)
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
export const treeWindow = (
|
|
139
|
+
state: TuiState,
|
|
140
|
+
height: number,
|
|
141
|
+
): { readonly rows: ReadonlyArray<TreeRow>; readonly more: number } => {
|
|
142
|
+
const rows = treeRows(state)
|
|
143
|
+
if (rows.length <= height) return { rows, more: 0 }
|
|
144
|
+
const here = rows.findIndex((row) => row.fileIndex === state.patchIndex)
|
|
145
|
+
const anchor = here === -1 ? 0 : here
|
|
146
|
+
const start = Math.max(0, Math.min(rows.length - height, anchor - Math.floor(height / 2)))
|
|
147
|
+
return { rows: rows.slice(start, start + height), more: rows.length - height }
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
export const commentsOn = (state: TuiState, fileIndex: number): number => {
|
|
151
|
+
const patch = state.patches[fileIndex]
|
|
152
|
+
if (patch === undefined) return 0
|
|
153
|
+
return state.pending.filter((entry) => entry.file === patch.path).length
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
const firstShownLine = (patch: Patch): number => {
|
|
157
|
+
const found = patch.rows.find((row) => Option.isSome(row.newLine))
|
|
158
|
+
return found === undefined ? 1 : Option.getOrElse(found.newLine, () => 1)
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
const lastShownLine = (patch: Patch): number => {
|
|
162
|
+
const found = patch.rows.findLast((row) => Option.isSome(row.newLine))
|
|
163
|
+
return found === undefined ? 0 : Option.getOrElse(found.newLine, () => 0)
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
export const hiddenLines = (state: TuiState): number => {
|
|
167
|
+
const patch = selectedPatch(state)
|
|
168
|
+
if (patch === undefined) return 0
|
|
169
|
+
const between = patch.hunks.reduce((total, hunk) => total + hunk.skipped, 0)
|
|
170
|
+
const above = Math.max(0, firstShownLine(patch) - 1)
|
|
171
|
+
const lines = state.source.at(-1)?.trim() === "" ? state.source.length - 1 : state.source.length
|
|
172
|
+
const below = lines === 0 ? 0 : Math.max(0, lines - lastShownLine(patch))
|
|
173
|
+
return between + above + below
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
export const markedRows = (state: TuiState): ReadonlySet<number> => {
|
|
177
|
+
const patch = selectedPatch(state)
|
|
178
|
+
if (patch === undefined) return new Set()
|
|
179
|
+
const here = state.pending.filter((entry) => entry.file === patch.path)
|
|
180
|
+
const rows = here.flatMap((entry) =>
|
|
181
|
+
patch.rows
|
|
182
|
+
.filter((row) =>
|
|
183
|
+
Option.match(row.newLine, {
|
|
184
|
+
onNone: () => false,
|
|
185
|
+
onSome: (line) => line >= entry.start && line <= entry.end,
|
|
186
|
+
}),
|
|
187
|
+
)
|
|
188
|
+
.map((row) => row.index),
|
|
189
|
+
)
|
|
190
|
+
return new Set(rows)
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
export const snippetOf = (state: TuiState, limit: number): ReadonlyArray<string> => {
|
|
194
|
+
const patch = selectedPatch(state)
|
|
195
|
+
if (patch === undefined) return []
|
|
196
|
+
const [from, to] = selectionRange(state)
|
|
197
|
+
return patch.rows
|
|
198
|
+
.slice(from, to + 1)
|
|
199
|
+
.slice(0, limit)
|
|
200
|
+
.map((row) => `${String(lineOf(row)).padStart(4)} ${row.text}`)
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
const lineOf = (row: Patch["rows"][number]): string =>
|
|
204
|
+
Option.match(row.newLine, { onNone: () => "-", onSome: (line) => String(line) })
|
|
205
|
+
|
|
206
|
+
export const composeTarget = (state: TuiState): string => {
|
|
207
|
+
const patch = selectedPatch(state)
|
|
208
|
+
if (patch === undefined) return ""
|
|
209
|
+
const [from, to] = selectionRange(state)
|
|
210
|
+
const first = sourceLineAt(state, from) ?? from + 1
|
|
211
|
+
const last = sourceLineAt(state, to) ?? to + 1
|
|
212
|
+
const span = first === last ? `${first}` : `${first}-${last}`
|
|
213
|
+
return `Comment on ${patch.path}:${span}`
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
export const selectionReadout = (state: TuiState): string => {
|
|
217
|
+
const patch = selectedPatch(state)
|
|
218
|
+
if (patch === undefined || !state.selecting) return ""
|
|
219
|
+
const [from, to] = selectionRange(state)
|
|
220
|
+
const lines = to - from + 1
|
|
221
|
+
return `${patch.path} ${lines} lines`
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
export const WHOLE_FILE = 100_000
|
|
225
|
+
|
|
226
|
+
export const CONTEXT_STEPS: ReadonlyArray<number> = [3, 10, 25, 60, WHOLE_FILE]
|
|
227
|
+
|
|
228
|
+
export const stepContext = (current: number, delta: number): number => {
|
|
229
|
+
const at = CONTEXT_STEPS.indexOf(current)
|
|
230
|
+
const next = Math.max(0, Math.min(CONTEXT_STEPS.length - 1, (at === -1 ? 0 : at) + delta))
|
|
231
|
+
return CONTEXT_STEPS[next] ?? current
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
export const newLineAt = (state: TuiState, row: number): number | undefined => {
|
|
235
|
+
const rows = selectedPatch(state)?.rows ?? []
|
|
236
|
+
const ahead = rows.slice(row).find((candidate) => Option.isSome(candidate.newLine))
|
|
237
|
+
if (ahead !== undefined) return Option.getOrUndefined(ahead.newLine)
|
|
238
|
+
const behind = rows.slice(0, row).findLast((candidate) => Option.isSome(candidate.newLine))
|
|
239
|
+
return behind === undefined ? undefined : Option.getOrUndefined(behind.newLine)
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
export const sourceLineAt = (state: TuiState, row: number): number | undefined => {
|
|
243
|
+
const patch = selectedPatch(state)
|
|
244
|
+
const found = patch?.rows[row]
|
|
245
|
+
if (found === undefined) return undefined
|
|
246
|
+
return Option.getOrUndefined(found.newLine) ?? Option.getOrUndefined(found.oldLine)
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
export const rowAtSourceLine = (patch: Patch, line: number): number => {
|
|
250
|
+
const found = patch.rows.find((row) =>
|
|
251
|
+
Option.match(row.newLine, { onNone: () => false, onSome: (value) => value >= line }),
|
|
252
|
+
)
|
|
253
|
+
return found?.index ?? 0
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
const lineOnSide = (row: Patch["rows"][number], side: "old" | "new"): number | undefined =>
|
|
257
|
+
Option.getOrUndefined(side === "old" ? row.oldLine : row.newLine)
|
|
258
|
+
|
|
259
|
+
export const commentRowsIn = (state: TuiState, fileIndex: number): ReadonlyArray<number> => {
|
|
260
|
+
const patch = state.patches[fileIndex]
|
|
261
|
+
if (patch === undefined) return []
|
|
262
|
+
const notes = [...state.pending, ...state.sent].filter((entry) => entry.file === patch.path)
|
|
263
|
+
const rows = patch.rows.filter((row) =>
|
|
264
|
+
notes.some((note) => lineOnSide(row, note.side) === note.end),
|
|
265
|
+
)
|
|
266
|
+
return rows.map((row) => row.index).toSorted((left, right) => left - right)
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
export const filesWithComments = (state: TuiState): ReadonlyArray<number> =>
|
|
270
|
+
state.patches.flatMap((patch, index) =>
|
|
271
|
+
[...state.pending, ...state.sent].some((entry) => entry.file === patch.path) ? [index] : [],
|
|
272
|
+
)
|
|
273
|
+
|
|
274
|
+
export const hunkStarts = (state: TuiState): ReadonlyArray<number> => {
|
|
275
|
+
const patch = selectedPatch(state)
|
|
276
|
+
return patch === undefined ? [] : patch.hunks.map((hunk) => hunk.startRow)
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
export const nextUnreviewed = (state: TuiState, from: number): number | undefined => {
|
|
280
|
+
const order = fileOrder(state)
|
|
281
|
+
const start = Math.max(0, order.indexOf(from))
|
|
282
|
+
const rotated = [...order.slice(start + 1), ...order.slice(0, start + 1)]
|
|
283
|
+
return rotated.find((index) => !isReviewed(state, index))
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
export const reviewedCount = (state: TuiState): string =>
|
|
287
|
+
`${state.vouched.length}/${state.patches.length} reviewed`
|
|
288
|
+
|
|
289
|
+
export const countsOf = (state: TuiState, fileIndex: number): string => {
|
|
290
|
+
const patch = state.patches[fileIndex]
|
|
291
|
+
if (patch === undefined) return ""
|
|
292
|
+
const total = patch.added + patch.removed
|
|
293
|
+
return total === 0 ? "" : String(total)
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
export const fileOrder = (state: TuiState): ReadonlyArray<number> =>
|
|
297
|
+
onSteps(state)
|
|
298
|
+
? stepFiles(state, state.stepIndex)
|
|
299
|
+
: treeRows(state).flatMap((row) => (row.fileIndex === undefined ? [] : [row.fileIndex]))
|
|
300
|
+
|
|
301
|
+
export const stepFile = (state: TuiState, delta: number): number => {
|
|
302
|
+
const order = fileOrder(state)
|
|
303
|
+
const position = order.indexOf(state.patchIndex)
|
|
304
|
+
if (position === -1) return order[0] ?? state.patchIndex
|
|
305
|
+
const next = Math.max(0, Math.min(order.length - 1, position + delta))
|
|
306
|
+
return order[next] ?? state.patchIndex
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
export const selectionRange = (state: TuiState): readonly [number, number] =>
|
|
310
|
+
state.anchorRow <= state.cursor ? [state.anchorRow, state.cursor] : [state.cursor, state.anchorRow]
|