@dxos/react-ui-terminal 0.0.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/dist/lib/chunk-cli.mjs +500 -0
- package/dist/lib/chunk-cli.mjs.map +1 -0
- package/dist/lib/cli.mjs +2 -0
- package/dist/lib/index.mjs +157 -0
- package/dist/lib/index.mjs.map +1 -0
- package/dist/lib/testing.mjs +41 -0
- package/dist/lib/testing.mjs.map +1 -0
- package/dist/types/src/cli/bridge.d.ts +42 -0
- package/dist/types/src/cli/bridge.d.ts.map +1 -0
- package/dist/types/src/cli/console.d.ts +16 -0
- package/dist/types/src/cli/console.d.ts.map +1 -0
- package/dist/types/src/cli/console.test.d.ts +2 -0
- package/dist/types/src/cli/console.test.d.ts.map +1 -0
- package/dist/types/src/cli/context.d.ts +17 -0
- package/dist/types/src/cli/context.d.ts.map +1 -0
- package/dist/types/src/cli/index.d.ts +9 -0
- package/dist/types/src/cli/index.d.ts.map +1 -0
- package/dist/types/src/cli/input.d.ts +14 -0
- package/dist/types/src/cli/input.d.ts.map +1 -0
- package/dist/types/src/cli/input.test.d.ts +2 -0
- package/dist/types/src/cli/input.test.d.ts.map +1 -0
- package/dist/types/src/cli/line-editor.d.ts +35 -0
- package/dist/types/src/cli/line-editor.d.ts.map +1 -0
- package/dist/types/src/cli/line-editor.test.d.ts +2 -0
- package/dist/types/src/cli/line-editor.test.d.ts.map +1 -0
- package/dist/types/src/cli/shell.d.ts +30 -0
- package/dist/types/src/cli/shell.d.ts.map +1 -0
- package/dist/types/src/cli/terminal.d.ts +12 -0
- package/dist/types/src/cli/terminal.d.ts.map +1 -0
- package/dist/types/src/cli/testing.d.ts +20 -0
- package/dist/types/src/cli/testing.d.ts.map +1 -0
- package/dist/types/src/cli/tokenize.d.ts +12 -0
- package/dist/types/src/cli/tokenize.d.ts.map +1 -0
- package/dist/types/src/components/Terminal/Terminal.d.ts +30 -0
- package/dist/types/src/components/Terminal/Terminal.d.ts.map +1 -0
- package/dist/types/src/components/Terminal/Terminal.stories.d.ts +18 -0
- package/dist/types/src/components/Terminal/Terminal.stories.d.ts.map +1 -0
- package/dist/types/src/components/Terminal/index.d.ts +2 -0
- package/dist/types/src/components/Terminal/index.d.ts.map +1 -0
- package/dist/types/src/components/Terminal/theme.d.ts +7 -0
- package/dist/types/src/components/Terminal/theme.d.ts.map +1 -0
- package/dist/types/src/components/index.d.ts +2 -0
- package/dist/types/src/components/index.d.ts.map +1 -0
- package/dist/types/src/index.d.ts +2 -0
- package/dist/types/src/index.d.ts.map +1 -0
- package/dist/types/src/testing.d.ts +23 -0
- package/dist/types/src/testing.d.ts.map +1 -0
- package/dist/types/tsconfig.tsbuildinfo +1 -0
- package/package.json +65 -0
- package/src/cli/bridge.ts +102 -0
- package/src/cli/console.test.ts +73 -0
- package/src/cli/console.ts +91 -0
- package/src/cli/context.ts +26 -0
- package/src/cli/index.ts +13 -0
- package/src/cli/input.test.ts +109 -0
- package/src/cli/input.ts +132 -0
- package/src/cli/line-editor.test.ts +141 -0
- package/src/cli/line-editor.ts +199 -0
- package/src/cli/shell.ts +103 -0
- package/src/cli/terminal.ts +43 -0
- package/src/cli/testing.ts +51 -0
- package/src/cli/tokenize.ts +35 -0
- package/src/components/Terminal/Terminal.stories.tsx +114 -0
- package/src/components/Terminal/Terminal.tsx +118 -0
- package/src/components/Terminal/index.ts +5 -0
- package/src/components/Terminal/theme.ts +87 -0
- package/src/components/index.ts +5 -0
- package/src/index.ts +5 -0
- package/src/testing.ts +56 -0
package/package.json
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@dxos/react-ui-terminal",
|
|
3
|
+
"version": "0.0.0",
|
|
4
|
+
"description": "Browser terminal emulator for @effect/cli applications.",
|
|
5
|
+
"homepage": "https://dxos.org",
|
|
6
|
+
"bugs": "https://github.com/dxos/dxos/issues",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "https://github.com/dxos/dxos"
|
|
10
|
+
},
|
|
11
|
+
"license": "FSL-1.1-Apache-2.0",
|
|
12
|
+
"author": "DXOS.org",
|
|
13
|
+
"type": "module",
|
|
14
|
+
"exports": {
|
|
15
|
+
".": {
|
|
16
|
+
"source": "./src/index.ts",
|
|
17
|
+
"types": "./dist/types/src/index.d.ts",
|
|
18
|
+
"import": "./dist/lib/index.mjs"
|
|
19
|
+
},
|
|
20
|
+
"./cli": {
|
|
21
|
+
"source": "./src/cli/index.ts",
|
|
22
|
+
"types": "./dist/types/src/cli/index.d.ts",
|
|
23
|
+
"import": "./dist/lib/cli.mjs"
|
|
24
|
+
},
|
|
25
|
+
"./testing": {
|
|
26
|
+
"source": "./src/testing.ts",
|
|
27
|
+
"types": "./dist/types/src/testing.d.ts",
|
|
28
|
+
"import": "./dist/lib/testing.mjs"
|
|
29
|
+
}
|
|
30
|
+
},
|
|
31
|
+
"types": "dist/types/src/index.d.ts",
|
|
32
|
+
"files": [
|
|
33
|
+
"dist",
|
|
34
|
+
"src"
|
|
35
|
+
],
|
|
36
|
+
"dependencies": {
|
|
37
|
+
"@dxos/invariant": "workspace:*",
|
|
38
|
+
"@dxos/react-ui": "workspace:*",
|
|
39
|
+
"@dxos/ui-theme": "workspace:*",
|
|
40
|
+
"@dxos/ui-types": "workspace:*",
|
|
41
|
+
"@effect/cli": "catalog:",
|
|
42
|
+
"@effect/platform": "catalog:",
|
|
43
|
+
"@xterm/addon-fit": "catalog:",
|
|
44
|
+
"@xterm/xterm": "catalog:",
|
|
45
|
+
"effect": "catalog:"
|
|
46
|
+
},
|
|
47
|
+
"devDependencies": {
|
|
48
|
+
"@dxos/effect": "workspace:*",
|
|
49
|
+
"@dxos/storybook-utils": "workspace:*",
|
|
50
|
+
"@types/react": "catalog:",
|
|
51
|
+
"@types/react-dom": "catalog:",
|
|
52
|
+
"react": "catalog:",
|
|
53
|
+
"react-dom": "catalog:",
|
|
54
|
+
"vite": "catalog:"
|
|
55
|
+
},
|
|
56
|
+
"peerDependencies": {
|
|
57
|
+
"@dxos/react-ui": "workspace:^",
|
|
58
|
+
"@dxos/ui-theme": "workspace:^",
|
|
59
|
+
"react": "catalog:",
|
|
60
|
+
"react-dom": "catalog:"
|
|
61
|
+
},
|
|
62
|
+
"publishConfig": {
|
|
63
|
+
"access": "public"
|
|
64
|
+
}
|
|
65
|
+
}
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2026 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import type * as Terminal from '@effect/platform/Terminal';
|
|
6
|
+
import type { IDisposable, Terminal as XtermTerminal } from '@xterm/xterm';
|
|
7
|
+
|
|
8
|
+
import { decodeInput } from './input';
|
|
9
|
+
|
|
10
|
+
export type InputHandler = (input: Terminal.UserInput) => void;
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* The surface the shell and the Effect `Terminal` service need from a terminal: a write sink and a
|
|
14
|
+
* stack of keypress subscribers. Depending on this rather than the xterm class keeps the rest of
|
|
15
|
+
* this module free of any renderer.
|
|
16
|
+
*/
|
|
17
|
+
export interface TerminalBridge {
|
|
18
|
+
readonly columns: number;
|
|
19
|
+
readonly rows: number;
|
|
20
|
+
readonly atLineStart: boolean;
|
|
21
|
+
write(text: string): void;
|
|
22
|
+
clear(): void;
|
|
23
|
+
subscribe(handler: InputHandler): () => void;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Adapts an xterm instance to {@link TerminalBridge}.
|
|
28
|
+
*/
|
|
29
|
+
export class XtermBridge implements TerminalBridge {
|
|
30
|
+
#terminal: XtermTerminal;
|
|
31
|
+
#subscribers: InputHandler[] = [];
|
|
32
|
+
#subscription: IDisposable;
|
|
33
|
+
#atLineStart = true;
|
|
34
|
+
|
|
35
|
+
constructor(terminal: XtermTerminal) {
|
|
36
|
+
this.#terminal = terminal;
|
|
37
|
+
this.#subscription = terminal.onData((data) => this.#handleData(data));
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
get columns(): number {
|
|
41
|
+
return this.#terminal.cols;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
get rows(): number {
|
|
45
|
+
return this.#terminal.rows;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Whether the cursor sits at the start of a row, so callers can avoid a blank line when a command
|
|
50
|
+
* has already terminated its output with a newline.
|
|
51
|
+
*/
|
|
52
|
+
get atLineStart(): boolean {
|
|
53
|
+
return this.#atLineStart;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Writes text, translating bare newlines to CRLF since xterm does not reset the column on `\n`.
|
|
58
|
+
*/
|
|
59
|
+
write(text: string): void {
|
|
60
|
+
if (text.length === 0) {
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
this.#terminal.write(text.replace(/\r?\n/g, '\r\n'));
|
|
65
|
+
this.#atLineStart = text.endsWith('\n');
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
clear(): void {
|
|
69
|
+
this.#terminal.write('\x1b[2J\x1b[H');
|
|
70
|
+
this.#atLineStart = true;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Subscribes to keypresses. The most recent subscriber wins, so a prompt opened by a running
|
|
75
|
+
* command takes over from the shell's line editor and hands back when its scope closes.
|
|
76
|
+
*/
|
|
77
|
+
subscribe(handler: InputHandler): () => void {
|
|
78
|
+
this.#subscribers.push(handler);
|
|
79
|
+
return () => {
|
|
80
|
+
const index = this.#subscribers.indexOf(handler);
|
|
81
|
+
if (index !== -1) {
|
|
82
|
+
this.#subscribers.splice(index, 1);
|
|
83
|
+
}
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
dispose(): void {
|
|
88
|
+
this.#subscription.dispose();
|
|
89
|
+
this.#subscribers.length = 0;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
#handleData(data: string): void {
|
|
93
|
+
const handler = this.#subscribers.at(-1);
|
|
94
|
+
if (!handler) {
|
|
95
|
+
return;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
for (const input of decodeInput(data)) {
|
|
99
|
+
handler(input);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2026 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import * as Console from 'effect/Console';
|
|
6
|
+
import * as Effect from 'effect/Effect';
|
|
7
|
+
import { describe, expect, test } from 'vitest';
|
|
8
|
+
|
|
9
|
+
import { EffectEx } from '@dxos/effect';
|
|
10
|
+
|
|
11
|
+
import * as XtermConsole from './console';
|
|
12
|
+
import { TestBridge } from './testing';
|
|
13
|
+
|
|
14
|
+
describe('console', () => {
|
|
15
|
+
test('writes a line per log', async () => {
|
|
16
|
+
const bridge = new TestBridge();
|
|
17
|
+
await EffectEx.runPromise(Console.log('hello').pipe(Effect.provide(XtermConsole.layer(bridge))));
|
|
18
|
+
expect(bridge.rendered).to.eq('hello\n');
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
// Guards the fix for output vanishing: `Console.log` resolves against the `currentServices` fiber
|
|
22
|
+
// ref, so providing the tag with `Layer.succeed` would leave this writing to the host console.
|
|
23
|
+
test('the layer overrides the default console', async () => {
|
|
24
|
+
const bridge = new TestBridge();
|
|
25
|
+
await EffectEx.runPromise(
|
|
26
|
+
Effect.gen(function* () {
|
|
27
|
+
yield* Console.log('first');
|
|
28
|
+
yield* Console.error('second');
|
|
29
|
+
}).pipe(Effect.provide(XtermConsole.layer(bridge))),
|
|
30
|
+
);
|
|
31
|
+
|
|
32
|
+
expect(bridge.rendered).to.eq('first\nsecond\n');
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
test('joins multiple arguments with a space', async () => {
|
|
36
|
+
const bridge = new TestBridge();
|
|
37
|
+
await EffectEx.runPromise(Console.log('a', 'b', 'c').pipe(Effect.provide(XtermConsole.layer(bridge))));
|
|
38
|
+
expect(bridge.rendered).to.eq('a b c\n');
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
test('serializes non-string values', async () => {
|
|
42
|
+
const bridge = new TestBridge();
|
|
43
|
+
await EffectEx.runPromise(Console.log({ id: 1 }).pipe(Effect.provide(XtermConsole.layer(bridge))));
|
|
44
|
+
expect(bridge.rendered).to.contain('"id": 1');
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
test('renders an error with its stack', async () => {
|
|
48
|
+
const bridge = new TestBridge();
|
|
49
|
+
const error = new Error('boom');
|
|
50
|
+
await EffectEx.runPromise(Console.error(error).pipe(Effect.provide(XtermConsole.layer(bridge))));
|
|
51
|
+
expect(bridge.rendered).to.contain('boom');
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
test('survives a value that cannot be serialized', async () => {
|
|
55
|
+
const bridge = new TestBridge();
|
|
56
|
+
const circular: Record<string, unknown> = {};
|
|
57
|
+
circular.self = circular;
|
|
58
|
+
await EffectEx.runPromise(Console.log(circular).pipe(Effect.provide(XtermConsole.layer(bridge))));
|
|
59
|
+
expect(bridge.rendered).to.contain('object');
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
test('clear resets the screen', async () => {
|
|
63
|
+
const bridge = new TestBridge();
|
|
64
|
+
await EffectEx.runPromise(Console.clear.pipe(Effect.provide(XtermConsole.layer(bridge))));
|
|
65
|
+
expect(bridge.writes).to.contain('<clear>');
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
test('ANSI escapes pass through untouched', async () => {
|
|
69
|
+
const bridge = new TestBridge();
|
|
70
|
+
await EffectEx.runPromise(Console.log('\x1b[36mcyan\x1b[0m').pipe(Effect.provide(XtermConsole.layer(bridge))));
|
|
71
|
+
expect(bridge.rendered).to.eq('\x1b[36mcyan\x1b[0m\n');
|
|
72
|
+
});
|
|
73
|
+
});
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2026 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import * as Console from 'effect/Console';
|
|
6
|
+
import * as Effect from 'effect/Effect';
|
|
7
|
+
import * as Layer from 'effect/Layer';
|
|
8
|
+
|
|
9
|
+
import type { TerminalBridge } from './bridge';
|
|
10
|
+
|
|
11
|
+
const stringify = (value: unknown): string => {
|
|
12
|
+
if (typeof value === 'string') {
|
|
13
|
+
return value;
|
|
14
|
+
}
|
|
15
|
+
if (value instanceof Error) {
|
|
16
|
+
return value.stack ?? value.message;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
try {
|
|
20
|
+
return JSON.stringify(value, null, 2) ?? String(value);
|
|
21
|
+
} catch {
|
|
22
|
+
return String(value);
|
|
23
|
+
}
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
const format = (args: ReadonlyArray<unknown>): string => args.map(stringify).join(' ');
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Routes Effect's `Console` to the terminal.
|
|
30
|
+
*
|
|
31
|
+
* `@effect/cli` renders help, usage errors, and wizard output as ANSI text through this service,
|
|
32
|
+
* which xterm interprets natively — so the browser gets the same styled output as a shell.
|
|
33
|
+
*/
|
|
34
|
+
export const make = (bridge: TerminalBridge): Console.Console => {
|
|
35
|
+
const write = (args: ReadonlyArray<unknown>) => bridge.write(format(args) + '\n');
|
|
36
|
+
|
|
37
|
+
const unsafe: Console.UnsafeConsole = {
|
|
38
|
+
assert: (condition, ...args) => {
|
|
39
|
+
if (!condition) {
|
|
40
|
+
write(['Assertion failed:', ...args]);
|
|
41
|
+
}
|
|
42
|
+
},
|
|
43
|
+
clear: () => bridge.clear(),
|
|
44
|
+
count: () => {},
|
|
45
|
+
countReset: () => {},
|
|
46
|
+
debug: (...args) => write(args),
|
|
47
|
+
dir: (item) => write([item]),
|
|
48
|
+
dirxml: (...args) => write(args),
|
|
49
|
+
error: (...args) => write(args),
|
|
50
|
+
group: (...args) => write(args),
|
|
51
|
+
groupCollapsed: (...args) => write(args),
|
|
52
|
+
groupEnd: () => {},
|
|
53
|
+
info: (...args) => write(args),
|
|
54
|
+
log: (...args) => write(args),
|
|
55
|
+
table: (tabularData) => write([tabularData]),
|
|
56
|
+
time: () => {},
|
|
57
|
+
timeEnd: () => {},
|
|
58
|
+
timeLog: (_label, ...args) => write(args),
|
|
59
|
+
trace: (...args) => write(args),
|
|
60
|
+
warn: (...args) => write(args),
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
return Console.Console.of({
|
|
64
|
+
[Console.TypeId]: Console.TypeId,
|
|
65
|
+
assert: (condition, ...args) => Effect.sync(() => unsafe.assert(condition, ...args)),
|
|
66
|
+
clear: Effect.sync(() => unsafe.clear()),
|
|
67
|
+
count: (label) => Effect.sync(() => unsafe.count(label)),
|
|
68
|
+
countReset: (label) => Effect.sync(() => unsafe.countReset(label)),
|
|
69
|
+
debug: (...args) => Effect.sync(() => unsafe.debug(...args)),
|
|
70
|
+
dir: (item, options) => Effect.sync(() => unsafe.dir(item, options)),
|
|
71
|
+
dirxml: (...args) => Effect.sync(() => unsafe.dirxml(...args)),
|
|
72
|
+
error: (...args) => Effect.sync(() => unsafe.error(...args)),
|
|
73
|
+
group: (options) => Effect.sync(() => unsafe.group(options?.label)),
|
|
74
|
+
groupEnd: Effect.sync(() => unsafe.groupEnd()),
|
|
75
|
+
info: (...args) => Effect.sync(() => unsafe.info(...args)),
|
|
76
|
+
log: (...args) => Effect.sync(() => unsafe.log(...args)),
|
|
77
|
+
table: (tabularData, properties) => Effect.sync(() => unsafe.table(tabularData, properties)),
|
|
78
|
+
time: (label) => Effect.sync(() => unsafe.time(label)),
|
|
79
|
+
timeEnd: (label) => Effect.sync(() => unsafe.timeEnd(label)),
|
|
80
|
+
timeLog: (label, ...args) => Effect.sync(() => unsafe.timeLog(label, ...args)),
|
|
81
|
+
trace: (...args) => Effect.sync(() => unsafe.trace(...args)),
|
|
82
|
+
warn: (...args) => Effect.sync(() => unsafe.warn(...args)),
|
|
83
|
+
unsafe,
|
|
84
|
+
});
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* `Console.log` resolves against the `currentServices` fiber ref rather than the layer context, so
|
|
89
|
+
* this must go through `setConsole` — providing the tag with `Layer.succeed` is silently ignored.
|
|
90
|
+
*/
|
|
91
|
+
export const layer = (bridge: TerminalBridge): Layer.Layer<never> => Console.setConsole(make(bridge));
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2026 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import * as FileSystem from '@effect/platform/FileSystem';
|
|
6
|
+
import * as Path from '@effect/platform/Path';
|
|
7
|
+
import type * as Terminal from '@effect/platform/Terminal';
|
|
8
|
+
import * as Layer from 'effect/Layer';
|
|
9
|
+
|
|
10
|
+
import type { XtermBridge } from './bridge';
|
|
11
|
+
import * as XtermConsole from './console';
|
|
12
|
+
import * as XtermTerminal from './terminal';
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* The browser counterpart to `BunContext.layer`.
|
|
16
|
+
*
|
|
17
|
+
* `CliApp.Environment` is only `FileSystem | Path | Terminal`: `Path` is already pure JS upstream
|
|
18
|
+
* and the CLI's browser-safe commands never touch the filesystem, leaving `Terminal` as the sole
|
|
19
|
+
* service needing a real implementation. The console override is merged in too — contributing no
|
|
20
|
+
* services of its own — so command output and the CLI's ANSI help text land in the terminal
|
|
21
|
+
* instead of the devtools console.
|
|
22
|
+
*/
|
|
23
|
+
export type Provided = Terminal.Terminal | FileSystem.FileSystem | Path.Path;
|
|
24
|
+
|
|
25
|
+
export const layer = (bridge: XtermBridge): Layer.Layer<Provided> =>
|
|
26
|
+
Layer.mergeAll(XtermTerminal.layer(bridge), XtermConsole.layer(bridge), FileSystem.layerNoop({}), Path.layer);
|
package/src/cli/index.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2026 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
export * as XtermConsole from './console';
|
|
6
|
+
export * as XtermContext from './context';
|
|
7
|
+
export * as XtermTerminal from './terminal';
|
|
8
|
+
|
|
9
|
+
export * from './bridge';
|
|
10
|
+
export * from './input';
|
|
11
|
+
export * from './line-editor';
|
|
12
|
+
export * from './shell';
|
|
13
|
+
export * from './tokenize';
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2026 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import * as Option from 'effect/Option';
|
|
6
|
+
import { describe, expect, test } from 'vitest';
|
|
7
|
+
|
|
8
|
+
import { decodeInput, isQuitInput } from './input';
|
|
9
|
+
import { rewriteHelpAliases, tokenize } from './tokenize';
|
|
10
|
+
|
|
11
|
+
describe('decodeInput', () => {
|
|
12
|
+
test('decodes printable characters', () => {
|
|
13
|
+
const [input] = decodeInput('a');
|
|
14
|
+
expect(input.key).to.deep.eq({ name: 'a', ctrl: false, meta: false, shift: false });
|
|
15
|
+
expect(Option.getOrNull(input.input)).to.eq('a');
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
test('reports shift for capitals but keeps the lowercase name', () => {
|
|
19
|
+
const [input] = decodeInput('A');
|
|
20
|
+
expect(input.key.name).to.eq('a');
|
|
21
|
+
expect(input.key.shift).to.be.true;
|
|
22
|
+
expect(Option.getOrNull(input.input)).to.eq('A');
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
test('names the space key', () => {
|
|
26
|
+
expect(decodeInput(' ')[0].key.name).to.eq('space');
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
test('decodes carriage return as return', () => {
|
|
30
|
+
expect(decodeInput('\r')[0].key.name).to.eq('return');
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
test('decodes backspace', () => {
|
|
34
|
+
expect(decodeInput('\x7f')[0].key.name).to.eq('backspace');
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
test('decodes arrow key escape sequences', () => {
|
|
38
|
+
expect(decodeInput('\x1b[A')[0].key.name).to.eq('up');
|
|
39
|
+
expect(decodeInput('\x1b[B')[0].key.name).to.eq('down');
|
|
40
|
+
expect(decodeInput('\x1b[C')[0].key.name).to.eq('right');
|
|
41
|
+
expect(decodeInput('\x1b[D')[0].key.name).to.eq('left');
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
test('prefers the longer escape sequence when one is a prefix of another', () => {
|
|
45
|
+
expect(decodeInput('\x1b[3~')[0].key.name).to.eq('delete');
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
test('decodes control chords', () => {
|
|
49
|
+
const [input] = decodeInput('\x03');
|
|
50
|
+
expect(input.key.name).to.eq('c');
|
|
51
|
+
expect(input.key.ctrl).to.be.true;
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
test('decodes alt-modified keys', () => {
|
|
55
|
+
const [input] = decodeInput('\x1bf');
|
|
56
|
+
expect(input.key.name).to.eq('f');
|
|
57
|
+
expect(input.key.meta).to.be.true;
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
test('expands a pasted chunk into one event per character', () => {
|
|
61
|
+
const inputs = decodeInput('paste');
|
|
62
|
+
expect(inputs).to.have.length(5);
|
|
63
|
+
expect(inputs.map((input) => Option.getOrNull(input.input)).join('')).to.eq('paste');
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
test('splits a mixed chunk into its constituent keys', () => {
|
|
67
|
+
const inputs = decodeInput('hi\r');
|
|
68
|
+
expect(inputs.map((input) => input.key.name)).to.deep.eq(['h', 'i', 'return']);
|
|
69
|
+
});
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
describe('isQuitInput', () => {
|
|
73
|
+
test('detects the interrupt chords', () => {
|
|
74
|
+
expect(isQuitInput(decodeInput('\x03')[0])).to.be.true;
|
|
75
|
+
expect(isQuitInput(decodeInput('\x04')[0])).to.be.true;
|
|
76
|
+
expect(isQuitInput(decodeInput('c')[0])).to.be.false;
|
|
77
|
+
});
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
describe('tokenize', () => {
|
|
81
|
+
test('splits on whitespace', () => {
|
|
82
|
+
expect(tokenize('space list')).to.deep.eq(['space', 'list']);
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
test('keeps quoted segments intact', () => {
|
|
86
|
+
expect(tokenize('space create "My Space"')).to.deep.eq(['space', 'create', 'My Space']);
|
|
87
|
+
expect(tokenize("greet 'ada lovelace'")).to.deep.eq(['greet', 'ada lovelace']);
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
test('returns nothing for blank input', () => {
|
|
91
|
+
expect(tokenize(' ')).to.deep.eq([]);
|
|
92
|
+
});
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
describe('rewriteHelpAliases', () => {
|
|
96
|
+
test('maps a bare help alias to the top-level flag', () => {
|
|
97
|
+
expect(rewriteHelpAliases(['help'])).to.deep.eq(['--help']);
|
|
98
|
+
expect(rewriteHelpAliases(['?'])).to.deep.eq(['--help']);
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
test('moves a help alias to a trailing flag', () => {
|
|
102
|
+
expect(rewriteHelpAliases(['help', 'space', 'list'])).to.deep.eq(['space', 'list', '--help']);
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
test('leaves other commands untouched', () => {
|
|
106
|
+
expect(rewriteHelpAliases(['space', 'list'])).to.deep.eq(['space', 'list']);
|
|
107
|
+
expect(rewriteHelpAliases([])).to.deep.eq([]);
|
|
108
|
+
});
|
|
109
|
+
});
|
package/src/cli/input.ts
ADDED
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2026 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import type * as Terminal from '@effect/platform/Terminal';
|
|
6
|
+
import * as Option from 'effect/Option';
|
|
7
|
+
|
|
8
|
+
type KeyOptions = {
|
|
9
|
+
input?: string;
|
|
10
|
+
ctrl?: boolean;
|
|
11
|
+
meta?: boolean;
|
|
12
|
+
shift?: boolean;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
const makeInput = (
|
|
16
|
+
name: string,
|
|
17
|
+
{ input, ctrl = false, meta = false, shift = false }: KeyOptions = {},
|
|
18
|
+
): Terminal.UserInput => ({
|
|
19
|
+
input: Option.fromNullable(input),
|
|
20
|
+
key: { name, ctrl, meta, shift },
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Control characters that name a key directly rather than resolving to a ctrl-modified letter.
|
|
25
|
+
*/
|
|
26
|
+
const CONTROL_KEYS: Record<string, string> = {
|
|
27
|
+
'\r': 'return',
|
|
28
|
+
'\n': 'return',
|
|
29
|
+
'\t': 'tab',
|
|
30
|
+
'\b': 'backspace',
|
|
31
|
+
'\x7f': 'backspace',
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* CSI/SS3 sequences (minus the leading escape) emitted by xterm for navigation keys.
|
|
36
|
+
* Ordered longest-first so `[3~` is matched before a shorter entry sharing its prefix.
|
|
37
|
+
*/
|
|
38
|
+
const ESCAPE_SEQUENCES: ReadonlyArray<readonly [string, string]> = [
|
|
39
|
+
['[1~', 'home'],
|
|
40
|
+
['[3~', 'delete'],
|
|
41
|
+
['[4~', 'end'],
|
|
42
|
+
['[5~', 'pageup'],
|
|
43
|
+
['[6~', 'pagedown'],
|
|
44
|
+
['[A', 'up'],
|
|
45
|
+
['[B', 'down'],
|
|
46
|
+
['[C', 'right'],
|
|
47
|
+
['[D', 'left'],
|
|
48
|
+
['[F', 'end'],
|
|
49
|
+
['[H', 'home'],
|
|
50
|
+
['OA', 'up'],
|
|
51
|
+
['OB', 'down'],
|
|
52
|
+
['OC', 'right'],
|
|
53
|
+
['OD', 'left'],
|
|
54
|
+
];
|
|
55
|
+
|
|
56
|
+
const matchEscapeSequence = (data: string, index: number): { name: string; length: number } | undefined => {
|
|
57
|
+
for (const [sequence, name] of ESCAPE_SEQUENCES) {
|
|
58
|
+
if (data.startsWith(sequence, index)) {
|
|
59
|
+
return { name, length: sequence.length };
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
return undefined;
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Decodes a chunk of xterm `onData` output into the keypress events that `@effect/platform`
|
|
68
|
+
* consumers (notably `@effect/cli` prompts) expect, using the same key names as Node's readline.
|
|
69
|
+
*
|
|
70
|
+
* Decoding the raw data stream rather than `onKey` DOM events is what makes paste work: a pasted
|
|
71
|
+
* string arrives as one chunk and expands to one event per character.
|
|
72
|
+
*/
|
|
73
|
+
export const decodeInput = (data: string): Terminal.UserInput[] => {
|
|
74
|
+
const inputs: Terminal.UserInput[] = [];
|
|
75
|
+
let index = 0;
|
|
76
|
+
|
|
77
|
+
while (index < data.length) {
|
|
78
|
+
const char = data[index];
|
|
79
|
+
|
|
80
|
+
if (char === '\x1b') {
|
|
81
|
+
const escape = matchEscapeSequence(data, index + 1);
|
|
82
|
+
if (escape) {
|
|
83
|
+
inputs.push(makeInput(escape.name));
|
|
84
|
+
index += escape.length + 1;
|
|
85
|
+
continue;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
// A lone escape followed by a printable character is how alt-modified keys arrive.
|
|
89
|
+
const next = data[index + 1];
|
|
90
|
+
if (next !== undefined && next >= ' ' && next !== '\x7f') {
|
|
91
|
+
inputs.push(makeInput(next.toLowerCase(), { input: next, meta: true }));
|
|
92
|
+
index += 2;
|
|
93
|
+
continue;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
inputs.push(makeInput('escape'));
|
|
97
|
+
index += 1;
|
|
98
|
+
continue;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
const control = CONTROL_KEYS[char];
|
|
102
|
+
if (control) {
|
|
103
|
+
inputs.push(makeInput(control));
|
|
104
|
+
index += 1;
|
|
105
|
+
continue;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
const code = char.charCodeAt(0);
|
|
109
|
+
if (code < 0x20) {
|
|
110
|
+
inputs.push(makeInput(String.fromCharCode(code + 0x60), { ctrl: true }));
|
|
111
|
+
index += 1;
|
|
112
|
+
continue;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
const lower = char.toLowerCase();
|
|
116
|
+
inputs.push(
|
|
117
|
+
makeInput(char === ' ' ? 'space' : lower, {
|
|
118
|
+
input: char,
|
|
119
|
+
shift: char !== lower,
|
|
120
|
+
}),
|
|
121
|
+
);
|
|
122
|
+
index += 1;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
return inputs;
|
|
126
|
+
};
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* Ctrl-C and Ctrl-D end an input stream, matching the platform-node default.
|
|
130
|
+
*/
|
|
131
|
+
export const isQuitInput = (input: Terminal.UserInput): boolean =>
|
|
132
|
+
input.key.ctrl && (input.key.name === 'c' || input.key.name === 'd');
|