File size: 1,859 Bytes
0dbc9de | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 | import { createComponent, createContext, type JSX, useContext } from "solid-js"
export type TuiPaths = Readonly<{
cwd: string
home: string
state: string
worktree: string
}>
export type TuiTerminalEnvironment = Readonly<{
platform: string
multiplexer?: "tmux" | "screen"
displayServer?: "wayland" | "x11"
}>
export type TuiStartup = Readonly<{
initialRoute?: unknown
skipInitialLoading: boolean
}>
const PathsContext = createContext<TuiPaths>()
const TerminalEnvironmentContext = createContext<TuiTerminalEnvironment>()
const StartupContext = createContext<TuiStartup>()
function provider<T>(context: ReturnType<typeof createContext<T>>, value: T, children: () => JSX.Element) {
return createComponent(context.Provider, {
value: Object.freeze({ ...value }),
get children() {
return children()
},
})
}
export function TuiPathsProvider(props: { value: TuiPaths; children: JSX.Element }) {
return provider(PathsContext, props.value, () => props.children)
}
export function TuiTerminalEnvironmentProvider(props: { value: TuiTerminalEnvironment; children: JSX.Element }) {
return provider(TerminalEnvironmentContext, props.value, () => props.children)
}
export function TuiStartupProvider(props: { value: TuiStartup; children: JSX.Element }) {
return provider(StartupContext, props.value, () => props.children)
}
function required<T>(context: ReturnType<typeof createContext<T>>, name: string) {
const value = useContext(context)
if (!value) throw new Error(`${name} is missing`)
return value
}
export function useTuiPaths() {
return required(PathsContext, "TuiPathsProvider")
}
export function useTuiTerminalEnvironment() {
return required(TerminalEnvironmentContext, "TuiTerminalEnvironmentProvider")
}
export function useTuiStartup() {
return required(StartupContext, "TuiStartupProvider")
}
|