ShtukbaseStandard
файл пакета→ .shtukbase/packages/tui/actions/shot.ts

actions/shot.ts

60 строк, 3.3 КБ.

Файл как есть в пакете: код и комментарии в нём не переводятся.

  1. // shtuk run tui shot <view> [address] [keys]: one screen of the terminal front as plain text, without a terminal.
  2. // Views: sidebar, page, rows, board, screen (the whole window, as `shtukbase tui shot` draws it).
  3. import { front, needBin, reachNode, render, splitArgs } from '../lib/tui.ts';
  4. const VIEWS: Record<string, { cmd: string; needsAddress: boolean; size: string[] }> = {
  5. sidebar: { cmd: 'sidebar', needsAddress: false, size: ['--width', '60'] },
  6. page: { cmd: 'page', needsAddress: true, size: [] },
  7. rows: { cmd: 'rows', needsAddress: true, size: ['--width', '120'] },
  8. board: { cmd: 'board', needsAddress: true, size: ['--width', '120', '--height', '30'] },
  9. screen: { cmd: 'shot', needsAddress: false, size: ['--width', '120', '--height', '36'] },
  10. };
  11. const USAGE =
  12. 'shtuk run tui shot <view> [address] [keys]\n' +
  13. ' sidebar [folders] - the sidebar: pages, collections, boards (address: which folders to expand: a,b/c)\n' +
  14. ' page <address> - a page as text (--lines 0..200)\n' +
  15. ' rows <address> - a collection or view as a table (--sort field --desc --find word --limit 40)\n' +
  16. ' board <address> - a collection or view as a board by the state field\n' +
  17. ' screen [address] - the whole window, as in a terminal (--view 1 kanban, --preview, --width 120 --height 36;\n' +
  18. ' --keys enter,down,c - press keys before the shot;\n' +
  19. ' node screens: @contours, @rules, @runs, @automations, @resources, @progress)\n' +
  20. 'Address: the path of a page in the project without .md: work/tasks, base/contours. --own-node allows a temporary node.';
  21. export function address(a: string | undefined): string {
  22. return (a ?? '').replace(/^\/+/, '').replace(/\.md$/, '');
  23. }
  24. export async function shot(root: string, argv: string[]): Promise<{ code: number; lines: string[]; err?: string }> {
  25. const [view, ...rest] = argv;
  26. const spec = view ? VIEWS[view] : undefined;
  27. if (!spec) return { code: 2, lines: [], err: USAGE };
  28. const { free, flags, ownNode } = splitArgs(rest);
  29. const addr = address(free[0]);
  30. if (spec.needsAddress && !addr) return { code: 2, lines: [], err: `tui shot ${view}: a page or collection address is needed, for example work/tasks` };
  31. const bin = needBin(root);
  32. const node = await reachNode(bin, root, { ownNode });
  33. if (typeof node === 'string') return { code: 2, lines: [], err: `tui: ${node}` };
  34. try {
  35. const args = [spec.cmd, '--space', root];
  36. if (view === 'sidebar') { if (addr) args.push('--open', addr); }
  37. else if (addr) args.push(addr);
  38. 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]);
  39. const extra: Record<string, string> = {};
  40. const k = flags.indexOf('--keys');
  41. if (k >= 0) { extra.SHTUKBASE_SHOT_KEYS = flags[k + 1] ?? ''; flags.splice(k, 2); }
  42. args.push(...flags);
  43. const r = front(bin, node, args, root, extra);
  44. if (r.status !== 0) return { code: 1, lines: [], err: `tui: ${r.err.trim() || 'the command failed without a message'}` };
  45. return { code: 0, lines: render(r.out) };
  46. } finally {
  47. node.stop();
  48. }
  49. }
  50. if (import.meta.main) {
  51. const r = await shot(process.cwd(), process.argv.slice(2));
  52. if (r.lines.length) console.log(r.lines.join('\n'));
  53. if (r.err) console.log(r.err);
  54. process.exit(r.code);
  55. }