brsh 2.8.1 → 3.1.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.
Files changed (44) hide show
  1. package/.github/workflows/release.yml +3 -1
  2. package/CHANGELOG.md +32 -0
  3. package/POSIX-CHECKLIST.md +3 -3
  4. package/Readme.md +138 -223
  5. package/bin/Readme.md +70 -24
  6. package/bin/brsh-shell.js +16 -131
  7. package/bin/exportFileSystem.js +4 -0
  8. package/bin/zsh-server.js +261 -0
  9. package/dist/brsh.css +1 -1
  10. package/dist/shell.es.js +1020 -648
  11. package/dist/shell.es.js.map +1 -1
  12. package/dist/shell.min.js +34 -19
  13. package/dist/shell.min.js.map +1 -1
  14. package/dist/terminal.cjs.js +77 -100
  15. package/dist/terminal.cjs.js.map +1 -1
  16. package/dist/terminal.es.js +1382 -603
  17. package/dist/terminal.es.js.map +1 -1
  18. package/dist/terminal.min.js +77 -100
  19. package/dist/terminal.min.js.map +1 -1
  20. package/dist/wbt.es.js +410 -238
  21. package/dist/wbt.es.js.map +1 -1
  22. package/dist/wbt.min.js +10 -8
  23. package/dist/wbt.min.js.map +1 -1
  24. package/index.js +472 -0
  25. package/lib/Command.js +30 -0
  26. package/lib/InteractiveCommand.js +28 -0
  27. package/lib/Readme.md +111 -12
  28. package/lib/ReplCommand.js +15 -0
  29. package/lib/Terminal.js +769 -510
  30. package/lib/TtyStream.js +181 -0
  31. package/lib/WasmCommand.js +26 -5
  32. package/lib/builtins/cat.js +2 -2
  33. package/lib/builtins/grep.js +2 -2
  34. package/lib/local/clear.js +3 -0
  35. package/lib/terminal/DomRenderer.js +325 -0
  36. package/lib/terminal/PtyBridge.js +169 -0
  37. package/lib/terminal/ScreenBuffer.js +594 -0
  38. package/lib/terminal/VtParser.js +728 -0
  39. package/lib/wbt/index.js +3 -2
  40. package/lib/wbt/registry.js +15 -1
  41. package/package.json +4 -2
  42. package/styles/terminal.css +33 -4
  43. package/test/terminal.test.js +762 -40
  44. package/test/wbt.test.js +92 -0
package/lib/Readme.md CHANGED
@@ -1,21 +1,120 @@
1
- # Extending Command
1
+ # Writing Custom Commands (`lib/`)
2
+
3
+ `brsh` exposes four command base classes on the `Shell` constructor so you can write everything from simple one-shot utilities to full-screen interactive TUI applications and WebAssembly tools:
4
+
5
+ * `Shell.Command` — Standard CLI command (output lines, exit codes, streaming output, piped stdin, optional filesystem access)
6
+ * `Shell.InteractiveCommand` — Full-screen interactive TUI application (alternate screen buffer `?1049h`, raw key input, terminal dimensions)
7
+ * `Shell.ReplCommand` — Hybrid CLI + interactive full-screen REPL command
8
+ * `Shell.WasmCommand` / `Shell.createWasmCommand` — WASI / Emscripten / ESM WebAssembly command wrapper
9
+
10
+ ---
11
+
12
+ ## 1. Standard Commands (`Shell.Command`)
13
+
14
+ Extend `Shell.Command` and register your class via `shell.context.setCommand(MyCommand)` (or place it in `/bin` inside your virtual filesystem):
2
15
 
3
- to create a new command you will need to extend Command, you can either access command as a Static property of Shell:
4
16
  ```js
5
- class Test extends Shell.Command {
6
- ...
17
+ const Shell = require('brsh');
18
+
19
+ class GreetCommand extends Shell.Command {
20
+ static get name() { return 'greet'; }
21
+ get name() { return 'greet'; }
22
+
23
+ // Set to true if the command needs access to `this.fs` (sandboxed filesystem)
24
+ get requiresFilesystem() { return false; }
25
+
26
+ async run() {
27
+ const target = this.arguments[0] || 'world';
28
+ this.stdOut = `Hello, ${target}!`;
29
+ this.exitCode = 0;
30
+ }
7
31
  }
32
+
33
+ shell.context.setCommand(GreetCommand);
8
34
  ```
