The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 06:00 UTC
| 1 | /** @jsxRuntime automatic */ |
| 2 | /** @jsxImportSource @alchemy.run/sigil */ |
| 3 | import { useWindowSize } from "@alchemy.run/sigil"; |
| 4 | import { createContext, useContext, useMemo } from "@alchemy.run/sigil/react"; |
| 5 | import type { ReactNode } from "react"; |
| 6 | import { glyphsFor, theme, type KeyHint } from "../../../Util/Theme.ts"; |
| 7 | import type { TerminalCapabilities } from "../types.ts"; |
| 8 | |
| 9 | const defaults: TerminalCapabilities = { |
| 10 | input: false, |
| 11 | columns: 80, |
| 12 | rows: 24, |
| 13 | colors: false, |
| 14 | unicode: true, |
| 15 | alternateScreen: false, |
| 16 | }; |
| 17 | |
| 18 | const EnvironmentContext = createContext<TerminalCapabilities>(defaults); |
| 19 | |
| 20 | /** |
| 21 | * Resize-following variant, split out so the static renderer (`renderToString` |
| 22 | * for every print/format) never attaches a `resize` listener to stdout. |
| 23 | */ |
| 24 | type ObservingEnvironmentProps = { |
| 25 | readonly capabilities: TerminalCapabilities; |
| 26 | readonly children?: ReactNode; |
| 27 | }; |
| 28 | |
| 29 | function ObservingEnvironment({ capabilities, children }: ObservingEnvironmentProps) { |
| 30 | const window = useWindowSize(); |
| 31 | const environment = useMemo( |
| 32 | () => ({ ...capabilities, columns: window.columns, rows: window.rows }), |
| 33 | [capabilities, window.columns, window.rows], |
| 34 | ); |
| 35 | return <EnvironmentContext.Provider value={environment}>{children}</EnvironmentContext.Provider>; |
| 36 | } |
| 37 | |
| 38 | type CliEnvironmentProps = { |
| 39 | readonly capabilities: TerminalCapabilities; |
| 40 | /** Follow Sigil's terminal resize subscription for a live renderer. */ |
| 41 | readonly observeWindow?: boolean; |
| 42 | readonly children?: ReactNode; |
| 43 | }; |
| 44 | |
| 45 | export function CliEnvironment({ |
| 46 | capabilities, |
| 47 | observeWindow = false, |
| 48 | children, |
| 49 | }: CliEnvironmentProps) { |
| 50 | return observeWindow ? ( |
| 51 | <ObservingEnvironment capabilities={capabilities}>{children}</ObservingEnvironment> |
| 52 | ) : ( |
| 53 | <EnvironmentContext.Provider value={capabilities}>{children}</EnvironmentContext.Provider> |
| 54 | ); |
| 55 | } |
| 56 | |
| 57 | export const useCliEnvironment = () => useContext(EnvironmentContext); |
| 58 | |
| 59 | export const useGlyphs = () => glyphsFor(useCliEnvironment().unicode); |
| 60 | |
| 61 | /** Border style matching the terminal's Unicode capability. */ |
| 62 | export const useBorderStyle = () => |
| 63 | useCliEnvironment().unicode ? ("single" as const) : ("classic" as const); |
| 64 | |
| 65 | const asciiKeyHint: KeyHint = { |
| 66 | enter: "enter", |
| 67 | upDown: "up/down", |
| 68 | leftRight: "left/right", |
| 69 | escape: "esc", |
| 70 | space: "space", |
| 71 | tab: "tab", |
| 72 | yesNo: "y/n", |
| 73 | }; |
| 74 | |
| 75 | /** Key-hint labels for `KeyBar` footers, honoring the ASCII fallback. */ |
| 76 | export const useKeyGlyphs = (): KeyHint => |
| 77 | useCliEnvironment().unicode ? theme.keyHint : asciiKeyHint; |