@celljs/ta-harness 0.0.0-stage → 4.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +13 -2
- package/esm/node/brand-extension.d.ts +15 -0
- package/esm/node/brand-extension.js +37 -0
- package/esm/node/brand-extension.js.map +1 -0
- package/esm/node/bug-report.d.ts +19 -0
- package/esm/node/bug-report.js +38 -0
- package/esm/node/bug-report.js.map +1 -0
- package/esm/node/dino-logo.d.ts +18 -0
- package/esm/node/dino-logo.js +66 -0
- package/esm/node/dino-logo.js.map +1 -0
- package/esm/node/exec-extension.d.ts +15 -0
- package/esm/node/exec-extension.js +28 -0
- package/esm/node/exec-extension.js.map +1 -0
- package/esm/node/exec-mcp-transport.d.ts +27 -0
- package/esm/node/exec-mcp-transport.js +76 -0
- package/esm/node/exec-mcp-transport.js.map +1 -0
- package/esm/node/exec-operations.d.ts +22 -0
- package/esm/node/exec-operations.js +95 -0
- package/esm/node/exec-operations.js.map +1 -0
- package/esm/node/extensions.d.ts +24 -0
- package/esm/node/extensions.js +61 -0
- package/esm/node/extensions.js.map +1 -0
- package/esm/node/glob-util.d.ts +7 -0
- package/esm/node/glob-util.js +57 -0
- package/esm/node/glob-util.js.map +1 -0
- package/esm/node/index.d.ts +13 -0
- package/esm/node/index.js +14 -0
- package/esm/node/index.js.map +1 -0
- package/esm/node/pi-types.d.ts +13 -0
- package/esm/node/pi-types.js +2 -0
- package/esm/node/pi-types.js.map +1 -0
- package/esm/node/session-lock.d.ts +27 -0
- package/esm/node/session-lock.js +96 -0
- package/esm/node/session-lock.js.map +1 -0
- package/esm/node/ta-brand.d.ts +14 -0
- package/esm/node/ta-brand.js +44 -0
- package/esm/node/ta-brand.js.map +1 -0
- package/esm/node/ta-cli.d.ts +13 -0
- package/esm/node/ta-cli.js +50 -0
- package/esm/node/ta-cli.js.map +1 -0
- package/esm/node/ta-header.d.ts +61 -0
- package/esm/node/ta-header.js +149 -0
- package/esm/node/ta-header.js.map +1 -0
- package/esm/package.json +3 -0
- package/lib/node/brand-extension.d.ts +16 -0
- package/lib/node/brand-extension.d.ts.map +1 -0
- package/lib/node/brand-extension.js +41 -0
- package/lib/node/brand-extension.js.map +1 -0
- package/lib/node/brand-extension.spec.d.ts +2 -0
- package/lib/node/brand-extension.spec.d.ts.map +1 -0
- package/lib/node/brand-extension.spec.js +57 -0
- package/lib/node/brand-extension.spec.js.map +1 -0
- package/lib/node/bug-report.d.ts +20 -0
- package/lib/node/bug-report.d.ts.map +1 -0
- package/lib/node/bug-report.js +41 -0
- package/lib/node/bug-report.js.map +1 -0
- package/lib/node/dino-logo.d.ts +19 -0
- package/lib/node/dino-logo.d.ts.map +1 -0
- package/lib/node/dino-logo.js +69 -0
- package/lib/node/dino-logo.js.map +1 -0
- package/lib/node/exec-extension.d.ts +16 -0
- package/lib/node/exec-extension.d.ts.map +1 -0
- package/lib/node/exec-extension.js +32 -0
- package/lib/node/exec-extension.js.map +1 -0
- package/lib/node/exec-mcp-transport.d.ts +28 -0
- package/lib/node/exec-mcp-transport.d.ts.map +1 -0
- package/lib/node/exec-mcp-transport.js +80 -0
- package/lib/node/exec-mcp-transport.js.map +1 -0
- package/lib/node/exec-operations.d.ts +23 -0
- package/lib/node/exec-operations.d.ts.map +1 -0
- package/lib/node/exec-operations.js +99 -0
- package/lib/node/exec-operations.js.map +1 -0
- package/lib/node/exec-operations.spec.d.ts +2 -0
- package/lib/node/exec-operations.spec.d.ts.map +1 -0
- package/lib/node/exec-operations.spec.js +76 -0
- package/lib/node/exec-operations.spec.js.map +1 -0
- package/lib/node/extensions.d.ts +25 -0
- package/lib/node/extensions.d.ts.map +1 -0
- package/lib/node/extensions.js +65 -0
- package/lib/node/extensions.js.map +1 -0
- package/lib/node/extensions.spec.d.ts +2 -0
- package/lib/node/extensions.spec.d.ts.map +1 -0
- package/lib/node/extensions.spec.js +94 -0
- package/lib/node/extensions.spec.js.map +1 -0
- package/lib/node/glob-util.d.ts +8 -0
- package/lib/node/glob-util.d.ts.map +1 -0
- package/lib/node/glob-util.js +60 -0
- package/lib/node/glob-util.js.map +1 -0
- package/lib/node/glob-util.spec.d.ts +2 -0
- package/lib/node/glob-util.spec.d.ts.map +1 -0
- package/lib/node/glob-util.spec.js +27 -0
- package/lib/node/glob-util.spec.js.map +1 -0
- package/lib/node/index.d.ts +14 -0
- package/lib/node/index.d.ts.map +1 -0
- package/lib/node/index.js +30 -0
- package/lib/node/index.js.map +1 -0
- package/lib/node/pi-types.d.ts +14 -0
- package/lib/node/pi-types.d.ts.map +1 -0
- package/lib/node/pi-types.js +3 -0
- package/lib/node/pi-types.js.map +1 -0
- package/lib/node/session-lock.d.ts +28 -0
- package/lib/node/session-lock.d.ts.map +1 -0
- package/lib/node/session-lock.js +100 -0
- package/lib/node/session-lock.js.map +1 -0
- package/lib/node/session-lock.spec.d.ts +2 -0
- package/lib/node/session-lock.spec.d.ts.map +1 -0
- package/lib/node/session-lock.spec.js +53 -0
- package/lib/node/session-lock.spec.js.map +1 -0
- package/lib/node/ta-brand.d.ts +15 -0
- package/lib/node/ta-brand.d.ts.map +1 -0
- package/lib/node/ta-brand.js +47 -0
- package/lib/node/ta-brand.js.map +1 -0
- package/lib/node/ta-brand.spec.d.ts +2 -0
- package/lib/node/ta-brand.spec.d.ts.map +1 -0
- package/lib/node/ta-brand.spec.js +43 -0
- package/lib/node/ta-brand.spec.js.map +1 -0
- package/lib/node/ta-cli.d.ts +14 -0
- package/lib/node/ta-cli.d.ts.map +1 -0
- package/lib/node/ta-cli.js +53 -0
- package/lib/node/ta-cli.js.map +1 -0
- package/lib/node/ta-header.d.ts +62 -0
- package/lib/node/ta-header.d.ts.map +1 -0
- package/lib/node/ta-header.js +153 -0
- package/lib/node/ta-header.js.map +1 -0
- package/lib/node/ta-header.spec.d.ts +2 -0
- package/lib/node/ta-header.spec.d.ts.map +1 -0
- package/lib/node/ta-header.spec.js +67 -0
- package/lib/node/ta-header.spec.js.map +1 -0
- package/package.json +57 -4
- package/src/node/brand-extension.spec.ts +63 -0
- package/src/node/brand-extension.ts +43 -0
- package/src/node/bug-report.ts +48 -0
- package/src/node/dino-logo.ts +70 -0
- package/src/node/exec-extension.ts +33 -0
- package/src/node/exec-mcp-transport.ts +89 -0
- package/src/node/exec-operations.spec.ts +83 -0
- package/src/node/exec-operations.ts +106 -0
- package/src/node/extensions.spec.ts +110 -0
- package/src/node/extensions.ts +69 -0
- package/src/node/glob-util.spec.ts +29 -0
- package/src/node/glob-util.ts +47 -0
- package/src/node/index.ts +13 -0
- package/src/node/pi-types.ts +15 -0
- package/src/node/session-lock.spec.ts +56 -0
- package/src/node/session-lock.ts +105 -0
- package/src/node/ta-brand.spec.ts +46 -0
- package/src/node/ta-brand.ts +41 -0
- package/src/node/ta-cli.ts +58 -0
- package/src/node/ta-header.spec.ts +74 -0
- package/src/node/ta-header.ts +192 -0
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { parseArgs } from 'node:util';
|
|
2
|
+
import { PiBundleUtil } from '@celljs/ai-coding-agent/lib/node/pi/pi-bundle-util';
|
|
3
|
+
import { BrandExtension } from './brand-extension';
|
|
4
|
+
import { SessionLockExtension, TaExtensionUtil } from './extensions';
|
|
5
|
+
import type { PiSdk } from './pi-types';
|
|
6
|
+
import { SessionLock } from './session-lock';
|
|
7
|
+
import { TaBrand } from './ta-brand';
|
|
8
|
+
|
|
9
|
+
const SERVE_HELP = `Usage: ta serve [--host <host>] [--port <port>]
|
|
10
|
+
|
|
11
|
+
Serves pi sessions over WebSocket to any number of clients. Stops on SIGINT or SIGTERM.
|
|
12
|
+
|
|
13
|
+
--host Address to listen on. Default: 127.0.0.1
|
|
14
|
+
--port Port to listen on. Default: a free port
|
|
15
|
+
|
|
16
|
+
The URL and the access token are written to serve.json in the agent directory.
|
|
17
|
+
Set TA_SERVE_TOKEN to choose the token.
|
|
18
|
+
`;
|
|
19
|
+
|
|
20
|
+
export interface TaCliOptions {
|
|
21
|
+
/** The directory with ta's package.json and pi's assets. */
|
|
22
|
+
brandDir: string;
|
|
23
|
+
/** The built server of `ta serve`. */
|
|
24
|
+
serveEntry: string;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export namespace TaCli {
|
|
28
|
+
/**
|
|
29
|
+
* `serve` starts the server; anything else is pi's command as `ta`, with ta's built-in extensions. Nothing
|
|
30
|
+
* here loads Cell, so that `ta` costs what `pi` costs.
|
|
31
|
+
*/
|
|
32
|
+
export async function run(argv: string[], options: TaCliOptions): Promise<void> {
|
|
33
|
+
TaBrand.activate(options.brandDir);
|
|
34
|
+
if (argv[0] === 'serve') {
|
|
35
|
+
await serve(argv.slice(1), options.serveEntry);
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
const sdk: PiSdk = await import(/* @vite-ignore */ PiBundleUtil.specifier());
|
|
39
|
+
const builtIn = TaExtensionUtil.bundle([new SessionLockExtension(new SessionLock()), new BrandExtension(sdk)]);
|
|
40
|
+
await sdk.main(argv, { extensionFactories: [builtIn] });
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
async function serve(argv: string[], entry: string): Promise<void> {
|
|
44
|
+
const { values } = parseArgs({ args: argv, options: { host: { type: 'string' }, port: { type: 'string' }, help: { type: 'boolean', short: 'h' } } });
|
|
45
|
+
if (values.help) {
|
|
46
|
+
process.stdout.write(SERVE_HELP);
|
|
47
|
+
return;
|
|
48
|
+
}
|
|
49
|
+
// Read by the server like any other key of its configuration.
|
|
50
|
+
if (values.host) {
|
|
51
|
+
process.env.CELL_SERVER__HOST = values.host;
|
|
52
|
+
}
|
|
53
|
+
if (values.port) {
|
|
54
|
+
process.env.CELL_SERVER__PORT = values.port;
|
|
55
|
+
}
|
|
56
|
+
await import(/* @vite-ignore */ entry);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { describe, expect, it } from 'bun:test';
|
|
2
|
+
import { stripVTControlCharacters } from 'node:util';
|
|
3
|
+
import { DinoLogo } from './dino-logo';
|
|
4
|
+
import { type HeaderTheme, TaHeader } from './ta-header';
|
|
5
|
+
|
|
6
|
+
const plain = stripVTControlCharacters;
|
|
7
|
+
|
|
8
|
+
const theme: HeaderTheme = {
|
|
9
|
+
fg: (_color, text) => `\x1b[2m${text}\x1b[22m`,
|
|
10
|
+
bold: text => `\x1b[1m${text}\x1b[22m`,
|
|
11
|
+
getColorMode: () => 'truecolor',
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
const KEYS: Record<string, string> = { 'app.interrupt': 'escape', 'app.clear': 'ctrl+c', 'app.exit': 'ctrl+d', 'app.tools.expand': 'ctrl+o' };
|
|
15
|
+
const header = (logo = true) => new TaHeader(theme, { version: '4.4.1', keyText: key => KEYS[key] ?? '', logo });
|
|
16
|
+
|
|
17
|
+
describe('TaHeader', () => {
|
|
18
|
+
it('never renders a line wider than it is given', () => {
|
|
19
|
+
for (const expanded of [false, true]) {
|
|
20
|
+
const component = header();
|
|
21
|
+
component.setExpanded(expanded);
|
|
22
|
+
for (let width = 10; width <= 160; width++) {
|
|
23
|
+
for (const line of component.render(width)) {
|
|
24
|
+
expect(plain(line).length).toBeLessThanOrEqual(width);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
it('draws the dinosaur beside the name, version and keys when there is room', () => {
|
|
31
|
+
const lines = header().render(100).map(plain);
|
|
32
|
+
expect(lines.length).toBe(DinoLogo.HEIGHT + 1);
|
|
33
|
+
expect(lines.some(line => line.includes('█') && line.includes('ta v4.4.1'))).toBe(true);
|
|
34
|
+
expect(lines.join('\n')).toContain('escape interrupt · ctrl+c clear · ctrl+d exit');
|
|
35
|
+
expect(lines.join('\n')).toContain('/ commands · ! bash · ctrl+o more');
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
it('leaves the dinosaur out where the terminal is narrow or cannot draw it', () => {
|
|
39
|
+
expect(header().render(50).map(plain).join('')).not.toContain('█');
|
|
40
|
+
expect(header(false).render(120).map(plain).join('')).not.toContain('█');
|
|
41
|
+
expect(plain(header().render(50)[0])).toBe(' ta v4.4.1');
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
it('lists every bound key when expanded, and skips unbound ones', () => {
|
|
45
|
+
const component = header();
|
|
46
|
+
component.setExpanded(true);
|
|
47
|
+
const text = component.render(100).map(plain).join('\n');
|
|
48
|
+
expect(text).toContain('ctrl+o');
|
|
49
|
+
expect(text).toContain('to expand tools');
|
|
50
|
+
expect(text).toContain('to run bash (no context)');
|
|
51
|
+
expect(text).not.toContain('to suspend');
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
it('ends with an empty line, apart from what pi shows below', () => {
|
|
55
|
+
expect(header().render(100).at(-1)).toBe('');
|
|
56
|
+
});
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
describe('DinoLogo', () => {
|
|
60
|
+
it('is 16 columns by 8 lines in the brand green', () => {
|
|
61
|
+
const lines = DinoLogo.lines('truecolor');
|
|
62
|
+
expect(lines).toHaveLength(8);
|
|
63
|
+
for (const line of lines) {
|
|
64
|
+
expect(plain(line)).toHaveLength(DinoLogo.WIDTH);
|
|
65
|
+
expect(line).toStartWith('\x1b[38;2;16;185;129m');
|
|
66
|
+
}
|
|
67
|
+
expect(DinoLogo.lines('256color')[0]).toStartWith('\x1b[38;5;36m');
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
it('is not drawn in Apple Terminal', () => {
|
|
71
|
+
expect(DinoLogo.isSupported({ TERM_PROGRAM: 'Apple_Terminal' })).toBe(false);
|
|
72
|
+
expect(DinoLogo.isSupported({ TERM_PROGRAM: 'iTerm.app' })).toBe(true);
|
|
73
|
+
});
|
|
74
|
+
});
|
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
import { type ColorMode, DinoLogo } from './dino-logo';
|
|
2
|
+
|
|
3
|
+
/** The part of pi's theme the header uses. */
|
|
4
|
+
export interface HeaderTheme {
|
|
5
|
+
fg(color: 'accent' | 'dim' | 'muted' | 'text', text: string): string;
|
|
6
|
+
bold(text: string): string;
|
|
7
|
+
getColorMode(): ColorMode;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
/** A key and what it does; keys come from the user's keybindings. */
|
|
11
|
+
export interface KeyHint {
|
|
12
|
+
/** A keybinding id of pi (`app.interrupt`), or the key itself (`/`). */
|
|
13
|
+
key: string;
|
|
14
|
+
description: string;
|
|
15
|
+
/** `key` is the key itself, not a keybinding id. */
|
|
16
|
+
raw?: boolean;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export interface TaHeaderOptions {
|
|
20
|
+
version: string;
|
|
21
|
+
/** The keys of a keybinding of pi, as text; empty when unbound. */
|
|
22
|
+
keyText(keybinding: string): string;
|
|
23
|
+
/** Draws the dinosaur. Default: where the terminal can show it. */
|
|
24
|
+
logo?: boolean;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** Text with its width in columns, known before it is styled. */
|
|
28
|
+
interface Span {
|
|
29
|
+
text: string;
|
|
30
|
+
width: number;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const INDENT = 2;
|
|
34
|
+
const GAP = 4;
|
|
35
|
+
const SEPARATOR = ' · ';
|
|
36
|
+
/** Below this width the logo leaves too little room for the text. */
|
|
37
|
+
const LOGO_MIN_WIDTH = INDENT + DinoLogo.WIDTH + GAP + 36;
|
|
38
|
+
|
|
39
|
+
/** What the header shows, a line per group where the width allows; the expanded list is pi's startup help. */
|
|
40
|
+
export const COMPACT_HINTS: KeyHint[][] = [
|
|
41
|
+
[
|
|
42
|
+
{ key: 'app.interrupt', description: 'interrupt' },
|
|
43
|
+
{ key: 'app.clear', description: 'clear' },
|
|
44
|
+
{ key: 'app.exit', description: 'exit' },
|
|
45
|
+
],
|
|
46
|
+
[
|
|
47
|
+
{ key: '/', description: 'commands', raw: true },
|
|
48
|
+
{ key: '!', description: 'bash', raw: true },
|
|
49
|
+
{ key: 'app.tools.expand', description: 'more' },
|
|
50
|
+
],
|
|
51
|
+
];
|
|
52
|
+
|
|
53
|
+
export const EXPANDED_HINTS: KeyHint[] = [
|
|
54
|
+
{ key: 'app.interrupt', description: 'to interrupt' },
|
|
55
|
+
{ key: 'app.clear', description: 'to clear' },
|
|
56
|
+
{ key: 'app.exit', description: 'to exit (empty)' },
|
|
57
|
+
{ key: 'app.suspend', description: 'to suspend' },
|
|
58
|
+
{ key: 'tui.editor.deleteToLineEnd', description: 'to delete to end' },
|
|
59
|
+
{ key: 'app.thinking.cycle', description: 'to cycle thinking level' },
|
|
60
|
+
{ key: 'app.model.cycleForward', description: 'to cycle models' },
|
|
61
|
+
{ key: 'app.model.select', description: 'to select model' },
|
|
62
|
+
{ key: 'app.tools.expand', description: 'to expand tools' },
|
|
63
|
+
{ key: 'app.thinking.toggle', description: 'to expand thinking' },
|
|
64
|
+
{ key: 'app.editor.external', description: 'for external editor' },
|
|
65
|
+
{ key: '/', description: 'for commands', raw: true },
|
|
66
|
+
{ key: '!', description: 'to run bash', raw: true },
|
|
67
|
+
{ key: '!!', description: 'to run bash (no context)', raw: true },
|
|
68
|
+
{ key: 'app.message.followUp', description: 'to queue follow-up' },
|
|
69
|
+
{ key: 'app.message.dequeue', description: 'to edit all queued messages' },
|
|
70
|
+
{ key: 'app.clipboard.pasteImage', description: 'to paste images or files' },
|
|
71
|
+
];
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* ta's startup header in pi's terminal UI: the dinosaur, the name and version, and the keys that
|
|
75
|
+
* matter first; all of pi's keys when expanded (the key that expands tools). Every line fits the
|
|
76
|
+
* width it is given: the terminal UI refuses wider ones.
|
|
77
|
+
*/
|
|
78
|
+
export class TaHeader {
|
|
79
|
+
protected expanded = false;
|
|
80
|
+
|
|
81
|
+
constructor(
|
|
82
|
+
protected readonly theme: HeaderTheme,
|
|
83
|
+
protected readonly options: TaHeaderOptions,
|
|
84
|
+
) {}
|
|
85
|
+
|
|
86
|
+
setExpanded(expanded: boolean): void {
|
|
87
|
+
this.expanded = expanded;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
invalidate(): void {}
|
|
91
|
+
|
|
92
|
+
render(width: number): string[] {
|
|
93
|
+
const withLogo = (this.options.logo ?? DinoLogo.isSupported()) && width >= LOGO_MIN_WIDTH;
|
|
94
|
+
const lines = withLogo ? this.besideLogo(width) : this.indented(this.intro(width - INDENT));
|
|
95
|
+
if (this.expanded) {
|
|
96
|
+
lines.push('', ...this.indented(this.keyTable(width - INDENT)));
|
|
97
|
+
}
|
|
98
|
+
// pi puts no space between a header and what follows it.
|
|
99
|
+
lines.push('');
|
|
100
|
+
return lines;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/** The intro, vertically centered next to the logo. */
|
|
104
|
+
protected besideLogo(width: number): string[] {
|
|
105
|
+
const logo = DinoLogo.lines(this.theme.getColorMode());
|
|
106
|
+
const intro = this.intro(width - INDENT - DinoLogo.WIDTH - GAP);
|
|
107
|
+
const top = Math.max(0, Math.floor((logo.length - intro.length) / 2));
|
|
108
|
+
const pad = ' '.repeat(INDENT);
|
|
109
|
+
const gap = ' '.repeat(GAP);
|
|
110
|
+
return Array.from({ length: Math.max(logo.length, top + intro.length) }, (_, i) => {
|
|
111
|
+
const text = intro[i - top]?.text ?? '';
|
|
112
|
+
return `${pad}${logo[i] ?? ' '.repeat(DinoLogo.WIDTH)}${text ? gap + text : ''}`;
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
protected intro(width: number): Span[] {
|
|
117
|
+
const { theme } = this;
|
|
118
|
+
const title = `ta v${this.options.version}`;
|
|
119
|
+
const green = (text: string) => DinoLogo.paint(theme.getColorMode(), text);
|
|
120
|
+
return [
|
|
121
|
+
this.span(title, width, text => (text === title ? `${theme.bold(green('ta'))} ${theme.fg('dim', title.slice(4))}` : green(text))),
|
|
122
|
+
this.span('a coding agent on Cell and pi', width, text => theme.fg('muted', text)),
|
|
123
|
+
{ text: '', width: 0 },
|
|
124
|
+
...COMPACT_HINTS.flatMap(group => this.flow(this.hints(group), width)),
|
|
125
|
+
{ text: '', width: 0 },
|
|
126
|
+
this.span('Ask ta how to use or extend it.', width, text => theme.fg('dim', text)),
|
|
127
|
+
];
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/** Hints in as few lines as the width allows, a hint never split. */
|
|
131
|
+
protected flow(hints: Span[], width: number): Span[] {
|
|
132
|
+
const separator = this.theme.fg('dim', SEPARATOR);
|
|
133
|
+
const lines: Span[] = [];
|
|
134
|
+
for (const hint of hints) {
|
|
135
|
+
const last = lines[lines.length - 1];
|
|
136
|
+
if (last && last.width + SEPARATOR.length + hint.width <= width) {
|
|
137
|
+
last.text += separator + hint.text;
|
|
138
|
+
last.width += SEPARATOR.length + hint.width;
|
|
139
|
+
} else if (hint.width <= width) {
|
|
140
|
+
lines.push({ ...hint });
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
return lines;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/** All keys, in as many aligned columns as fit. */
|
|
147
|
+
protected keyTable(width: number): Span[] {
|
|
148
|
+
const hints = this.resolve(EXPANDED_HINTS);
|
|
149
|
+
const keyWidth = Math.max(...hints.map(hint => hint.keys.length));
|
|
150
|
+
const cellWidth = keyWidth + 2 + Math.max(...hints.map(hint => hint.description.length));
|
|
151
|
+
const columnWidth = cellWidth + GAP;
|
|
152
|
+
const columns = Math.max(1, Math.floor((width + GAP) / columnWidth));
|
|
153
|
+
const rows = Math.ceil(hints.length / columns);
|
|
154
|
+
const lines: Span[] = [];
|
|
155
|
+
for (let row = 0; row < rows; row++) {
|
|
156
|
+
const cells = hints.slice(row * columns, row * columns + columns).map(hint => {
|
|
157
|
+
const description = hint.description.padEnd(cellWidth - keyWidth - 2);
|
|
158
|
+
return `${this.theme.fg('text', hint.keys.padEnd(keyWidth))} ${this.theme.fg('muted', description)}`;
|
|
159
|
+
});
|
|
160
|
+
const lineWidth = cells.length * columnWidth - GAP;
|
|
161
|
+
// One column that is still too wide: the descriptions are cut.
|
|
162
|
+
lines.push(
|
|
163
|
+
lineWidth <= width
|
|
164
|
+
? { text: cells.join(' '.repeat(GAP)), width: lineWidth }
|
|
165
|
+
: this.span(`${hints[row].keys} ${hints[row].description}`, width, text => this.theme.fg('muted', text)),
|
|
166
|
+
);
|
|
167
|
+
}
|
|
168
|
+
return lines;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
protected hints(hints: KeyHint[]): Span[] {
|
|
172
|
+
return this.resolve(hints).map(({ keys, description }) => ({
|
|
173
|
+
text: `${this.theme.fg('text', keys)} ${this.theme.fg('muted', description)}`,
|
|
174
|
+
width: keys.length + 1 + description.length,
|
|
175
|
+
}));
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/** The hints whose keys are bound, with their keys as text. */
|
|
179
|
+
protected resolve(hints: KeyHint[]): { keys: string; description: string }[] {
|
|
180
|
+
return hints.map(hint => ({ keys: hint.raw ? hint.key : this.options.keyText(hint.key), description: hint.description })).filter(hint => hint.keys);
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/** Plain text, cut to the width, then styled. */
|
|
184
|
+
protected span(plain: string, width: number, style: (text: string) => string): Span {
|
|
185
|
+
const text = plain.length > width ? `${plain.slice(0, Math.max(0, width - 1))}…` : plain;
|
|
186
|
+
return { text: text ? style(text) : '', width: Math.min(plain.length, width) };
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
protected indented(spans: Span[]): string[] {
|
|
190
|
+
return spans.map(span => (span.text ? ' '.repeat(INDENT) + span.text : ''));
|
|
191
|
+
}
|
|
192
|
+
}
|