70160
Ce
cel-tui / files
cel-tui / examples / select.ts
8.1 KB
1import { Divider, Select, Spacer, VDivider } from "@cel-tui/components"; 2import { cel, HStack, ProcessTerminal, Text, VStack } from "@cel-tui/core"; 3 4interface ThemePreset { 5 id: string; 6 name: string; 7 vibe: string; 8 summary: string; 9 accent: AccentColor; 10 keywords: string[]; 11 palette: readonly string[]; 12} 13 14type AccentColor = 15 "color06" | "color05" | "color03" | "color02" | "color04" | "color01"; 16 17const THEMES: ThemePreset[] = [ 18 { 19 id: "catppuccin/mocha", 20 name: "Catppuccin Mocha", 21 vibe: "cozy purple dusk", 22 summary: 23 "A warm, low-contrast palette for long terminal sessions, writing, and dashboards that should feel calm instead of clinical.", 24 accent: "color05", 25 keywords: ["cozy", "purple", "soft", "night", "editorial"], 26 palette: ["mauve", "rosewater", "teal"], 27 }, 28 { 29 id: "nord/frost", 30 name: "Nord Frost", 31 vibe: "icy blue minimalism", 32 summary: 33 "Cool blues, crisp contrast, and a restrained look that works well for logs, monitoring, and data-heavy layouts.", 34 accent: "color06", 35 keywords: ["cool", "blue", "minimal", "ops", "monitoring"], 36 palette: ["frost", "snow", "storm"], 37 }, 38 { 39 id: "tokyo-night/storm", 40 name: "Tokyo Night Storm", 41 vibe: "electric city glow", 42 summary: 43 "A sharper dark theme with vibrant accents that helps demos feel energetic without getting noisy.", 44 accent: "color04", 45 keywords: ["neon", "city", "energetic", "sharp", "modern"], 46 palette: ["indigo", "cyan", "amber"], 47 }, 48 { 49 id: "gruvbox/material", 50 name: "Gruvbox Material", 51 vibe: "earthy amber studio", 52 summary: 53 "Muted warmth and clear hierarchy. Great when you want a dense interface to stay readable and grounded.", 54 accent: "color03", 55 keywords: ["warm", "amber", "retro", "earthy", "dense"], 56 palette: ["sand", "olive", "rust"], 57 }, 58 { 59 id: "forest/mint", 60 name: "Forest Mint", 61 vibe: "fresh green utility", 62 summary: 63 "High-signal greens for positive actions, health checks, and clean success-heavy dashboards.", 64 accent: "color02", 65 keywords: ["green", "fresh", "success", "health", "utility"], 66 palette: ["mint", "sage", "pine"], 67 }, 68 { 69 id: "ember/redline", 70 name: "Ember Redline", 71 vibe: "alert-focused crimson", 72 summary: 73 "Built for warnings, incident tooling, and views where urgency should be visible immediately.", 74 accent: "color01", 75 keywords: ["red", "alert", "incident", "urgent", "ops"], 76 palette: ["ember", "flame", "ash"], 77 }, 78]; 79 80const themeById = new Map(THEMES.map((theme) => [theme.id, theme])); 81let selectedThemeId = THEMES[0]!.id; 82let selectQuery = ""; 83let selectCursor = 0; 84let selectHighlightIndex = 0; 85 86const themeItems = THEMES.map((theme) => ({ 87 label: theme.name, 88 value: theme.id, 89 filterText: `${theme.name} ${theme.vibe} ${theme.keywords.join(" ")}`, 90})); 91 92const themeSelect = Select({ 93 items: themeItems, 94 maxVisible: 7, 95 placeholder: "search themes or keywords...", 96 highlightColor: "color06", 97 onQueryChange: (query) => { 98 selectQuery = query; 99 }, 100 onCursorChange: (cursor) => { 101 selectCursor = cursor; 102 }, 103 onHighlightChange: (highlightIndex) => { 104 selectHighlightIndex = highlightIndex; 105 }, 106 onCancel: () => themeSelect.reset(), 107 filter: (items, query) => { 108 const terms = query.toLowerCase().split(/\s+/).filter(Boolean); 109 return items.filter((item) => { 110 const searchable = item.filterText.toLowerCase(); 111 return terms.every((term) => searchable.includes(term)); 112 }); 113 }, 114 renderRow: (item, { highlighted, indicator }) => { 115 const theme = themeById.get(item.value); 116 return HStack({ gap: 1 }, [ 117 Text(highlighted ? indicator : " "), 118 Text(item.label, { bold: highlighted }), 119 Text(theme?.vibe ?? "", { fgColor: "color08", italic: true }), 120 ]); 121 }, 122 onSelect: (value) => { 123 selectedThemeId = value; 124 cel.render(); 125 }, 126}); 127 128function activeTheme(): ThemePreset { 129 return themeById.get(selectedThemeId) ?? THEMES[0]!; 130} 131 132function resetDemo() { 133 selectedThemeId = THEMES[0]!.id; 134 selectQuery = ""; 135 selectCursor = 0; 136 selectHighlightIndex = 0; 137 themeSelect.reset(); 138 cel.render(); 139} 140 141function quit() { 142 cel.stop(); 143 process.exit(0); 144} 145 146function sampleFilters() { 147 return HStack( 148 { flexWrap: "wrap", gap: 1 }, 149 ["cozy", "blue", "alert", "modern", "success"].map((sample) => 150 Text(` ${sample} `, { 151 fgColor: "color15", 152 bgColor: "color08", 153 }), 154 ), 155 ); 156} 157 158function paletteRow(theme: ThemePreset) { 159 return HStack( 160 { flexWrap: "wrap", gap: 1 }, 161 theme.palette.map((label) => 162 Text(` ${label} `, { 163 fgColor: "color00", 164 bgColor: theme.accent, 165 bold: true, 166 }), 167 ), 168 ); 169} 170 171function previewPane(theme: ThemePreset) { 172 return VStack({ flex: 1, gap: 1 }, [ 173 HStack({ gap: 1 }, [ 174 Text(" Theme Preview ", { 175 fgColor: "color00", 176 bgColor: theme.accent, 177 bold: true, 178 }), 179 Text(theme.id, { fgColor: "color08" }), 180 ]), 181 Text(theme.name, { bold: true, fgColor: theme.accent }), 182 Text(theme.vibe, { fgColor: theme.accent, italic: true }), 183 Text(theme.summary, { wrap: "word" }), 184 Divider({ fgColor: "color08" }), 185 Text("Palette vibe", { bold: true, fgColor: "color08" }), 186 paletteRow(theme), 187 Text("Keywords", { bold: true, fgColor: "color08" }), 188 HStack( 189 { flexWrap: "wrap", gap: 1 }, 190 theme.keywords.map((keyword) => 191 Text(` ${keyword} `, { 192 fgColor: theme.accent, 193 bgColor: "color08", 194 bold: true, 195 }), 196 ), 197 ), 198 Divider({ fgColor: "color08" }), 199 Text("Why this is a good Select demo", { bold: true, fgColor: "color08" }), 200 Text( 201 "• Uses rich items with display text separate from the selected value.", 202 ), 203 Text("• Filter text includes mood words, so prefix search feels useful."), 204 Text( 205 "• Uses a controlled query/cursor/highlight model suitable for async overlays.", 206 ), 207 Spacer(), 208 Text("Try typing one of the sample filters above, then press Enter.", { 209 fgColor: "color08", 210 italic: true, 211 wrap: "word", 212 }), 213 ]); 214} 215 216function pickerPane(theme: ThemePreset) { 217 return VStack({ gap: 1 }, [ 218 Text("Select component demo", { bold: true, fgColor: theme.accent }), 219 Text("A palette picker for theme presets.", { 220 fgColor: "color08", 221 italic: true, 222 }), 223 Divider({ fgColor: "color08" }), 224 Text("Sample filters", { bold: true, fgColor: "color08" }), 225 sampleFilters(), 226 Text("Picker", { bold: true, fgColor: "color08" }), 227 themeSelect({ 228 items: themeItems, 229 query: selectQuery, 230 cursor: selectCursor, 231 highlightIndex: selectHighlightIndex, 232 }), 233 ]); 234} 235 236cel.init(new ProcessTerminal()); 237cel.viewport(() => { 238 const cols = process.stdout.columns || 80; 239 const theme = activeTheme(); 240 const wide = cols >= 80; 241 const paneWidth = cols >= 100 ? 42 : 36; 242 243 return VStack( 244 { 245 height: "100%", 246 padding: { x: 2, y: 1 }, 247 onKeyPress: (key) => { 248 if (key === "ctrl+q" || key === "ctrl+c") { 249 quit(); 250 } 251 if (key === "ctrl+r") { 252 resetDemo(); 253 } 254 }, 255 }, 256 [ 257 HStack({ gap: 1 }, [ 258 Text("◈", { fgColor: theme.accent, bold: true }), 259 Text("Theme Preset Picker", { bold: true }), 260 Spacer(), 261 Text(theme.vibe, { fgColor: "color08", italic: true }), 262 ]), 263 Text("Search by vibe, inspect the value, and compare the preview pane.", { 264 fgColor: "color08", 265 }), 266 Divider({ fgColor: "color08" }), 267 ...(wide 268 ? [ 269 HStack({ flex: 1 }, [ 270 VStack({ width: paneWidth, padding: { x: 1 } }, [ 271 pickerPane(theme), 272 ]), 273 VDivider({ fgColor: "color08" }), 274 VStack({ flex: 1, padding: { x: 1 } }, [previewPane(theme)]), 275 ]), 276 ] 277 : [ 278 pickerPane(theme), 279 Divider({ fgColor: "color08" }), 280 previewPane(theme), 281 ]), 282 Divider({ fgColor: "color08" }), 283 Text("Tab/filter/enter · Ctrl+R reset · Ctrl+Q quit", { 284 fgColor: "color08", 285 }), 286 ], 287 ); 288});