@2cmddev/2cmd 0.0.0-stage → 0.1.5

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.
Files changed (93) hide show
  1. package/.opencode/command/2cmd-audit.md +5 -0
  2. package/.opencode/command/2cmd-debt.md +5 -0
  3. package/.opencode/command/2cmd-gain.md +5 -0
  4. package/.opencode/command/2cmd-help.md +5 -0
  5. package/.opencode/command/2cmd-review.md +5 -0
  6. package/.opencode/command/2cmd.md +5 -0
  7. package/.opencode/plugins/2cmd-frontmatter.cjs +63 -0
  8. package/.opencode/plugins/2cmd.mjs +206 -0
  9. package/.opencode/plugins/index.js +4 -0
  10. package/.qoder/rules/2cmd.md +31 -0
  11. package/.qoder-plugin/plugin.json +16 -0
  12. package/.zcode-plugin/plugin.json +13 -0
  13. package/AGENTS.md +33 -0
  14. package/LICENSE +22 -0
  15. package/README.md +133 -2
  16. package/codex/answer.mjs +164 -0
  17. package/codex/data.mjs +31 -0
  18. package/codex/pty.py +80 -0
  19. package/codex/stats.mjs +105 -0
  20. package/codex/terminal.mjs +388 -0
  21. package/dsh/LICENSE +22 -0
  22. package/dsh/board.js +782 -0
  23. package/dsh/build.mjs +47 -0
  24. package/dsh/bundle.json +2104 -0
  25. package/dsh/client.js +877 -0
  26. package/dsh/cordis.patch.yml +3 -0
  27. package/dsh/index.js +185 -0
  28. package/dsh/ledger.js +50 -0
  29. package/dsh/market.js +152 -0
  30. package/dsh/package.json +24 -0
  31. package/dsh/prices.json +1812 -0
  32. package/dsh/settings-text.mjs +520 -0
  33. package/dsh/settings.js +42 -0
  34. package/dsh/stats.js +106 -0
  35. package/dsh/store.js +32 -0
  36. package/dsh/update-prices.mjs +9 -0
  37. package/dsh/update.js +27 -0
  38. package/dsh/voice-helper/Info.plist +13 -0
  39. package/dsh/voice-helper/Transcript.swift +21 -0
  40. package/dsh/voice-helper/build.sh +10 -0
  41. package/dsh/voice-helper/main.swift +70 -0
  42. package/dsh/voice-helper/test.swift +17 -0
  43. package/dsh/voice.js +63 -0
  44. package/hooks/2cmd-activate.js +219 -0
  45. package/hooks/2cmd-antigravity.js +64 -0
  46. package/hooks/2cmd-band-import.js +305 -0
  47. package/hooks/2cmd-config.js +186 -0
  48. package/hooks/2cmd-instructions.js +104 -0
  49. package/hooks/2cmd-map.js +200 -0
  50. package/hooks/2cmd-mode-tracker.js +248 -0
  51. package/hooks/2cmd-runtime.js +245 -0
  52. package/hooks/2cmd-statusline.ps1 +24 -0
  53. package/hooks/2cmd-statusline.sh +18 -0
  54. package/hooks/2cmd-subagent.js +100 -0
  55. package/hooks/band/board.ts +720 -0
  56. package/hooks/band/buttons.ts +60 -0
  57. package/hooks/band/i18n/de.ts +149 -0
  58. package/hooks/band/i18n/en.ts +150 -0
  59. package/hooks/band/i18n/es.ts +149 -0
  60. package/hooks/band/i18n/fr.ts +149 -0
  61. package/hooks/band/i18n/index.ts +42 -0
  62. package/hooks/band/i18n/ja.ts +149 -0
  63. package/hooks/band/i18n/ko.ts +149 -0
  64. package/hooks/band/i18n/pt.ts +149 -0
  65. package/hooks/band/i18n/ru.ts +149 -0
  66. package/hooks/band/i18n/vi.ts +149 -0
  67. package/hooks/band/i18n/zh-Hans.ts +149 -0
  68. package/hooks/band/i18n/zh-Hant.ts +149 -0
  69. package/hooks/band/logo.ts +3 -0
  70. package/hooks/band/metrics.ts +373 -0
  71. package/hooks/band/register.tsx +681 -0
  72. package/hooks/band/types.d.ts +45 -0
  73. package/hooks/band/windows-board.mjs +20 -0
  74. package/hooks/claude-band.json +6 -0
  75. package/hooks/claude-codex-hooks.json +41 -0
  76. package/hooks/codebuddy-hooks.json +20 -0
  77. package/hooks/copilot-hooks.json +21 -0
  78. package/hooks/cursor-hooks.json +17 -0
  79. package/hooks/qoder-hooks.json +26 -0
  80. package/hooks/zcode-hooks.json +26 -0
  81. package/package.json +56 -4
  82. package/pi-extension/index.js +258 -0
  83. package/pi-extension/names.js +19 -0
  84. package/pi-extension/package.json +8 -0
  85. package/scripts/cursor-hooks.js +145 -0
  86. package/scripts/uninstall.js +92 -0
  87. package/skills/2cmd/SKILL.md +55 -0
  88. package/skills/2cmd-audit/SKILL.md +105 -0
  89. package/skills/2cmd-debt/SKILL.md +46 -0
  90. package/skills/2cmd-gain/SKILL.md +53 -0
  91. package/skills/2cmd-help/SKILL.md +87 -0
  92. package/skills/2cmd-review/SKILL.md +143 -0
  93. package/skills/plugin.json +5 -0