9
-
10
- or Require Command directly:
35
+
36
+ ### Properties & Methods on `Command`
37
+
38
+ | Property / Method | Description |
39
+ | --- | --- |
40
+ | `this.arguments` | `Array<String>` of parsed command-line arguments |
41
+ | `this.context` | Read-only shell context (`getVar(name)`, `getAlias(name)`, `getCommand(name)`) |
42
+ | `this.fs` | Sandboxed filesystem instance (only populated when `get requiresFilesystem() { return true; }`) |
43
+ | `this.stdin` | Piped input string when invoked in a pipeline (`cmd1 \| mycmd`), or `this.stdinStream` (`TtyStream`) |
44
+ | `this.stdout` / `this.stderr` | `TtyStream` instances (`columns`, `rows`, `write(chunk)`) |
45
+ | `this.stdOut = line` | Appends a line to standard output |
46
+ | `this.stdErr = line` | Appends a line to standard error |
47
+ | `this.exitCode = code` | Sets the numeric exit code (defaults to `0`, or `127` on unhandled rejection) |
48
+ | `this.flush()` | Immediately flushes any queued `stdOut` / `stdErr` lines to the shell before `run()` finishes (also supports `\r` in-place progress lines) |
49
+ | `this.captureInput` | Return `true` to receive live user keystrokes via `onInput(key)` while the command is running (e.g. `read`) |
50
+
51
+ ### Sandboxed Filesystem API (`this.fs`)
52
+
53
+ When `get requiresFilesystem() { return true; }` is set on a command, `this.fs` provides:
54
+ * `getFileByPath(path)` / `setFileByPath(path, content, createDir)` / `deleteFileByPath(path)`
55
+ * `exists(path)` / `stat(path)` / `getSize(path)` / `absolutePath(path)`
56
+ * `getMode(path)` / `setMode(path, mode)` / `isReadable(path)` / `isWritable(path)` / `isExecutable(path)`
57
+ * `isSymlink(path)` / `readSymlink(path)` / `createSymlink(target, linkPath)`
58
+
59
+ ---
60
+
61
+ ## 2. Full-Screen Interactive Apps (`Shell.InteractiveCommand`)
62
+
63
+ Extend `Shell.InteractiveCommand` for full-screen terminal applications (like `pico`). `InteractiveCommand` automatically switches the terminal into the alternate screen buffer (`\x1b[?1049h`) and raw input mode on start, and restores the primary screen on exit.
64
+
11
65
  ```js
12
- const Command = require('shell.js/lib/Command');
13
-
14
- class Test extends Command {
15
- ...
66
+ class MyEditor extends Shell.InteractiveCommand {
67
+ static get name() { return 'myeditor'; }
68
+ get name() { return 'myeditor'; }
69
+ get requiresFilesystem() { return true; }
70
+
71
+ run() {
72
+ return new Promise((resolve) => {
73
+ this._exit = resolve;
74
+ this.render();
75
+ });
76
+ }
77
+
78
+ onInput(key) {
79
+ if (key === '^X' || key === 'Escape') {
80
+ this._exit();
81
+ return;
82
+ }
83
+ this.render(key);
84
+ }
85
+
86
+ render(lastKey = '') {
87
+ const { cols, rows } = this.terminalSize;
88
+ this.stdOut = `\x1b[2J\x1b[HLast key: ${lastKey} (${cols}x${rows})\nPress Ctrl+X to exit.`;
89
+ this.flush();
90
+ }
16
91
  }
17
92
  ```
18
93
 
19
- for some examples see the [implementation for piratemedia.tv](https://github.com/eliotstocker/piratemedia.tv/tree/master/fs/bin)
94
+ `onInput(key)` receives:
95
+ * Printable characters: `'a'`, `'B'`, `'1'`, `' '`, etc.
96
+ * Named navigation keys: `'ArrowUp'`, `'ArrowDown'`, `'ArrowLeft'`, `'ArrowRight'`, `'Enter'`, `'Backspace'`, `'Delete'`, `'Escape'`, `'Home'`, `'End'`, `'PageUp'`, `'PageDown'`, `'Tab'`
97
+ * Control combinations: `'^A'` through `'^Z'`
98
+
99
+ See [demo/pico.js](../demo/pico.js) for a complete implementation.
100
+
101
+ ---
102
+
103
+ ## 3. Interactive REPL Commands (`Shell.ReplCommand`)
20
104
 
21
- more info coming soon...
105
+ Extend `Shell.ReplCommand` when a command supports both non-interactive CLI invocations (`runNonInteractive()`) and a full-screen interactive REPL with history, scrolling, and multi-line input (either by overriding `shouldBeInteractive()` / `initRepl()` / `evaluateReplLine(line)` or by calling `this.startRepl(opts)` dynamically).
106
+
107
+ ---
108
+
109
+ ## 4. WebAssembly Commands (`Shell.createWasmCommand`)
110
+
111
+ Wrap any WASI `.wasm` binary or ESM/Emscripten module into a `brsh` command:
112
+
113
+ ```js
114
+ const MyWasmTool = Shell.createWasmCommand({
115
+ name: 'mytool',
116
+ wasmUrl: 'https://example.com/mytool.wasm'
117
+ });
118
+
119
+ shell.context.setCommand(MyWasmTool);
120
+ ```
@@ -76,6 +76,9 @@ class ReplCommand extends InteractiveCommand {
76
76
  this._dynamicReplOpts = opts;
77
77
  this._dynamicReplBuffer = '';
78
78
  this._dynamicReplMultiline = false;
79
+ if (typeof this._enterAltScreen === 'function') {
80
+ this._enterAltScreen();
81
+ }
79
82
  return this._launchInteractiveReplLoop();
80
83
  }
81
84
 
@@ -244,6 +247,18 @@ class ReplCommand extends InteractiveCommand {
244
247
  return;
245
248
  }
246
249
 
250
+ if (key === '^W') {
251
+ let pos = this._replCursor;
252
+ while (pos > 0 && this._replInput[pos - 1] === ' ') pos--;
253
+ while (pos > 0 && this._replInput[pos - 1] !== ' ') pos--;
254
+ this._replInput =
255
+ this._replInput.slice(0, pos) +
256
+ this._replInput.slice(this._replCursor);
257
+ this._replCursor = pos;
258
+ this._renderRepl();
259
+ return;
260
+ }
261
+
247
262
  if (key === 'Delete') {
248
263
  if (this._replCursor < this._replInput.length) {
249
264
  this._replInput =