|
2 | 2 | import { useTerminalDimensions, type JSX } from "@opentui/solid" |
3 | 3 | import { useBindings, useKeymapSelector } from "@opentui/keymap/solid" |
4 | 4 | import { RGBA, VignetteEffect, type KeyEvent, type Renderable } from "@opentui/core" |
5 | | -import { resolveBindingSections, type BindingSectionsConfig, type BindingValue } from "@opentui/keymap/extras" |
6 | | -import type { Binding } from "@opentui/keymap" |
| 5 | +import { createBindingLookup, type BindingConfig } from "@opentui/keymap/extras" |
7 | 6 | import type { TuiPlugin, TuiPluginApi, TuiPluginMeta, TuiPluginModule, TuiSlotPlugin } from "@opencode-ai/plugin/tui" |
8 | 7 |
|
9 | 8 | const tabs = ["overview", "counter", "help"] |
10 | 9 | const command = { |
11 | | - modal: "plugin.smoke.modal", |
12 | | - screen: "plugin.smoke.screen", |
13 | | - alert: "plugin.smoke.alert", |
14 | | - confirm: "plugin.smoke.confirm", |
15 | | - prompt: "plugin.smoke.prompt", |
16 | | - select: "plugin.smoke.select", |
17 | | - host: "plugin.smoke.host", |
18 | | - home: "plugin.smoke.home", |
19 | | - toast: "plugin.smoke.toast", |
20 | | - dialog_close: "plugin.smoke.dialog.close", |
21 | | - local_push: "plugin.smoke.local.push", |
22 | | - local_pop: "plugin.smoke.local.pop", |
23 | | - screen_home: "plugin.smoke.screen.home", |
24 | | - screen_left: "plugin.smoke.screen.left", |
25 | | - screen_right: "plugin.smoke.screen.right", |
26 | | - screen_up: "plugin.smoke.screen.up", |
27 | | - screen_down: "plugin.smoke.screen.down", |
28 | | - screen_modal: "plugin.smoke.screen.modal", |
29 | | - screen_local: "plugin.smoke.screen.local", |
30 | | - screen_host: "plugin.smoke.screen.host", |
31 | | - screen_alert: "plugin.smoke.screen.alert", |
32 | | - screen_confirm: "plugin.smoke.screen.confirm", |
33 | | - screen_prompt: "plugin.smoke.screen.prompt", |
34 | | - screen_select: "plugin.smoke.screen.select", |
35 | | - modal_accept: "plugin.smoke.modal.accept", |
36 | | - modal_close: "plugin.smoke.modal.close", |
37 | | -} as const |
38 | | - |
39 | | -const sectionNames = ["global", "dialog", "local", "screen", "modal"] as const |
40 | | -type SectionName = (typeof sectionNames)[number] |
41 | | -type SectionConfig = Record<string, BindingValue<Renderable, KeyEvent>> |
42 | | -type ResolvedSections = Record<SectionName, Binding<Renderable, KeyEvent>[]> |
43 | | -type SmokeKeymap = { |
44 | | - sections?: Partial<Record<SectionName, SectionConfig>> |
| 10 | + modal: "smoke_modal", |
| 11 | + screen: "smoke_screen", |
| 12 | + alert: "smoke_alert", |
| 13 | + confirm: "smoke_confirm", |
| 14 | + prompt: "smoke_prompt", |
| 15 | + select: "smoke_select", |
| 16 | + host: "smoke_host", |
| 17 | + home: "smoke_home", |
| 18 | + toast: "smoke_toast", |
| 19 | + dialog_close: "smoke_dialog_close", |
| 20 | + local_push: "smoke_local_push", |
| 21 | + local_pop: "smoke_local_pop", |
| 22 | + screen_home: "smoke_screen_home", |
| 23 | + screen_left: "smoke_screen_left", |
| 24 | + screen_right: "smoke_screen_right", |
| 25 | + screen_up: "smoke_screen_up", |
| 26 | + screen_down: "smoke_screen_down", |
| 27 | + screen_modal: "smoke_screen_modal", |
| 28 | + screen_local: "smoke_screen_local", |
| 29 | + screen_host: "smoke_screen_host", |
| 30 | + screen_alert: "smoke_screen_alert", |
| 31 | + screen_confirm: "smoke_screen_confirm", |
| 32 | + screen_prompt: "smoke_screen_prompt", |
| 33 | + screen_select: "smoke_screen_select", |
| 34 | + modal_accept: "smoke_modal_accept", |
| 35 | + modal_close: "smoke_modal_close", |
45 | 36 | } |
46 | 37 |
|
47 | | -type SmokeOptions = { |
48 | | - enabled?: boolean |
49 | | - label?: unknown |
50 | | - route?: unknown |
51 | | - vignette?: unknown |
52 | | - keymap?: SmokeKeymap |
53 | | -} |
| 38 | +type SmokeBindings = BindingConfig<Renderable, KeyEvent> |
54 | 39 |
|
55 | 40 | const defaultKeymap = { |
56 | | - global: { |
57 | | - [command.modal]: "ctrl+shift+m", |
58 | | - [command.screen]: "ctrl+shift+o", |
59 | | - }, |
60 | | - dialog: { |
61 | | - [command.dialog_close]: "escape", |
62 | | - }, |
63 | | - local: { |
64 | | - [command.local_push]: "enter,return", |
65 | | - [command.local_pop]: "escape,q,backspace", |
66 | | - }, |
67 | | - screen: { |
68 | | - [command.screen_home]: "escape,ctrl+h", |
69 | | - [command.screen_left]: "left,h", |
70 | | - [command.screen_right]: "right,l", |
71 | | - [command.screen_up]: "up,k", |
72 | | - [command.screen_down]: "down,j", |
73 | | - [command.screen_modal]: "ctrl+shift+m", |
74 | | - [command.screen_local]: "x", |
75 | | - [command.screen_host]: "z", |
76 | | - [command.screen_alert]: "a", |
77 | | - [command.screen_confirm]: "c", |
78 | | - [command.screen_prompt]: "p", |
79 | | - [command.screen_select]: "s", |
80 | | - }, |
81 | | - modal: { |
82 | | - [command.modal_accept]: "enter,return", |
83 | | - [command.modal_close]: "escape", |
84 | | - }, |
85 | | -} satisfies Record<SectionName, SectionConfig> |
| 41 | + [command.modal]: "ctrl+shift+m", |
| 42 | + [command.screen]: "ctrl+shift+o", |
| 43 | + [command.dialog_close]: "escape", |
| 44 | + [command.local_push]: "enter,return", |
| 45 | + [command.local_pop]: "escape,q,backspace", |
| 46 | + [command.screen_home]: "escape,ctrl+h", |
| 47 | + [command.screen_left]: "left,h", |
| 48 | + [command.screen_right]: "right,l", |
| 49 | + [command.screen_up]: "up,k", |
| 50 | + [command.screen_down]: "down,j", |
| 51 | + [command.screen_modal]: "ctrl+shift+m", |
| 52 | + [command.screen_local]: "x", |
| 53 | + [command.screen_host]: "z", |
| 54 | + [command.screen_alert]: "a", |
| 55 | + [command.screen_confirm]: "c", |
| 56 | + [command.screen_prompt]: "p", |
| 57 | + [command.screen_select]: "s", |
| 58 | + [command.modal_accept]: "enter,return", |
| 59 | + [command.modal_close]: "escape", |
| 60 | +} |
86 | 61 |
|
87 | 62 | const pick = (value: unknown, fallback: string) => { |
88 | 63 | if (typeof value !== "string") return fallback |
@@ -95,11 +70,14 @@ const num = (value: unknown, fallback: number) => { |
95 | 70 | return value |
96 | 71 | } |
97 | 72 |
|
| 73 | +const record = (value: unknown): value is Record<string, unknown> => |
| 74 | + !!value && typeof value === "object" && !Array.isArray(value) |
| 75 | + |
98 | 76 | type Cfg = { |
99 | 77 | label: string |
100 | 78 | route: string |
101 | 79 | vignette: number |
102 | | - keymap: SmokeKeymap | undefined |
| 80 | + keybinds: SmokeBindings | undefined |
103 | 81 | } |
104 | 82 |
|
105 | 83 | type Route = { |
@@ -116,12 +94,12 @@ type State = { |
116 | 94 | local: number |
117 | 95 | } |
118 | 96 |
|
119 | | -const cfg = (options: SmokeOptions | undefined) => { |
| 97 | +const cfg = (options: Record<string, unknown> | undefined) => { |
120 | 98 | return { |
121 | 99 | label: pick(options?.label, "smoke"), |
122 | 100 | route: pick(options?.route, "workspace-smoke"), |
123 | 101 | vignette: Math.max(0, num(options?.vignette, 0.35)), |
124 | | - keymap: options?.keymap, |
| 102 | + keybinds: record(options?.keybinds) ? (options.keybinds as SmokeBindings) : undefined, |
125 | 103 | } |
126 | 104 | } |
127 | 105 |
|
@@ -132,21 +110,8 @@ const names = (input: Cfg) => { |
132 | 110 | } |
133 | 111 | } |
134 | 112 |
|
135 | | -function createKeys(input: SmokeKeymap | undefined): { sections: ResolvedSections } { |
136 | | - const sections = resolveBindingSections( |
137 | | - { |
138 | | - global: { ...defaultKeymap.global, ...input?.sections?.global }, |
139 | | - dialog: { ...defaultKeymap.dialog, ...input?.sections?.dialog }, |
140 | | - local: { ...defaultKeymap.local, ...input?.sections?.local }, |
141 | | - screen: { ...defaultKeymap.screen, ...input?.sections?.screen }, |
142 | | - modal: { ...defaultKeymap.modal, ...input?.sections?.modal }, |
143 | | - } satisfies BindingSectionsConfig<Renderable, KeyEvent>, |
144 | | - { sections: sectionNames }, |
145 | | - ).sections |
146 | | - |
147 | | - return { |
148 | | - sections, |
149 | | - } |
| 113 | +function createKeys(input: SmokeBindings | undefined) { |
| 114 | + return createBindingLookup({ ...defaultKeymap, ...input }) |
150 | 115 | } |
151 | 116 |
|
152 | 117 | type Keys = ReturnType<typeof createKeys> |
@@ -376,7 +341,7 @@ const Screen = (props: { |
376 | 341 | }, |
377 | 342 | }, |
378 | 343 | ], |
379 | | - bindings: props.keys.sections.dialog, |
| 344 | + bindings: props.keys.gather("smoke.dialog", [command.dialog_close]), |
380 | 345 | })) |
381 | 346 |
|
382 | 347 | useBindings(() => ({ |
@@ -395,7 +360,7 @@ const Screen = (props: { |
395 | 360 | }, |
396 | 361 | }, |
397 | 362 | ], |
398 | | - bindings: props.keys.sections.local, |
| 363 | + bindings: props.keys.gather("smoke.local", [command.local_push, command.local_pop]), |
399 | 364 | })) |
400 | 365 |
|
401 | 366 | useBindings(() => ({ |
@@ -478,7 +443,20 @@ const Screen = (props: { |
478 | 443 | }, |
479 | 444 | }, |
480 | 445 | ], |
481 | | - bindings: props.keys.sections.screen, |
| 446 | + bindings: props.keys.gather("smoke.screen", [ |
| 447 | + command.screen_home, |
| 448 | + command.screen_left, |
| 449 | + command.screen_right, |
| 450 | + command.screen_up, |
| 451 | + command.screen_down, |
| 452 | + command.screen_modal, |
| 453 | + command.screen_local, |
| 454 | + command.screen_host, |
| 455 | + command.screen_alert, |
| 456 | + command.screen_confirm, |
| 457 | + command.screen_prompt, |
| 458 | + command.screen_select, |
| 459 | + ]), |
482 | 460 | })) |
483 | 461 | const shortcuts = useKeymapSelector((keymap) => { |
484 | 462 | const bindings = keymap.getCommandBindings({ |
@@ -687,7 +665,7 @@ const Modal = (props: { |
687 | 665 | }, |
688 | 666 | }, |
689 | 667 | ], |
690 | | - bindings: props.keys.sections.modal, |
| 668 | + bindings: props.keys.gather("smoke.modal", [command.modal_accept, command.modal_close]), |
691 | 669 | })) |
692 | 670 | const shortcuts = useKeymapSelector((keymap) => { |
693 | 671 | const bindings = keymap.getCommandBindings({ |
@@ -766,25 +744,8 @@ const home = (api: TuiPluginApi, input: Cfg) => ({ |
766 | 744 | }, |
767 | 745 | home_prompt(ctx, value) { |
768 | 746 | const skin = look(ctx.theme.current) |
769 | | - type Prompt = (props: { |
770 | | - workspaceID?: string |
771 | | - visible?: boolean |
772 | | - disabled?: boolean |
773 | | - onSubmit?: () => void |
774 | | - hint?: JSX.Element |
775 | | - right?: JSX.Element |
776 | | - showPlaceholder?: boolean |
777 | | - placeholders?: { |
778 | | - normal?: string[] |
779 | | - shell?: string[] |
780 | | - } |
781 | | - }) => JSX.Element |
782 | | - type Slot = ( |
783 | | - props: { name: string; mode?: unknown; children?: JSX.Element } & Record<string, unknown>, |
784 | | - ) => JSX.Element | null |
785 | | - const ui = api.ui as TuiPluginApi["ui"] & { Prompt: Prompt; Slot: Slot } |
786 | | - const Prompt = ui.Prompt |
787 | | - const Slot = ui.Slot |
| 747 | + const Prompt = api.ui.Prompt |
| 748 | + const Slot = api.ui.Slot |
788 | 749 | const normal = [ |
789 | 750 | `[SMOKE] route check for ${input.label}`, |
790 | 751 | "[SMOKE] confirm home_prompt slot override", |
@@ -1003,20 +964,29 @@ const reg = (api: TuiPluginApi, input: Cfg, keys: Keys) => { |
1003 | 964 | }, |
1004 | 965 | }, |
1005 | 966 | ], |
1006 | | - bindings: keys.sections.global, |
| 967 | + bindings: keys.gather("smoke.global", [ |
| 968 | + command.modal, |
| 969 | + command.screen, |
| 970 | + command.alert, |
| 971 | + command.confirm, |
| 972 | + command.prompt, |
| 973 | + command.select, |
| 974 | + command.host, |
| 975 | + command.home, |
| 976 | + command.toast, |
| 977 | + ]), |
1007 | 978 | }) |
1008 | 979 | } |
1009 | 980 |
|
1010 | 981 | const tui: TuiPlugin = async (api, options, meta) => { |
1011 | | - const input = options as SmokeOptions | undefined |
1012 | | - if (input?.enabled === false) return |
| 982 | + if (options?.enabled === false) return |
1013 | 983 |
|
1014 | 984 | await api.theme.install("./smoke-theme.json") |
1015 | 985 | api.theme.set("smoke-theme") |
1016 | 986 |
|
1017 | | - const value = cfg(input) |
| 987 | + const value = cfg(options) |
1018 | 988 | const route = names(value) |
1019 | | - const keys = createKeys(value.keymap) |
| 989 | + const keys = createKeys(value.keybinds) |
1020 | 990 | const fx = new VignetteEffect(value.vignette) |
1021 | 991 | const post = fx.apply.bind(fx) |
1022 | 992 | api.renderer.addPostProcessFn(post) |
|