teamsmith 0.0.0-stage → 0.1.2
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/LICENSE +21 -0
- package/README.md +158 -2
- package/bin/team.mjs +59 -0
- package/install.sh +94 -0
- package/package.json +37 -4
- package/skills/teamsmith/CHANGELOG.md +763 -0
- package/skills/teamsmith/SKILL.md +312 -0
- package/skills/teamsmith/extension/team-bg.ts +435 -0
- package/skills/teamsmith/extension/team-inbox-watch.ts +1412 -0
- package/skills/teamsmith/extension/team-notify.ts +663 -0
- package/skills/teamsmith/references/agent-adapters.md +643 -0
- package/skills/teamsmith/references/config.md +477 -0
- package/skills/teamsmith/references/meeting.md +124 -0
- package/skills/teamsmith/references/memory.md +133 -0
- package/skills/teamsmith/references/migration.md +227 -0
- package/skills/teamsmith/references/openspec.md +222 -0
- package/skills/teamsmith/references/philosophy.md +95 -0
- package/skills/teamsmith/references/protocol.md +712 -0
- package/skills/teamsmith/references/troubleshooting.md +1384 -0
- package/skills/teamsmith/references/workflows.md +448 -0
- package/skills/teamsmith/scripts/lib/cmd-agents.sh +1709 -0
- package/skills/teamsmith/scripts/lib/cmd-bg.sh +284 -0
- package/skills/teamsmith/scripts/lib/cmd-bootstrap.sh +311 -0
- package/skills/teamsmith/scripts/lib/cmd-config.sh +1156 -0
- package/skills/teamsmith/scripts/lib/cmd-death.sh +562 -0
- package/skills/teamsmith/scripts/lib/cmd-docs.sh +149 -0
- package/skills/teamsmith/scripts/lib/cmd-draft.sh +124 -0
- package/skills/teamsmith/scripts/lib/cmd-init.sh +212 -0
- package/skills/teamsmith/scripts/lib/cmd-meeting.sh +904 -0
- package/skills/teamsmith/scripts/lib/cmd-outbox.sh +196 -0
- package/skills/teamsmith/scripts/lib/cmd-project.sh +642 -0
- package/skills/teamsmith/scripts/lib/cmd-review.sh +1393 -0
- package/skills/teamsmith/scripts/lib/cmd-status.sh +1399 -0
- package/skills/teamsmith/scripts/lib/cmd-update.sh +191 -0
- package/skills/teamsmith/scripts/lib/cmd-watch.sh +1681 -0
- package/skills/teamsmith/scripts/lib/common.sh +4964 -0
- package/skills/teamsmith/scripts/lib/draft-entry.sh +51 -0
- package/skills/teamsmith/scripts/lib/outbox.sh +2042 -0
- package/skills/teamsmith/scripts/lib/tmux-argv.sh +165 -0
- package/skills/teamsmith/scripts/monitor.mjs +616 -0
- package/skills/teamsmith/scripts/panel/README.md +192 -0
- package/skills/teamsmith/scripts/panel/build.sh +47 -0
- package/skills/teamsmith/scripts/panel/bun.lock +109 -0
- package/skills/teamsmith/scripts/panel/draft-send.sh +68 -0
- package/skills/teamsmith/scripts/panel/package.json +24 -0
- package/skills/teamsmith/scripts/panel/panel.js +388 -0
- package/skills/teamsmith/scripts/panel/src/App.tsx +2150 -0
- package/skills/teamsmith/scripts/panel/src/clipboard.ts +154 -0
- package/skills/teamsmith/scripts/panel/src/compose.ts +691 -0
- package/skills/teamsmith/scripts/panel/src/data.ts +555 -0
- package/skills/teamsmith/scripts/panel/src/format.ts +50 -0
- package/skills/teamsmith/scripts/panel/src/layout.ts +2562 -0
- package/skills/teamsmith/scripts/panel/src/main.tsx +894 -0
- package/skills/teamsmith/scripts/panel/src/markdown.ts +340 -0
- package/skills/teamsmith/scripts/panel/src/sanitize.ts +42 -0
- package/skills/teamsmith/scripts/panel/src/settings.ts +154 -0
- package/skills/teamsmith/scripts/panel/src/strings/en.ts +416 -0
- package/skills/teamsmith/scripts/panel/src/strings/index.ts +65 -0
- package/skills/teamsmith/scripts/panel/src/strings/types.ts +9 -0
- package/skills/teamsmith/scripts/panel/src/strings/zh.ts +414 -0
- package/skills/teamsmith/scripts/panel/src/theme.ts +116 -0
- package/skills/teamsmith/scripts/panel/src/types.ts +652 -0
- package/skills/teamsmith/scripts/panel/src/width.ts +105 -0
- package/skills/teamsmith/scripts/panel/tsconfig.json +16 -0
- package/skills/teamsmith/scripts/shim/signal-gate +156 -0
- package/skills/teamsmith/scripts/shim/tmux +403 -0
- package/skills/teamsmith/scripts/team +143 -0
- package/skills/teamsmith/templates/AGENTS.section.md.tmpl +147 -0
- package/skills/teamsmith/templates/BOARD.md.tmpl +18 -0
- package/skills/teamsmith/templates/DECISIONS.md.tmpl +22 -0
- package/skills/teamsmith/templates/OWNERSHIP.md.tmpl +25 -0
- package/skills/teamsmith/templates/PROTOCOL.md.tmpl +84 -0
- package/skills/teamsmith/templates/ROADMAP.md.tmpl +29 -0
- package/skills/teamsmith/templates/config.sh.tmpl +106 -0
- package/skills/teamsmith/templates/memory-seed.md.tmpl +46 -0
- package/skills/teamsmith/templates/pm-prompt.md.tmpl +110 -0
- package/skills/teamsmith/templates/report.md.tmpl +36 -0
- package/skills/teamsmith/templates/task.md.tmpl +65 -0
- package/skills/teamsmith/templates/threads-README.md +17 -0
- package/skills/teamsmith/tests/bg-ledger-harness.mjs +682 -0
- package/skills/teamsmith/tests/bg-ledger.sh +307 -0
- package/skills/teamsmith/tests/checkout-shape-probe.sh +800 -0
- package/skills/teamsmith/tests/config-cli.sh +1562 -0
- package/skills/teamsmith/tests/container-tmux.sh +493 -0
- package/skills/teamsmith/tests/coverage-inventory.sh +173 -0
- package/skills/teamsmith/tests/death-cause.sh +564 -0
- package/skills/teamsmith/tests/delivery-truth.sh +1231 -0
- package/skills/teamsmith/tests/fake-tui.py +450 -0
- package/skills/teamsmith/tests/fixtures/delivery-truth-real/README.md +60 -0
- package/skills/teamsmith/tests/fixtures/delivery-truth-real/judge-second.py +139 -0
- package/skills/teamsmith/tests/fixtures/delivery-truth-real/mock-server.py +29 -0
- package/skills/teamsmith/tests/fixtures/delivery-truth-real/observe.ts +20 -0
- package/skills/teamsmith/tests/fixtures/delivery-truth-real/run-case.sh +79 -0
- package/skills/teamsmith/tests/fixtures/delivery-truth-real/scenario.sh +175 -0
- package/skills/teamsmith/tests/fixtures/p55/flip-p49.sh +229 -0
- package/skills/teamsmith/tests/fixtures/p71-incident/README.md +22 -0
- package/skills/teamsmith/tests/fixtures/p71-incident/dev2.seen +8 -0
- package/skills/teamsmith/tests/fixtures/p71-incident/dev2.wake +9 -0
- package/skills/teamsmith/tests/fixtures/p71-incident/pm-nudges.wake +3 -0
- package/skills/teamsmith/tests/fixtures/signal-lint-cases.txt +40 -0
- package/skills/teamsmith/tests/flip-glue.sh +162 -0
- package/skills/teamsmith/tests/flip-m12-26c.sh +129 -0
- package/skills/teamsmith/tests/flip-m12.sh +185 -0
- package/skills/teamsmith/tests/flip-m16.sh +166 -0
- package/skills/teamsmith/tests/flip-m17.sh +204 -0
- package/skills/teamsmith/tests/flip-m33.sh +284 -0
- package/skills/teamsmith/tests/flip-m34.sh +96 -0
- package/skills/teamsmith/tests/flip-m37.sh +185 -0
- package/skills/teamsmith/tests/flip-m4.3.sh +375 -0
- package/skills/teamsmith/tests/flip-m43.sh +128 -0
- package/skills/teamsmith/tests/flip-m44.sh +262 -0
- package/skills/teamsmith/tests/flip-m45.sh +244 -0
- package/skills/teamsmith/tests/flip-m46.sh +238 -0
- package/skills/teamsmith/tests/flip-m48.sh +304 -0
- package/skills/teamsmith/tests/flip-m6.2.sh +205 -0
- package/skills/teamsmith/tests/flip-m6.3.sh +283 -0
- package/skills/teamsmith/tests/flip-m6.4.sh +267 -0
- package/skills/teamsmith/tests/flip-m6.5.sh +113 -0
- package/skills/teamsmith/tests/flip-m7.1.sh +135 -0
- package/skills/teamsmith/tests/flip-m7.2.sh +321 -0
- package/skills/teamsmith/tests/flip-p144.sh +112 -0
- package/skills/teamsmith/tests/flip-p156.sh +133 -0
- package/skills/teamsmith/tests/flip-p158.sh +301 -0
- package/skills/teamsmith/tests/flip-p160.sh +182 -0
- package/skills/teamsmith/tests/flip-p171.sh +249 -0
- package/skills/teamsmith/tests/flip-p18.1.sh +95 -0
- package/skills/teamsmith/tests/flip-p20.sh +286 -0
- package/skills/teamsmith/tests/flip-p201.sh +249 -0
- package/skills/teamsmith/tests/flip-p210.sh +239 -0
- package/skills/teamsmith/tests/flip-p211.sh +152 -0
- package/skills/teamsmith/tests/flip-p217.sh +288 -0
- package/skills/teamsmith/tests/flip-p22.sh +123 -0
- package/skills/teamsmith/tests/flip-p23.sh +239 -0
- package/skills/teamsmith/tests/flip-p25.sh +165 -0
- package/skills/teamsmith/tests/flip-p71.sh +184 -0
- package/skills/teamsmith/tests/flip-p72.sh +202 -0
- package/skills/teamsmith/tests/frames/README.md +176 -0
- package/skills/teamsmith/tests/frames/p78-conversation-rule-below-box.txt +6 -0
- package/skills/teamsmith/tests/frames/p78-cursor-mid-draft.txt +6 -0
- package/skills/teamsmith/tests/frames/p78-draft-rule-below-cursor-line.txt +6 -0
- package/skills/teamsmith/tests/frames/p78-draft-rule-below-cursor.txt +6 -0
- package/skills/teamsmith/tests/frames/p78-draft-rule-blank-region.txt +6 -0
- package/skills/teamsmith/tests/frames/p78-draft-rule-only.txt +5 -0
- package/skills/teamsmith/tests/frames/p78-spinner-row-below-cursor.txt +6 -0
- package/skills/teamsmith/tests/frames/p78-wider-rule-below-cursor.txt +6 -0
- package/skills/teamsmith/tests/frames/p86-f1-mixed-width-disjoint-box.txt +8 -0
- package/skills/teamsmith/tests/frames/p86-f1-narrower-width-disjoint-box.txt +8 -0
- package/skills/teamsmith/tests/frames/p86-f1-spinner-top-disjoint-box.txt +8 -0
- package/skills/teamsmith/tests/frames/pi-0.85.1-update-banner.txt +30 -0
- package/skills/teamsmith/tests/frames/pi-0.87.0-draft-half-sentence.txt +30 -0
- package/skills/teamsmith/tests/frames/pi-0.87.0-empty-box.txt +30 -0
- package/skills/teamsmith/tests/frames/pi-0.87.0-one-line-draft.txt +30 -0
- package/skills/teamsmith/tests/frames/pi-0.87.0-project-trust-prompt.txt +30 -0
- package/skills/teamsmith/tests/frames/pi-0.99.2-empty-editor.txt +32 -0
- package/skills/teamsmith/tests/frames/pi-0.99.2-human-draft.txt +32 -0
- package/skills/teamsmith/tests/frames/pi-1.0.0-git-error-in-box.txt +32 -0
- package/skills/teamsmith/tests/gate-guard.sh +112 -0
- package/skills/teamsmith/tests/guard-matrix.sh +233 -0
- package/skills/teamsmith/tests/install-shape.sh +731 -0
- package/skills/teamsmith/tests/ledger-ban.sh +612 -0
- package/skills/teamsmith/tests/lib/box-judge.sh +68 -0
- package/skills/teamsmith/tests/lib/checkout-shape.sh +129 -0
- package/skills/teamsmith/tests/lib/loop-scan.awk +114 -0
- package/skills/teamsmith/tests/lib/pty-wait.sh +676 -0
- package/skills/teamsmith/tests/lib/section-guard.sh +855 -0
- package/skills/teamsmith/tests/lib/shell-lex.pl +261 -0
- package/skills/teamsmith/tests/lib/tmp-root.sh +528 -0
- package/skills/teamsmith/tests/lib/tmux-cap.sh +35 -0
- package/skills/teamsmith/tests/lib/tmux-iso-call-probe.sh +81 -0
- package/skills/teamsmith/tests/lib/tmux-iso-probe.sh +46 -0
- package/skills/teamsmith/tests/lib/tmux-iso.sh +381 -0
- package/skills/teamsmith/tests/load-experiment.sh +290 -0
- package/skills/teamsmith/tests/loop-inventory.tsv +84 -0
- package/skills/teamsmith/tests/panel-b2-slow-stub.sh +19 -0
- package/skills/teamsmith/tests/panel-b2.sh +1183 -0
- package/skills/teamsmith/tests/panel-b3-pty-mouse.py +240 -0
- package/skills/teamsmith/tests/panel-b3-pty-tmux-mouse.py +138 -0
- package/skills/teamsmith/tests/panel-b3-stub.sh +279 -0
- package/skills/teamsmith/tests/panel-b3.sh +1301 -0
- package/skills/teamsmith/tests/panel-choices.sh +572 -0
- package/skills/teamsmith/tests/panel-clipboard-model.ts +187 -0
- package/skills/teamsmith/tests/panel-compose-model.ts +290 -0
- package/skills/teamsmith/tests/panel-contrast.mjs +81 -0
- package/skills/teamsmith/tests/panel-cpu-premise.sh +173 -0
- package/skills/teamsmith/tests/panel-cpu.sh +391 -0
- package/skills/teamsmith/tests/panel-flip-m54.sh +207 -0
- package/skills/teamsmith/tests/panel-flip-p123.sh +278 -0
- package/skills/teamsmith/tests/panel-flip-p125.sh +318 -0
- package/skills/teamsmith/tests/panel-flip-p128.sh +259 -0
- package/skills/teamsmith/tests/panel-keyprobe-pty.py +99 -0
- package/skills/teamsmith/tests/panel-keyprobe-stub.sh +41 -0
- package/skills/teamsmith/tests/panel-keyprobe.sh +82 -0
- package/skills/teamsmith/tests/panel-keyprobe.tsx +116 -0
- package/skills/teamsmith/tests/panel-knobs.sh +83 -0
- package/skills/teamsmith/tests/panel-p21.sh +2181 -0
- package/skills/teamsmith/tests/panel-snapshots.sh +802 -0
- package/skills/teamsmith/tests/panel-strings.mjs +195 -0
- package/skills/teamsmith/tests/perf.sh +740 -0
- package/skills/teamsmith/tests/pm-box-real.sh +350 -0
- package/skills/teamsmith/tests/pulse-nudge-key.sh +563 -0
- package/skills/teamsmith/tests/routes.sh +1652 -0
- package/skills/teamsmith/tests/section-budgets.tsv +150 -0
- package/skills/teamsmith/tests/section-guard.sh +231 -0
- package/skills/teamsmith/tests/section-needs-audit.sh +94 -0
- package/skills/teamsmith/tests/section-paths.tsv +142 -0
- package/skills/teamsmith/tests/section-select.sh +655 -0
- package/skills/teamsmith/tests/signal-gate.sh +366 -0
- package/skills/teamsmith/tests/signal-lint-legacy.txt +14 -0
- package/skills/teamsmith/tests/signal-lint.pl +416 -0
- package/skills/teamsmith/tests/skill-load.mjs +65 -0
- package/skills/teamsmith/tests/smoke.sh +19711 -0
- package/skills/teamsmith/tests/snapshots/zh-dark-120-detail.txt +32 -0
- package/skills/teamsmith/tests/snapshots/zh-dark-120-p4.txt +32 -0
- package/skills/teamsmith/tests/snapshots/zh-dark-120.txt +32 -0
- package/skills/teamsmith/tests/snapshots/zh-dark-160-p4.txt +32 -0
- package/skills/teamsmith/tests/snapshots/zh-dark-160.txt +32 -0
- package/skills/teamsmith/tests/snapshots/zh-dark-59-detail.txt +32 -0
- package/skills/teamsmith/tests/snapshots/zh-dark-59-p4.txt +32 -0
- package/skills/teamsmith/tests/snapshots/zh-dark-59.txt +32 -0
- package/skills/teamsmith/tests/snapshots/zh-dark-99-p4.txt +32 -0
- package/skills/teamsmith/tests/snapshots/zh-dark-99.txt +32 -0
- package/skills/teamsmith/tests/snapshots/zh-light-120-detail.txt +32 -0
- package/skills/teamsmith/tests/snapshots/zh-light-120-p4.txt +32 -0
- package/skills/teamsmith/tests/snapshots/zh-light-120.txt +32 -0
- package/skills/teamsmith/tests/snapshots/zh-light-160-p4.txt +32 -0
- package/skills/teamsmith/tests/snapshots/zh-light-160.txt +32 -0
- package/skills/teamsmith/tests/snapshots/zh-light-59-detail.txt +32 -0
- package/skills/teamsmith/tests/snapshots/zh-light-59-p4.txt +32 -0
- package/skills/teamsmith/tests/snapshots/zh-light-59.txt +32 -0
- package/skills/teamsmith/tests/snapshots/zh-light-99-p4.txt +32 -0
- package/skills/teamsmith/tests/snapshots/zh-light-99.txt +32 -0
- package/skills/teamsmith/tests/spec-ledger-refs.tsv +78 -0
- package/skills/teamsmith/tests/spec-refs.sh +826 -0
- package/skills/teamsmith/tests/task-header-model.sh +267 -0
- package/skills/teamsmith/tests/team-bg-flip.sh +239 -0
- package/skills/teamsmith/tests/team-bg-harness.mjs +392 -0
- package/skills/teamsmith/tests/team-bg-stop.sh +434 -0
- package/skills/teamsmith/tests/team-inbox-watch-flip.sh +191 -0
- package/skills/teamsmith/tests/team-inbox-watch-harness.mjs +1559 -0
- package/skills/teamsmith/tests/tmp-hygiene.sh +1190 -0
- package/skills/teamsmith/tests/tmux-lint-legacy.txt +31 -0
- package/skills/teamsmith/tests/tmux-lint.pl +604 -0
- package/skills/teamsmith-init/SKILL.md +100 -0
- package/skills/teamsmith-init/references/bootstrap.md +74 -0
- package/skills/teamsmith-init/templates/bootstrap-prompt.md.tmpl +46 -0
|
@@ -0,0 +1,691 @@
|
|
|
1
|
+
// The compose entry (pulse-console B2): the pure model behind the bottom input line.
|
|
2
|
+
//
|
|
3
|
+
// Everything here is a function of its arguments — no I/O, no timers — so the Ink frame, the
|
|
4
|
+
// send bridge's receipt mapping and the pty fixtures all read the same rules. The three things
|
|
5
|
+
// that are contractual rather than cosmetic:
|
|
6
|
+
//
|
|
7
|
+
// * one read from the terminal is one input event (E6 §2.3): text is appended char by char,
|
|
8
|
+
// Backspace removes one **codepoint** (not one UTF-16 unit), and a wide glyph counts two
|
|
9
|
+
// columns of cursor — the cursor column is what `tmux display -p '#{cursor_x}'` measures;
|
|
10
|
+
// * a paste is not a submit: a `\r` (or CRLF) inside a chunk becomes a line break (E6 §2.2),
|
|
11
|
+
// only a lone Enter submits, and bracketed-paste markers are swallowed;
|
|
12
|
+
// * the receipt maps the send command's rc + machine tokens to exactly three honest states
|
|
13
|
+
// (delivered / queued / held) — never from the human prose the CLI prints alongside them.
|
|
14
|
+
|
|
15
|
+
import { dispWidth } from './width.js'
|
|
16
|
+
import { fill, type Strings } from './strings/index.js'
|
|
17
|
+
|
|
18
|
+
/** The console's own draft file, inside the project's state directory (E6 §3.7). */
|
|
19
|
+
export const DRAFT_FILE = 'draft.md'
|
|
20
|
+
|
|
21
|
+
/** `> ` — two columns, so `中文ab` puts the real terminal cursor at column 8 (E6 §1.2). */
|
|
22
|
+
export const PROMPT = '> '
|
|
23
|
+
export const CONTINUATION = ' '
|
|
24
|
+
|
|
25
|
+
export type ComposeMode = 'message' | 'reason' | 'filter' | 'setting'
|
|
26
|
+
|
|
27
|
+
export interface Receipt {
|
|
28
|
+
state: 'delivered' | 'queued' | 'held' | 'error'
|
|
29
|
+
/** The machine token(s) the state was mapped from (rc / outcome / result). */
|
|
30
|
+
token: string
|
|
31
|
+
/** One human line for the input area; already sanitized by the caller. */
|
|
32
|
+
detail: string
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export interface Intake {
|
|
36
|
+
/** Text to append to the draft (line breaks normalized to `\n`). */
|
|
37
|
+
append: string
|
|
38
|
+
/** True only for a lone Enter. */
|
|
39
|
+
submit: boolean
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** Split a chunk into its bracketed-paste body, if the markers are present. */
|
|
43
|
+
function pasteBody(chunk: string): { body: string | null; open: boolean; close: boolean } {
|
|
44
|
+
// Ink may strip the leading ESC from an escape sequence (E6 §1.1c), so both forms are
|
|
45
|
+
// accepted: `\x1b[200~…` and the bare `[200~…` Ink hands over after consuming the ESC.
|
|
46
|
+
const open = /(?:\u001b)?\[200~/.exec(chunk)
|
|
47
|
+
const close = /(?:\u001b)?\[201~/.exec(chunk)
|
|
48
|
+
if (!open && !close) return { body: null, open: false, close: false }
|
|
49
|
+
let body = ''
|
|
50
|
+
if (open) {
|
|
51
|
+
const after = chunk.slice((open.index ?? 0) + open[0].length)
|
|
52
|
+
const end = close ? after.indexOf(close[0]) : -1
|
|
53
|
+
body = end >= 0 ? after.slice(0, end) : after
|
|
54
|
+
return { body, open: true, close: close !== null || end >= 0 }
|
|
55
|
+
}
|
|
56
|
+
const end = close ? chunk.indexOf(close[0]) : -1
|
|
57
|
+
body = end >= 0 ? chunk.slice(0, end) : chunk
|
|
58
|
+
return { body, open: false, close: close !== null }
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** CRLF/CR → LF; nothing else is touched. */
|
|
62
|
+
export function normalizeNewlines(text: string): string {
|
|
63
|
+
return String(text ?? '').replace(/\r\n/g, '\n').replace(/\r/g, '\n')
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/** Drop control characters a paste may carry (a tab is kept as a space). */
|
|
67
|
+
function stripControls(text: string): string {
|
|
68
|
+
let out = ''
|
|
69
|
+
for (const ch of text) {
|
|
70
|
+
const cp = ch.codePointAt(0) ?? 0
|
|
71
|
+
if (ch === '\n' || ch === '\t') {
|
|
72
|
+
out += ch === '\t' ? ' ' : '\n'
|
|
73
|
+
continue
|
|
74
|
+
}
|
|
75
|
+
if (cp < 0x20 || cp === 0x7f) continue
|
|
76
|
+
out += ch
|
|
77
|
+
}
|
|
78
|
+
return out
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* One input event → what it does to the draft.
|
|
83
|
+
*
|
|
84
|
+
* `pasteOpen` is the caller's carry-over flag: a bracketed paste may arrive split across reads,
|
|
85
|
+
* so `[200~` without a closing marker keeps every following chunk in paste mode until `[201~`.
|
|
86
|
+
*/
|
|
87
|
+
export function intake(chunk: string, pasteOpen = false): Intake & { pasteOpen: boolean } {
|
|
88
|
+
const raw = String(chunk ?? '')
|
|
89
|
+
if (pasteOpen) {
|
|
90
|
+
const { body, close } = pasteBody(raw)
|
|
91
|
+
const text = body === null ? raw : body
|
|
92
|
+
const nextOpen = !close && body === null
|
|
93
|
+
return { append: stripControls(normalizeNewlines(text)), submit: false, pasteOpen: nextOpen }
|
|
94
|
+
}
|
|
95
|
+
const { body, open, close } = pasteBody(raw)
|
|
96
|
+
if (open || body !== null) {
|
|
97
|
+
const text = body ?? ''
|
|
98
|
+
return { append: stripControls(normalizeNewlines(text)), submit: false, pasteOpen: open && !close }
|
|
99
|
+
}
|
|
100
|
+
// A lone newline is the Enter key; newlines inside a longer chunk are a paste's line breaks.
|
|
101
|
+
if (raw === '\r' || raw === '\n' || raw === '\r\n') return { append: '', submit: true, pasteOpen: false }
|
|
102
|
+
return { append: stripControls(normalizeNewlines(raw)), submit: false, pasteOpen: false }
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/** Backspace: remove one codepoint, never half of a surrogate pair (CJK/emoji safe). */
|
|
106
|
+
export function backspace(draft: string): string {
|
|
107
|
+
const cps = [...String(draft ?? '')]
|
|
108
|
+
cps.pop()
|
|
109
|
+
return cps.join('')
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/** Append text produced by the editor relay (or any non-terminal source), normalized. */
|
|
113
|
+
export function appendText(draft: string, text: string): string {
|
|
114
|
+
return String(draft ?? '') + normalizeNewlines(stripControls(text))
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/** The prompt for the mode: the reason prompt names why standby is being switched on. */
|
|
118
|
+
export function promptOf(mode: ComposeMode): string {
|
|
119
|
+
return PROMPT
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* The visible lines of the input area; the last one ends where the cursor belongs.
|
|
124
|
+
*
|
|
125
|
+
* A long logical line wraps by **display width** (CJK counts two columns) and every wrapped or
|
|
126
|
+
* pasted line gets the continuation prefix, so the input area never writes a row wider than the
|
|
127
|
+
* pane and the cursor column is always the width of the last rendered line.
|
|
128
|
+
*/
|
|
129
|
+
/**
|
|
130
|
+
* The draft as drawn rows: the one map `inputLines`, the cursor placement and the window share.
|
|
131
|
+
*
|
|
132
|
+
* `room` is what one drawn row has after the prompt/continuation prefix (both are two columns).
|
|
133
|
+
* Every row but the text's first carries the continuation prefix, so a uniform room is exactly
|
|
134
|
+
* the old per-line greedy wrap; the difference is that each row now knows the codepoint span it
|
|
135
|
+
* draws — which is what lets the insertion point and the window agree with the pixels.
|
|
136
|
+
*/
|
|
137
|
+
export interface ComposeRow {
|
|
138
|
+
/** The row's first codepoint index (inclusive) into `[...draft]`. */
|
|
139
|
+
start: number
|
|
140
|
+
/** One past the row's last codepoint index (exclusive). */
|
|
141
|
+
end: number
|
|
142
|
+
/** The logical (`\n`-separated) line this row belongs to. */
|
|
143
|
+
line: number
|
|
144
|
+
/** The row's text, without the prompt/continuation prefix. */
|
|
145
|
+
text: string
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/** Wrap `text` into display rows of at most `room` columns (a wide glyph is one codepoint). */
|
|
149
|
+
export function wrapRows(text: string, room: number): ComposeRow[] {
|
|
150
|
+
const cps = [...String(text ?? '')]
|
|
151
|
+
const cap = Math.max(1, Math.floor(room) || 1)
|
|
152
|
+
const rows: ComposeRow[] = []
|
|
153
|
+
let line = 0
|
|
154
|
+
let start = 0
|
|
155
|
+
let cur = ''
|
|
156
|
+
let i = 0
|
|
157
|
+
while (i <= cps.length) {
|
|
158
|
+
if (i === cps.length || cps[i] === '\n') {
|
|
159
|
+
rows.push({ start, end: i, line, text: cur })
|
|
160
|
+
line += 1
|
|
161
|
+
start = i + 1
|
|
162
|
+
cur = ''
|
|
163
|
+
i += 1
|
|
164
|
+
continue
|
|
165
|
+
}
|
|
166
|
+
const ch = cps[i]
|
|
167
|
+
if (cur && dispWidth(cur + ch) > cap) {
|
|
168
|
+
rows.push({ start, end: i, line, text: cur })
|
|
169
|
+
start = i
|
|
170
|
+
cur = ch
|
|
171
|
+
} else {
|
|
172
|
+
cur += ch
|
|
173
|
+
}
|
|
174
|
+
i += 1
|
|
175
|
+
}
|
|
176
|
+
return rows.length ? rows : [{ start: 0, end: 0, line: 0, text: '' }]
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/** The visible lines of the input area; the last one ends where the cursor belongs. */
|
|
180
|
+
export function inputLines(mode: ComposeMode, draft: string, width: number): string[] {
|
|
181
|
+
void mode
|
|
182
|
+
const room = Math.max(1, (Math.floor(width) || 1) - dispWidth(PROMPT))
|
|
183
|
+
return wrapRows(draft, room).map((row, i) => `${i === 0 ? promptOf(mode) : CONTINUATION}${row.text}`)
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/** Cursor column (0-based) for the input area's last line. */
|
|
187
|
+
export function cursorColumn(lines: string[]): number {
|
|
188
|
+
const last = lines[lines.length - 1] ?? ''
|
|
189
|
+
return dispWidth(last)
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
// ------------------------------------------------------------ the insertion-point model
|
|
193
|
+
|
|
194
|
+
/** The draft and the insertion point, as codepoint indices (a wide glyph is one step). */
|
|
195
|
+
export interface ComposeState {
|
|
196
|
+
text: string
|
|
197
|
+
cursor: number
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
export type ComposeMotion =
|
|
201
|
+
| 'left'
|
|
202
|
+
| 'right'
|
|
203
|
+
| 'wordLeft'
|
|
204
|
+
| 'wordRight'
|
|
205
|
+
| 'lineStart'
|
|
206
|
+
| 'lineEnd'
|
|
207
|
+
| 'up'
|
|
208
|
+
| 'down'
|
|
209
|
+
| 'pageUp'
|
|
210
|
+
| 'pageDown'
|
|
211
|
+
|
|
212
|
+
/** Codepoints in, codepoints out: never a UTF-16 unit, so a surrogate pair is never split. */
|
|
213
|
+
export function cpLength(text: string): number {
|
|
214
|
+
return [...String(text ?? '')].length
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
/** Insert `chunk` at the insertion point. */
|
|
218
|
+
export function insertAt(text: string, cursor: number, chunk: string): ComposeState {
|
|
219
|
+
const cps = [...String(text ?? '')]
|
|
220
|
+
const at = Math.max(0, Math.min(cps.length, Math.floor(cursor) || 0))
|
|
221
|
+
const add = [...normalizeNewlines(chunk)]
|
|
222
|
+
return { text: [...cps.slice(0, at), ...add, ...cps.slice(at)].join(''), cursor: at + add.length }
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
/** Backspace: remove the codepoint before the point, never half of a wide glyph. */
|
|
226
|
+
export function backspaceAt(text: string, cursor: number): ComposeState {
|
|
227
|
+
const cps = [...String(text ?? '')]
|
|
228
|
+
const at = Math.max(0, Math.min(cps.length, Math.floor(cursor) || 0))
|
|
229
|
+
if (at === 0) return { text: cps.join(''), cursor: 0 }
|
|
230
|
+
cps.splice(at - 1, 1)
|
|
231
|
+
return { text: cps.join(''), cursor: at - 1 }
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
/** Delete: remove the codepoint after the point. */
|
|
235
|
+
export function deleteAt(text: string, cursor: number): ComposeState {
|
|
236
|
+
const cps = [...String(text ?? '')]
|
|
237
|
+
const at = Math.max(0, Math.min(cps.length, Math.floor(cursor) || 0))
|
|
238
|
+
if (at >= cps.length) return { text: cps.join(''), cursor: at }
|
|
239
|
+
cps.splice(at, 1)
|
|
240
|
+
return { text: cps.join(''), cursor: at }
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
/** The first codepoint index of the logical (`\n`-separated) line containing `cursor`. */
|
|
244
|
+
export function lineStartOf(cps: string[], cursor: number): number {
|
|
245
|
+
let i = Math.max(0, Math.min(cps.length, cursor))
|
|
246
|
+
while (i > 0 && cps[i - 1] !== '\n') i -= 1
|
|
247
|
+
return i
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
/** One past the last codepoint of the logical line containing `cursor`. */
|
|
251
|
+
export function lineEndOf(cps: string[], cursor: number): number {
|
|
252
|
+
let i = Math.max(0, Math.min(cps.length, cursor))
|
|
253
|
+
while (i < cps.length && cps[i] !== '\n') i += 1
|
|
254
|
+
return i
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
/** The row index the insertion point is drawn on (a boundary belongs to the next row). */
|
|
258
|
+
export function rowOfCursor(rows: ComposeRow[], cursor: number): number {
|
|
259
|
+
const inside = rows.findIndex((r) => cursor >= r.start && cursor < r.end)
|
|
260
|
+
if (inside >= 0) return inside
|
|
261
|
+
// The cursor sits exactly at a row's end: a wrapped row's end belongs to the row it starts, an
|
|
262
|
+
// explicit break's end belongs to the row before the `\n` — both are that row.
|
|
263
|
+
const atEnd = rows.findIndex((r) => cursor === r.end)
|
|
264
|
+
return atEnd >= 0 ? atEnd : rows.length - 1
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
/** Map a display column inside `row` to the codepoint index closest to it (from the left). */
|
|
268
|
+
function indexAtColumn(cps: string[], row: ComposeRow, column: number): number {
|
|
269
|
+
let w = 0
|
|
270
|
+
for (let i = row.start; i < row.end; i += 1) {
|
|
271
|
+
if (w + dispWidth(cps[i]) > column) return i
|
|
272
|
+
w += dispWidth(cps[i])
|
|
273
|
+
}
|
|
274
|
+
return row.end
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
/**
|
|
278
|
+
* pi's word segmenter and punctuation rule, ported (`@earendil-works/pi-coding-agent`'s
|
|
279
|
+
* `findWordBackward`/`findWordForward` with `granularity: 'word'`): words come from
|
|
280
|
+
* `Intl.Segmenter`, and a punctuation run inside a word-like segment is a separate stop. A runtime
|
|
281
|
+
* without full ICU throws here instead of silently drifting — the model fixture pins CJK, ASCII and
|
|
282
|
+
* punctuation cases.
|
|
283
|
+
*/
|
|
284
|
+
const PUNCTUATION = /[(){}[\]<>.,;:'"!?+\-=*/\\|&%^$#@~`]/
|
|
285
|
+
let wordSegmenter: Intl.Segmenter | null = null
|
|
286
|
+
function segmentsOf(text: string): { segment: string; isWordLike: boolean }[] {
|
|
287
|
+
if (!wordSegmenter) wordSegmenter = new Intl.Segmenter(undefined, { granularity: 'word' })
|
|
288
|
+
const out: { segment: string; isWordLike: boolean }[] = []
|
|
289
|
+
for (const s of wordSegmenter.segment(text)) out.push({ segment: s.segment, isWordLike: s.isWordLike === true })
|
|
290
|
+
return out
|
|
291
|
+
}
|
|
292
|
+
const isSpace = (s: string): boolean => /^\s+$/.test(s) && s.length > 0
|
|
293
|
+
|
|
294
|
+
/** pi's `findWordBackward`: the start of the word before `cursor` inside one logical line. */
|
|
295
|
+
export function findWordBackward(text: string, cursor: number): number {
|
|
296
|
+
if (cursor <= 0) return 0
|
|
297
|
+
const segs = segmentsOf(text.slice(0, cursor))
|
|
298
|
+
let at = cursor
|
|
299
|
+
while (segs.length > 0 && isSpace(segs[segs.length - 1].segment)) at -= segs.pop()!.segment.length
|
|
300
|
+
if (segs.length === 0) return at
|
|
301
|
+
const last = segs[segs.length - 1]
|
|
302
|
+
if (last.isWordLike) {
|
|
303
|
+
const matches = [...last.segment.matchAll(new RegExp(PUNCTUATION.source, 'g'))]
|
|
304
|
+
if (matches.length === 0) at -= last.segment.length
|
|
305
|
+
else {
|
|
306
|
+
const m = matches[matches.length - 1]
|
|
307
|
+
at -= last.segment.length - ((m.index ?? 0) + m[0].length)
|
|
308
|
+
}
|
|
309
|
+
} else {
|
|
310
|
+
while (segs.length > 0) {
|
|
311
|
+
const prev = segs[segs.length - 1]
|
|
312
|
+
if (prev.isWordLike || isSpace(prev.segment)) break
|
|
313
|
+
at -= segs.pop()!.segment.length
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
return Math.max(0, at)
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
/** pi's `findWordForward`: the end of the word after `cursor` inside one logical line. */
|
|
320
|
+
export function findWordForward(text: string, cursor: number): number {
|
|
321
|
+
if (cursor >= text.length) return text.length
|
|
322
|
+
const segs = segmentsOf(text.slice(cursor))
|
|
323
|
+
let i = 0
|
|
324
|
+
let at = cursor
|
|
325
|
+
while (i < segs.length && isSpace(segs[i].segment)) {
|
|
326
|
+
at += segs[i].segment.length
|
|
327
|
+
i += 1
|
|
328
|
+
}
|
|
329
|
+
if (i >= segs.length) return at
|
|
330
|
+
const next = segs[i]
|
|
331
|
+
if (next.isWordLike) {
|
|
332
|
+
const m = PUNCTUATION.exec(next.segment)
|
|
333
|
+
at += m && m.index !== undefined ? m.index : next.segment.length
|
|
334
|
+
} else {
|
|
335
|
+
while (i < segs.length && !segs[i].isWordLike && !isSpace(segs[i].segment)) {
|
|
336
|
+
at += segs[i].segment.length
|
|
337
|
+
i += 1
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
return Math.min(text.length, at)
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
/** The logical (`\n`-separated) line's bounds around `cursor`. */
|
|
344
|
+
export function lineBounds(text: string, cursor: number): { start: number; end: number } {
|
|
345
|
+
const cps = [...String(text ?? '')]
|
|
346
|
+
const at = Math.max(0, Math.min(cps.length, Math.floor(cursor) || 0))
|
|
347
|
+
return { start: lineStartOf(cps, at), end: lineEndOf(cps, at) }
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
/** pi's word rules within the logical line that holds `cursor` (the movement's parity target). */
|
|
351
|
+
export function wordBackwardInLine(text: string, cursor: number): number {
|
|
352
|
+
const cps = [...String(text ?? '')]
|
|
353
|
+
const at = Math.max(0, Math.min(cps.length, Math.floor(cursor) || 0))
|
|
354
|
+
const line = lineStartOf(cps, at)
|
|
355
|
+
return line + findWordBackward(cps.slice(line, lineEndOf(cps, at)).join(''), at - line)
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
export function wordForwardInLine(text: string, cursor: number): number {
|
|
359
|
+
const cps = [...String(text ?? '')]
|
|
360
|
+
const at = Math.max(0, Math.min(cps.length, Math.floor(cursor) || 0))
|
|
361
|
+
const line = lineStartOf(cps, at)
|
|
362
|
+
return line + findWordForward(cps.slice(line, lineEndOf(cps, at)).join(''), at - line)
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
/**
|
|
366
|
+
* The start of the word before the point, with the whitespace run in front of it: pi deletes
|
|
367
|
+
* `beta ` when the point sits right after it, and ` gamma` when the point is at the line's end —
|
|
368
|
+
* the requirement's `ctrl+w` ("the word `gamma` and the space before it are cut").
|
|
369
|
+
*/
|
|
370
|
+
export function wordBeforeStart(text: string, cursor: number): number {
|
|
371
|
+
const cps = [...String(text ?? '')]
|
|
372
|
+
const at = Math.max(0, Math.min(cps.length, Math.floor(cursor) || 0))
|
|
373
|
+
const line = lineStartOf(cps, at)
|
|
374
|
+
let from = wordBackwardInLine(cps.join(''), at)
|
|
375
|
+
while (from > line && /\s/.test(cps[from - 1])) from -= 1
|
|
376
|
+
return from
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
/** The end of the word after the point (pi's forward rule skips the cursor-side whitespace). */
|
|
380
|
+
export function wordAfterEnd(text: string, cursor: number): number {
|
|
381
|
+
return wordForwardInLine(text, cursor)
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
/**
|
|
385
|
+
* Move the insertion point. `left`/`right` step one codepoint, the word motions stop at pi's word
|
|
386
|
+
* boundaries, `lineStart`/`lineEnd` are the **logical** line's bounds (pi's `cursorLineStart/End`),
|
|
387
|
+
* `up`/`down` walk the **visual** rows at the same display column clamped to the shorter row (pi's
|
|
388
|
+
* `moveCursor(±1, 0)`), and the page motions walk a page of visual rows. `width` is the input
|
|
389
|
+
* area's drawn width (the row map's room is derived from it), `pageRows` the window's capacity.
|
|
390
|
+
*/
|
|
391
|
+
export function moveCursor(
|
|
392
|
+
text: string,
|
|
393
|
+
cursor: number,
|
|
394
|
+
motion: ComposeMotion,
|
|
395
|
+
opts: { width: number; pageRows?: number },
|
|
396
|
+
): number {
|
|
397
|
+
const cps = [...String(text ?? '')]
|
|
398
|
+
const at = Math.max(0, Math.min(cps.length, Math.floor(cursor) || 0))
|
|
399
|
+
switch (motion) {
|
|
400
|
+
case 'left':
|
|
401
|
+
return Math.max(0, at - 1)
|
|
402
|
+
case 'right':
|
|
403
|
+
return Math.min(cps.length, at + 1)
|
|
404
|
+
case 'lineStart':
|
|
405
|
+
return lineStartOf(cps, at)
|
|
406
|
+
case 'lineEnd':
|
|
407
|
+
return lineEndOf(cps, at)
|
|
408
|
+
case 'wordLeft':
|
|
409
|
+
return wordBackwardInLine(cps.join(''), at)
|
|
410
|
+
case 'wordRight':
|
|
411
|
+
return wordForwardInLine(cps.join(''), at)
|
|
412
|
+
default:
|
|
413
|
+
break
|
|
414
|
+
}
|
|
415
|
+
const room = Math.max(1, (Math.floor(opts.width) || 1) - dispWidth(PROMPT))
|
|
416
|
+
const rows = wrapRows(cps.join(''), room)
|
|
417
|
+
const cur = rowOfCursor(rows, at)
|
|
418
|
+
const page = Math.max(1, Math.floor(opts.pageRows ?? 1) || 1)
|
|
419
|
+
const delta = motion === 'up' ? -1 : motion === 'down' ? 1 : motion === 'pageUp' ? -page : page
|
|
420
|
+
const target = rows[Math.max(0, Math.min(rows.length - 1, cur + delta))]
|
|
421
|
+
if (!target || target === rows[cur]) return at
|
|
422
|
+
const column = dispWidth(cps.slice(rows[cur].start, at).join(''))
|
|
423
|
+
return indexAtColumn(cps, target, column)
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
/** The windowed view of the draft: what is drawn, where the point is drawn, what is hidden. */
|
|
427
|
+
export interface ComposeView {
|
|
428
|
+
/** The drawn rows, prefixes included (an optional hidden-rows marker first). */
|
|
429
|
+
lines: string[]
|
|
430
|
+
/** The index into `lines` of the row holding the insertion point. */
|
|
431
|
+
cursorLine: number
|
|
432
|
+
/** The insertion point's display column within that drawn row (prefix included). */
|
|
433
|
+
cursorColumn: number
|
|
434
|
+
/** The full-draft row index the window starts at. */
|
|
435
|
+
start: number
|
|
436
|
+
/** Full-draft rows hidden above the window. */
|
|
437
|
+
hiddenAbove: number
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
/**
|
|
441
|
+
* Build the windowed input area. The window shows at most `maxRows` rows (half the pane, per the
|
|
442
|
+
* requirement), always contains the insertion point's row, and counts the rows it hides above on
|
|
443
|
+
* its own top edge (a marker row, so at most `maxRows - 1` draft rows then). The draft's first row
|
|
444
|
+
* keeps the `> ` prompt; every following row (and the continuation of a wrapped line) the
|
|
445
|
+
* continuation prefix.
|
|
446
|
+
*/
|
|
447
|
+
export function cursorView(
|
|
448
|
+
mode: ComposeMode,
|
|
449
|
+
draft: string,
|
|
450
|
+
cursor: number,
|
|
451
|
+
width: number,
|
|
452
|
+
maxRows: number,
|
|
453
|
+
hiddenAboveText: string,
|
|
454
|
+
): ComposeView {
|
|
455
|
+
const room = Math.max(1, (Math.floor(width) || 1) - dispWidth(PROMPT))
|
|
456
|
+
const rows = wrapRows(draft, room)
|
|
457
|
+
const cap = Math.max(1, Math.floor(maxRows) || 1)
|
|
458
|
+
const at = Math.max(0, Math.min(cpLength(draft), Math.floor(cursor) || 0))
|
|
459
|
+
const cur = rowOfCursor(rows, at)
|
|
460
|
+
const clamp = (v: number, lo: number, hi: number): number => Math.max(lo, Math.min(hi, v))
|
|
461
|
+
let visible = cap
|
|
462
|
+
let start = clamp(cur - visible + 1, 0, Math.max(0, rows.length - visible))
|
|
463
|
+
if (start > 0 && cap > 1) {
|
|
464
|
+
visible = cap - 1
|
|
465
|
+
start = clamp(cur - visible + 1, 0, Math.max(0, rows.length - visible))
|
|
466
|
+
}
|
|
467
|
+
const marker = start > 0 && cap > 1
|
|
468
|
+
const lines: string[] = marker ? [fill(hiddenAboveText, { n: start })] : []
|
|
469
|
+
for (let i = start; i < Math.min(rows.length, start + visible); i += 1) {
|
|
470
|
+
lines.push(`${i === 0 ? promptOf(mode) : CONTINUATION}${rows[i].text}`)
|
|
471
|
+
}
|
|
472
|
+
const row = rows[cur] ?? rows[0]
|
|
473
|
+
const prefix = `${cur === 0 ? promptOf(mode) : CONTINUATION}`
|
|
474
|
+
return {
|
|
475
|
+
lines,
|
|
476
|
+
cursorLine: (marker ? 1 : 0) + (cur - start),
|
|
477
|
+
cursorColumn: dispWidth(prefix) + dispWidth([...String(draft ?? '')].slice(row.start, at).join('')),
|
|
478
|
+
start,
|
|
479
|
+
hiddenAbove: start,
|
|
480
|
+
}
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
// ------------------------------------------------------------ the kill ring, undo and the key map
|
|
484
|
+
|
|
485
|
+
/** Newest entry first; `lastDirection` is what makes consecutive kills accumulate (pi's rule). */
|
|
486
|
+
export interface KillRing {
|
|
487
|
+
entries: string[]
|
|
488
|
+
lastDirection: 'backward' | 'forward' | null
|
|
489
|
+
}
|
|
490
|
+
|
|
491
|
+
export const KILL_RING_MAX = 10
|
|
492
|
+
/** What a deletion cut, its side, and where it landed: the App needs the text to push and the edges
|
|
493
|
+
* to apply the edit. */
|
|
494
|
+
export interface KillSpan {
|
|
495
|
+
from: number
|
|
496
|
+
to: number
|
|
497
|
+
text: string
|
|
498
|
+
direction: 'backward' | 'forward'
|
|
499
|
+
}
|
|
500
|
+
|
|
501
|
+
/** The kill span for one deletion intent, clamped to what the draft actually has. */
|
|
502
|
+
export function killSpan(
|
|
503
|
+
text: string,
|
|
504
|
+
cursor: number,
|
|
505
|
+
unit: 'charBefore' | 'charAfter' | 'wordBefore' | 'wordAfter' | 'lineBefore' | 'lineAfter',
|
|
506
|
+
): KillSpan | null {
|
|
507
|
+
const cps = [...String(text ?? '')]
|
|
508
|
+
const at = Math.max(0, Math.min(cps.length, Math.floor(cursor) || 0))
|
|
509
|
+
const line = lineBounds(cps.join(''), at)
|
|
510
|
+
let from = at
|
|
511
|
+
let to = at
|
|
512
|
+
switch (unit) {
|
|
513
|
+
case 'charBefore':
|
|
514
|
+
from = Math.max(0, at - 1)
|
|
515
|
+
break
|
|
516
|
+
case 'charAfter':
|
|
517
|
+
to = Math.min(cps.length, at + 1)
|
|
518
|
+
break
|
|
519
|
+
case 'wordBefore':
|
|
520
|
+
from = wordBeforeStart(cps.join(''), at)
|
|
521
|
+
break
|
|
522
|
+
case 'wordAfter':
|
|
523
|
+
to = wordAfterEnd(cps.join(''), at)
|
|
524
|
+
break
|
|
525
|
+
case 'lineBefore':
|
|
526
|
+
from = line.start
|
|
527
|
+
break
|
|
528
|
+
case 'lineAfter':
|
|
529
|
+
to = line.end
|
|
530
|
+
break
|
|
531
|
+
}
|
|
532
|
+
if (from === to) return null
|
|
533
|
+
const direction: 'backward' | 'forward' = from < at ? 'backward' : 'forward'
|
|
534
|
+
return { from, to, text: cps.slice(from, to).join(''), direction }
|
|
535
|
+
}
|
|
536
|
+
|
|
537
|
+
/** Push one deletion onto the ring: same-side consecutive kills accumulate into the head entry. */
|
|
538
|
+
export function pushKill(ring: KillRing, text: string, direction: 'backward' | 'forward'): KillRing {
|
|
539
|
+
if (!text) return ring
|
|
540
|
+
if (ring.lastDirection === direction && ring.entries.length > 0) {
|
|
541
|
+
const head = direction === 'backward' ? text + ring.entries[0] : ring.entries[0] + text
|
|
542
|
+
return { entries: [head, ...ring.entries.slice(1)], lastDirection: direction }
|
|
543
|
+
}
|
|
544
|
+
return { entries: [text, ...ring.entries].slice(0, KILL_RING_MAX), lastDirection: direction }
|
|
545
|
+
}
|
|
546
|
+
|
|
547
|
+
/** Any non-kill action breaks the accumulation chain (pi resets `lastAction`). */
|
|
548
|
+
export function resetKillDirection(ring: KillRing): KillRing {
|
|
549
|
+
return ring.lastDirection === null ? ring : { ...ring, lastDirection: null }
|
|
550
|
+
}
|
|
551
|
+
|
|
552
|
+
/** The yank target for `popIndex` pops after a yank (0 = the newest entry). */
|
|
553
|
+
export function ringEntry(ring: KillRing, popIndex: number): string | null {
|
|
554
|
+
if (ring.entries.length === 0) return null
|
|
555
|
+
return ring.entries[popIndex % ring.entries.length] ?? null
|
|
556
|
+
}
|
|
557
|
+
|
|
558
|
+
/** A bounded undo history of `{text, cursor}` snapshots, newest last. */
|
|
559
|
+
export const UNDO_MAX = 100
|
|
560
|
+
export interface UndoSnapshot {
|
|
561
|
+
text: string
|
|
562
|
+
cursor: number
|
|
563
|
+
}
|
|
564
|
+
|
|
565
|
+
/** Snapshot the draft before a mutation; the oldest entries fall off at the bound. */
|
|
566
|
+
export function pushUndo(stack: UndoSnapshot[], text: string, cursor: number): void {
|
|
567
|
+
stack.push({ text: String(text ?? ''), cursor: Math.max(0, Math.floor(cursor) || 0) })
|
|
568
|
+
if (stack.length > UNDO_MAX) stack.splice(0, stack.length - UNDO_MAX)
|
|
569
|
+
}
|
|
570
|
+
|
|
571
|
+
/** Pop the last snapshot, or null when the history is empty. */
|
|
572
|
+
export function popUndo(stack: UndoSnapshot[]): UndoSnapshot | null {
|
|
573
|
+
return stack.pop() ?? null
|
|
574
|
+
}
|
|
575
|
+
|
|
576
|
+
/** The subset of Ink's `Key` the compose key map reads (kept structural so the model stays pure). */
|
|
577
|
+
export interface ComposeKeyFlags {
|
|
578
|
+
ctrl?: boolean
|
|
579
|
+
meta?: boolean
|
|
580
|
+
shift?: boolean
|
|
581
|
+
return?: boolean
|
|
582
|
+
escape?: boolean
|
|
583
|
+
upArrow?: boolean
|
|
584
|
+
downArrow?: boolean
|
|
585
|
+
leftArrow?: boolean
|
|
586
|
+
rightArrow?: boolean
|
|
587
|
+
pageUp?: boolean
|
|
588
|
+
pageDown?: boolean
|
|
589
|
+
backspace?: boolean
|
|
590
|
+
delete?: boolean
|
|
591
|
+
home?: boolean
|
|
592
|
+
end?: boolean
|
|
593
|
+
}
|
|
594
|
+
|
|
595
|
+
export type ComposeIntent =
|
|
596
|
+
| { kind: 'insert'; text: string }
|
|
597
|
+
| { kind: 'newline' }
|
|
598
|
+
| { kind: 'submit' }
|
|
599
|
+
| { kind: 'cancel' }
|
|
600
|
+
| { kind: 'move'; motion: ComposeMotion }
|
|
601
|
+
| { kind: 'kill'; unit: 'charBefore' | 'charAfter' | 'wordBefore' | 'wordAfter' | 'lineBefore' | 'lineAfter' }
|
|
602
|
+
| { kind: 'yank' }
|
|
603
|
+
| { kind: 'yankPop' }
|
|
604
|
+
| { kind: 'undo' }
|
|
605
|
+
| { kind: 'none' }
|
|
606
|
+
|
|
607
|
+
/**
|
|
608
|
+
* One terminal event → one compose intent (design §4). Both encodings reach this through the
|
|
609
|
+
* *pairs* Ink's parser produces (`ctrl+j` is `('\n', {})` legacy and `('j', {ctrl: true})` from
|
|
610
|
+
* `CSI 106;5u`; `shift+enter` is `('\r', {return: true, shift: true})`), which is exactly why the
|
|
611
|
+
* pty fixtures can send the Kitty bytes and assert the effect. A `ctrl`/`alt`-modified key with no
|
|
612
|
+
* binding is consumed (`none`) and never inserts its letter.
|
|
613
|
+
*/
|
|
614
|
+
export function composeKey(input: string, key: ComposeKeyFlags): ComposeIntent {
|
|
615
|
+
const ch = String(input ?? '')
|
|
616
|
+
const mod = Boolean(key.ctrl || key.meta)
|
|
617
|
+
if (key.escape) return { kind: 'cancel' }
|
|
618
|
+
// `\r` submits, a lone `\n` (and `ctrl+j`, which is the same byte in the legacy encoding and a
|
|
619
|
+
// `CSI 106;5u` under the Kitty protocol) inserts a break. `shift+enter` inserts one only when the
|
|
620
|
+
// terminal reports it; an unsupporting terminal sends plain `\r` and it submits like Enter.
|
|
621
|
+
if (key.return) return key.shift ? { kind: 'newline' } : { kind: 'submit' }
|
|
622
|
+
if (key.ctrl && ch === 'j') return { kind: 'newline' }
|
|
623
|
+
if (key.leftArrow) return { kind: 'move', motion: key.meta || key.ctrl ? 'wordLeft' : 'left' }
|
|
624
|
+
if (key.rightArrow) return { kind: 'move', motion: key.meta || key.ctrl ? 'wordRight' : 'right' }
|
|
625
|
+
if (key.upArrow) return { kind: 'move', motion: 'up' }
|
|
626
|
+
if (key.downArrow) return { kind: 'move', motion: 'down' }
|
|
627
|
+
if (key.pageUp) return { kind: 'move', motion: 'pageUp' }
|
|
628
|
+
if (key.pageDown) return { kind: 'move', motion: 'pageDown' }
|
|
629
|
+
if (key.home || (key.ctrl && ch === 'a')) return { kind: 'move', motion: 'lineStart' }
|
|
630
|
+
if (key.end || (key.ctrl && ch === 'e')) return { kind: 'move', motion: 'lineEnd' }
|
|
631
|
+
if (key.backspace) return { kind: 'kill', unit: key.meta ? 'wordBefore' : 'charBefore' }
|
|
632
|
+
if (key.delete) return { kind: 'kill', unit: key.meta ? 'wordAfter' : 'charAfter' }
|
|
633
|
+
if (key.ctrl && ch === 'w') return { kind: 'kill', unit: 'wordBefore' }
|
|
634
|
+
if (key.ctrl && ch === 'd') return { kind: 'kill', unit: 'charAfter' }
|
|
635
|
+
if (key.ctrl && ch === 'u') return { kind: 'kill', unit: 'lineBefore' }
|
|
636
|
+
if (key.ctrl && ch === 'k') return { kind: 'kill', unit: 'lineAfter' }
|
|
637
|
+
if (key.meta && ch === 'b') return { kind: 'move', motion: 'wordLeft' }
|
|
638
|
+
if (key.meta && ch === 'f') return { kind: 'move', motion: 'wordRight' }
|
|
639
|
+
if (key.meta && ch === 'd') return { kind: 'kill', unit: 'wordAfter' }
|
|
640
|
+
if (key.ctrl && ch === 'b') return { kind: 'move', motion: 'left' }
|
|
641
|
+
if (key.ctrl && ch === 'f') return { kind: 'move', motion: 'right' }
|
|
642
|
+
if (key.ctrl && ch === 'y') return { kind: 'yank' }
|
|
643
|
+
if (key.meta && ch === 'y') return { kind: 'yankPop' }
|
|
644
|
+
// `ctrl+-` exists only through the Kitty protocol (the legacy byte is not reported); the parser
|
|
645
|
+
// hands it over as `('-', {ctrl: true})`.
|
|
646
|
+
if (key.ctrl && ch === '-') return { kind: 'undo' }
|
|
647
|
+
if (mod) return { kind: 'none' }
|
|
648
|
+
// A lone LF is the legacy `ctrl+j` byte.
|
|
649
|
+
if (ch === '\n') return { kind: 'newline' }
|
|
650
|
+
if (!ch || ch.startsWith('\u001b')) return { kind: 'none' }
|
|
651
|
+
return { kind: 'insert', text: ch }
|
|
652
|
+
}
|
|
653
|
+
|
|
654
|
+
/**
|
|
655
|
+
* The send command's machine result → one of the three honest receipt states.
|
|
656
|
+
*
|
|
657
|
+
* `rc` is the bridge's exit code, `outcome` is `TEAM_SEND_OUTCOME` and `result` is the finer
|
|
658
|
+
* `TEAM_OUTBOX_RESULT` (delivered | held | busy | queued | offline | terminal | skip) — the CLI's
|
|
659
|
+
* own tokens, read from the bridge that runs the guarded send in its own shell. Human prose is
|
|
660
|
+
* never consulted.
|
|
661
|
+
*/
|
|
662
|
+
export function mapReceipt(rc: number, outcome: string, result: string, detail = ''): Receipt {
|
|
663
|
+
const token = `rc=${rc} outcome=${outcome || '-'} result=${result || '-'}`
|
|
664
|
+
if (rc !== 0) return { state: 'error', token, detail: detail || 'send failed (target undeliverable)' }
|
|
665
|
+
if (result === 'held' || result === 'terminal') return { state: 'held', token, detail }
|
|
666
|
+
if (outcome === 'delivered' || outcome === 'forced' || outcome === 'unknown-sent' || outcome === 'duplicate') {
|
|
667
|
+
return { state: 'delivered', token, detail }
|
|
668
|
+
}
|
|
669
|
+
return { state: 'queued', token, detail }
|
|
670
|
+
}
|
|
671
|
+
|
|
672
|
+
/** Parse the bridge's one machine line: `rc=<n> outcome=<token> result=<token>`. */
|
|
673
|
+
export function parseSendResult(stdout: string): { rc: number; outcome: string; result: string } | null {
|
|
674
|
+
const m = /(?:^|\n)\s*rc=(-?\d+)\s+outcome=(\S*)\s+result=(\S*)\s*(?:\n|$)/.exec(String(stdout ?? ''))
|
|
675
|
+
if (!m) return null
|
|
676
|
+
return { rc: Number(m[1]), outcome: m[2] === '-' ? '' : m[2], result: m[3] === '-' ? '' : m[3] }
|
|
677
|
+
}
|
|
678
|
+
|
|
679
|
+
/** The one-line receipt shown under the frame, in the active language's voice. */
|
|
680
|
+
export function receiptLine(receipt: Receipt, s: Strings): string {
|
|
681
|
+
switch (receipt.state) {
|
|
682
|
+
case 'delivered':
|
|
683
|
+
return s.receiptDelivered
|
|
684
|
+
case 'queued':
|
|
685
|
+
return s.receiptQueued
|
|
686
|
+
case 'held':
|
|
687
|
+
return s.receiptHeld
|
|
688
|
+
default:
|
|
689
|
+
return fill(s.receiptError, { detail: receipt.detail || s.sendUnconfirmed })
|
|
690
|
+
}
|
|
691
|
+
}
|