@devries-cameron20 / t3code Mirror · read-only
Fork 0

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

main
tsx 227 lines 6.79 KB
Raw
1 /** @jsxRuntime automatic */
2 /** @jsxImportSource @alchemy.run/sigil */
3 import { Box as SigilBox, type DOMElement } from "@alchemy.run/sigil";
4 import { forwardRef } from "@alchemy.run/sigil/react";
5 import type {
6 ComponentProps,
7 ForwardRefExoticComponent,
8 PropsWithoutRef,
9 ReactNode,
10 RefAttributes,
11 } from "react";
12 import { theme } from "../../../Util/Theme.ts";
13 import { useCliEnvironment, useGlyphs } from "./Environment.tsx";
14 import { Text } from "./Typography.tsx";
15
16 // Windowed list that clips to the rows its container leaves over; it draws no
17 // colors of its own, so it needs no theme-aware wrapper.
18 export { VirtualList } from "@alchemy.run/sigil";
19
20 export type BoxProps = ComponentProps<typeof SigilBox>;
21
22 /** Theme-aware Sigil container used by CLI layouts. */
23 export const Box: ForwardRefExoticComponent<PropsWithoutRef<BoxProps> & RefAttributes<DOMElement>> =
24 forwardRef<DOMElement, BoxProps>(function Box(props, ref) {
25 const { colors } = useCliEnvironment();
26 const {
27 borderColor: _borderColor,
28 borderTopColor: _borderTopColor,
29 borderBottomColor: _borderBottomColor,
30 borderLeftColor: _borderLeftColor,
31 borderRightColor: _borderRightColor,
32 borderBackgroundColor: _borderBackgroundColor,
33 borderTopBackgroundColor: _borderTopBackgroundColor,
34 borderBottomBackgroundColor: _borderBottomBackgroundColor,
35 borderLeftBackgroundColor: _borderLeftBackgroundColor,
36 borderRightBackgroundColor: _borderRightBackgroundColor,
37 backgroundColor: _backgroundColor,
38 borderDimColor: _borderDimColor,
39 borderTopDimColor: _borderTopDimColor,
40 borderBottomDimColor: _borderBottomDimColor,
41 borderLeftDimColor: _borderLeftDimColor,
42 borderRightDimColor: _borderRightDimColor,
43 ...colorless
44 } = props;
45 return <SigilBox {...(colors ? props : colorless)} ref={ref} />;
46 });
47
48 export interface StackProps extends Omit<BoxProps, "flexDirection"> {
49 readonly gap?: number;
50 }
51
52 /** Vertical layout primitive. */
53 export function Stack({ children, gap = 0, ...props }: StackProps) {
54 return (
55 <Box flexDirection="column" gap={gap} {...props}>
56 {children}
57 </Box>
58 );
59 }
60
61 export interface RowProps extends Omit<
62 BoxProps,
63 "flexDirection" | "alignItems" | "justifyContent"
64 > {
65 readonly gap?: number;
66 readonly align?: "flex-start" | "center" | "flex-end";
67 readonly justify?: "flex-start" | "center" | "flex-end" | "space-between" | "space-around";
68 }
69
70 /** Horizontal layout primitive. */
71 export function Row({
72 children,
73 gap = 1,
74 align = "flex-start",
75 justify = "flex-start",
76 ...props
77 }: RowProps) {
78 return (
79 <Box flexDirection="row" gap={gap} alignItems={align} justifyContent={justify} {...props}>
80 {children}
81 </Box>
82 );
83 }
84
85 type HeadingProps = {
86 readonly children?: ReactNode;
87 /** Set false to drop the section glyph prefix (e.g. help-screen headings). */
88 readonly glyph?: boolean;
89 };
90
91 export function Heading({ children, glyph = true }: HeadingProps) {
92 const glyphs = useGlyphs();
93 return (
94 <Box>
95 <Text bold color={theme.color.brand}>
96 {glyph ? `${glyphs.section} ` : null}
97 {children}
98 </Text>
99 </Box>
100 );
101 }
102
103 type SectionHeadingProps = {
104 readonly children?: ReactNode;
105 readonly annotation?: ReactNode;
106 };
107
108 export function SectionHeading({ children, annotation }: SectionHeadingProps) {
109 return (
110 <Text>
111 <Text bold color={theme.color.brand}>
112 {children}
113 </Text>
114 {annotation === undefined ? null : <Text tone="muted"> · {annotation}</Text>}
115 </Text>
116 );
117 }
118
119 type GutterProps = {
120 readonly depth?: number;
121 readonly children?: ReactNode;
122 };
123
124 export function Gutter({ depth = 1, children }: GutterProps) {
125 return (
126 <Box paddingLeft={Math.max(0, depth) * theme.space.indent}>
127 <Box flexDirection="column" flexGrow={1}>
128 {children}
129 </Box>
130 </Box>
131 );
132 }
133
134 /**
135 * The centering clamp shared by every windowed list: a window of `count` rows
136 * over `length` items keeping `cursor` as close to the middle as the list
137 * bounds allow.
138 */
139 export const listWindow = (
140 length: number,
141 cursor: number,
142 count: number,
143 ): { readonly start: number; readonly end: number } => {
144 const start = Math.max(0, Math.min(cursor - Math.floor(count / 2), length - count));
145 return { start, end: Math.min(length, start + count) };
146 };
147
148 /**
149 * Horizontal window over variable-width items (tab chips): expands around
150 * `active` — alternating right/left so the selection stays roughly centered —
151 * until `available` columns are exhausted. Returns `[start, end)` like
152 * {@link listWindow}; `active` is always inside the window.
153 */
154 export const tabsWindow = (
155 widths: ReadonlyArray<number>,
156 active: number,
157 available: number,
158 gap = 1,
159 ): { readonly start: number; readonly end: number } => {
160 if (widths.length === 0) return { start: 0, end: 0 };
161 const cursor = Math.max(0, Math.min(active, widths.length - 1));
162 let start = cursor;
163 let end = cursor + 1;
164 let used = widths[cursor]!;
165 let takeRight = true;
166 while (true) {
167 const canRight = end < widths.length && used + gap + widths[end]! <= available;
168 const canLeft = start > 0 && used + gap + widths[start - 1]! <= available;
169 if (!canRight && !canLeft) break;
170 if ((takeRight && canRight) || !canLeft) {
171 used += gap + widths[end]!;
172 end++;
173 } else {
174 start--;
175 used += gap + widths[start]!;
176 }
177 takeRight = !takeRight;
178 }
179 return { start, end };
180 };
181
182 /**
183 * Window for prompt lists that render "↑ N more"/"↓ N more" indicator rows:
184 * reserves two of `visibleCount`'s rows for the indicators when overflowing so
185 * the widget stays within the requested height.
186 */
187 export const overflowListWindow = (
188 length: number,
189 cursor: number,
190 visibleCount: number,
191 ): { readonly start: number; readonly end: number } =>
192 listWindow(length, cursor, Math.max(1, length > visibleCount ? visibleCount - 2 : visibleCount));
193
194 /** Windowed list keeping `cursor` centered; each item renders as a block. */
195 type ViewportProps<Item> = {
196 readonly items: ReadonlyArray<Item>;
197 readonly cursor?: number;
198 readonly height: number;
199 readonly renderItem: (item: Item, index: number) => ReactNode;
200 readonly getKey: (item: Item, index: number) => string;
201 readonly empty?: ReactNode;
202 };
203
204 export function Viewport<Item>({
205 items,
206 cursor = 0,
207 height,
208 renderItem,
209 getKey,
210 empty,
211 }: ViewportProps<Item>) {
212 if (items.length === 0) return <>{empty}</>;
213 const selected = Math.max(0, Math.min(cursor, items.length - 1));
214 const { start, end } = listWindow(items.length, selected, Math.max(1, height));
215 return (
216 <Stack>
217 {items.slice(start, end).map((item, offset) => {
218 const index = start + offset;
219 return (
220 <Box key={getKey(item, index)} flexDirection="column">
221 {renderItem(item, index)}
222 </Box>
223 );
224 })}
225 </Stack>
226 );
227 }