70160
Ce
cel-tui / files
5.2 KB
1/** 2 * cel-tui: Tag Cloud 3 * 4 * Demonstrates `flexWrap: "wrap"` on HStack — tags flow 5 * left-to-right and wrap to the next line when they exceed 6 * the terminal width. Resize the terminal to see tags reflow. 7 * 8 * Click or press Enter to toggle tags. Tab/Shift+Tab to 9 * navigate. Ctrl+Q to quit. 10 * 11 * Run: bun run examples/tags.ts 12 */ 13 14import { Button, Divider, Spacer } from "@cel-tui/components"; 15import { cel, HStack, ProcessTerminal, Text, VStack } from "@cel-tui/core"; 16 17// ─── Data ─────────────────────────────────────────────────────── 18 19interface Tag { 20 label: string; 21 color: "color06" | "color05" | "color03" | "color02" | "color04" | "color01"; 22} 23 24const TAGS: Tag[] = [ 25 { label: "TypeScript", color: "color04" }, 26 { label: "Rust", color: "color01" }, 27 { label: "Go", color: "color06" }, 28 { label: "Python", color: "color03" }, 29 { label: "JavaScript", color: "color03" }, 30 { label: "C++", color: "color04" }, 31 { label: "Zig", color: "color05" }, 32 { label: "Haskell", color: "color05" }, 33 { label: "Elixir", color: "color05" }, 34 { label: "Ruby", color: "color01" }, 35 { label: "Kotlin", color: "color02" }, 36 { label: "Swift", color: "color01" }, 37 { label: "Lua", color: "color04" }, 38 { label: "OCaml", color: "color03" }, 39 { label: "Clojure", color: "color02" }, 40 { label: "Scala", color: "color01" }, 41 { label: "Nim", color: "color03" }, 42 { label: "Julia", color: "color02" }, 43 { label: "Erlang", color: "color01" }, 44 { label: "Dart", color: "color06" }, 45 { label: "C#", color: "color02" }, 46 { label: "F#", color: "color06" }, 47 { label: "Gleam", color: "color05" }, 48 { label: "Odin", color: "color04" }, 49]; 50 51// ─── State ────────────────────────────────────────────────────── 52 53const selected = new Set<string>(); 54 55function toggle(label: string) { 56 if (selected.has(label)) selected.delete(label); 57 else selected.add(label); 58 cel.render(); 59} 60 61function clearAll() { 62 selected.clear(); 63 cel.render(); 64} 65 66function quit() { 67 cel.stop(); 68 process.exit(); 69} 70 71// ─── UI ───────────────────────────────────────────────────────── 72 73function tagChip(tag: Tag) { 74 const on = selected.has(tag.label); 75 const check = on ? "✓ " : " "; 76 return Button(` ${check}${tag.label} `, { 77 onClick: () => toggle(tag.label), 78 fgColor: on ? "color00" : tag.color, 79 bgColor: on ? tag.color : undefined, 80 bold: on, 81 focusStyle: { 82 fgColor: "color00", 83 bgColor: tag.color, 84 bold: true, 85 }, 86 }); 87} 88 89cel.init(new ProcessTerminal()); 90 91cel.viewport(() => { 92 const selTags = TAGS.filter((t) => selected.has(t.label)); 93 94 return VStack( 95 { 96 height: "100%", 97 padding: { x: 2, y: 1 }, 98 onKeyPress: (key) => { 99 if (key === "ctrl+q" || key === "ctrl+c") quit(); 100 if (key === "ctrl+r") clearAll(); 101 }, 102 }, 103 [ 104 // Header 105 HStack({ gap: 1 }, [ 106 Text("◆", { fgColor: "color06", bold: true }), 107 Text("Pick your languages", { bold: true }), 108 Text(`(${selected.size}/${TAGS.length})`, { 109 fgColor: "color08", 110 }), 111 ]), 112 Text("Tab navigate · Enter toggle · resize to reflow", { 113 fgColor: "color08", 114 italic: true, 115 }), 116 Divider({ fgColor: "color08" }), 117 118 // Tag cloud — the flexWrap showcase 119 HStack( 120 { flexWrap: "wrap", gap: 1, padding: { y: 1 } }, 121 TAGS.map(tagChip), 122 ), 123 124 Divider({ fgColor: "color08" }), 125 126 // Selection summary 127 ...(selTags.length > 0 128 ? [ 129 Text("Selected:", { bold: true, fgColor: "color08" }), 130 HStack( 131 { flexWrap: "wrap", gap: 1 }, 132 selTags.map((t) => 133 Text(` ${t.label} `, { 134 fgColor: "color00", 135 bgColor: t.color, 136 bold: true, 137 }), 138 ), 139 ), 140 ] 141 : [ 142 Text("No languages selected yet.", { 143 fgColor: "color08", 144 italic: true, 145 }), 146 ]), 147 148 Spacer(), 149 150 // Footer hints — also wrapping 151 HStack({ flexWrap: "wrap", gap: 1 }, [ 152 HStack({ gap: 0 }, [ 153 Text(" Tab ", { 154 bgColor: "color08", 155 fgColor: "color00", 156 bold: true, 157 }), 158 Text(" navigate", { fgColor: "color08" }), 159 ]), 160 HStack({ gap: 0 }, [ 161 Text(" Enter ", { 162 bgColor: "color08", 163 fgColor: "color00", 164 bold: true, 165 }), 166 Text(" toggle", { fgColor: "color08" }), 167 ]), 168 HStack({ gap: 0 }, [ 169 Text(" Ctrl+R ", { 170 bgColor: "color08", 171 fgColor: "color00", 172 bold: true, 173 }), 174 Text(" clear", { fgColor: "color08" }), 175 ]), 176 HStack({ gap: 0 }, [ 177 Text(" Ctrl+Q ", { 178 bgColor: "color08", 179 fgColor: "color00", 180 bold: true, 181 }), 182 Text(" quit", { fgColor: "color08" }), 183 ]), 184 ]), 185 ], 186 ); 187});