-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathmenu-render.ts
More file actions
327 lines (300 loc) · 10.4 KB
/
menu-render.ts
File metadata and controls
327 lines (300 loc) · 10.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
import { Match } from "effect"
import { Box, Text } from "ink"
import React from "react"
import type { ProjectItem } from "@effect-template/lib/usecases/projects"
import type { CreateInputs, CreateStep } from "./menu-types.js"
import { createSteps, menuItems } from "./menu-types.js"
// CHANGE: render menu views with Ink without JSX
// WHY: keep UI logic separate from input/state reducers
// QUOTE(ТЗ): "TUI? Красивый, удобный"
// REF: user-request-2026-02-01-tui
// SOURCE: n/a
// FORMAT THEOREM: forall v: view(v) -> render(v)
// PURITY: SHELL
// EFFECT: n/a
// INVARIANT: menu renders all items once
// COMPLEXITY: O(n)
export const renderStepLabel = (step: CreateStep, defaults: CreateInputs): string =>
Match.value(step).pipe(
Match.when("repoUrl", () => "Repo URL"),
Match.when("repoRef", () => `Repo ref [${defaults.repoRef}]`),
Match.when("outDir", () => `Output dir [${defaults.outDir}]`),
Match.when("runUp", () => `Run docker compose up now? [${defaults.runUp ? "Y" : "n"}]`),
Match.when(
"mcpPlaywright",
() => `Enable Playwright MCP (Chromium sidecar)? [${defaults.enableMcpPlaywright ? "y" : "N"}]`
),
Match.when(
"force",
() => `Force recreate (overwrite files + wipe volumes)? [${defaults.force ? "y" : "N"}]`
),
Match.exhaustive
)
const renderMessage = (message: string | null): React.ReactElement | null => {
if (!message) {
return null
}
return React.createElement(
Box,
{ marginTop: 1 },
React.createElement(Text, { color: "magenta" }, message)
)
}
const renderLayout = (
title: string,
body: ReadonlyArray<React.ReactElement>,
message: string | null
): React.ReactElement => {
const el = React.createElement
const messageView = renderMessage(message)
const tail = messageView ? [messageView] : []
return el(
Box,
{ flexDirection: "column", padding: 1, borderStyle: "round" },
el(Text, { color: "cyan", bold: true }, title),
...body,
...tail
)
}
const compactElements = (
items: ReadonlyArray<React.ReactElement | null>
): ReadonlyArray<React.ReactElement> => items.filter((item): item is React.ReactElement => item !== null)
const renderMenuHints = (el: typeof React.createElement): React.ReactElement =>
el(
Box,
{ marginTop: 1, flexDirection: "column" },
el(Text, { color: "gray" }, "Hints:"),
el(Text, { color: "gray" }, " - Paste repo URL to create directly."),
el(
Text,
{ color: "gray" },
" - Aliases: create/c, select/s, info/i, status/ps, logs/l, down/d, down-all/da, delete/del, quit/q"
),
el(Text, { color: "gray" }, " - Use arrows and Enter to run.")
)
const renderMenuMessage = (
el: typeof React.createElement,
message: string | null
): React.ReactElement | null => {
if (!message || message.length === 0) {
return null
}
return el(
Box,
{ marginTop: 1, flexDirection: "column" },
...message
.split("\n")
.map((line, index) => el(Text, { key: `${index}-${line}`, color: "magenta" }, line))
)
}
export const renderMenu = (
cwd: string,
activeDir: string | null,
selected: number,
busy: boolean,
message: string | null
): React.ReactElement => {
const el = React.createElement
const activeLabel = `Active: ${activeDir ?? "(none)"}`
const cwdLabel = `CWD: ${cwd}`
const items = menuItems.map((item, index) => {
const indexLabel = `${index + 1})`
const prefix = index === selected ? ">" : " "
return el(
Text,
{ key: item.label, color: index === selected ? "green" : "white" },
`${prefix} ${indexLabel} ${item.label}`
)
})
const busyView = busy
? el(Box, { marginTop: 1 }, el(Text, { color: "yellow" }, "Running..."))
: null
const messageView = renderMenuMessage(el, message)
const hints = renderMenuHints(el)
return renderLayout(
"docker-git",
compactElements([
el(Text, null, activeLabel),
el(Text, null, cwdLabel),
el(Box, { flexDirection: "column", marginTop: 1 }, ...items),
hints,
busyView,
messageView
]),
null
)
}
export const renderCreate = (
label: string,
buffer: string,
message: string | null,
stepIndex: number,
defaults: CreateInputs
): React.ReactElement => {
const el = React.createElement
const steps = createSteps.map((step, index) =>
el(
Text,
{ key: step, color: index === stepIndex ? "green" : "gray" },
`${index === stepIndex ? ">" : " "} ${renderStepLabel(step, defaults)}`
)
)
return renderLayout(
"docker-git / Create",
[
el(Box, { flexDirection: "column", marginTop: 1 }, ...steps),
el(
Box,
{ marginTop: 1 },
el(Text, null, `${label}: `),
el(Text, { color: "green" }, buffer)
),
el(Box, { marginTop: 1 }, el(Text, { color: "gray" }, "Enter = next, Esc = cancel."))
],
message
)
}
const formatRepoRef = (repoRef: string): string => {
const trimmed = repoRef.trim()
const prPrefix = "refs/pull/"
if (trimmed.startsWith(prPrefix)) {
const rest = trimmed.slice(prPrefix.length)
const number = rest.split("/")[0] ?? rest
return `PR#${number}`
}
return trimmed.length > 0 ? trimmed : "main"
}
const renderSelectDetails = (
el: typeof React.createElement,
purpose: SelectPurpose,
item: ProjectItem | undefined
): ReadonlyArray<React.ReactElement> => {
if (!item) {
return [el(Text, { color: "gray", wrap: "truncate" }, "No project selected.")]
}
const refLabel = formatRepoRef(item.repoRef)
const authSuffix = item.authorizedKeysExists ? "" : " (missing)"
return Match.value(purpose).pipe(
Match.when("Info", () => [
el(Text, { color: "cyan", bold: true, wrap: "truncate" }, "Connection info"),
el(Text, { wrap: "wrap" }, `Project directory: ${item.projectDir}`),
el(Text, { wrap: "wrap" }, `Container: ${item.containerName}`),
el(Text, { wrap: "wrap" }, `Service: ${item.serviceName}`),
el(Text, { wrap: "wrap" }, `SSH command: ${item.sshCommand}`),
el(Text, { wrap: "wrap" }, `Repo: ${item.repoUrl} (${refLabel})`),
el(Text, { wrap: "wrap" }, `Workspace: ${item.targetDir}`),
el(Text, { wrap: "wrap" }, `Authorized keys: ${item.authorizedKeysPath}${authSuffix}`),
el(Text, { wrap: "wrap" }, `Env global: ${item.envGlobalPath}`),
el(Text, { wrap: "wrap" }, `Env project: ${item.envProjectPath}`),
el(Text, { wrap: "wrap" }, `Codex auth: ${item.codexAuthPath} -> ${item.codexHome}`)
]),
Match.when("Delete", () => [
el(Text, { color: "cyan", bold: true, wrap: "truncate" }, "Delete project"),
el(Text, { wrap: "wrap" }, `Project directory: ${item.projectDir}`),
el(Text, { wrap: "wrap" }, `Container: ${item.containerName}`),
el(Text, { wrap: "wrap" }, `Repo: ${item.repoUrl} (${refLabel})`),
el(Text, { wrap: "wrap" }, "Removes the project folder (no git history rewrite).")
]),
Match.orElse(() => [
el(Text, { color: "cyan", bold: true, wrap: "truncate" }, "Details"),
el(Text, { wrap: "truncate" }, `Repo: ${item.repoUrl}`),
el(Text, { wrap: "truncate" }, `Ref: ${item.repoRef}`),
el(Text, { wrap: "truncate" }, `Project dir: ${item.projectDir}`),
el(Text, { wrap: "truncate" }, `Workspace: ${item.targetDir}`),
el(Text, { wrap: "truncate" }, `SSH: ${item.sshCommand}`)
])
)
}
type SelectPurpose = "Connect" | "Down" | "Info" | "Delete"
const selectTitle = (purpose: SelectPurpose): string =>
Match.value(purpose).pipe(
Match.when("Connect", () => "docker-git / Select project"),
Match.when("Down", () => "docker-git / Stop container"),
Match.when("Info", () => "docker-git / Show connection info"),
Match.when("Delete", () => "docker-git / Delete project"),
Match.exhaustive
)
const selectHint = (purpose: SelectPurpose): string =>
Match.value(purpose).pipe(
Match.when("Connect", () => "Enter = select + SSH, Esc = back"),
Match.when("Down", () => "Enter = stop container, Esc = back"),
Match.when("Info", () => "Use arrows to browse details, Enter = set active, Esc = back"),
Match.when("Delete", () => "Enter = ask/confirm delete, Esc = cancel"),
Match.exhaustive
)
const buildSelectLabels = (
items: ReadonlyArray<ProjectItem>,
selected: number
): ReadonlyArray<string> =>
items.map((item, index) => {
const prefix = index === selected ? ">" : " "
const refLabel = formatRepoRef(item.repoRef)
return `${prefix} ${index + 1}. ${item.displayName} (${refLabel})`
})
const computeListWidth = (labels: ReadonlyArray<string>): number => {
const maxLabelWidth = labels.length > 0 ? Math.max(...labels.map((label) => label.length)) : 24
return Math.min(Math.max(maxLabelWidth + 2, 28), 54)
}
const renderSelectListBox = (
el: typeof React.createElement,
items: ReadonlyArray<ProjectItem>,
selected: number,
labels: ReadonlyArray<string>,
width: number
): React.ReactElement => {
const list = labels.map((label, index) =>
el(
Text,
{
key: items[index]?.projectDir ?? String(index),
color: index === selected ? "green" : "white",
wrap: "truncate"
},
label
)
)
return el(
Box,
{ flexDirection: "column", width },
...(list.length > 0 ? list : [el(Text, { color: "gray" }, "No projects found.")])
)
}
const renderSelectDetailsBox = (
el: typeof React.createElement,
purpose: SelectPurpose,
items: ReadonlyArray<ProjectItem>,
selected: number
): React.ReactElement => {
const details = renderSelectDetails(el, purpose, items[selected])
return el(
Box,
{ flexDirection: "column", marginLeft: 2, flexGrow: 1 },
...details
)
}
export const renderSelect = (
purpose: SelectPurpose,
items: ReadonlyArray<ProjectItem>,
selected: number,
confirmDelete: boolean,
message: string | null
): React.ReactElement => {
const el = React.createElement
const listLabels = buildSelectLabels(items, selected)
const listWidth = computeListWidth(listLabels)
const listBox = renderSelectListBox(el, items, selected, listLabels, listWidth)
const detailsBox = renderSelectDetailsBox(el, purpose, items, selected)
const baseHint = selectHint(purpose)
const deleteHint = purpose === "Delete" && confirmDelete
? "Confirm mode: Enter = delete now, Esc = cancel"
: baseHint
const hints = el(Box, { marginTop: 1 }, el(Text, { color: "gray" }, deleteHint))
return renderLayout(
selectTitle(purpose),
[
el(Box, { flexDirection: "row", marginTop: 1 }, listBox, detailsBox),
hints
],
message
)
}