@@ -0,0 +1,388 @@
1
+ #!/usr/bin/env node
2
+ // 2Cmd panel for the Codex CLI: runs the person's own `codex` in a pseudo-terminal two rows shorter than the window
3
+ // and draws the panel in those two rows, under Codex's own footer. Nothing in Codex is changed or patched.
4
+ // Usage: 2cmd-codex [codex arguments] (package.json bin), or node codex/terminal.mjs. TWOCMD_PANEL=off runs plain codex,
5
+ // for hosts such as terminal apps that show their own 2Cmd panel. macOS and Linux, with python3 for the pseudo-terminal (pty.py).
6
+ // The panel shows only while Codex uses the alternate screen (its default); inline mode gets the whole window.
7
+ import { spawn, spawnSync } from 'node:child_process'
8
+ import { accessSync, appendFileSync, constants, readdirSync, readFileSync, realpathSync, statSync } from 'node:fs'
9
+ import { createRequire } from 'node:module'
10
+ import { delimiter, join } from 'node:path'
11
+ import { fileURLToPath } from 'node:url'
12
+
13
+ const { RUNTIME_MODES } = createRequire(import.meta.url)('../hooks/2cmd-config.js')
14
+ export const OPEN_ROWS = 6 // header, model, four stats
15
+
16
+ /** Columns a string takes in a terminal: East Asian wide and fullwidth characters take two. */
17
+ export function width(text) {
18
+ let n = 0
19
+ for (const ch of text.replace(/\x1b\[[\d;]*m/g, '')) n += /[ᄀ-ᅟ⺀-〾ぁ-꓏가-힣豈-﫿︰-﹏＀-⦆¢-₩]|[\u{20000}-\u{3fffd}]/u.test(ch) ? 2 : 1
20
+ return n
21
+ }
22
+
23
+ /**
24
+ * Watches Codex's output on its way to the screen. It keeps Codex's scroll region out of the panel rows and says
25
+ * when the stream is between sequences and characters, the only place the panel may be drawn in.
26
+ */
27
+ export function createScreenFilter(codexRows) {
28
+ let state = 'ground'
29
+ let csi = ''
30
+ let utf8 = 0 // continuation bytes still to come
31
+ let saved = false // between Codex's own cursor save and restore
32
+ const flags = { alt: false, paste: false, sync: false, clear: false }
33
+
34
+ function onCsi(params, final) {
35
+ if (final === 'r' && flags.alt) {
36
+ const [top, bottom] = params.split(';').map(Number)
37
+ return `\x1b[${top || 1};${Math.min(bottom || codexRows(), codexRows())}r`
38
+ }
39
+ if (params.startsWith('?') && (final === 'h' || final === 'l')) {
40
+ for (const mode of params.slice(1).split(';')) {
41
+ if (mode === '1049' || mode === '1047' || mode === '47') flags.alt = final === 'h'
42
+ if (mode === '2004') flags.paste = final === 'h'
43
+ if (mode === '2026') flags.sync = final === 'h'
44
+ }
45
+ flags.clear ||= /\b(1049|1047|47)\b/.test(params)
46
+ }
47
+ if (final === 'J' && params !== '1') flags.clear = true // "below the cursor" also wipes the panel rows
48
+ if (params === '' && (final === 's' || final === 'u')) saved = final === 's'
49
+ return null
50
+ }
51
+
52
+ function filter(chunk) {
53
+ const out = []
54
+ let from = 0 // start of bytes passed through unchanged
55
+ for (let i = 0; i < chunk.length; i++) {
56
+ const b = chunk[i]
57
+ if (state === 'ground') {
58
+ if (utf8 > 0 && b >= 0x80 && b < 0xc0) utf8--
59
+ else if (b === 0x1b) { state = 'esc'; utf8 = 0 }
60
+ else utf8 = b >= 0xf0 ? 3 : b >= 0xe0 ? 2 : b >= 0xc0 ? 1 : 0
61
+ } else if (state === 'esc') {
62
+ if (b === 0x5b) { state = 'csi'; csi = '' }
63
+ else if (b === 0x5d || b === 0x50 || b === 0x58 || b === 0x5e || b === 0x5f) state = 'string'
64
+ else if (b >= 0x20 && b <= 0x2f) state = 'escInter'
65
+ else {
66
+ if (b === 0x37) saved = true
67
+ if (b === 0x38) saved = false
68
+ if (b === 0x63) Object.assign(flags, { alt: false, clear: true }) // full reset
69
+ state = 'ground'
70
+ }
71
+ } else if (state === 'escInter') {
72
+ if (b < 0x20 || b > 0x2f) state = 'ground'
73
+ } else if (state === 'csi') {
74
+ if (b >= 0x40 && b <= 0x7e) {
75
+ state = 'ground'
76
+ const start = i - csi.length - 2
77
+ const replaced = onCsi(csi, String.fromCharCode(b))
78
+ if (replaced !== null && start >= from) {
79
+ out.push(chunk.subarray(from, start), Buffer.from(replaced))
80
+ from = i + 1
81
+ } else if (replaced !== null) flags.clear = true // split between reads: the next panel draw sets the region again
82
+ } else csi += String.fromCharCode(b)
83
+ } else if (state === 'string') {
84
+ if (b === 0x07) state = 'ground'
85
+ else if (b === 0x1b) state = 'stringEsc'
86
+ } else if (state === 'stringEsc') state = b === 0x5c ? 'ground' : 'string'
87
+ }
88
+ out.push(chunk.subarray(from))
89
+ return Buffer.concat(out)
90
+ }
91
+
92
+ /** Drawing here cannot split a sequence, a character or one of Codex's frames. */
93
+ const safe = () => state === 'ground' && utf8 === 0 && !saved && !flags.sync
94
+ /** Codex has been quiet a while, so whatever still looks open was never going to close. Returns what it was. */
95
+ function settle() {
96
+ const stuck = safe() ? null : JSON.stringify({ state, utf8, saved, sync: flags.sync })
97
+ state = 'ground'; utf8 = 0; saved = false; flags.sync = false
98
+ return stuck
99
+ }
100
+ return { filter, safe, settle, flags }
101
+ }
102
+
103
+ const SGR_MOUSE = /\x1b\[<(\d+);(\d+);(\d+)([Mm])/g
104
+ // Ctrl+\ as a plain byte, or in the kitty keyboard protocol Codex turns on where the terminal has it (:3 = release).
105
+ const SHORTCUT = /\x1c|\x1b\[92(?::\d*)*;5(?::([123]))?u/g
106
+
107
+ /**
108
+ * Takes what the person types. Clicks in the panel rows and the shortcut are the panel's; the rest goes to Codex.
109
+ * Returns the bytes for Codex and the panel's actions: { click: column, row: panel row from 0 } or { shortcut: true }.
110
+ */
111
+ export function splitInput(text, codexRows, panelShown) {
112
+ const actions = []
113
+ if (!panelShown) return { forward: text, actions }
114
+ const forward = text
115
+ .replace(SGR_MOUSE, (whole, button, x, y, kind) => {
116
+ if (Number(y) <= codexRows) return whole
117
+ if (button === '0' && kind === 'M') actions.push({ click: Number(x), row: Number(y) - codexRows - 1 })
118
+ return '' // releases, motion and the wheel over the panel stay with it
119
+ })
120
+ .replace(SHORTCUT, (whole, event) => {
121
+ if (event !== '3') actions.push({ shortcut: true })
122
+ return ''
123
+ })
124
+ return { forward, actions }
125
+ }
126
+
127
+ const BUTTON_LABELS = { fix: 'reviewFix', commit: 'reviewCommit', push: 'reviewPush', copy: 'copy', review: 'review', audit: 'audit' }
128
+ const style = (text, codes) => (codes ? `\x1b[${codes}m${text}\x1b[0m` : text)
129
+
130
+ /**
131
+ * The panel rows for a window `cols` wide (2, or OPEN_ROWS when `open`), and where each clickable part is.
132
+ * `data` is the answer to { op: 'state' } from codex/answer.mjs, or null before this window's conversation is known.
133
+ */
134
+ export function layout(t, data, cols, notice = '', open = false) {
135
+ const max = cols - 1 // never write the last column, so the terminal does not wrap
136
+ const rows = []
137
+ const hits = []
138
+ const line = (parts) => {
139
+ let text = ''
140
+ let used = 0
141
+ for (const { label, codes, action, gap = 1 } of parts) {
142
+ const space = used ? ' '.repeat(gap) : ''
143
+ if (used + gap + width(label) > max) break
144
+ const from = used + space.length + 1 // mouse columns start at 1
145
+ used += space.length + width(label)
146
+ text += space + style(label, codes)
147
+ if (action) hits.push({ row: rows.length, from, to: used, action })
148
+ }
149
+ rows.push(text)
150
+ }
151
+ line([
152
+ { label: ` 2Cmd ${open ? '▾' : '▸'} `, codes: '1;7', action: { toggle: true } },
153
+ ...RUNTIME_MODES.map(mode => ({
154
+ label: ` ${mode === 'off' ? t.modeOff : t[mode]} `,
155
+ // The mode that will apply gets the badge's background; the one it replaces stays underlined until then.
156
+ codes: mode === (data?.pending ?? data?.mode) ? '1;7' : mode === data?.mode ? '4' : '2',
157
+ action: { mode },
158
+ gap: 0,
159
+ })),
160
+ ...(data?.pending ? [{ label: t.pending, codes: '2' }] : []), // next to the modes it is about
161
+ { label: '│', codes: '2' },
162
+ // Copy has its own place after the buttons, so a Copy chosen in the settings is not shown twice.
163
+ ...(data?.buttons ?? []).filter(item => item.kind !== 'none' && item.kind !== 'copy').map(item => ({
164
+ label: `[${item.kind === 'prompt' ? item.label : t[BUTTON_LABELS[item.kind]]}]`,
165
+ codes: '36',
166
+ action: { button: item },
167
+ })),
168
+ { label: `[${t.copy}]`, codes: '36', action: { button: { kind: 'copy' } } },
169
+ { label: `[${t.board}]`, codes: '36', action: { board: true } },
170
+ ...(open && notice ? [{ label: notice, codes: '2', gap: 2 }] : []),
171
+ ])
172
+ const waiting = data?.mode == null
173
+ if (!open) {
174
+ line(waiting || notice
175
+ ? [{ label: ` ${notice || t.waiting}`, codes: '2' }]
176
+ : data.cells.map(([label, value], i) => ({ label: `${i ? '' : ' '}${style(label, '2')} ${value}`, gap: 3 })))
177
+ return { rows, hits }
178
+ }
179
+ if (waiting) line([{ label: ` ${t.waiting}`, codes: '2' }])
180
+ else {
181
+ // One fact a row, with its detail after it, like the open panel in Claude Code.
182
+ const pad = Math.max(...[t.model, ...data.cells.map(([label]) => label)].map(width)) + 2
183
+ for (const [label, value, sub] of [[t.model, data.model || '–'], ...data.cells]) {
184
+ line([{ label: ` ${style(label, '2')}${' '.repeat(pad - width(label))}${value}${sub ? style(` · ${sub}`, '2') : ''}` }])
185
+ }
186
+ }
187
+ while (rows.length < OPEN_ROWS) rows.push('')
188
+ return { rows, hits }
189
+ }
190
+
191
+ /** The newest conversation this window's Codex wrote, from the files the 2Cmd hooks keep in `live/`. */
192
+ export function findConversation(liveDir, cwds, since) {
193
+ let best = null
194
+ let files = []
195
+ try { files = readdirSync(liveDir) } catch { return null }
196
+ for (const name of files) {
197
+ if (!name.endsWith('.json')) continue
198
+ try {
199
+ const time = statSync(join(liveDir, name)).mtimeMs
200
+ if (time < since || (best && time <= best.time)) continue
201
+ if (cwds.includes(JSON.parse(readFileSync(join(liveDir, name), 'utf8')).cwd)) best = { id: name.slice(0, -5), time }
202
+ } catch { /* a file being replaced */ }
203
+ }
204
+ return best?.id ?? null
205
+ }
206
+
207
+ function findCodex() {
208
+ if (process.env.TWOCMD_CODEX) return process.env.TWOCMD_CODEX
209
+ const self = fileURLToPath(import.meta.url)
210
+ for (const dir of (process.env.PATH ?? '').split(delimiter)) {
211
+ try {
212
+ accessSync(join(dir, 'codex'), constants.X_OK)
213
+ if (realpathSync(join(dir, 'codex')) !== self) return join(dir, 'codex') // a `codex` that is this wrapper would start itself forever
214
+ } catch {}
215
+ }
216
+ const bundled = '/Applications/ChatGPT.app/Contents/Resources/codex-cli/bin/codex' // the CLI inside the Codex app
217
+ try { accessSync(bundled, constants.X_OK); return bundled } catch { return null }
218
+ }
219
+
220
+ async function main() {
221
+ const codex = findCodex()
222
+ if (!codex) {
223
+ console.error('2cmd: codex not found. Install the Codex CLI or set TWOCMD_CODEX to it.')
224
+ process.exit(127)
225
+ }
226
+ const args = process.argv.slice(2)
227
+ // Orca sets ORCA_PANE_KEY in each of its terminals and shows 2Cmd in its own sidebar (orca/).
228
+ // Without python3 and its Unix pseudo-terminal (Windows, or a Mac without it), Codex still starts, just without the panel.
229
+ const ptyWorks = () => spawnSync('python3', ['-c', 'import pty, termios'], { stdio: 'ignore' }).status === 0
230
+ if (!process.stdin.isTTY || !process.stdout.isTTY || process.env.TWOCMD_PANEL === 'off' || process.env.ORCA_PANE_KEY || !ptyWorks()) {
231
+ spawn(codex, args, { stdio: 'inherit' }).on('exit', code => process.exit(code ?? 1))
232
+ return
233
+ }
234
+
235
+ // The shared modules warn on stderr, which is Codex's screen here.
236
+ console.warn = () => {}
237
+ const { answer } = await import('./data.mjs')
238
+ // The Mac's own language first, as in the desktop panels; terminals set LANG on their own, often to en_US.
239
+ const macLanguage = () => process.platform === 'darwin'
240
+ ? spawnSync('/usr/bin/defaults', ['read', '-g', 'AppleLanguages'], { encoding: 'utf8' }).stdout?.match(/"?([A-Za-z]{2,3}(?:-[A-Za-z0-9]+)*)"?/)?.[1]
241
+ : undefined
242
+ const locale = process.env.LC_ALL || process.env.LC_MESSAGES
243
+ const language = locale ? locale.split('.')[0].replace('_', '-')
244
+ : macLanguage() || process.env.LANG?.split('.')[0].replace('_', '-') || Intl.DateTimeFormat().resolvedOptions().locale
245
+ const { t } = await answer({ op: 'start', language })
246
+ const liveDir = join(process.env.TWOCMD_DATA, 'live')
247
+ const cwds = [...new Set([process.cwd(), realpathSync(process.cwd())])]
248
+ const started = Date.now() - 2000
249
+
250
+ const out = process.stdout
251
+ let open = false
252
+ const panelShown = () => screen.flags.alt && out.rows >= 12
253
+ const panelRows = () => (open && out.rows >= 20 ? OPEN_ROWS : 2)
254
+ const codexRows = () => (panelShown() ? out.rows - panelRows() : out.rows)
255
+ const screen = createScreenFilter(codexRows)
256
+
257
+ const bridge = spawn('python3', [fileURLToPath(new URL('./pty.py', import.meta.url)), String(out.rows), String(out.columns), codex, ...args], {
258
+ stdio: ['pipe', 'pipe', 'inherit', 'pipe'],
259
+ })
260
+ bridge.on('error', () => {
261
+ process.stdin.setRawMode(false)
262
+ console.error('2cmd: could not start python3 for the panel. Run codex directly.')
263
+ process.exit(127)
264
+ })
265
+ let lastSize = `${out.rows} ${out.columns}`
266
+ const sendSize = () => {
267
+ const size = `${codexRows()} ${out.columns}`
268
+ if (size !== lastSize) bridge.stdio[3].write(`${size}\n`)
269
+ lastSize = size
270
+ }
271
+
272
+ let id = null
273
+ let early = null // a mode chosen before this window's conversation is known
274
+ let data = null
275
+ let notice = ''
276
+ let noticeUntil = 0
277
+ let drawn = '' // what the panel rows show now
278
+ let hits = []
279
+ let regionSet = false
280
+
281
+ // force: redraw even if unchanged, since Codex can scroll or overwrite the rows without clearing the screen.
282
+ function draw(force = false) {
283
+ if (!panelShown()) {
284
+ if (regionSet && screen.safe()) { out.write('\x1b7\x1b[r\x1b8'); regionSet = false; drawn = '' }
285
+ return
286
+ }
287
+ if (!screen.safe()) return
288
+ const view = layout(t, data ?? (early ? { pending: early } : null), out.columns, Date.now() < noticeUntil ? notice : '', panelRows() > 2)
289
+ hits = view.hits
290
+ const top = out.rows - panelRows()
291
+ const text = `\x1b7\x1b[1;${top}r${view.rows.map((row, i) => `\x1b[${top + 1 + i};1H\x1b[0m\x1b[2K${row}`).join('')}\x1b[0m\x1b8`
292
+ if (text === drawn && !screen.flags.clear && !force) return
293
+ screen.flags.clear = false
294
+ regionSet = true
295
+ drawn = text
296
+ out.write(text)
297
+ }
298
+
299
+ async function refresh() {
300
+ id = findConversation(liveDir, cwds, started) ?? id
301
+ if (id && early) await answer({ op: 'mode', id, mode: early }).catch(() => say(t.saveFail))
302
+ if (id) early = null
303
+ try { data = id ? await answer({ op: 'state', id, language }) : null } catch { data = null }
304
+ draw(true)
305
+ }
306
+
307
+ function say(text) {
308
+ notice = text
309
+ noticeUntil = Date.now() + 3000
310
+ draw()
311
+ }
312
+
313
+ async function act(action) {
314
+ if (action.toggle) {
315
+ open = !open
316
+ sendSize() // Codex redraws in its new height and clears the screen, which redraws the panel
317
+ return draw(true)
318
+ }
319
+ if (action.board) return answer({ op: 'board', language }).catch(() => say(t.boardFail))
320
+ if (!id && action.mode) { early = action.mode; return draw(true) } // kept until the first message names the conversation
321
+ if (!id) return say(t.waiting)
322
+ if (action.mode) {
323
+ await answer({ op: 'mode', id, mode: action.mode })
324
+ return refresh()
325
+ }
326
+ const { button } = action
327
+ if (button.kind === 'copy') {
328
+ await answer({ op: 'copy', id }).then(() => say(t.copied), () => say(t.copyFail.replace('{r}', 'pbcopy')))
329
+ return
330
+ }
331
+ // Pasted, so Codex takes it as one piece of text at the cursor instead of keystrokes that open its popups.
332
+ bridge.stdin.write(screen.flags.paste ? `\x1b[200~${button.text} \x1b[201~` : `${button.text} `)
333
+ }
334
+
335
+ let quiet = null
336
+ bridge.stdout.on('data', (chunk) => {
337
+ const wasAlt = screen.flags.alt
338
+ out.write(screen.filter(chunk))
339
+ if (screen.flags.alt !== wasAlt) sendSize()
340
+ draw()
341
+ clearTimeout(quiet)
342
+ quiet = setTimeout(() => {
343
+ const stuck = screen.settle()
344
+ if (stuck && process.env.TWOCMD_PANEL_LOG) appendFileSync(process.env.TWOCMD_PANEL_LOG, `${new Date().toISOString()} ${stuck}\n`)
345
+ if (stuck) draw(true)
346
+ }, 500)
347
+ })
348
+
349
+ let carry = ''
350
+ let carryTimer = null
351
+ process.stdin.setRawMode(true)
352
+ process.stdin.on('data', (chunk) => {
353
+ clearTimeout(carryTimer)
354
+ let text = carry + chunk.toString('latin1') // one byte per character, so nothing is lost on the way back
355
+ carry = ''
356
+ const tail = text.match(/\x1b(?:\[[<\d;:]*)?$/) // a sequence cut between two reads
357
+ if (tail) {
358
+ carry = tail[0]
359
+ text = text.slice(0, -carry.length)
360
+ carryTimer = setTimeout(() => { bridge.stdin.write(Buffer.from(carry, 'latin1')); carry = '' }, 30)
361
+ }
362
+ const { forward, actions } = splitInput(text, codexRows(), panelShown())
363
+ if (forward) bridge.stdin.write(Buffer.from(forward, 'latin1'))
364
+ for (const { click, row, shortcut } of actions) {
365
+ if (shortcut) {
366
+ const now = early ?? data?.pending ?? data?.mode ?? 'reuse'
367
+ act({ mode: RUNTIME_MODES[(RUNTIME_MODES.indexOf(now) + 1) % RUNTIME_MODES.length] }).catch(() => say(t.saveFail))
368
+ } else {
369
+ const hit = hits.find(h => h.row === row && click >= h.from && click <= h.to)
370
+ if (hit) act(hit.action).catch(() => say(t.saveFail))
371
+ }
372
+ }
373
+ })
374
+
375
+ out.on('resize', () => { sendSize(); drawn = ''; draw() })
376
+ const timer = setInterval(refresh, 1000)
377
+ refresh()
378
+
379
+ bridge.on('exit', (code, signal) => {
380
+ clearInterval(timer)
381
+ out.write('\x1b7\x1b[r\x1b8')
382
+ process.stdin.setRawMode(false)
383
+ process.exit(code ?? (signal ? 1 : 0))
384
+ })
385
+ for (const signal of ['SIGTERM', 'SIGHUP']) process.on(signal, () => bridge.kill(signal))
386
+ }
387
+
388
+ if (process.argv[1] && realpathSync(process.argv[1]) === fileURLToPath(import.meta.url)) main()
package/dsh/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 DietrichGebert
4
+ Copyright (c) 2026 2Cmd contributors
5
+
6
+ Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ of this software and associated documentation files (the "Software"), to deal
8
+ in the Software without restriction, including without limitation the rights
9
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ copies of the Software, and to permit persons to whom the Software is
11
+ furnished to do so, subject to the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be included in all
14
+ copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ SOFTWARE.