tui-smoke.tsx 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019
  1. /** @jsxImportSource @opentui/solid */
  2. import { useTerminalDimensions, type JSX } from "@opentui/solid"
  3. import { useBindings, useKeymapSelector } from "@opentui/keymap/solid"
  4. import { RGBA, VignetteEffect, type KeyEvent, type Renderable } from "@opentui/core"
  5. import { createBindingLookup, type BindingConfig } from "@opentui/keymap/extras"
  6. import type { TuiPlugin, TuiPluginApi, TuiPluginMeta, TuiPluginModule, TuiSlotPlugin } from "@opencode-ai/plugin/tui"
  7. const tabs = ["overview", "counter", "help"]
  8. const command = {
  9. modal: "smoke_modal",
  10. screen: "smoke_screen",
  11. alert: "smoke_alert",
  12. confirm: "smoke_confirm",
  13. prompt: "smoke_prompt",
  14. select: "smoke_select",
  15. host: "smoke_host",
  16. home: "smoke_home",
  17. toast: "smoke_toast",
  18. dialog_close: "smoke_dialog_close",
  19. local_push: "smoke_local_push",
  20. local_pop: "smoke_local_pop",
  21. screen_home: "smoke_screen_home",
  22. screen_left: "smoke_screen_left",
  23. screen_right: "smoke_screen_right",
  24. screen_up: "smoke_screen_up",
  25. screen_down: "smoke_screen_down",
  26. screen_modal: "smoke_screen_modal",
  27. screen_local: "smoke_screen_local",
  28. screen_host: "smoke_screen_host",
  29. screen_alert: "smoke_screen_alert",
  30. screen_confirm: "smoke_screen_confirm",
  31. screen_prompt: "smoke_screen_prompt",
  32. screen_select: "smoke_screen_select",
  33. modal_accept: "smoke_modal_accept",
  34. modal_close: "smoke_modal_close",
  35. }
  36. type SmokeBindings = BindingConfig<Renderable, KeyEvent>
  37. const defaultKeymap = {
  38. [command.modal]: "ctrl+shift+m",
  39. [command.screen]: "ctrl+shift+o",
  40. [command.dialog_close]: "escape",
  41. [command.local_push]: "enter,return",
  42. [command.local_pop]: "escape,q,backspace",
  43. [command.screen_home]: "escape,ctrl+h",
  44. [command.screen_left]: "left,h",
  45. [command.screen_right]: "right,l",
  46. [command.screen_up]: "up,k",
  47. [command.screen_down]: "down,j",
  48. [command.screen_modal]: "ctrl+shift+m",
  49. [command.screen_local]: "x",
  50. [command.screen_host]: "z",
  51. [command.screen_alert]: "a",
  52. [command.screen_confirm]: "c",
  53. [command.screen_prompt]: "p",
  54. [command.screen_select]: "s",
  55. [command.modal_accept]: "enter,return",
  56. [command.modal_close]: "escape",
  57. }
  58. const pick = (value: unknown, fallback: string) => {
  59. if (typeof value !== "string") return fallback
  60. if (!value.trim()) return fallback
  61. return value
  62. }
  63. const num = (value: unknown, fallback: number) => {
  64. if (typeof value !== "number") return fallback
  65. return value
  66. }
  67. const record = (value: unknown): value is Record<string, unknown> =>
  68. !!value && typeof value === "object" && !Array.isArray(value)
  69. type Cfg = {
  70. label: string
  71. route: string
  72. vignette: number
  73. keybinds: SmokeBindings | undefined
  74. }
  75. type Route = {
  76. modal: string
  77. screen: string
  78. }
  79. type State = {
  80. tab: number
  81. count: number
  82. source: string
  83. note: string
  84. selected: string
  85. local: number
  86. }
  87. const cfg = (options: Record<string, unknown> | undefined) => {
  88. return {
  89. label: pick(options?.label, "smoke"),
  90. route: pick(options?.route, "workspace-smoke"),
  91. vignette: Math.max(0, num(options?.vignette, 0.35)),
  92. keybinds: record(options?.keybinds) ? (options.keybinds as SmokeBindings) : undefined,
  93. }
  94. }
  95. const names = (input: Cfg) => {
  96. return {
  97. modal: `${input.route}.modal`,
  98. screen: `${input.route}.screen`,
  99. }
  100. }
  101. function createKeys(input: SmokeBindings | undefined) {
  102. return createBindingLookup({ ...defaultKeymap, ...input })
  103. }
  104. type Keys = ReturnType<typeof createKeys>
  105. const ui = {
  106. panel: "#1d1d1d",
  107. border: "#4a4a4a",
  108. text: "#f0f0f0",
  109. muted: "#a5a5a5",
  110. accent: "#5f87ff",
  111. }
  112. type Color = RGBA | string
  113. const ink = (map: Record<string, unknown>, name: string, fallback: string): Color => {
  114. const value = map[name]
  115. if (typeof value === "string") return value
  116. if (value instanceof RGBA) return value
  117. return fallback
  118. }
  119. const look = (map: Record<string, unknown>) => {
  120. return {
  121. panel: ink(map, "backgroundPanel", ui.panel),
  122. border: ink(map, "border", ui.border),
  123. text: ink(map, "text", ui.text),
  124. muted: ink(map, "textMuted", ui.muted),
  125. accent: ink(map, "primary", ui.accent),
  126. selected: ink(map, "selectedListItemText", ui.text),
  127. }
  128. }
  129. const tone = (api: TuiPluginApi) => {
  130. return look(api.theme.current)
  131. }
  132. type Skin = {
  133. panel: Color
  134. border: Color
  135. text: Color
  136. muted: Color
  137. accent: Color
  138. selected: Color
  139. }
  140. const Btn = (props: { txt: string; run: () => void; skin: Skin; on?: boolean }) => {
  141. return (
  142. <box
  143. onMouseUp={() => {
  144. props.run()
  145. }}
  146. backgroundColor={props.on ? props.skin.accent : props.skin.border}
  147. paddingLeft={1}
  148. paddingRight={1}
  149. >
  150. <text fg={props.on ? props.skin.selected : props.skin.text}>{props.txt}</text>
  151. </box>
  152. )
  153. }
  154. const parse = (params: Record<string, unknown> | undefined) => {
  155. const tab = typeof params?.tab === "number" ? params.tab : 0
  156. const count = typeof params?.count === "number" ? params.count : 0
  157. const source = typeof params?.source === "string" ? params.source : "unknown"
  158. const note = typeof params?.note === "string" ? params.note : ""
  159. const selected = typeof params?.selected === "string" ? params.selected : ""
  160. const local = typeof params?.local === "number" ? params.local : 0
  161. return {
  162. tab: Math.max(0, Math.min(tab, tabs.length - 1)),
  163. count,
  164. source,
  165. note,
  166. selected,
  167. local: Math.max(0, local),
  168. }
  169. }
  170. const current = (api: TuiPluginApi, route: Route) => {
  171. const value = api.route.current
  172. const ok = Object.values(route).includes(value.name)
  173. if (!ok) return parse(undefined)
  174. if (!("params" in value)) return parse(undefined)
  175. return parse(value.params)
  176. }
  177. const opts = [
  178. {
  179. title: "Overview",
  180. value: 0,
  181. description: "Switch to overview tab",
  182. },
  183. {
  184. title: "Counter",
  185. value: 1,
  186. description: "Switch to counter tab",
  187. },
  188. {
  189. title: "Help",
  190. value: 2,
  191. description: "Switch to help tab",
  192. },
  193. ]
  194. const host = (api: TuiPluginApi, input: Cfg, skin: Skin) => {
  195. api.ui.dialog.setSize("medium")
  196. api.ui.dialog.replace(() => (
  197. <box paddingBottom={1} paddingLeft={2} paddingRight={2} gap={1} flexDirection="column">
  198. <text fg={skin.text}>
  199. <b>{input.label} host overlay</b>
  200. </text>
  201. <text fg={skin.muted}>Using api.ui.dialog stack with built-in backdrop</text>
  202. <text fg={skin.muted}>esc closes · depth {api.ui.dialog.depth}</text>
  203. <box flexDirection="row" gap={1}>
  204. <Btn txt="close" run={() => api.ui.dialog.clear()} skin={skin} on />
  205. </box>
  206. </box>
  207. ))
  208. }
  209. const warn = (api: TuiPluginApi, route: Route, value: State) => {
  210. const DialogAlert = api.ui.DialogAlert
  211. api.ui.dialog.setSize("medium")
  212. api.ui.dialog.replace(() => (
  213. <DialogAlert
  214. title="Smoke alert"
  215. message="Testing built-in alert dialog"
  216. onConfirm={() => api.route.navigate(route.screen, { ...value, source: "alert" })}
  217. />
  218. ))
  219. }
  220. const check = (api: TuiPluginApi, route: Route, value: State) => {
  221. const DialogConfirm = api.ui.DialogConfirm
  222. api.ui.dialog.setSize("medium")
  223. api.ui.dialog.replace(() => (
  224. <DialogConfirm
  225. title="Smoke confirm"
  226. message="Apply +1 to counter?"
  227. onConfirm={() => api.route.navigate(route.screen, { ...value, count: value.count + 1, source: "confirm" })}
  228. onCancel={() => api.route.navigate(route.screen, { ...value, source: "confirm-cancel" })}
  229. />
  230. ))
  231. }
  232. const entry = (api: TuiPluginApi, route: Route, value: State) => {
  233. const DialogPrompt = api.ui.DialogPrompt
  234. api.ui.dialog.setSize("medium")
  235. api.ui.dialog.replace(() => (
  236. <DialogPrompt
  237. title="Smoke prompt"
  238. value={value.note}
  239. onConfirm={(note) => {
  240. api.ui.dialog.clear()
  241. api.route.navigate(route.screen, { ...value, note, source: "prompt" })
  242. }}
  243. onCancel={() => {
  244. api.ui.dialog.clear()
  245. api.route.navigate(route.screen, value)
  246. }}
  247. />
  248. ))
  249. }
  250. const picker = (api: TuiPluginApi, route: Route, value: State) => {
  251. const DialogSelect = api.ui.DialogSelect
  252. api.ui.dialog.setSize("medium")
  253. api.ui.dialog.replace(() => (
  254. <DialogSelect
  255. title="Smoke select"
  256. options={opts}
  257. current={value.tab}
  258. onSelect={(item) => {
  259. api.ui.dialog.clear()
  260. api.route.navigate(route.screen, {
  261. ...value,
  262. tab: typeof item.value === "number" ? item.value : value.tab,
  263. selected: item.title,
  264. source: "select",
  265. })
  266. }}
  267. />
  268. ))
  269. }
  270. const Screen = (props: {
  271. api: TuiPluginApi
  272. input: Cfg
  273. route: Route
  274. keys: Keys
  275. meta: TuiPluginMeta
  276. params?: Record<string, unknown>
  277. }) => {
  278. const dim = useTerminalDimensions()
  279. const value = parse(props.params)
  280. const skin = tone(props.api)
  281. const set = (local: number, base?: State) => {
  282. const next = base ?? current(props.api, props.route)
  283. props.api.route.navigate(props.route.screen, { ...next, local: Math.max(0, local), source: "local" })
  284. }
  285. const push = (base?: State) => {
  286. const next = base ?? current(props.api, props.route)
  287. set(next.local + 1, next)
  288. }
  289. const open = () => {
  290. const next = current(props.api, props.route)
  291. if (next.local > 0) return
  292. set(1, next)
  293. }
  294. const pop = (base?: State) => {
  295. const next = base ?? current(props.api, props.route)
  296. set(Math.max(0, next.local - 1), next)
  297. }
  298. const show = () => {
  299. setTimeout(() => {
  300. open()
  301. }, 0)
  302. }
  303. const screenActive = () => props.api.route.current.name === props.route.screen
  304. useBindings(() => ({
  305. enabled: () => screenActive() && props.api.ui.dialog.open,
  306. commands: [
  307. {
  308. name: command.dialog_close,
  309. run() {
  310. props.api.ui.dialog.clear()
  311. },
  312. },
  313. ],
  314. bindings: props.keys.gather("smoke.dialog", [command.dialog_close]),
  315. }))
  316. useBindings(() => ({
  317. enabled: () => screenActive() && !props.api.ui.dialog.open && current(props.api, props.route).local > 0,
  318. commands: [
  319. {
  320. name: command.local_push,
  321. run() {
  322. push(current(props.api, props.route))
  323. },
  324. },
  325. {
  326. name: command.local_pop,
  327. run() {
  328. pop(current(props.api, props.route))
  329. },
  330. },
  331. ],
  332. bindings: props.keys.gather("smoke.local", [command.local_push, command.local_pop]),
  333. }))
  334. useBindings(() => ({
  335. enabled: () => screenActive() && !props.api.ui.dialog.open && current(props.api, props.route).local === 0,
  336. commands: [
  337. {
  338. name: command.screen_home,
  339. run() {
  340. props.api.route.navigate("home")
  341. },
  342. },
  343. {
  344. name: command.screen_left,
  345. run() {
  346. const next = current(props.api, props.route)
  347. props.api.route.navigate(props.route.screen, { ...next, tab: (next.tab - 1 + tabs.length) % tabs.length })
  348. },
  349. },
  350. {
  351. name: command.screen_right,
  352. run() {
  353. const next = current(props.api, props.route)
  354. props.api.route.navigate(props.route.screen, { ...next, tab: (next.tab + 1) % tabs.length })
  355. },
  356. },
  357. {
  358. name: command.screen_up,
  359. run() {
  360. const next = current(props.api, props.route)
  361. props.api.route.navigate(props.route.screen, { ...next, count: next.count + 1 })
  362. },
  363. },
  364. {
  365. name: command.screen_down,
  366. run() {
  367. const next = current(props.api, props.route)
  368. props.api.route.navigate(props.route.screen, { ...next, count: next.count - 1 })
  369. },
  370. },
  371. {
  372. name: command.screen_modal,
  373. run() {
  374. props.api.route.navigate(props.route.modal, current(props.api, props.route))
  375. },
  376. },
  377. {
  378. name: command.screen_local,
  379. run() {
  380. open()
  381. },
  382. },
  383. {
  384. name: command.screen_host,
  385. run() {
  386. host(props.api, props.input, skin)
  387. },
  388. },
  389. {
  390. name: command.screen_alert,
  391. run() {
  392. warn(props.api, props.route, current(props.api, props.route))
  393. },
  394. },
  395. {
  396. name: command.screen_confirm,
  397. run() {
  398. check(props.api, props.route, current(props.api, props.route))
  399. },
  400. },
  401. {
  402. name: command.screen_prompt,
  403. run() {
  404. entry(props.api, props.route, current(props.api, props.route))
  405. },
  406. },
  407. {
  408. name: command.screen_select,
  409. run() {
  410. picker(props.api, props.route, current(props.api, props.route))
  411. },
  412. },
  413. ],
  414. bindings: props.keys.gather("smoke.screen", [
  415. command.screen_home,
  416. command.screen_left,
  417. command.screen_right,
  418. command.screen_up,
  419. command.screen_down,
  420. command.screen_modal,
  421. command.screen_local,
  422. command.screen_host,
  423. command.screen_alert,
  424. command.screen_confirm,
  425. command.screen_prompt,
  426. command.screen_select,
  427. ]),
  428. }))
  429. const shortcuts = useKeymapSelector((keymap) => {
  430. const bindings = keymap.getCommandBindings({
  431. visibility: "registered",
  432. commands: [
  433. command.screen_home,
  434. command.screen_up,
  435. command.screen_down,
  436. command.screen_modal,
  437. command.screen_alert,
  438. command.screen_confirm,
  439. command.screen_prompt,
  440. command.screen_select,
  441. command.screen_local,
  442. command.screen_host,
  443. command.local_push,
  444. command.local_pop,
  445. ],
  446. })
  447. return {
  448. screen_home: props.api.keys.formatBindings(bindings.get(command.screen_home)) ?? "",
  449. screen_up: props.api.keys.formatBindings(bindings.get(command.screen_up)) ?? "",
  450. screen_down: props.api.keys.formatBindings(bindings.get(command.screen_down)) ?? "",
  451. screen_modal: props.api.keys.formatBindings(bindings.get(command.screen_modal)) ?? "",
  452. screen_alert: props.api.keys.formatBindings(bindings.get(command.screen_alert)) ?? "",
  453. screen_confirm: props.api.keys.formatBindings(bindings.get(command.screen_confirm)) ?? "",
  454. screen_prompt: props.api.keys.formatBindings(bindings.get(command.screen_prompt)) ?? "",
  455. screen_select: props.api.keys.formatBindings(bindings.get(command.screen_select)) ?? "",
  456. screen_local: props.api.keys.formatBindings(bindings.get(command.screen_local)) ?? "",
  457. screen_host: props.api.keys.formatBindings(bindings.get(command.screen_host)) ?? "",
  458. local_push: props.api.keys.formatBindings(bindings.get(command.local_push)) ?? "",
  459. local_pop: props.api.keys.formatBindings(bindings.get(command.local_pop)) ?? "",
  460. }
  461. })
  462. return (
  463. <box width={dim().width} height={dim().height} backgroundColor={skin.panel} position="relative">
  464. <box
  465. flexDirection="column"
  466. width="100%"
  467. height="100%"
  468. paddingTop={1}
  469. paddingBottom={1}
  470. paddingLeft={2}
  471. paddingRight={2}
  472. >
  473. <box flexDirection="row" justifyContent="space-between" paddingBottom={1}>
  474. <text fg={skin.text}>
  475. <b>{props.input.label} screen</b>
  476. <span style={{ fg: skin.muted }}> plugin route</span>
  477. </text>
  478. <text fg={skin.muted}>{shortcuts().screen_home} home</text>
  479. </box>
  480. <box flexDirection="row" gap={1} paddingBottom={1}>
  481. {tabs.map((item, i) => {
  482. const on = value.tab === i
  483. return (
  484. <Btn
  485. txt={item}
  486. run={() => props.api.route.navigate(props.route.screen, { ...value, tab: i })}
  487. skin={skin}
  488. on={on}
  489. />
  490. )
  491. })}
  492. </box>
  493. <box
  494. border
  495. borderColor={skin.border}
  496. paddingTop={1}
  497. paddingBottom={1}
  498. paddingLeft={2}
  499. paddingRight={2}
  500. flexGrow={1}
  501. >
  502. {value.tab === 0 ? (
  503. <box flexDirection="column" gap={1}>
  504. <text fg={skin.text}>Route: {props.route.screen}</text>
  505. <text fg={skin.muted}>plugin state: {props.meta.state}</text>
  506. <text fg={skin.muted}>
  507. first: {props.meta.state === "first" ? "yes" : "no"} · updated:{" "}
  508. {props.meta.state === "updated" ? "yes" : "no"} · loads: {props.meta.load_count}
  509. </text>
  510. <text fg={skin.muted}>plugin source: {props.meta.source}</text>
  511. <text fg={skin.muted}>source: {value.source}</text>
  512. <text fg={skin.muted}>note: {value.note || "(none)"}</text>
  513. <text fg={skin.muted}>selected: {value.selected || "(none)"}</text>
  514. <text fg={skin.muted}>local stack depth: {value.local}</text>
  515. <text fg={skin.muted}>host stack open: {props.api.ui.dialog.open ? "yes" : "no"}</text>
  516. </box>
  517. ) : null}
  518. {value.tab === 1 ? (
  519. <box flexDirection="column" gap={1}>
  520. <text fg={skin.text}>Counter: {value.count}</text>
  521. <text fg={skin.muted}>
  522. {shortcuts().screen_up} / {shortcuts().screen_down} change value
  523. </text>
  524. </box>
  525. ) : null}
  526. {value.tab === 2 ? (
  527. <box flexDirection="column" gap={1}>
  528. <text fg={skin.muted}>
  529. {shortcuts().screen_modal} modal | {shortcuts().screen_alert} alert | {shortcuts().screen_confirm}{" "}
  530. confirm | {shortcuts().screen_prompt} prompt | {shortcuts().screen_select} select
  531. </text>
  532. <text fg={skin.muted}>
  533. {shortcuts().screen_local} local stack | {shortcuts().screen_host} host stack
  534. </text>
  535. <text fg={skin.muted}>
  536. local open: {shortcuts().local_push} push nested · {shortcuts().local_pop} close
  537. </text>
  538. <text fg={skin.muted}>{shortcuts().screen_home} returns home</text>
  539. </box>
  540. ) : null}
  541. </box>
  542. <box flexDirection="row" gap={1} paddingTop={1}>
  543. <Btn txt="go home" run={() => props.api.route.navigate("home")} skin={skin} />
  544. <Btn txt="modal" run={() => props.api.route.navigate(props.route.modal, value)} skin={skin} on />
  545. <Btn txt="local overlay" run={show} skin={skin} />
  546. <Btn txt="host overlay" run={() => host(props.api, props.input, skin)} skin={skin} />
  547. <Btn txt="alert" run={() => warn(props.api, props.route, value)} skin={skin} />
  548. <Btn txt="confirm" run={() => check(props.api, props.route, value)} skin={skin} />
  549. <Btn txt="prompt" run={() => entry(props.api, props.route, value)} skin={skin} />
  550. <Btn txt="select" run={() => picker(props.api, props.route, value)} skin={skin} />
  551. </box>
  552. </box>
  553. <box
  554. visible={value.local > 0}
  555. width={dim().width}
  556. height={dim().height}
  557. alignItems="center"
  558. position="absolute"
  559. zIndex={3000}
  560. paddingTop={dim().height / 4}
  561. left={0}
  562. top={0}
  563. backgroundColor={RGBA.fromInts(0, 0, 0, 160)}
  564. onMouseUp={() => {
  565. pop()
  566. }}
  567. >
  568. <box
  569. onMouseUp={(evt) => {
  570. evt.stopPropagation()
  571. }}
  572. width={60}
  573. maxWidth={dim().width - 2}
  574. backgroundColor={skin.panel}
  575. border
  576. borderColor={skin.border}
  577. paddingTop={1}
  578. paddingBottom={1}
  579. paddingLeft={2}
  580. paddingRight={2}
  581. gap={1}
  582. flexDirection="column"
  583. >
  584. <text fg={skin.text}>
  585. <b>{props.input.label} local overlay</b>
  586. </text>
  587. <text fg={skin.muted}>Plugin-owned stack depth: {value.local}</text>
  588. <text fg={skin.muted}>
  589. {shortcuts().local_push} push nested · {shortcuts().local_pop} pop/close
  590. </text>
  591. <box flexDirection="row" gap={1}>
  592. <Btn txt="push" run={push} skin={skin} on />
  593. <Btn txt="pop" run={pop} skin={skin} />
  594. </box>
  595. </box>
  596. </box>
  597. </box>
  598. )
  599. }
  600. const Modal = (props: {
  601. api: TuiPluginApi
  602. input: Cfg
  603. route: Route
  604. keys: Keys
  605. params?: Record<string, unknown>
  606. }) => {
  607. const Dialog = props.api.ui.Dialog
  608. const value = parse(props.params)
  609. const skin = tone(props.api)
  610. useBindings(() => ({
  611. enabled: () => props.api.route.current.name === props.route.modal,
  612. commands: [
  613. {
  614. name: command.modal_accept,
  615. run() {
  616. props.api.route.navigate(props.route.screen, { ...parse(props.params), source: "modal" })
  617. },
  618. },
  619. {
  620. name: command.modal_close,
  621. run() {
  622. props.api.route.navigate("home")
  623. },
  624. },
  625. ],
  626. bindings: props.keys.gather("smoke.modal", [command.modal_accept, command.modal_close]),
  627. }))
  628. const shortcuts = useKeymapSelector((keymap) => {
  629. const bindings = keymap.getCommandBindings({
  630. visibility: "registered",
  631. commands: [command.modal, command.screen, command.modal_accept, command.modal_close],
  632. })
  633. return {
  634. modal: props.api.keys.formatBindings(bindings.get(command.modal)) ?? "",
  635. screen: props.api.keys.formatBindings(bindings.get(command.screen)) ?? "",
  636. modal_accept: props.api.keys.formatBindings(bindings.get(command.modal_accept)) ?? "",
  637. modal_close: props.api.keys.formatBindings(bindings.get(command.modal_close)) ?? "",
  638. }
  639. })
  640. return (
  641. <box width="100%" height="100%" backgroundColor={skin.panel}>
  642. <Dialog onClose={() => props.api.route.navigate("home")}>
  643. <box paddingBottom={1} paddingLeft={2} paddingRight={2} gap={1} flexDirection="column">
  644. <text fg={skin.text}>
  645. <b>{props.input.label} modal</b>
  646. </text>
  647. <text fg={skin.muted}>{shortcuts().modal} modal command</text>
  648. <text fg={skin.muted}>{shortcuts().screen} screen command</text>
  649. <text fg={skin.muted}>
  650. {shortcuts().modal_accept} opens screen · {shortcuts().modal_close} closes
  651. </text>
  652. <box flexDirection="row" gap={1}>
  653. <Btn
  654. txt="open screen"
  655. run={() => props.api.route.navigate(props.route.screen, { ...value, source: "modal" })}
  656. skin={skin}
  657. on
  658. />
  659. <Btn txt="cancel" run={() => props.api.route.navigate("home")} skin={skin} />
  660. </box>
  661. </box>
  662. </Dialog>
  663. </box>
  664. )
  665. }
  666. const home = (api: TuiPluginApi, input: Cfg) => ({
  667. slots: {
  668. home_logo(ctx) {
  669. const map = ctx.theme.current
  670. const skin = look(map)
  671. const art = [
  672. " $$\\",
  673. " $$ |",
  674. " $$$$$$$\\ $$$$$$\\$$$$\\ $$$$$$\\ $$ | $$\\ $$$$$$\\",
  675. "$$ _____|$$ _$$ _$$\\ $$ __$$\\ $$ | $$ |$$ __$$\\",
  676. "\\$$$$$$\\ $$ / $$ / $$ |$$ / $$ |$$$$$$ / $$$$$$$$ |",
  677. " \\____$$\\ $$ | $$ | $$ |$$ | $$ |$$ _$$< $$ ____|",
  678. "$$$$$$$ |$$ | $$ | $$ |\\$$$$$$ |$$ | \\$$\\ \\$$$$$$$\\",
  679. "\\_______/ \\__| \\__| \\__| \\______/ \\__| \\__| \\_______|",
  680. ]
  681. const fill = [
  682. skin.accent,
  683. skin.muted,
  684. ink(map, "info", ui.accent),
  685. skin.text,
  686. ink(map, "success", ui.accent),
  687. ink(map, "warning", ui.accent),
  688. ink(map, "secondary", ui.accent),
  689. ink(map, "error", ui.accent),
  690. ]
  691. return (
  692. <box flexDirection="column">
  693. {art.map((line, i) => (
  694. <text fg={fill[i]}>{line}</text>
  695. ))}
  696. </box>
  697. )
  698. },
  699. home_prompt(ctx, value) {
  700. const skin = look(ctx.theme.current)
  701. const Prompt = api.ui.Prompt
  702. const Slot = api.ui.Slot
  703. const normal = [
  704. `[SMOKE] route check for ${input.label}`,
  705. "[SMOKE] confirm home_prompt slot override",
  706. "[SMOKE] verify prompt-right slot passthrough",
  707. ]
  708. const shell = ["printf '[SMOKE] home prompt\n'", "git status --short", "bun --version"]
  709. const hint = (
  710. <box flexShrink={0} flexDirection="row" gap={1}>
  711. <text fg={skin.muted}>
  712. <span style={{ fg: skin.accent }}>•</span> smoke home prompt
  713. </text>
  714. </box>
  715. )
  716. return (
  717. <Prompt
  718. workspaceID={value.workspace_id}
  719. hint={hint}
  720. right={
  721. <box flexDirection="row" gap={1}>
  722. <Slot name="home_prompt_right" workspace_id={value.workspace_id} />
  723. <Slot name="smoke_prompt_right" workspace_id={value.workspace_id} label={input.label} />
  724. </box>
  725. }
  726. placeholders={{ normal, shell }}
  727. />
  728. )
  729. },
  730. home_prompt_right(ctx, value) {
  731. const skin = look(ctx.theme.current)
  732. const id = value.workspace_id?.slice(0, 8) ?? "none"
  733. return (
  734. <text fg={skin.muted}>
  735. <span style={{ fg: skin.accent }}>{input.label}</span> home:{id}
  736. </text>
  737. )
  738. },
  739. session_prompt_right(ctx, value) {
  740. const skin = look(ctx.theme.current)
  741. return (
  742. <text fg={skin.muted}>
  743. <span style={{ fg: skin.accent }}>{input.label}</span> session:{value.session_id.slice(0, 8)}
  744. </text>
  745. )
  746. },
  747. smoke_prompt_right(ctx, value) {
  748. const skin = look(ctx.theme.current)
  749. const id = typeof value.workspace_id === "string" ? value.workspace_id.slice(0, 8) : "none"
  750. const label = typeof value.label === "string" ? value.label : input.label
  751. return (
  752. <text fg={skin.muted}>
  753. <span style={{ fg: skin.accent }}>{label}</span> custom:{id}
  754. </text>
  755. )
  756. },
  757. home_bottom(ctx) {
  758. const skin = look(ctx.theme.current)
  759. const text = "extra content in the unified home bottom slot"
  760. return (
  761. <box width="100%" maxWidth={75} alignItems="center" paddingTop={1} flexShrink={0} gap={1}>
  762. <box
  763. border
  764. borderColor={skin.border}
  765. backgroundColor={skin.panel}
  766. paddingTop={1}
  767. paddingBottom={1}
  768. paddingLeft={2}
  769. paddingRight={2}
  770. width="100%"
  771. >
  772. <text fg={skin.muted}>
  773. <span style={{ fg: skin.accent }}>{input.label}</span> {text}
  774. </text>
  775. </box>
  776. </box>
  777. )
  778. },
  779. },
  780. })
  781. const block = (input: Cfg, order: number, title: string, text: string): TuiSlotPlugin => ({
  782. order,
  783. slots: {
  784. sidebar_content(ctx, value) {
  785. const skin = look(ctx.theme.current)
  786. return (
  787. <box
  788. border
  789. borderColor={skin.border}
  790. backgroundColor={skin.panel}
  791. paddingTop={1}
  792. paddingBottom={1}
  793. paddingLeft={2}
  794. paddingRight={2}
  795. flexDirection="column"
  796. gap={1}
  797. >
  798. <text fg={skin.accent}>
  799. <b>{title}</b>
  800. </text>
  801. <text fg={skin.text}>{text}</text>
  802. <text fg={skin.muted}>
  803. {input.label} order {order} · session {value.session_id.slice(0, 8)}
  804. </text>
  805. </box>
  806. )
  807. },
  808. },
  809. })
  810. const slot = (api: TuiPluginApi, input: Cfg): TuiSlotPlugin[] => [
  811. home(api, input),
  812. block(input, 50, "Smoke above", "renders above internal sidebar blocks"),
  813. block(input, 250, "Smoke between", "renders between internal sidebar blocks"),
  814. block(input, 650, "Smoke below", "renders below internal sidebar blocks"),
  815. ]
  816. const reg = (api: TuiPluginApi, input: Cfg, keys: Keys) => {
  817. const route = names(input)
  818. api.keymap.registerLayer({
  819. commands: [
  820. {
  821. name: command.modal,
  822. title: `${input.label} modal`,
  823. category: "Plugin",
  824. namespace: "palette",
  825. slashName: "smoke",
  826. run() {
  827. api.route.navigate(route.modal, { source: "command" })
  828. },
  829. },
  830. {
  831. name: command.screen,
  832. title: `${input.label} screen`,
  833. category: "Plugin",
  834. namespace: "palette",
  835. slashName: "smoke-screen",
  836. run() {
  837. api.route.navigate(route.screen, { source: "command", tab: 0, count: 0 })
  838. },
  839. },
  840. {
  841. name: command.alert,
  842. title: `${input.label} alert dialog`,
  843. category: "Plugin",
  844. namespace: "palette",
  845. slashName: "smoke-alert",
  846. run() {
  847. warn(api, route, current(api, route))
  848. },
  849. },
  850. {
  851. name: command.confirm,
  852. title: `${input.label} confirm dialog`,
  853. category: "Plugin",
  854. namespace: "palette",
  855. slashName: "smoke-confirm",
  856. run() {
  857. check(api, route, current(api, route))
  858. },
  859. },
  860. {
  861. name: command.prompt,
  862. title: `${input.label} prompt dialog`,
  863. category: "Plugin",
  864. namespace: "palette",
  865. slashName: "smoke-prompt",
  866. run() {
  867. entry(api, route, current(api, route))
  868. },
  869. },
  870. {
  871. name: command.select,
  872. title: `${input.label} select dialog`,
  873. category: "Plugin",
  874. namespace: "palette",
  875. slashName: "smoke-select",
  876. run() {
  877. picker(api, route, current(api, route))
  878. },
  879. },
  880. {
  881. name: command.host,
  882. title: `${input.label} host overlay`,
  883. category: "Plugin",
  884. namespace: "palette",
  885. slashName: "smoke-host",
  886. run() {
  887. host(api, input, tone(api))
  888. },
  889. },
  890. {
  891. name: command.home,
  892. title: `${input.label} go home`,
  893. category: "Plugin",
  894. namespace: "palette",
  895. enabled: () => api.route.current.name !== "home",
  896. run() {
  897. api.route.navigate("home")
  898. },
  899. },
  900. {
  901. name: command.toast,
  902. title: `${input.label} toast`,
  903. category: "Plugin",
  904. namespace: "palette",
  905. run() {
  906. api.ui.toast({
  907. variant: "info",
  908. title: "Smoke",
  909. message: "Plugin toast works",
  910. duration: 2000,
  911. })
  912. },
  913. },
  914. ],
  915. bindings: keys.gather("smoke.global", [
  916. command.modal,
  917. command.screen,
  918. command.alert,
  919. command.confirm,
  920. command.prompt,
  921. command.select,
  922. command.host,
  923. command.home,
  924. command.toast,
  925. ]),
  926. })
  927. }
  928. const tui: TuiPlugin = async (api, options, meta) => {
  929. if (options?.enabled === false) return
  930. await api.theme.install("./smoke-theme.json")
  931. api.theme.set("smoke-theme")
  932. const value = cfg(options)
  933. const route = names(value)
  934. const keys = createKeys(value.keybinds)
  935. const fx = new VignetteEffect(value.vignette)
  936. const post = fx.apply.bind(fx)
  937. api.renderer.addPostProcessFn(post)
  938. api.lifecycle.onDispose(() => {
  939. api.renderer.removePostProcessFn(post)
  940. })
  941. api.route.register([
  942. {
  943. name: route.screen,
  944. render: ({ params }) => <Screen api={api} input={value} route={route} keys={keys} meta={meta} params={params} />,
  945. },
  946. {
  947. name: route.modal,
  948. render: ({ params }) => <Modal api={api} input={value} route={route} keys={keys} params={params} />,
  949. },
  950. ])
  951. reg(api, value, keys)
  952. for (const item of slot(api, value)) {
  953. api.slots.register(item)
  954. }
  955. }
  956. const plugin: TuiPluginModule & { id: string } = {
  957. id: "tui-smoke",
  958. tui,
  959. }
  960. export default plugin