opencode-v2-double-ctrl-c 0.0.0-stage → 0.3.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/CHANGELOG.md +48 -0
- package/LICENSE +19 -0
- package/README.md +113 -2
- package/package.json +38 -4
- package/src/tui.ts +170 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
## 0.3.2
|
|
4
|
+
|
|
5
|
+
- README pre-publish TODOs removed now the repo is public.
|
|
6
|
+
- Dev toolchain: TypeScript 7.0.2, `@opencode/plugin` 2.4.2; CI uses
|
|
7
|
+
`actions/setup-node` 7.1.0. No shipped-code changes.
|
|
8
|
+
|
|
9
|
+
## 0.3.1
|
|
10
|
+
|
|
11
|
+
- Interrupt toast now matches the host two-press `Esc` behavior: it reads
|
|
12
|
+
`<key> again to interrupt` with a fixed 5s duration instead of falsely
|
|
13
|
+
claiming `interrupted` on the arming press. `window` still only affects
|
|
14
|
+
exit; the 5s interrupt timing is the host's.
|
|
15
|
+
- README corrected: while a turn is running `ctrl+c` never exits (exit
|
|
16
|
+
applies once idle); the host statusline names `esc` but `ctrl+c` arms
|
|
17
|
+
the same host counter.
|
|
18
|
+
- README rewritten for regular users: plain-language behavior up front,
|
|
19
|
+
internals demoted to Notes/Credits, local symlink testing documented.
|
|
20
|
+
|
|
21
|
+
## 0.3.0
|
|
22
|
+
|
|
23
|
+
- Fail-safe unknown prompt state: a missing/destroyed/unreadable editor
|
|
24
|
+
arms with a warning toast and never exits or interrupts, so an upstream
|
|
25
|
+
rename of the undocumented `currentFocusedEditor` cannot exit with
|
|
26
|
+
unsent text.
|
|
27
|
+
- Whitespace-only prompt counts as empty (trimmed check).
|
|
28
|
+
- `session.interrupt` now shows an `interrupted` toast.
|
|
29
|
+
- Toast timing: confirmations use the host 5s default; only the
|
|
30
|
+
`again to exit` hint sets a duration, mirroring the exit window.
|
|
31
|
+
- Stash restore hint reworded: shows your `prompt.stash.pop` binding when
|
|
32
|
+
bound, otherwise points at the `Stash pop` palette entry (it ships
|
|
33
|
+
unbound — corrected the `Up` claim in README).
|
|
34
|
+
- New `typecheck`/`check`/`pack:check` npm aliases; options narrowed to
|
|
35
|
+
`Record<string, unknown>`.
|
|
36
|
+
|
|
37
|
+
## 0.2.0
|
|
38
|
+
|
|
39
|
+
- Stash-first clear (Codex parity, upstream issue #1): with text, dispatch
|
|
40
|
+
`prompt.stash` when available (toast with restore hint), falling back to
|
|
41
|
+
`prompt.clear` on older hosts.
|
|
42
|
+
- New `stash` option (default `true`); set `stash: false` for destructive
|
|
43
|
+
upstream v1 behavior.
|
|
44
|
+
|
|
45
|
+
## 0.1.0
|
|
46
|
+
|
|
47
|
+
- Initial v2-only port of `tannerbruhn/opencode-double-ctrl-c`.
|
|
48
|
+
- `interrupt` defaults to `true` (upstream: `false`).
|
package/LICENSE
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
Copyright (c) 2026 Tomas Mlcoch
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
4
|
+
of this software and associated documentation files (the “Software”), to deal
|
|
5
|
+
in the Software without restriction, including without limitation the rights
|
|
6
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
7
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
8
|
+
furnished to do so, subject to the following conditions:
|
|
9
|
+
|
|
10
|
+
The above copyright notice and this permission notice shall be included in all
|
|
11
|
+
copies or substantial portions of the Software.
|
|
12
|
+
|
|
13
|
+
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
14
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
15
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
16
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
17
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
18
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
19
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,114 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Double Ctrl+C plugin for OpenCode v2
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
**Stop quitting OpenCode by accident**
|
|
4
|
+
|
|
5
|
+
This is a plugin for the OpenCode v2 (CLI/TUI) that makes `ctrl+c` safe to press because the first press never quits.
|
|
6
|
+
|
|
7
|
+
- **When you have something typed** -> it saves (stash) the text first to clear the prompt.
|
|
8
|
+
- **When you have agent running** -> it behaves like if you press `Esc` to stop the agent first.
|
|
9
|
+
- **When nothing is running and typed** -> it shows "press again to exit" and only the second press within a short moment quits.
|
|
10
|
+
|
|
11
|
+
No network access, no files written, nothing running in the background.
|
|
12
|
+
|
|
13
|
+
The 10-second install that ends accidental quits and feels familiar if you use Claude Code / Codex CLI.
|
|
14
|
+
|
|
15
|
+
Requires OpenCode `>= 2`.
|
|
16
|
+
|
|
17
|
+
## Install
|
|
18
|
+
|
|
19
|
+
Preferred install:
|
|
20
|
+
|
|
21
|
+
```sh
|
|
22
|
+
opencode plugin add opencode-v2-double-ctrl-c
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
Manual install — add to your `cli.json` (`~/.config/opencode/cli.json` for global use):
|
|
26
|
+
|
|
27
|
+
```json
|
|
28
|
+
{
|
|
29
|
+
"$schema": "https://opencode.ai/v2/cli.json",
|
|
30
|
+
"plugins": ["opencode-v2-double-ctrl-c"]
|
|
31
|
+
}
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
Then restart the TUI.
|
|
35
|
+
|
|
36
|
+
## Options
|
|
37
|
+
|
|
38
|
+
Tune the plugin in the `cli.json` file:
|
|
39
|
+
|
|
40
|
+
```json
|
|
41
|
+
{
|
|
42
|
+
"plugins": [["opencode-v2-double-ctrl-c", { "window": 1500, "interrupt": true, "stash": true, "key": "ctrl+c" }]]
|
|
43
|
+
}
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
| Option | Default | What it changes |
|
|
47
|
+
| ------ | ------- | --------------- |
|
|
48
|
+
| `window` | `1500` | How quickly the second press must follow to quit, in milliseconds. Lower is stricter. Must be `> 0`. |
|
|
49
|
+
| `interrupt` | `true` | Empty prompt while the agent is running: stop the agent first (`true`), or offer to quit instead (`false`). |
|
|
50
|
+
| `stash` | `true` | What happens to typed text on `ctrl+c`: save it for restore (`true`), or discard it (`false`). |
|
|
51
|
+
| `key` | `ctrl+c` | Which key is guarded. Must be a non-empty key string. |
|
|
52
|
+
|
|
53
|
+
Bad values fall back to defaults - a typo in config never crashes the TUI.
|
|
54
|
+
|
|
55
|
+
Revert to OpenCode default of discarding text instead of saving it:
|
|
56
|
+
|
|
57
|
+
```json
|
|
58
|
+
{
|
|
59
|
+
"plugins": [["opencode-v2-double-ctrl-c", { "stash": false }]]
|
|
60
|
+
}
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
Revert to OpenCode default of never interrupting with `ctrl+c`:
|
|
64
|
+
|
|
65
|
+
```json
|
|
66
|
+
{
|
|
67
|
+
"plugins": [["opencode-v2-double-ctrl-c", { "interrupt": false }]]
|
|
68
|
+
}
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
Note: the exit timing follows `window`, while the interrupt confirmation timing is fixed by OpenCode itself.
|
|
72
|
+
|
|
73
|
+
## If a single press still quits
|
|
74
|
+
|
|
75
|
+
Another keybinding is winning over this plugin. As a fallback, free up the built-in quit binding in `cli.json`:
|
|
76
|
+
|
|
77
|
+
```json
|
|
78
|
+
{
|
|
79
|
+
"keybinds": { "app.exit": "ctrl+d,<leader>q" }
|
|
80
|
+
}
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
## Development
|
|
84
|
+
|
|
85
|
+
No install needed for local testing - symlink this repo into a project where you want to test it:
|
|
86
|
+
|
|
87
|
+
```sh
|
|
88
|
+
mkdir -p .opencode/plugins
|
|
89
|
+
ln -s /path/to/opencode-v2-double-ctrl-c .opencode/plugins/opencode-v2-double-ctrl-c
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
Check it with:
|
|
93
|
+
|
|
94
|
+
```sh
|
|
95
|
+
ll .opencode/plugins/opencode-v2-double-ctrl-c
|
|
96
|
+
# lrwxrwxrwx ... .opencode/plugins/opencode-v2-double-ctrl-c -> /path/to/opencode-v2-double-ctrl-c
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
Restart the TUI and the plugin loads from the symlink.
|
|
100
|
+
|
|
101
|
+
## Notes
|
|
102
|
+
|
|
103
|
+
- Attached files without typed text count as empty prompt - the plugin looks at text only.
|
|
104
|
+
- Whitespace-only input counts as empty prompt.
|
|
105
|
+
|
|
106
|
+
## Credits
|
|
107
|
+
|
|
108
|
+
Inspired by `tannerbruhn/opencode-double-ctrl-c` for OpenCode v1.
|
|
109
|
+
|
|
110
|
+
## Support
|
|
111
|
+
|
|
112
|
+
- Issues: `https://github.com/tojaj/opencode-v2-double-ctrl-c/issues`
|
|
113
|
+
- Version history: see `CHANGELOG.md`
|
|
114
|
+
- License: MIT
|
package/package.json
CHANGED
|
@@ -1,6 +1,40 @@
|
|
|
1
1
|
{
|
|
2
|
+
"$schema": "https://json.schemastore.org/package.json",
|
|
2
3
|
"name": "opencode-v2-double-ctrl-c",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
4
|
+
"version": "0.3.2",
|
|
5
|
+
"description": "Double-press ctrl+c to clear the prompt or exit the OpenCode v2 TUI (Claude Code / Codex CLI parity).",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"type": "module",
|
|
8
|
+
"scripts": {
|
|
9
|
+
"typecheck": "tsc --noEmit",
|
|
10
|
+
"check": "tsc --noEmit",
|
|
11
|
+
"pack:check": "npm pack --dry-run"
|
|
12
|
+
},
|
|
13
|
+
"keywords": ["opencode", "opencode-plugin", "tui", "keybind", "productivity", "ctrl-c"],
|
|
14
|
+
"homepage": "https://github.com/tojaj/opencode-v2-double-ctrl-c",
|
|
15
|
+
"repository": {
|
|
16
|
+
"type": "git",
|
|
17
|
+
"url": "git+https://github.com/tojaj/opencode-v2-double-ctrl-c.git"
|
|
18
|
+
},
|
|
19
|
+
"bugs": {
|
|
20
|
+
"url": "https://github.com/tojaj/opencode-v2-double-ctrl-c/issues"
|
|
21
|
+
},
|
|
22
|
+
"exports": {
|
|
23
|
+
"./tui": "./src/tui.ts",
|
|
24
|
+
"./package.json": "./package.json"
|
|
25
|
+
},
|
|
26
|
+
"files": ["src", "README.md", "LICENSE", "CHANGELOG.md"],
|
|
27
|
+
"publishConfig": {
|
|
28
|
+
"access": "public"
|
|
29
|
+
},
|
|
30
|
+
"peerDependencies": {
|
|
31
|
+
"@opencode/plugin": ">=2"
|
|
32
|
+
},
|
|
33
|
+
"devDependencies": {
|
|
34
|
+
"@opencode/plugin": ">=2",
|
|
35
|
+
"typescript": "^7.0.2"
|
|
36
|
+
},
|
|
37
|
+
"engines": {
|
|
38
|
+
"opencode": ">=2"
|
|
39
|
+
}
|
|
40
|
+
}
|
package/src/tui.ts
ADDED
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
import { Plugin } from "@opencode/plugin/tui"
|
|
2
|
+
|
|
3
|
+
const DEFAULT_WINDOW = 1500
|
|
4
|
+
// Host shows 5s toasts when `duration` is omitted (toast.tsx); only the
|
|
5
|
+
// arm-to-exit hint carries timing semantics, so only it sets one (capped
|
|
6
|
+
// at the host default).
|
|
7
|
+
const MAX_TOAST_DURATION = 5000
|
|
8
|
+
const DEFAULT_KEY = "ctrl+c"
|
|
9
|
+
const DEFAULT_INTERRUPT = true
|
|
10
|
+
const DEFAULT_STASH = true
|
|
11
|
+
|
|
12
|
+
type EditorLike = {
|
|
13
|
+
readonly plainText?: unknown
|
|
14
|
+
readonly isDestroyed?: boolean
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
function readEditor(ctx: Plugin.Context): EditorLike | null | undefined {
|
|
18
|
+
// FRAGILE/UNDOCUMENTED: `currentFocusedEditor` is not part of the stable
|
|
19
|
+
// plugin Context API — it comes from the underlying CliRenderer
|
|
20
|
+
// (`@opentui/core`). The host reads the same object for its managed
|
|
21
|
+
// textarea layer (`packages/tui/src/context/keymap.tsx:94` checks
|
|
22
|
+
// `instanceof TextareaRenderable`), and the mini prompt falls back the
|
|
23
|
+
// same way (`packages/tui/src/mini/footer.prompt.tsx:980`:
|
|
24
|
+
// `area && !area.isDestroyed ? area.plainText : draft.text`).
|
|
25
|
+
// Duck-typed on purpose so this plugin does not need a hard
|
|
26
|
+
// `@opentui/core` dependency.
|
|
27
|
+
const renderer = ctx.renderer as unknown as {
|
|
28
|
+
readonly currentFocusedEditor?: EditorLike | null
|
|
29
|
+
}
|
|
30
|
+
return renderer.currentFocusedEditor
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function promptState(ctx: Plugin.Context): "text" | "empty" | "unknown" {
|
|
34
|
+
const editor = readEditor(ctx)
|
|
35
|
+
if (!editor || editor.isDestroyed) return "unknown"
|
|
36
|
+
if (typeof editor.plainText !== "string") return "unknown"
|
|
37
|
+
return editor.plainText.trim().length > 0 ? "text" : "empty"
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function hasCommand(ctx: Plugin.Context, id: string): boolean {
|
|
41
|
+
try {
|
|
42
|
+
return ctx.keymap.commands().some((c) => c.id === id)
|
|
43
|
+
} catch {
|
|
44
|
+
return false
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function stashRestoreHint(ctx: Plugin.Context): string {
|
|
49
|
+
// Prefer the user's actual binding when discoverable. Otherwise point at
|
|
50
|
+
// the palette entry by title: `prompt.stash.pop` ships with no default
|
|
51
|
+
// keybinding (`bind: false`), so naming the command ID or a key would
|
|
52
|
+
// send users down a dead end.
|
|
53
|
+
try {
|
|
54
|
+
const shortcuts = ctx.keymap.shortcuts("prompt.stash.pop")
|
|
55
|
+
if (shortcuts.length > 0) return `stashed — press ${shortcuts[0]} to restore`
|
|
56
|
+
} catch {
|
|
57
|
+
// Ignore and fall through to the palette hint.
|
|
58
|
+
}
|
|
59
|
+
return `stashed — "Stash pop" in the palette restores it`
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function isSessionRunning(ctx: Plugin.Context): boolean {
|
|
63
|
+
const route = ctx.ui.router.current()
|
|
64
|
+
if (route.type !== "session") return false
|
|
65
|
+
try {
|
|
66
|
+
return ctx.data.session.status(route.sessionID) === "running"
|
|
67
|
+
} catch {
|
|
68
|
+
// Stale/deleted session: fall back to the arm+toast path.
|
|
69
|
+
return false
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function resolveOptions(raw: Readonly<Record<string, unknown>>) {
|
|
74
|
+
const windowRaw = raw.window
|
|
75
|
+
const keyRaw = raw.key
|
|
76
|
+
const interruptRaw = raw.interrupt
|
|
77
|
+
const stashRaw = raw.stash
|
|
78
|
+
const windowMs =
|
|
79
|
+
typeof windowRaw === "number" && Number.isFinite(windowRaw) && windowRaw > 0 ? windowRaw : DEFAULT_WINDOW
|
|
80
|
+
const key = typeof keyRaw === "string" && keyRaw.trim().length > 0 ? keyRaw : DEFAULT_KEY
|
|
81
|
+
const interrupt = typeof interruptRaw === "boolean" ? interruptRaw : DEFAULT_INTERRUPT
|
|
82
|
+
const stash = typeof stashRaw === "boolean" ? stashRaw : DEFAULT_STASH
|
|
83
|
+
return { windowMs, key, interrupt, stash }
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
export default Plugin.define({
|
|
87
|
+
id: "double-ctrl-c",
|
|
88
|
+
setup(ctx) {
|
|
89
|
+
const { windowMs, key, interrupt, stash } = resolveOptions(ctx.options)
|
|
90
|
+
// Setup-closure local (not `storage.memory`): a hot-reload re-runs
|
|
91
|
+
// `setup` and resets the double-press window instead of letting a
|
|
92
|
+
// stale `armed` timestamp survive and exit on the next press.
|
|
93
|
+
let armed = 0
|
|
94
|
+
|
|
95
|
+
ctx.keymap.layer(() => ({
|
|
96
|
+
priority: 1000,
|
|
97
|
+
commands: [
|
|
98
|
+
{
|
|
99
|
+
id: "double_ctrl_c.press",
|
|
100
|
+
title: "Stash or clear the prompt, or exit on a double press",
|
|
101
|
+
group: "Plugin",
|
|
102
|
+
bind: key,
|
|
103
|
+
run() {
|
|
104
|
+
const state = promptState(ctx)
|
|
105
|
+
// 1. Text present -> stash it (Codex parity), falling back to
|
|
106
|
+
// clear on hosts without `prompt.stash` (v1 parity:
|
|
107
|
+
// text-only check, file parts ignored — see README).
|
|
108
|
+
// Whitespace-only counts as empty (trimmed above).
|
|
109
|
+
if (state === "text") {
|
|
110
|
+
armed = 0
|
|
111
|
+
if (stash && hasCommand(ctx, "prompt.stash")) {
|
|
112
|
+
ctx.keymap.dispatch("prompt.stash")
|
|
113
|
+
ctx.ui.toast.show({
|
|
114
|
+
variant: "info",
|
|
115
|
+
message: stashRestoreHint(ctx),
|
|
116
|
+
})
|
|
117
|
+
return
|
|
118
|
+
}
|
|
119
|
+
ctx.keymap.dispatch("prompt.clear")
|
|
120
|
+
return
|
|
121
|
+
}
|
|
122
|
+
// 1b. Unknown editor state -> fail safe: arm but never exit.
|
|
123
|
+
// If `currentFocusedEditor` disappears/renames upstream,
|
|
124
|
+
// treating it as empty would exit with unsent text. Instead
|
|
125
|
+
// just re-arm and tell the user; the host `app.exit` binding
|
|
126
|
+
// remains as an escape hatch.
|
|
127
|
+
if (state === "unknown") {
|
|
128
|
+
armed = Date.now()
|
|
129
|
+
ctx.ui.toast.show({
|
|
130
|
+
variant: "warning",
|
|
131
|
+
message: "prompt state unknown — not exiting",
|
|
132
|
+
})
|
|
133
|
+
return
|
|
134
|
+
}
|
|
135
|
+
// 2. Empty prompt + running turn + interrupt:true -> arm/interrupt
|
|
136
|
+
// like Esc: the host needs two `session.interrupt` dispatches
|
|
137
|
+
// within 5s to actually interrupt, so one dispatch only arms.
|
|
138
|
+
if (interrupt && isSessionRunning(ctx)) {
|
|
139
|
+
armed = 0
|
|
140
|
+
ctx.keymap.dispatch("session.interrupt")
|
|
141
|
+
ctx.ui.toast.show({
|
|
142
|
+
variant: "info",
|
|
143
|
+
message: `${key} again to interrupt`,
|
|
144
|
+
// Host two-press window is a fixed 5s (unlike the
|
|
145
|
+
// configurable exit `window`), so mirror that here.
|
|
146
|
+
duration: MAX_TOAST_DURATION,
|
|
147
|
+
})
|
|
148
|
+
return
|
|
149
|
+
}
|
|
150
|
+
// 3. Otherwise double-press to exit.
|
|
151
|
+
const now = Date.now()
|
|
152
|
+
if (now - armed <= windowMs) {
|
|
153
|
+
armed = 0
|
|
154
|
+
ctx.keymap.dispatch("app.exit")
|
|
155
|
+
return
|
|
156
|
+
}
|
|
157
|
+
armed = now
|
|
158
|
+
ctx.ui.toast.show({
|
|
159
|
+
variant: "info",
|
|
160
|
+
message: `${key} again to exit`,
|
|
161
|
+
// The only time-sensitive hint: mirror the exit window so it
|
|
162
|
+
// never promises (or hides) an arming that already expired.
|
|
163
|
+
duration: Math.min(windowMs, MAX_TOAST_DURATION),
|
|
164
|
+
})
|
|
165
|
+
},
|
|
166
|
+
},
|
|
167
|
+
],
|
|
168
|
+
}))
|
|
169
|
+
},
|
|
170
|
+
})
|