diff --git a/deno.jsonc b/deno.jsonc index 927f5ee..64ac42b 100644 --- a/deno.jsonc +++ b/deno.jsonc @@ -18,7 +18,6 @@ "@cliffy/keypress": "jsr:@cliffy/keypress@1.0.0-rc.7", "@cliffy/table": "jsr:@cliffy/table@1.0.0-rc.7", "@eta-dev/eta": "jsr:@eta-dev/eta@3.5.0", - "@std/collections": "jsr:@std/collections@1.0.9", "@std/fmt": "jsr:@std/fmt@1.0.3", "@std/path": "jsr:@std/path@1.0.8", "@std/yaml": "jsr:@std/yaml@1.0.5", diff --git a/deno.lock b/deno.lock index dc0cbaa..0b25622 100644 --- a/deno.lock +++ b/deno.lock @@ -7,7 +7,6 @@ "jsr:@cliffy/keypress@1.0.0-rc.7": "1.0.0-rc.7", "jsr:@cliffy/table@1.0.0-rc.7": "1.0.0-rc.7", "jsr:@eta-dev/eta@3.5.0": "3.5.0", - "jsr:@std/collections@1.0.9": "1.0.9", "jsr:@std/encoding@~1.0.5": "1.0.7", "jsr:@std/fmt@1.0.3": "1.0.3", "jsr:@std/fmt@~1.0.2": "1.0.3", @@ -49,9 +48,6 @@ "@eta-dev/eta@3.5.0": { "integrity": "6b70827efc14c7cbf08498ac7a922ecab003641caf3852a6cb5b1b12ee58fb37" }, - "@std/collections@1.0.9": { - "integrity": "4f58104ead08a04a2199374247f07befe50ba01d9cca8cbb23ab9a0419921e71" - }, "@std/encoding@1.0.7": { "integrity": "f631247c1698fef289f2de9e2a33d571e46133b38d042905e3eac3715030a82d" }, @@ -88,7 +84,6 @@ "jsr:@cliffy/keypress@1.0.0-rc.7", "jsr:@cliffy/table@1.0.0-rc.7", "jsr:@eta-dev/eta@3.5.0", - "jsr:@std/collections@1.0.9", "jsr:@std/fmt@1.0.3", "jsr:@std/path@1.0.8", "jsr:@std/yaml@1.0.5", diff --git a/src/cli.ts b/src/cli.ts index fb8f691..a98ecc7 100644 --- a/src/cli.ts +++ b/src/cli.ts @@ -1,16 +1,15 @@ import { Eta } from '@eta-dev/eta' -import { deepMerge } from '@std/collections' import { join as joinPath } from '@std/path' +import { parse as parseYaml } from '@std/yaml' +import { parseArgs } from 'node:util' +import VERSION from '../VERSION' with { type: 'text' } import { XDG_CONFIG_HOME } from './const.ts' import { AbortError, KeyParseError, UndefinedKeyError } from './errors.ts' import { type Dependencies, main } from './main.ts' import { TUI } from './tui.ts' import { type Binding } from './types/Binding.ts' -import { type Context, defaultContext } from './types/Context.ts' +import { defaultContext, mergeContext, PartialContext } from './types/Context.ts' import { getKeySymbol, renderPrompt, renderTable } from './ui.ts' -import { parse as parseYaml } from '@std/yaml' -import { parseArgs } from 'node:util' -import VERSION from '../VERSION' with { type: 'text' } import WIDGET_TEMPLATE from './widget.eta' with { type: 'text' } const { values: opts } = parseArgs({ @@ -52,16 +51,17 @@ async function loadYaml(path: string) { } const fetchContextWaiting = (async () => { - const found = await loadYaml(joinPath(XDG_CONFIG_HOME, 'wk', 'config.yaml')).catch(() => undefined) + const found = await loadYaml(joinPath(XDG_CONFIG_HOME, 'wk', 'config.yaml')).catch(() => undefined) if (found === undefined) { return defaultContext } - return deepMerge(defaultContext, found) + return mergeContext(found) })() -const loadBindings = (path: string) => loadYaml(path).catch(() => []) -const fetchGlobalBindingsWaiting = loadBindings(joinPath(XDG_CONFIG_HOME, 'wk', 'bindings.yaml')) -const fetchLocalBindingsWaiting = loadBindings(joinPath(Deno.cwd(), 'wk.bindings.yaml')) +const fetchBindingsWaiting = Promise.all([ + loadYaml(joinPath(XDG_CONFIG_HOME, 'wk', 'bindings.yaml')).catch(() => []).catch(() => []), + loadYaml(joinPath(Deno.cwd(), 'wk.bindings.yaml')).catch(() => []).catch(() => []), +]).then(([globalBindings, localBindings]) => [...globalBindings, ...localBindings]) const tty = await Deno.open('/dev/tty', { read: true, write: true }) const tui = new TUI(tty, tty) @@ -69,7 +69,7 @@ const tui = new TUI(tty, tty) try { tui.init(opts['up-one-line'] === 'true' ? true : opts['up-one-line'] === 'false' ? false : 'auto') - const ctx = await fetchContextWaiting + const [ctx, bindings] = await Promise.all([fetchContextWaiting, fetchBindingsWaiting]) let timeoutTimerId: number | undefined const handleTimeout = () => { @@ -90,9 +90,6 @@ try { }, } - const [globalBindings, localBindings] = await Promise.all([fetchGlobalBindingsWaiting, fetchLocalBindingsWaiting]) - const bindings = [...globalBindings, ...localBindings] - const { key: _, desc: __, diff --git a/src/types/Context.ts b/src/types/Context.ts index ef6aa3b..2442e16 100644 --- a/src/types/Context.ts +++ b/src/types/Context.ts @@ -27,6 +27,29 @@ export type Context = { } } +export type PartialContext = { + outputDelimiter?: string + timeout?: number + symbols?: { + prompt?: string + breadcrumb?: string + separator?: string + group?: string + keys?: Record + } + colors?: { + prompt?: Color + breadcrumb?: Color + separator?: Color + group?: Color + inputKeys?: Color + lastInputKey?: Color + bindingKey?: Color + bindingIcon?: Color + bindingDescription?: Color + } +} + export const defaultContext: Context = { outputDelimiter: '\t', timeout: 0, @@ -81,3 +104,22 @@ export const defaultContext: Context = { bindingDescription: 8, }, } + +export function mergeContext(userDefinedContext: PartialContext): Context { + return { + ...defaultContext, + ...userDefinedContext, + symbols: { + ...defaultContext.symbols, + ...userDefinedContext.symbols, + keys: { + ...defaultContext.symbols.keys, + ...userDefinedContext.symbols?.keys, + }, + }, + colors: { + ...defaultContext.colors, + ...userDefinedContext.colors, + }, + } +}