command.ts
This file
Path
apps/cli/src/tui/command.ts
99 lines · 2.9 KiB · TypeScript
Last changed by
Merge build/comments-cli-protocol-checks into mainoscabrielbefore adoptionc86204f
History of this fileTake it
Merge branch 'build/710-today-tui'oscabrielbefore adoption8f57dc3
lines 1–6why, one step back1import { Client, Description, TodayOf } from "@repo/protocol/contracts";2import { ProjectName } from "@repo/protocol/ids";3import type { NotDecider, UnknownProject } from "@repo/protocol/refusal";4import { Context, Effect, Option, Schema } from "effect";5import { Command, Flag } from "effect/cli";6Merge cli/exit-codes into mainoscabrielbefore adoptionc19a862
line 7why, one step back7import { endRefused, Output } from "../output.ts";Merge branch 'build/710-today-tui'oscabrielbefore adoption8f57dc3
lines 8–16why, one step back8import { views } from "../render.ts";9import { payloadFor, runTodayTui, TuiRefused } from "./run.ts";1011const COMMAND = "today";1213const encodeToday = Schema.encodeUnknownEffect(TodayOf.successSchema);1415const reported = (json: boolean) => {16 const once = (refusal: NotDecider | UnknownProject) =>Merge cli/exit-codes into mainoscabrielbefore adoptionc19a862
lines 17–18why, one step back17 Effect.flatMap(Effect.service(Output), (output) =>18 endRefused(output, { command: COMMAND, json, refusals: [refusal] })Merge branch 'build/710-today-tui'oscabrielbefore adoption8f57dc3
lines 19–99why, one step back19 );2021 return { NotDecider: once, UnknownProject: once };22};2324const print = (input: {25 readonly json: boolean;26 readonly project: Option.Option<ProjectName>;27}) =>28 Effect.gen(function* () {29 const client = yield* Client;30 const output = yield* Output;3132 const page = yield* client33 .today(payloadFor(input.project))34 .pipe(Effect.catchTags(reported(input.json)));3536 const data = yield* Effect.orDie(encodeToday(page));3738 const view = Option.getOrElse(views.today(page), () => {39 const text = JSON.stringify(data, null, 2);4041 return { markdown: ["```json", text, "```"].join("\n"), text };42 });4344 return yield* output.success({45 command: COMMAND,46 data,47 json: input.json,48 view,49 });50 });5152const full = (input: {53 readonly json: boolean;54 readonly project: Option.Option<ProjectName>;55}) =>56 input.json57 ? Effect.fail(58 new TuiRefused({ reason: "--json asks for one document for a script" })59 )60 : runTodayTui({ project: input.project }).pipe(61 Effect.catchTags(reported(false))62 );6364export const today = Command.make(65 COMMAND,66 {67 json: Flag.Boolean("json").pipe(68 Flag.withDefault(false),69 Flag.withDescription(70 "Print one versioned JSON envelope on stdout, and nothing else there"71 )72 ),73 project: Flag.String("project").pipe(74 Flag.withSchema(ProjectName),75 Flag.optional,76 Flag.withDescription(77 "The project whose decisions to show; every project you decide for when left out"78 )79 ),80 tui: Flag.Boolean("tui").pipe(81 Flag.withDefault(false),82 Flag.withDescription(83 "Open Today full-screen and take its decisions with the keyboard; for a person at a terminal"84 )85 ),86 },87 ({ json, project, tui }) =>88 Effect.gen(function* () {89 if (tui) {90 return yield* full({ json, project });91 }9293 return yield* print({ json, project });94 }).pipe(Effect.withSpan(`cli.${COMMAND}`))95).pipe(96 Command.withDescription(97 `${Context.get(TodayOf.annotations, Description)} It only reads, unless --tui opens it full-screen to take decisions at a terminal.`98 )99);