introduce opentui keymap as sole key/cmd engine (#26053)
This commit is contained in:
@@ -1,7 +1,9 @@
|
||||
import { createOpencodeClient } from "@opencode-ai/sdk/v2"
|
||||
import { RGBA, type CliRenderer } from "@opentui/core"
|
||||
import { createPluginKeybind } from "../../src/cli/cmd/tui/context/plugin-keybinds"
|
||||
import type { HostPluginApi } from "../../src/cli/cmd/tui/plugin/slots"
|
||||
import { LegacyKeymapTransform } from "../../src/cli/cmd/tui/config/legacy-keymap-transform"
|
||||
import { ConfigKeybinds } from "../../src/config/keybinds"
|
||||
import { createTuiResolvedKeymap } from "./tui-runtime"
|
||||
|
||||
type Count = {
|
||||
event_add: number
|
||||
@@ -84,8 +86,8 @@ type Opts = {
|
||||
client?: HostPluginApi["client"] | (() => HostPluginApi["client"])
|
||||
renderer?: HostPluginApi["renderer"]
|
||||
count?: Count
|
||||
keybind?: Partial<HostPluginApi["keybind"]>
|
||||
tuiConfig?: HostPluginApi["tuiConfig"]
|
||||
keymap?: HostPluginApi["keymap"]
|
||||
tuiConfig?: Partial<HostPluginApi["tuiConfig"]>
|
||||
app?: Partial<HostPluginApi["app"]>
|
||||
state?: {
|
||||
ready?: HostPluginApi["state"]["ready"]
|
||||
@@ -109,6 +111,15 @@ type Opts = {
|
||||
}
|
||||
}
|
||||
|
||||
function tuiConfig(input?: Partial<HostPluginApi["tuiConfig"]>): HostPluginApi["tuiConfig"] {
|
||||
const keybinds = ConfigKeybinds.Keybinds.parse(input?.keybinds ?? {})
|
||||
return {
|
||||
...input,
|
||||
keybinds,
|
||||
keymap: input?.keymap ?? createTuiResolvedKeymap(LegacyKeymapTransform.create(input?.keybinds ?? {})),
|
||||
}
|
||||
}
|
||||
|
||||
export function createTuiPluginApi(opts: Opts = {}): HostPluginApi {
|
||||
const kv: Record<string, unknown> = {}
|
||||
const count = opts.count
|
||||
@@ -128,10 +139,6 @@ export function createTuiPluginApi(opts: Opts = {}): HostPluginApi {
|
||||
let size: "medium" | "large" | "xlarge" = "medium"
|
||||
const has = opts.theme?.has ?? (() => false)
|
||||
let selected = opts.theme?.selected ?? "opencode"
|
||||
const key = {
|
||||
match: opts.keybind?.match ?? (() => false),
|
||||
print: opts.keybind?.print ?? ((name: string) => name),
|
||||
}
|
||||
const set =
|
||||
opts.theme?.set ??
|
||||
((name: string) => {
|
||||
@@ -145,6 +152,26 @@ export function createTuiPluginApi(opts: Opts = {}): HostPluginApi {
|
||||
return this
|
||||
},
|
||||
}
|
||||
const keymap =
|
||||
opts.keymap ??
|
||||
({
|
||||
acquireResource(_key: symbol, setup: () => () => void) {
|
||||
const dispose = setup()
|
||||
return () => {
|
||||
dispose()
|
||||
}
|
||||
},
|
||||
registerLayer() {
|
||||
if (count) count.command_add += 1
|
||||
return () => {
|
||||
if (!count) return
|
||||
count.command_drop += 1
|
||||
}
|
||||
},
|
||||
runCommand() {
|
||||
return { ok: true } as const
|
||||
},
|
||||
} as unknown as HostPluginApi["keymap"])
|
||||
|
||||
function kvGet(name: string): unknown
|
||||
function kvGet<Value>(name: string, fallback: Value): Value
|
||||
@@ -160,6 +187,10 @@ export function createTuiPluginApi(opts: Opts = {}): HostPluginApi {
|
||||
return opts.app?.version ?? "0.0.0-test"
|
||||
},
|
||||
},
|
||||
keys: {
|
||||
formatSequence: () => "",
|
||||
formatBindings: () => undefined,
|
||||
},
|
||||
get client() {
|
||||
return client()
|
||||
},
|
||||
@@ -192,17 +223,7 @@ export function createTuiPluginApi(opts: Opts = {}): HostPluginApi {
|
||||
return () => {}
|
||||
},
|
||||
},
|
||||
command: {
|
||||
register: () => {
|
||||
if (count) count.command_add += 1
|
||||
return () => {
|
||||
if (!count) return
|
||||
count.command_drop += 1
|
||||
}
|
||||
},
|
||||
trigger: () => {},
|
||||
show: () => {},
|
||||
},
|
||||
keymap,
|
||||
route: {
|
||||
register: () => {
|
||||
if (count) count.route_add += 1
|
||||
@@ -247,15 +268,7 @@ export function createTuiPluginApi(opts: Opts = {}): HostPluginApi {
|
||||
},
|
||||
},
|
||||
},
|
||||
keybind: {
|
||||
...key,
|
||||
create:
|
||||
opts.keybind?.create ??
|
||||
((defaults, over) => {
|
||||
return createPluginKeybind(key, defaults, over)
|
||||
}),
|
||||
},
|
||||
tuiConfig: opts.tuiConfig ?? {},
|
||||
tuiConfig: tuiConfig(opts.tuiConfig),
|
||||
kv: {
|
||||
get: kvGet,
|
||||
set(name, value) {
|
||||
|
||||
@@ -1,8 +1,47 @@
|
||||
import { spyOn } from "bun:test"
|
||||
import path from "path"
|
||||
import type { KeyEvent, Renderable } from "@opentui/core"
|
||||
import { resolveBindingSections, type BindingSectionsConfig } from "@opentui/keymap/extras"
|
||||
import { TuiConfig } from "../../src/cli/cmd/tui/config/tui"
|
||||
import { LegacyKeymapTransform } from "../../src/cli/cmd/tui/config/legacy-keymap-transform"
|
||||
import { ConfigKeybinds } from "../../src/config/keybinds"
|
||||
import {
|
||||
KeymapConfig,
|
||||
KeymapSectionNames,
|
||||
keymapBindingDefaults,
|
||||
type KeymapConfigInput,
|
||||
type KeymapSection,
|
||||
} from "../../src/cli/cmd/tui/config/tui-schema"
|
||||
|
||||
type PluginSpec = string | [string, Record<string, unknown>]
|
||||
type ResolvedInput = Omit<TuiConfig.Resolved, "keybinds" | "keymap"> & {
|
||||
keybinds?: TuiConfig.Resolved["keybinds"]
|
||||
keymap?: TuiConfig.Resolved["keymap"]
|
||||
}
|
||||
|
||||
export function createTuiResolvedKeymap(input: KeymapConfigInput): TuiConfig.Resolved["keymap"] {
|
||||
const config = KeymapConfig.parse(input)
|
||||
return {
|
||||
leader: !config.leader || config.leader === "none" ? "ctrl+x" : config.leader,
|
||||
leader_timeout: config.leader_timeout,
|
||||
...resolveBindingSections<Renderable, KeyEvent, BindingSectionsConfig<Renderable, KeyEvent>, KeymapSection>(
|
||||
config.sections,
|
||||
{
|
||||
sections: KeymapSectionNames,
|
||||
bindingDefaults: keymapBindingDefaults,
|
||||
},
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
export function createTuiResolvedConfig(input: ResolvedInput = {}): TuiConfig.Resolved {
|
||||
const keybinds = input.keybinds ?? ConfigKeybinds.Keybinds.parse({})
|
||||
return {
|
||||
...input,
|
||||
keybinds,
|
||||
keymap: input.keymap ?? createTuiResolvedKeymap(LegacyKeymapTransform.create(input.keybinds ?? {})),
|
||||
}
|
||||
}
|
||||
|
||||
export function mockTuiRuntime(dir: string, plugin: PluginSpec[], opts?: { plugin_enabled?: Record<string, boolean> }) {
|
||||
process.env.OPENCODE_PLUGIN_META_FILE = path.join(dir, "plugin-meta.json")
|
||||
@@ -14,11 +53,11 @@ export function mockTuiRuntime(dir: string, plugin: PluginSpec[], opts?: { plugi
|
||||
const wait = spyOn(TuiConfig, "waitForDependencies").mockResolvedValue()
|
||||
const cwd = spyOn(process, "cwd").mockImplementation(() => dir)
|
||||
|
||||
const config: TuiConfig.Info = {
|
||||
const config = createTuiResolvedConfig({
|
||||
plugin,
|
||||
plugin_origins,
|
||||
...(opts?.plugin_enabled && { plugin_enabled: opts.plugin_enabled }),
|
||||
}
|
||||
})
|
||||
|
||||
return {
|
||||
config,
|
||||
|
||||
Reference in New Issue
Block a user