файл пакета→ .shtukbase/packages/tui/actions/shot.ts
actions/shot.ts
60 строк, 3.3 КБ.
Файл как есть в пакете: код и комментарии в нём не переводятся.
- // shtuk run tui shot <view> [address] [keys]: one screen of the terminal front as plain text, without a terminal.
- // Views: sidebar, page, rows, board, screen (the whole window, as `shtukbase tui shot` draws it).
- import { front, needBin, reachNode, render, splitArgs } from '../lib/tui.ts';
- const VIEWS: Record<string, { cmd: string; needsAddress: boolean; size: string[] }> = {
- sidebar: { cmd: 'sidebar', needsAddress: false, size: ['--width', '60'] },
- page: { cmd: 'page', needsAddress: true, size: [] },
- rows: { cmd: 'rows', needsAddress: true, size: ['--width', '120'] },
- board: { cmd: 'board', needsAddress: true, size: ['--width', '120', '--height', '30'] },
- screen: { cmd: 'shot', needsAddress: false, size: ['--width', '120', '--height', '36'] },
- };
- const USAGE =
- 'shtuk run tui shot <view> [address] [keys]\n' +
- ' sidebar [folders] - the sidebar: pages, collections, boards (address: which folders to expand: a,b/c)\n' +
- ' page <address> - a page as text (--lines 0..200)\n' +
- ' rows <address> - a collection or view as a table (--sort field --desc --find word --limit 40)\n' +
- ' board <address> - a collection or view as a board by the state field\n' +
- ' screen [address] - the whole window, as in a terminal (--view 1 kanban, --preview, --width 120 --height 36;\n' +
- ' --keys enter,down,c - press keys before the shot;\n' +
- ' node screens: @contours, @rules, @runs, @automations, @resources, @progress)\n' +
- 'Address: the path of a page in the project without .md: work/tasks, base/contours. --own-node allows a temporary node.';
- export function address(a: string | undefined): string {
- return (a ?? '').replace(/^\/+/, '').replace(/\.md$/, '');
- }
- export async function shot(root: string, argv: string[]): Promise<{ code: number; lines: string[]; err?: string }> {
- const [view, ...rest] = argv;
- const spec = view ? VIEWS[view] : undefined;
- if (!spec) return { code: 2, lines: [], err: USAGE };
- const { free, flags, ownNode } = splitArgs(rest);
- const addr = address(free[0]);
- if (spec.needsAddress && !addr) return { code: 2, lines: [], err: `tui shot ${view}: a page or collection address is needed, for example work/tasks` };
- const bin = needBin(root);
- const node = await reachNode(bin, root, { ownNode });
- if (typeof node === 'string') return { code: 2, lines: [], err: `tui: ${node}` };
- try {
- const args = [spec.cmd, '--space', root];
- if (view === 'sidebar') { if (addr) args.push('--open', addr); }
- else if (addr) args.push(addr);
- for (let i = 0; i < spec.size.length; i += 2) if (!flags.includes(spec.size[i])) args.push(spec.size[i], spec.size[i + 1]);
- const extra: Record<string, string> = {};
- const k = flags.indexOf('--keys');
- if (k >= 0) { extra.SHTUKBASE_SHOT_KEYS = flags[k + 1] ?? ''; flags.splice(k, 2); }
- args.push(...flags);
- const r = front(bin, node, args, root, extra);
- if (r.status !== 0) return { code: 1, lines: [], err: `tui: ${r.err.trim() || 'the command failed without a message'}` };
- return { code: 0, lines: render(r.out) };
- } finally {
- node.stop();
- }
- }
- if (import.meta.main) {
- const r = await shot(process.cwd(), process.argv.slice(2));
- if (r.lines.length) console.log(r.lines.join('\n'));
- if (r.err) console.log(r.err);
- process.exit(r.code);
- }