feat(tui): add managed config interface
This commit is contained in:
parent
1e17202413
commit
3568dd1b99
52 changed files with 848 additions and 266 deletions
|
|
@ -1,4 +1,4 @@
|
|||
import { createSignal, type Setter } from "solid-js"
|
||||
import { createEffect, createSignal, type Setter } from "solid-js"
|
||||
import { createStore, unwrap } from "solid-js/store"
|
||||
import { createSimpleContext } from "./helper"
|
||||
import { Flock } from "@opencode-ai/core/util/flock"
|
||||
|
|
@ -6,10 +6,12 @@ import { Global } from "@opencode-ai/core/global"
|
|||
import { readJson, writeJsonAtomic } from "../util/persistence"
|
||||
import { useTuiPaths } from "./runtime"
|
||||
import path from "path"
|
||||
import { useTuiConfigOptional, type TuiConfig } from "../config"
|
||||
|
||||
export const { use: useKV, provider: KVProvider } = createSimpleContext({
|
||||
name: "KV",
|
||||
init: () => {
|
||||
init: (props: { config?: TuiConfig.Info }) => {
|
||||
const config = props.config ?? useTuiConfigOptional()
|
||||
const paths = useTuiPaths()
|
||||
void Global.Path.state
|
||||
const file = path.join(paths.state, "kv.json")
|
||||
|
|
@ -21,7 +23,12 @@ export const { use: useKV, provider: KVProvider } = createSimpleContext({
|
|||
|
||||
Flock.withLock(lock, () => readJson<Record<string, unknown>>(file))
|
||||
.then((x) => {
|
||||
setStore(x)
|
||||
const values: Record<string, any> = { ...x }
|
||||
Object.entries(configValues(config ?? {})).forEach(([key, value]) => {
|
||||
if (value === undefined) delete values[key]
|
||||
else values[key] = value
|
||||
})
|
||||
setStore(values)
|
||||
})
|
||||
.catch((error) => {
|
||||
console.error("Failed to read KV state", { error })
|
||||
|
|
@ -30,6 +37,14 @@ export const { use: useKV, provider: KVProvider } = createSimpleContext({
|
|||
setReady(true)
|
||||
})
|
||||
|
||||
createEffect(() => {
|
||||
if (!ready() || !config) return
|
||||
Object.entries(configValues(config)).forEach(([key, value]) => {
|
||||
if (value === undefined) setStore(key, undefined)
|
||||
else setStore(key, value)
|
||||
})
|
||||
})
|
||||
|
||||
const result = {
|
||||
get ready() {
|
||||
return ready()
|
||||
|
|
@ -64,3 +79,29 @@ export const { use: useKV, provider: KVProvider } = createSimpleContext({
|
|||
return result
|
||||
},
|
||||
})
|
||||
|
||||
function configValues(config: TuiConfig.Info) {
|
||||
const values: Record<string, any> = {}
|
||||
if (config.theme?.name !== undefined) values.theme = config.theme.name
|
||||
if (config.theme?.mode !== undefined) {
|
||||
values.theme_mode_lock = config.theme.mode === "system" ? undefined : config.theme.mode
|
||||
values.theme_mode = undefined
|
||||
}
|
||||
if (config.attention?.sound_pack !== undefined) values.attention_sound_pack = config.attention.sound_pack
|
||||
if (config.diffs?.wrap !== undefined) values.diff_wrap_mode = config.diffs.wrap
|
||||
if (config.diffs?.tree !== undefined) values.diff_viewer_show_file_tree = config.diffs.tree
|
||||
if (config.diffs?.single !== undefined) values.diff_viewer_single_patch = config.diffs.single
|
||||
if (config.diffs?.view !== undefined)
|
||||
values.diff_viewer_view = config.diffs.view === "auto" ? undefined : config.diffs.view
|
||||
if (config.terminal?.title !== undefined) values.terminal_title_enabled = config.terminal.title
|
||||
if (config.prompt?.editor !== undefined) values.file_context_enabled = config.prompt.editor
|
||||
if (config.prompt?.paste !== undefined) values.paste_summary_enabled = config.prompt.paste === "compact"
|
||||
if (config.session?.sidebar !== undefined) values.sidebar = config.session.sidebar
|
||||
if (config.session?.scrollbar !== undefined) values.scrollbar_visible = config.session.scrollbar
|
||||
if (config.session?.thinking !== undefined) values.thinking_mode = config.session.thinking
|
||||
if (config.session?.grouping !== undefined) values.exploration_grouping = config.session.grouping === "auto"
|
||||
if (config.hints?.tips !== undefined) values.tips_hidden = !config.hints.tips
|
||||
if (config.hints?.onboarding !== undefined) values.dismissed_getting_started = !config.hints.onboarding
|
||||
if (config.animations !== undefined) values.animations_enabled = config.animations
|
||||
return values
|
||||
}
|
||||
|
|
|
|||
|
|
@ -23,7 +23,7 @@ import { createEffect, createMemo, onCleanup, onMount } from "solid-js"
|
|||
import { createStore, produce } from "solid-js/store"
|
||||
import { createSimpleContext } from "./helper"
|
||||
import { useKV } from "./kv"
|
||||
import { useTuiConfig } from "../config/v1"
|
||||
import { useTuiConfig } from "../config"
|
||||
import { Global } from "@opencode-ai/core/global"
|
||||
import { Glob } from "@opencode-ai/core/util/glob"
|
||||
import { readFile } from "node:fs/promises"
|
||||
|
|
@ -118,14 +118,14 @@ export const { use: useTheme, provider: ThemeProvider } = createSimpleContext({
|
|||
if (!lock && pick(kv.get("theme_mode")) !== undefined) kv.set("theme_mode", undefined)
|
||||
draft.mode = mode
|
||||
draft.lock = lock
|
||||
const active = config.theme ?? kv.get("theme", "opencode")
|
||||
const active = config.theme?.name ?? kv.get("theme", "opencode")
|
||||
draft.active = typeof active === "string" ? active : "opencode"
|
||||
draft.ready = false
|
||||
}),
|
||||
)
|
||||
|
||||
createEffect(() => {
|
||||
const theme = config.theme
|
||||
const theme = config.theme?.name
|
||||
if (theme) setStore("active", theme)
|
||||
})
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue