Skip to content
Myr

command.ts

This file

Path

apps/cli/src/tui/command.ts

116 lines · 3.7 KiB · TypeScript

Last changed by

Merge cli/exit-codes into mainoscabrielbefore adoptionc19a862

History of this file

Take it

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

lines 1–14why, one step back
1/**
2 * `<cli> today`, written by hand so it can take `--tui`: without it, the3 * command prints Today exactly as the `today` contract's projected command4 * does (the same flags, view, `--json` envelope and refusals); with it, a5 * person at a terminal gets Today full-screen (`run.ts`). It stands in for6 * the projected command, as `init` and `env set` do for theirs, and calls7 * the same contract through the CLI's remote client.8 */9import { Client, Description, TodayOf } from "@repo/protocol/contracts";10import { ProjectName } from "@repo/protocol/ids";11import type { NotDecider, UnknownProject } from "@repo/protocol/refusal";12import { Context, Effect, Option, Schema } from "effect";13import { Command, Flag } from "effect/cli";14

Merge cli/exit-codes into mainoscabrielbefore adoptionc19a862

line 15why, one step back
15import { endRefused, Output } from "../output.ts";

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

lines 16–24why, one step back
16import { views } from "../render.ts";
17import { payloadFor, runTodayTui, TuiRefused } from "./run.ts";1819const COMMAND = "today";2021const encodeToday = Schema.encodeUnknownEffect(TodayOf.successSchema);2223/**24 * The `today` contract's refusals, each printed once where the command's

Merge cli/exit-codes into mainoscabrielbefore adoptionc19a862

line 25why, one step back
25 * refusals go; the command then ends `Reported`, with the refusal's exit code.

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

lines 26–28why, one step back
26 */
27const reported = (json: boolean) => {28 const once = (refusal: NotDecider | UnknownProject) =>

Merge cli/exit-codes into mainoscabrielbefore adoptionc19a862

lines 29–30why, one step back
29 Effect.flatMap(Effect.service(Output), (output) =>
30 endRefused(output, { command: COMMAND, json, refusals: [refusal] })

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

lines 31–116why, one step back
31 );
3233 return { NotDecider: once, UnknownProject: once };34};3536/** Today printed once, in the form the invocation policy chose. */37const print = (input: {38 readonly json: boolean;39 readonly project: Option.Option<ProjectName>;40}) =>41 Effect.gen(function* () {42 const client = yield* Client;43 const output = yield* Output;4445 const page = yield* client46 .today(payloadFor(input.project))47 .pipe(Effect.catchTags(reported(input.json)));4849 const data = yield* Effect.orDie(encodeToday(page));5051 const view = Option.getOrElse(views.today(page), () => {52 const text = JSON.stringify(data, null, 2);5354 return { markdown: ["```json", text, "```"].join("\n"), text };55 });5657 return yield* output.success({58 command: COMMAND,59 data,60 json: input.json,61 view,62 });63 });6465/**66 * Today full-screen. `--json` asks for one document for a script, which a67 * screen is not, so the two together are refused like a script's `--tui`.68 */69const full = (input: {70 readonly json: boolean;71 readonly project: Option.Option<ProjectName>;72}) =>73 input.json74 ? Effect.fail(75 new TuiRefused({ reason: "--json asks for one document for a script" })76 )77 : runTodayTui({ project: input.project }).pipe(78 Effect.catchTags(reported(false))79 );8081export const today = Command.make(82 COMMAND,83 {84 json: Flag.Boolean("json").pipe(85 Flag.withDefault(false),86 Flag.withDescription(87 "Print one versioned JSON envelope on stdout, and nothing else there"88 )89 ),90 project: Flag.String("project").pipe(91 Flag.withSchema(ProjectName),92 Flag.optional,93 Flag.withDescription(94 "The project whose decisions to show; every project you decide for when left out"95 )96 ),97 tui: Flag.Boolean("tui").pipe(98 Flag.withDefault(false),99 Flag.withDescription(100 "Open Today full-screen and take its decisions with the keyboard; for a person at a terminal"101 )102 ),103 },104 ({ json, project, tui }) =>105 Effect.gen(function* () {106 if (tui) {107 return yield* full({ json, project });108 }109110 return yield* print({ json, project });111 }).pipe(Effect.withSpan(`cli.${COMMAND}`))112).pipe(113 Command.withDescription(114 `${Context.get(TodayOf.annotations, Description)} It only reads, unless --tui opens it full-screen to take decisions at a terminal.`115 )116);