Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion deno.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
5 changes: 0 additions & 5 deletions deno.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 11 additions & 14 deletions src/cli.ts
Original file line number Diff line number Diff line change
@@ -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({
Expand Down Expand Up @@ -52,24 +51,25 @@ async function loadYaml<T>(path: string) {
}

const fetchContextWaiting = (async () => {
const found = await loadYaml<Context>(joinPath(XDG_CONFIG_HOME, 'wk', 'config.yaml')).catch(() => undefined)
const found = await loadYaml<PartialContext>(joinPath(XDG_CONFIG_HOME, 'wk', 'config.yaml')).catch(() => undefined)
if (found === undefined) {
return defaultContext
}
return deepMerge<Context>(defaultContext, found)
return mergeContext(found)
})()

const loadBindings = (path: string) => loadYaml<Binding[]>(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<Binding[]>(joinPath(XDG_CONFIG_HOME, 'wk', 'bindings.yaml')).catch(() => []).catch(() => []),
loadYaml<Binding[]>(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)

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 = () => {
Expand All @@ -90,9 +90,6 @@ try {
},
}

const [globalBindings, localBindings] = await Promise.all([fetchGlobalBindingsWaiting, fetchLocalBindingsWaiting])
const bindings = [...globalBindings, ...localBindings]

const {
key: _,
desc: __,
Expand Down
42 changes: 42 additions & 0 deletions src/types/Context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, string>
}
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,
Expand Down Expand Up @@ -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,
},
}
}