The best way to control Claude Code, Codex and more from any computer or phone

main
ts 73 lines 2.61 KB
Raw
1 import * as CliOutput from "effect/cli/CliOutput";
2 import type { FlagDoc, HelpDoc } from "effect/cli/HelpDoc";
3
4 /**
5 * Compact enum-style flag types so one long choice list (`--log-level`'s
6 * nine choices) doesn't widen the description column for every flag.
7 */
8 const compactFlagType = (type: string): string => {
9 if (type === "boolean" || type.length <= 24) return type;
10 const bracketed = type.startsWith("<") && type.endsWith(">");
11 const value = bracketed ? type.slice(1, -1) : type;
12 const choices = value.split("|");
13 const compact =
14 choices.length > 2 ? `${choices.slice(0, 2).join("|")}|…` : `${value.slice(0, 21)}…`;
15 return bracketed ? `<${compact}>` : compact;
16 };
17
18 const compactFlag = (flag: FlagDoc): FlagDoc => ({
19 ...flag,
20 type: compactFlagType(flag.type),
21 });
22
23 const compactDoc = (doc: HelpDoc): HelpDoc => ({
24 ...doc,
25 flags: doc.flags?.map(compactFlag),
26 globalFlags: doc.globalFlags?.map(compactFlag),
27 });
28
29 /**
30 * Bound every line to the terminal width, breaking at spaces; continuation
31 * lines keep the original indent plus two columns. The default formatter
32 * never wraps, so long descriptions would otherwise run off the terminal.
33 */
34 const wrapLines = (text: string, width: number): string =>
35 text
36 .split("\n")
37 .flatMap((line) => {
38 if (line.length <= width) return [line];
39 const continuation = `${line.match(/^\s*/)![0]} `;
40 const out: string[] = [];
41 let current: string | undefined;
42 for (const word of line.trim().split(/\s+/)) {
43 if (current === undefined) {
44 current = `${line.match(/^\s*/)![0]}${word}`;
45 } else if (current.length + word.length + 1 <= width) {
46 current += ` ${word}`;
47 } else {
48 out.push(current);
49 current = continuation + word;
50 }
51 }
52 if (current !== undefined) out.push(current);
53 return out;
54 })
55 .join("\n");
56
57 /**
58 * Help formatter for plain (non-interactive) runs: the default effect/cli
59 * formatter with colors off, enum flag types compacted, and output wrapped
60 * to the terminal width — CI logs, redirected output, and coding agents
61 * get parseable, bounded lines.
62 */
63 export const plainCliFormatter = (options: { columns: number }): CliOutput.Formatter => {
64 const columns = Math.max(60, Math.min(options.columns, 120));
65 const fallback = CliOutput.defaultFormatter({ colors: false });
66 return {
67 formatCliError: fallback.formatCliError,
68 formatError: fallback.formatError,
69 formatErrors: fallback.formatErrors,
70 formatVersion: fallback.formatVersion,
71 formatHelpDoc: (doc) => wrapLines(fallback.formatHelpDoc(compactDoc(doc)), columns),
72 };
73 };