Skip to content
Myr

screen.ts

This file

Path

apps/cli/src/tui/screen.ts

67 lines · 2.0 KiB · TypeScript

Last changed by

Merge build/comments-cli-protocol-checks into mainoscabrielbefore adoptionc86204f

History of this file

Take it

Merge build/comments-cli-protocol-checks into mainoscabrielbefore adoptionc86204f

line 1why, one step back
1// Loaded only when `--tui` opens Today, since importing OpenTUI loads its native library.

Merge branch 'build/710-today-tui'oscabrielbefore adoption8f57dc3

lines 2–13why, one step back
2import { createCliRenderer } from "@opentui/core";
3import type { CliRenderer, KeyEvent } from "@opentui/core";4import { createElement, createRoot, flushSync } from "@opentui/react";5import { Effect, Queue } from "effect";6import type { Scope } from "effect";78import { causeText, TuiRefused } from "./run.ts";9import type { Screen } from "./run.ts";10import { TodayView } from "./view.ts";1112const keyName = (key: KeyEvent) => (key.ctrl ? `ctrl+${key.name}` : key.name);13

Merge build/comments-cli-protocol-checks into mainoscabrielbefore adoptionc86204f

line 14why, one step back
14/** OpenTUI's root draws one element, so the last one is unmounted first. */

Merge branch 'build/710-today-tui'oscabrielbefore adoption8f57dc3

lines 15–47why, one step back
15export const screenOn = (
16 renderer: CliRenderer17): Effect.Effect<Screen, never, Scope.Scope> =>18 Effect.gen(function* () {19 const keys = yield* Queue.unbounded<string>();20 const root = createRoot(renderer);2122 const onKey = (key: KeyEvent) => {23 Queue.offerUnsafe(keys, keyName(key));24 };2526 yield* Effect.acquireRelease(27 Effect.sync(() => renderer.keyInput.on("keypress", onKey)),28 () =>29 Effect.sync(() => {30 renderer.keyInput.off("keypress", onKey);31 root.unmount();32 })33 );3435 return {36 forget: Effect.asVoid(Queue.clear(keys)),37 key: Queue.take(keys),38 show: (state) =>39 Effect.sync(() => {40 flushSync(() => {41 root.unmount();42 root.render(createElement(TodayView, { state }));43 });44 }),45 } satisfies Screen;46 });47

Merge build/comments-cli-protocol-checks into mainoscabrielbefore adoptionc86204f

line 48why, one step back
48/** Ctrl+C is a key here, which closes Today like `q`. */

Merge branch 'build/710-today-tui'oscabrielbefore adoption8f57dc3

lines 49–67why, one step back
49export const openTerminal: Effect.Effect<Screen, TuiRefused, Scope.Scope> =
50 Effect.acquireRelease(51 Effect.tryPromise({52 catch: (cause) =>53 new TuiRefused({54 reason: `the terminal would not open (${causeText(cause)})`,55 }),56 try: () =>57 createCliRenderer({58 exitOnCtrlC: false,59 screenMode: "alternate-screen",60 useMouse: false,61 }),62 }),63 (renderer) =>64 Effect.sync(() => {65 renderer.destroy();66 })67 ).pipe(Effect.flatMap(screenOn));