bunmicro 0.8.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 (237) hide show
  1. package/LICENSE +22 -0
  2. package/PORTING.md +34 -0
  3. package/README.md +153 -0
  4. package/bmi +5 -0
  5. package/bun.lock +17 -0
  6. package/bunmicro +5 -0
  7. package/hlw.md +5 -0
  8. package/package.json +18 -0
  9. package/runtime/colorschemes/atom-dark.micro +33 -0
  10. package/runtime/colorschemes/bubblegum.micro +31 -0
  11. package/runtime/colorschemes/cmc-16.micro +47 -0
  12. package/runtime/colorschemes/cmc-tc.micro +43 -0
  13. package/runtime/colorschemes/darcula.micro +34 -0
  14. package/runtime/colorschemes/default.micro +1 -0
  15. package/runtime/colorschemes/dracula-tc.micro +49 -0
  16. package/runtime/colorschemes/dukedark-tc.micro +38 -0
  17. package/runtime/colorschemes/dukelight-tc.micro +38 -0
  18. package/runtime/colorschemes/dukeubuntu-tc.micro +38 -0
  19. package/runtime/colorschemes/geany.micro +29 -0
  20. package/runtime/colorschemes/gotham.micro +29 -0
  21. package/runtime/colorschemes/gruvbox-tc.micro +29 -0
  22. package/runtime/colorschemes/gruvbox.micro +26 -0
  23. package/runtime/colorschemes/material-tc.micro +36 -0
  24. package/runtime/colorschemes/monokai-dark.micro +28 -0
  25. package/runtime/colorschemes/monokai.micro +34 -0
  26. package/runtime/colorschemes/one-dark.micro +39 -0
  27. package/runtime/colorschemes/railscast.micro +37 -0
  28. package/runtime/colorschemes/simple.micro +33 -0
  29. package/runtime/colorschemes/solarized-tc.micro +31 -0
  30. package/runtime/colorschemes/solarized.micro +30 -0
  31. package/runtime/colorschemes/sunny-day.micro +29 -0
  32. package/runtime/colorschemes/twilight.micro +40 -0
  33. package/runtime/colorschemes/zenburn.micro +30 -0
  34. package/runtime/help/actions.md +161 -0
  35. package/runtime/help/colors.md +421 -0
  36. package/runtime/help/commands.md +161 -0
  37. package/runtime/help/copypaste.md +149 -0
  38. package/runtime/help/defaultkeys.md +141 -0
  39. package/runtime/help/help.md +63 -0
  40. package/runtime/help/keybindings.md +760 -0
  41. package/runtime/help/linter.md +90 -0
  42. package/runtime/help/options.md +701 -0
  43. package/runtime/help/plugins.md +544 -0
  44. package/runtime/help/tutorial.md +112 -0
  45. package/runtime/jsplugins/chapter/chapter.js +108 -0
  46. package/runtime/jsplugins/diff/diff.js +46 -0
  47. package/runtime/jsplugins/example/example.js +108 -0
  48. package/runtime/jsplugins/linter/linter.js +281 -0
  49. package/runtime/plugins/autoclose/autoclose.lua +75 -0
  50. package/runtime/plugins/ftoptions/ftoptions.lua +17 -0
  51. package/runtime/plugins/literate/README.md +5 -0
  52. package/runtime/plugins/literate/literate.lua +55 -0
  53. package/runtime/plugins/status/help/status.md +21 -0
  54. package/runtime/plugins/status/status.lua +62 -0
  55. package/runtime/syntax/LICENSE +22 -0
  56. package/runtime/syntax/PowerShell.yaml +128 -0
  57. package/runtime/syntax/README.md +63 -0
  58. package/runtime/syntax/ada.yaml +43 -0
  59. package/runtime/syntax/apacheconf.yaml +59 -0
  60. package/runtime/syntax/arduino.yaml +101 -0
  61. package/runtime/syntax/asciidoc.yaml +51 -0
  62. package/runtime/syntax/asm.yaml +123 -0
  63. package/runtime/syntax/ats.yaml +99 -0
  64. package/runtime/syntax/awk.yaml +44 -0
  65. package/runtime/syntax/b.yaml +87 -0
  66. package/runtime/syntax/bat.yaml +57 -0
  67. package/runtime/syntax/c.yaml +60 -0
  68. package/runtime/syntax/caddyfile.yaml +23 -0
  69. package/runtime/syntax/cake.yaml +7 -0
  70. package/runtime/syntax/clojure.yaml +38 -0
  71. package/runtime/syntax/cmake.yaml +42 -0
  72. package/runtime/syntax/coffeescript.yaml +56 -0
  73. package/runtime/syntax/colortest.yaml +19 -0
  74. package/runtime/syntax/conky.yaml +17 -0
  75. package/runtime/syntax/cpp.yaml +91 -0
  76. package/runtime/syntax/crontab.yaml +36 -0
  77. package/runtime/syntax/crystal.yaml +72 -0
  78. package/runtime/syntax/csharp.yaml +52 -0
  79. package/runtime/syntax/css.yaml +44 -0
  80. package/runtime/syntax/csx.yaml +8 -0
  81. package/runtime/syntax/cuda.yaml +68 -0
  82. package/runtime/syntax/cython.yaml +52 -0
  83. package/runtime/syntax/d.yaml +121 -0
  84. package/runtime/syntax/dart.yaml +46 -0
  85. package/runtime/syntax/default.yaml +10 -0
  86. package/runtime/syntax/dockerfile.yaml +36 -0
  87. package/runtime/syntax/dot.yaml +29 -0
  88. package/runtime/syntax/elixir.yaml +30 -0
  89. package/runtime/syntax/elm.yaml +38 -0
  90. package/runtime/syntax/erb.yaml +42 -0
  91. package/runtime/syntax/erlang.yaml +45 -0
  92. package/runtime/syntax/fish.yaml +48 -0
  93. package/runtime/syntax/forth.yaml +34 -0
  94. package/runtime/syntax/fortran.yaml +64 -0
  95. package/runtime/syntax/freebsd-kernel.yaml +14 -0
  96. package/runtime/syntax/fsharp.yaml +48 -0
  97. package/runtime/syntax/gdscript.yaml +61 -0
  98. package/runtime/syntax/gemini.yaml +19 -0
  99. package/runtime/syntax/gentoo-ebuild.yaml +48 -0
  100. package/runtime/syntax/gentoo-etc-portage.yaml +23 -0
  101. package/runtime/syntax/git-commit.yaml +35 -0
  102. package/runtime/syntax/git-config.yaml +14 -0
  103. package/runtime/syntax/git-rebase-todo.yaml +19 -0
  104. package/runtime/syntax/gleam.yaml +69 -0
  105. package/runtime/syntax/glsl.yaml +26 -0
  106. package/runtime/syntax/gnuplot.yaml +15 -0
  107. package/runtime/syntax/go.yaml +62 -0
  108. package/runtime/syntax/godoc.yaml +17 -0
  109. package/runtime/syntax/golo.yaml +73 -0
  110. package/runtime/syntax/gomod.yaml +31 -0
  111. package/runtime/syntax/graphql.yaml +47 -0
  112. package/runtime/syntax/groff.yaml +30 -0
  113. package/runtime/syntax/groovy.yaml +111 -0
  114. package/runtime/syntax/haml.yaml +16 -0
  115. package/runtime/syntax/hare.yaml +52 -0
  116. package/runtime/syntax/haskell.yaml +52 -0
  117. package/runtime/syntax/hc.yaml +52 -0
  118. package/runtime/syntax/html.yaml +70 -0
  119. package/runtime/syntax/html4.yaml +25 -0
  120. package/runtime/syntax/html5.yaml +25 -0
  121. package/runtime/syntax/ini.yaml +23 -0
  122. package/runtime/syntax/inputrc.yaml +14 -0
  123. package/runtime/syntax/java.yaml +37 -0
  124. package/runtime/syntax/javascript.yaml +76 -0
  125. package/runtime/syntax/jinja2.yaml +19 -0
  126. package/runtime/syntax/json.yaml +39 -0
  127. package/runtime/syntax/jsonnet.yaml +92 -0
  128. package/runtime/syntax/julia.yaml +57 -0
  129. package/runtime/syntax/justfile.yaml +40 -0
  130. package/runtime/syntax/keymap.yaml +27 -0
  131. package/runtime/syntax/kickstart.yaml +16 -0
  132. package/runtime/syntax/kotlin.yaml +66 -0
  133. package/runtime/syntax/kvlang.yaml +67 -0
  134. package/runtime/syntax/ledger.yaml +14 -0
  135. package/runtime/syntax/lfe.yaml +17 -0
  136. package/runtime/syntax/lilypond.yaml +26 -0
  137. package/runtime/syntax/lisp.yaml +17 -0
  138. package/runtime/syntax/log.yaml +92 -0
  139. package/runtime/syntax/lua.yaml +111 -0
  140. package/runtime/syntax/mail.yaml +25 -0
  141. package/runtime/syntax/makefile.yaml +38 -0
  142. package/runtime/syntax/man.yaml +12 -0
  143. package/runtime/syntax/markdown.yaml +49 -0
  144. package/runtime/syntax/mc.yaml +23 -0
  145. package/runtime/syntax/meson.yaml +51 -0
  146. package/runtime/syntax/micro.yaml +34 -0
  147. package/runtime/syntax/mpdconf.yaml +13 -0
  148. package/runtime/syntax/msbuild.yaml +6 -0
  149. package/runtime/syntax/nanorc.yaml +16 -0
  150. package/runtime/syntax/nftables.yaml +30 -0
  151. package/runtime/syntax/nginx.yaml +22 -0
  152. package/runtime/syntax/nim.yaml +27 -0
  153. package/runtime/syntax/nix.yaml +32 -0
  154. package/runtime/syntax/nu.yaml +114 -0
  155. package/runtime/syntax/objc.yaml +60 -0
  156. package/runtime/syntax/ocaml.yaml +43 -0
  157. package/runtime/syntax/octave.yaml +83 -0
  158. package/runtime/syntax/odin.yaml +64 -0
  159. package/runtime/syntax/pascal.yaml +45 -0
  160. package/runtime/syntax/patch.yaml +14 -0
  161. package/runtime/syntax/peg.yaml +16 -0
  162. package/runtime/syntax/perl.yaml +58 -0
  163. package/runtime/syntax/php.yaml +60 -0
  164. package/runtime/syntax/pkg-config.yaml +12 -0
  165. package/runtime/syntax/po.yaml +12 -0
  166. package/runtime/syntax/pony.yaml +37 -0
  167. package/runtime/syntax/pov.yaml +21 -0
  168. package/runtime/syntax/privoxy-action.yaml +14 -0
  169. package/runtime/syntax/privoxy-config.yaml +10 -0
  170. package/runtime/syntax/privoxy-filter.yaml +12 -0
  171. package/runtime/syntax/proto.yaml +40 -0
  172. package/runtime/syntax/prql.yaml +84 -0
  173. package/runtime/syntax/puppet.yaml +22 -0
  174. package/runtime/syntax/python2.yaml +60 -0
  175. package/runtime/syntax/python3.yaml +62 -0
  176. package/runtime/syntax/r.yaml +32 -0
  177. package/runtime/syntax/raku.yaml +42 -0
  178. package/runtime/syntax/reST.yaml +18 -0
  179. package/runtime/syntax/renpy.yaml +15 -0
  180. package/runtime/syntax/rpmspec.yaml +43 -0
  181. package/runtime/syntax/ruby.yaml +73 -0
  182. package/runtime/syntax/rust.yaml +78 -0
  183. package/runtime/syntax/sage.yaml +60 -0
  184. package/runtime/syntax/scad.yaml +53 -0
  185. package/runtime/syntax/scala.yaml +33 -0
  186. package/runtime/syntax/sed.yaml +13 -0
  187. package/runtime/syntax/sh.yaml +69 -0
  188. package/runtime/syntax/sls.yaml +15 -0
  189. package/runtime/syntax/smalltalk.yaml +55 -0
  190. package/runtime/syntax/solidity.yaml +41 -0
  191. package/runtime/syntax/sql.yaml +35 -0
  192. package/runtime/syntax/stata.yaml +67 -0
  193. package/runtime/syntax/svelte.yaml +27 -0
  194. package/runtime/syntax/swift.yaml +103 -0
  195. package/runtime/syntax/systemd.yaml +16 -0
  196. package/runtime/syntax/tcl.yaml +18 -0
  197. package/runtime/syntax/terraform.yaml +87 -0
  198. package/runtime/syntax/tex.yaml +32 -0
  199. package/runtime/syntax/toml.yaml +56 -0
  200. package/runtime/syntax/twig.yaml +55 -0
  201. package/runtime/syntax/typescript.yaml +49 -0
  202. package/runtime/syntax/v.yaml +80 -0
  203. package/runtime/syntax/vala.yaml +26 -0
  204. package/runtime/syntax/verilog.yaml +60 -0
  205. package/runtime/syntax/vhdl.yaml +37 -0
  206. package/runtime/syntax/vi.yaml +31 -0
  207. package/runtime/syntax/vue.yaml +64 -0
  208. package/runtime/syntax/xml.yaml +37 -0
  209. package/runtime/syntax/xresources.yaml +14 -0
  210. package/runtime/syntax/yaml.yaml +34 -0
  211. package/runtime/syntax/yum.yaml +12 -0
  212. package/runtime/syntax/zig.yaml +52 -0
  213. package/runtime/syntax/zscript.yaml +72 -0
  214. package/runtime/syntax/zsh.yaml +52 -0
  215. package/src/buffer/buffer.js +126 -0
  216. package/src/buffer/loc.js +38 -0
  217. package/src/buffer/message.js +29 -0
  218. package/src/config/colorscheme.js +109 -0
  219. package/src/config/config.js +118 -0
  220. package/src/config/defaults.js +102 -0
  221. package/src/display/ansi-style.js +60 -0
  222. package/src/highlight/highlighter.js +237 -0
  223. package/src/highlight/parser.js +137 -0
  224. package/src/index.js +5942 -0
  225. package/src/lua/engine.js +38 -0
  226. package/src/platform/archive.js +50 -0
  227. package/src/platform/clipboard.js +160 -0
  228. package/src/platform/commands.js +140 -0
  229. package/src/plugins/js-bridge.js +902 -0
  230. package/src/plugins/manager.js +619 -0
  231. package/src/runtime/registry.js +89 -0
  232. package/src/screen/cell-buffer.js +81 -0
  233. package/src/screen/events.js +263 -0
  234. package/src/screen/screen.js +118 -0
  235. package/src/screen/vt100.js +391 -0
  236. package/src/shell/shell.js +70 -0
  237. package/todo.txt +359 -0
@@ -0,0 +1,391 @@
1
+ // Minimal VT100/ANSI terminal emulator for the terminal pane.
2
+ // Maintains a cell grid and parses common escape sequences.
3
+
4
+ const ANSI_COLORS = [
5
+ "black", "red", "green", "yellow", "blue", "magenta", "cyan", "white",
6
+ "brightblack", "brightred", "brightgreen", "brightyellow",
7
+ "brightblue", "brightmagenta", "brightcyan", "brightwhite",
8
+ ];
9
+
10
+ function blankCell() {
11
+ return { ch: " ", fg: "default", bg: "default", bold: false, italic: false, underline: false, reverse: false };
12
+ }
13
+
14
+ function findCSIEnd(str, start) {
15
+ for (let i = start; i < str.length; i++) {
16
+ const c = str.charCodeAt(i);
17
+ if (c >= 0x40 && c <= 0x7E) return i; // final byte
18
+ if (c < 0x20 && c !== 0x1b) return -1; // unexpected control
19
+ }
20
+ return -1; // incomplete
21
+ }
22
+
23
+ function toHex2(n) {
24
+ return ((n ?? 0) & 0xFF).toString(16).padStart(2, "0");
25
+ }
26
+
27
+ export class VT100 {
28
+ constructor(cols, rows) {
29
+ this.cols = Math.max(1, cols);
30
+ this.rows = Math.max(1, rows);
31
+ this.cells = [];
32
+ this.cx = 0;
33
+ this.cy = 0;
34
+ this.savedCursor = { x: 0, y: 0 };
35
+ this.scrollTop = 0;
36
+ this.scrollBottom = this.rows - 1;
37
+ this.sgr = { fg: "default", bg: "default", bold: false, italic: false, underline: false, reverse: false };
38
+ this.pending = "";
39
+ // scrollback
40
+ this.scrollback = []; // oldest first, each entry is a cols-length cell array
41
+ this.maxScrollback = 500;
42
+ this.scrollOffset = 0; // 0 = live view; n = n rows scrolled back into history
43
+ // mouse reporting: set by the application via ?1000h / ?1002h / ?1003h
44
+ this.mouseMode = false;
45
+ this._initCells();
46
+ }
47
+
48
+ _initCells() {
49
+ this.cells = Array.from({ length: this.cols * this.rows }, () => blankCell());
50
+ }
51
+
52
+ _idx(x, y) { return y * this.cols + x; }
53
+
54
+ _cell(x, y) {
55
+ if (x < 0 || x >= this.cols || y < 0 || y >= this.rows) return null;
56
+ return this.cells[this._idx(x, y)];
57
+ }
58
+
59
+ _setCell(x, y, ch) {
60
+ const cell = this._cell(x, y);
61
+ if (!cell) return;
62
+ cell.ch = ch;
63
+ cell.fg = this.sgr.fg;
64
+ cell.bg = this.sgr.bg;
65
+ cell.bold = this.sgr.bold;
66
+ cell.italic = this.sgr.italic;
67
+ cell.underline = this.sgr.underline;
68
+ cell.reverse = this.sgr.reverse;
69
+ }
70
+
71
+ _clearCell(x, y) {
72
+ const cell = this._cell(x, y);
73
+ if (cell) Object.assign(cell, blankCell());
74
+ }
75
+
76
+ _clearLineFrom(x, y) {
77
+ for (let i = x; i < this.cols; i++) this._clearCell(i, y);
78
+ }
79
+
80
+ _clearLineTo(x, y) {
81
+ for (let i = 0; i <= x; i++) this._clearCell(i, y);
82
+ }
83
+
84
+ _clearLine(y) {
85
+ for (let i = 0; i < this.cols; i++) this._clearCell(i, y);
86
+ }
87
+
88
+ _scrollUp(n = 1) {
89
+ for (let count = 0; count < n; count++) {
90
+ // Save the line scrolling off the top into scrollback history
91
+ const row = [];
92
+ for (let x = 0; x < this.cols; x++) row.push({ ...this.cells[this._idx(x, this.scrollTop)] });
93
+ this.scrollback.push(row);
94
+ if (this.scrollback.length > this.maxScrollback) this.scrollback.shift();
95
+
96
+ for (let y = this.scrollTop; y < this.scrollBottom; y++) {
97
+ for (let x = 0; x < this.cols; x++) {
98
+ this.cells[this._idx(x, y)] = { ...this.cells[this._idx(x, y + 1)] };
99
+ }
100
+ }
101
+ this._clearLine(this.scrollBottom);
102
+ }
103
+ }
104
+
105
+ // Adjust scrollback view. delta > 0 = scroll back (older), delta < 0 = scroll forward (newer).
106
+ scroll(delta) {
107
+ this.scrollOffset = Math.max(0, Math.min(this.scrollback.length, this.scrollOffset + delta));
108
+ }
109
+
110
+ _scrollDown(n = 1, fromRow = this.scrollTop) {
111
+ for (let count = 0; count < n; count++) {
112
+ for (let y = Math.min(this.scrollBottom, this.rows - 2); y >= fromRow; y--) {
113
+ for (let x = 0; x < this.cols; x++) {
114
+ this.cells[this._idx(x, y + 1)] = { ...this.cells[this._idx(x, y)] };
115
+ }
116
+ }
117
+ this._clearLine(fromRow);
118
+ }
119
+ }
120
+
121
+ _lineFeed() {
122
+ if (this.cy < this.scrollBottom) {
123
+ this.cy++;
124
+ } else {
125
+ this._scrollUp(1);
126
+ }
127
+ }
128
+
129
+ // Feed a chunk of terminal output. Returns array of response strings to send back.
130
+ feed(text) {
131
+ const data = this.pending + text;
132
+ this.pending = "";
133
+ const responses = [];
134
+ let i = 0;
135
+
136
+ while (i < data.length) {
137
+ const ch = data[i];
138
+ const code = data.charCodeAt(i);
139
+
140
+ if (ch === "\x1b") {
141
+ if (i + 1 >= data.length) { this.pending = data.slice(i); break; }
142
+ const next = data[i + 1];
143
+
144
+ if (next === "[") {
145
+ // CSI
146
+ const end = findCSIEnd(data, i + 2);
147
+ if (end < 0) { this.pending = data.slice(i); break; }
148
+ const params = data.slice(i + 2, end);
149
+ const final = data[end];
150
+ const resp = this._handleCSI(params, final);
151
+ if (resp) responses.push(resp);
152
+ i = end + 1;
153
+
154
+ } else if (next === "]") {
155
+ // OSC — find ST (\x1b\\) or BEL (\x07)
156
+ const bel = data.indexOf("\x07", i + 2);
157
+ const st = data.indexOf("\x1b\\", i + 2);
158
+ if (bel < 0 && st < 0) { this.pending = data.slice(i); break; }
159
+ const endOSC = (bel >= 0 && (st < 0 || bel < st)) ? bel : st + 1;
160
+ i = endOSC + 1;
161
+
162
+ } else if (next === "7") {
163
+ this.savedCursor = { x: this.cx, y: this.cy }; i += 2;
164
+ } else if (next === "8") {
165
+ this.cx = this.savedCursor.x; this.cy = this.savedCursor.y; i += 2;
166
+ } else if (next === "M") {
167
+ // Reverse index
168
+ if (this.cy === this.scrollTop) this._scrollDown(1);
169
+ else this.cy = Math.max(0, this.cy - 1);
170
+ i += 2;
171
+ } else if (next === "(" || next === ")" || next === "*" || next === "+") {
172
+ i += 3; // charset designation, skip designator
173
+ } else if (next === "c") {
174
+ // Full reset
175
+ this._initCells(); this.cx = this.cy = 0;
176
+ this.sgr = { fg: "default", bg: "default", bold: false, italic: false, underline: false, reverse: false };
177
+ this.scrollTop = 0; this.scrollBottom = this.rows - 1;
178
+ i += 2;
179
+ } else if (next === "=") {
180
+ i += 2; // application keypad mode, ignore
181
+ } else if (next === ">") {
182
+ i += 2; // normal keypad mode, ignore
183
+ } else {
184
+ i += 2; // unknown two-char escape
185
+ }
186
+
187
+ } else if (ch === "\r") {
188
+ this.cx = 0; i++;
189
+ } else if (ch === "\n" || ch === "\x0b" || ch === "\x0c") {
190
+ this._lineFeed(); i++;
191
+ } else if (ch === "\b") {
192
+ if (this.cx > 0) this.cx--; i++;
193
+ } else if (ch === "\t") {
194
+ this.cx = Math.min(this.cols - 1, (Math.floor(this.cx / 8) + 1) * 8); i++;
195
+ } else if (ch === "\x07") {
196
+ i++; // Bell: ignore
197
+ } else if (ch === "\x0e" || ch === "\x0f") {
198
+ i++; // SO/SI charset switch, ignore
199
+ } else if (code >= 0x20) {
200
+ // Printable
201
+ this._setCell(this.cx, this.cy, ch);
202
+ this.cx++;
203
+ if (this.cx >= this.cols) {
204
+ this.cx = 0; this._lineFeed();
205
+ }
206
+ i++;
207
+ } else {
208
+ i++; // other control: skip
209
+ }
210
+ }
211
+
212
+ return responses;
213
+ }
214
+
215
+ _handleCSI(params, final) {
216
+ // Check for private mode prefix
217
+ const isPrivate = params.startsWith("?");
218
+ const raw = isPrivate ? params.slice(1) : params;
219
+ const parts = raw === "" ? [0] : raw.split(";").map(p => p === "" ? 0 : Number(p));
220
+ const p1 = parts[0] ?? 0;
221
+ const p2 = parts[1] ?? 0;
222
+
223
+ switch (final) {
224
+ case "A": this.cy = Math.max(this.scrollTop, this.cy - Math.max(1, p1)); break;
225
+ case "B": this.cy = Math.min(this.scrollBottom, this.cy + Math.max(1, p1)); break;
226
+ case "C": this.cx = Math.min(this.cols - 1, this.cx + Math.max(1, p1)); break;
227
+ case "D": this.cx = Math.max(0, this.cx - Math.max(1, p1)); break;
228
+ case "E": this.cy = Math.min(this.rows - 1, this.cy + Math.max(1, p1)); this.cx = 0; break;
229
+ case "F": this.cy = Math.max(0, this.cy - Math.max(1, p1)); this.cx = 0; break;
230
+ case "G": this.cx = Math.min(this.cols - 1, Math.max(0, Math.max(1, p1) - 1)); break;
231
+ case "H":
232
+ case "f":
233
+ this.cy = Math.min(this.rows - 1, Math.max(0, Math.max(1, p1) - 1));
234
+ this.cx = Math.min(this.cols - 1, Math.max(0, Math.max(1, p2) - 1));
235
+ break;
236
+ case "J":
237
+ if (p1 === 0) {
238
+ this._clearLineFrom(this.cx, this.cy);
239
+ for (let y = this.cy + 1; y < this.rows; y++) this._clearLine(y);
240
+ } else if (p1 === 1) {
241
+ for (let y = 0; y < this.cy; y++) this._clearLine(y);
242
+ this._clearLineTo(this.cx, this.cy);
243
+ } else if (p1 === 2 || p1 === 3) {
244
+ this._initCells(); this.cx = this.cy = 0;
245
+ }
246
+ break;
247
+ case "K":
248
+ if (p1 === 0) this._clearLineFrom(this.cx, this.cy);
249
+ else if (p1 === 1) this._clearLineTo(this.cx, this.cy);
250
+ else if (p1 === 2) this._clearLine(this.cy);
251
+ break;
252
+ case "L": this._scrollDown(Math.max(1, p1), this.cy); break;
253
+ case "M": {
254
+ const n = Math.max(1, p1);
255
+ for (let i = 0; i < n; i++) {
256
+ for (let y = this.cy; y < this.scrollBottom; y++) {
257
+ for (let x = 0; x < this.cols; x++) {
258
+ this.cells[this._idx(x, y)] = { ...this.cells[this._idx(x, y + 1)] };
259
+ }
260
+ }
261
+ this._clearLine(this.scrollBottom);
262
+ }
263
+ break;
264
+ }
265
+ case "P": {
266
+ const n = Math.max(1, p1);
267
+ for (let x = this.cx; x < this.cols - n; x++)
268
+ this.cells[this._idx(x, this.cy)] = { ...this.cells[this._idx(x + n, this.cy)] };
269
+ for (let x = this.cols - n; x < this.cols; x++) this._clearCell(x, this.cy);
270
+ break;
271
+ }
272
+ case "S": this._scrollUp(Math.max(1, p1)); break;
273
+ case "T": this._scrollDown(Math.max(1, p1)); break;
274
+ case "X": {
275
+ const n = Math.max(1, p1);
276
+ for (let x = this.cx; x < Math.min(this.cols, this.cx + n); x++) this._clearCell(x, this.cy);
277
+ break;
278
+ }
279
+ case "d": this.cy = Math.min(this.rows - 1, Math.max(0, Math.max(1, p1) - 1)); break;
280
+ case "m": this._handleSGR(parts); break;
281
+ case "n":
282
+ if (p1 === 6) return `\x1b[${this.cy + 1};${this.cx + 1}R`; // CPR
283
+ if (p1 === 5) return "\x1b[0n"; // device status OK
284
+ break;
285
+ case "r":
286
+ this.scrollTop = Math.max(0, Math.max(1, p1) - 1);
287
+ this.scrollBottom = Math.min(this.rows - 1, (p2 || this.rows) - 1);
288
+ if (this.scrollTop >= this.scrollBottom) { this.scrollTop = 0; this.scrollBottom = this.rows - 1; }
289
+ break;
290
+ case "s": this.savedCursor = { x: this.cx, y: this.cy }; break;
291
+ case "u": this.cx = this.savedCursor.x; this.cy = this.savedCursor.y; break;
292
+ case "h":
293
+ if (isPrivate) {
294
+ for (const n of parts) {
295
+ if (n === 1000 || n === 1002 || n === 1003 || n === 1006) this.mouseMode = true;
296
+ }
297
+ }
298
+ break;
299
+ case "l":
300
+ if (isPrivate) {
301
+ for (const n of parts) {
302
+ if (n === 1000 || n === 1002 || n === 1003 || n === 1006) this.mouseMode = false;
303
+ }
304
+ }
305
+ break;
306
+ }
307
+ return null;
308
+ }
309
+
310
+ _handleSGR(parts) {
311
+ if (parts.length === 0 || (parts.length === 1 && parts[0] === 0)) {
312
+ this.sgr = { fg: "default", bg: "default", bold: false, italic: false, underline: false, reverse: false };
313
+ return;
314
+ }
315
+ let i = 0;
316
+ while (i < parts.length) {
317
+ const n = parts[i];
318
+ switch (true) {
319
+ case n === 0: this.sgr = { fg: "default", bg: "default", bold: false, italic: false, underline: false, reverse: false }; break;
320
+ case n === 1: this.sgr.bold = true; break;
321
+ case n === 2: break; // dim, ignore
322
+ case n === 3: this.sgr.italic = true; break;
323
+ case n === 4: this.sgr.underline = true; break;
324
+ case n === 7: this.sgr.reverse = true; break;
325
+ case n === 21:
326
+ case n === 22: this.sgr.bold = false; break;
327
+ case n === 23: this.sgr.italic = false; break;
328
+ case n === 24: this.sgr.underline = false; break;
329
+ case n === 27: this.sgr.reverse = false; break;
330
+ case n >= 30 && n <= 37: this.sgr.fg = ANSI_COLORS[n - 30]; break;
331
+ case n === 38: {
332
+ const mode = parts[i + 1];
333
+ if (mode === 5) { this.sgr.fg = parts[i + 2] ?? 0; i += 2; }
334
+ else if (mode === 2) {
335
+ this.sgr.fg = `#${toHex2(parts[i+2])}${toHex2(parts[i+3])}${toHex2(parts[i+4])}`;
336
+ i += 4;
337
+ }
338
+ break;
339
+ }
340
+ case n === 39: this.sgr.fg = "default"; break;
341
+ case n >= 40 && n <= 47: this.sgr.bg = ANSI_COLORS[n - 40]; break;
342
+ case n === 48: {
343
+ const mode = parts[i + 1];
344
+ if (mode === 5) { this.sgr.bg = parts[i + 2] ?? 0; i += 2; }
345
+ else if (mode === 2) {
346
+ this.sgr.bg = `#${toHex2(parts[i+2])}${toHex2(parts[i+3])}${toHex2(parts[i+4])}`;
347
+ i += 4;
348
+ }
349
+ break;
350
+ }
351
+ case n === 49: this.sgr.bg = "default"; break;
352
+ case n >= 90 && n <= 97: this.sgr.fg = ANSI_COLORS[n - 90 + 8]; break;
353
+ case n >= 100 && n <= 107: this.sgr.bg = ANSI_COLORS[n - 100 + 8]; break;
354
+ }
355
+ i++;
356
+ }
357
+ }
358
+
359
+ resize(cols, rows) {
360
+ cols = Math.max(1, cols);
361
+ rows = Math.max(1, rows);
362
+ const newCells = Array.from({ length: cols * rows }, () => blankCell());
363
+ for (let y = 0; y < Math.min(rows, this.rows); y++) {
364
+ for (let x = 0; x < Math.min(cols, this.cols); x++) {
365
+ newCells[y * cols + x] = { ...this.cells[this._idx(x, y)] };
366
+ }
367
+ }
368
+ this.cols = cols;
369
+ this.rows = rows;
370
+ this.cells = newCells;
371
+ this.cx = Math.min(this.cx, cols - 1);
372
+ this.cy = Math.min(this.cy, rows - 1);
373
+ this.scrollTop = 0;
374
+ this.scrollBottom = rows - 1;
375
+ }
376
+
377
+ getRow(y) {
378
+ if (y < 0 || y >= this.rows) return Array.from({ length: this.cols }, blankCell);
379
+ if (this.scrollOffset === 0) {
380
+ return this.cells.slice(y * this.cols, (y + 1) * this.cols);
381
+ }
382
+ // Map view row y to virtual history + live combined buffer
383
+ const histStart = this.scrollback.length - this.scrollOffset;
384
+ const virtualY = histStart + y;
385
+ if (virtualY < 0) return Array.from({ length: this.cols }, blankCell);
386
+ if (virtualY < this.scrollback.length) return this.scrollback[virtualY];
387
+ const liveY = virtualY - this.scrollback.length;
388
+ if (liveY >= this.rows) return Array.from({ length: this.cols }, blankCell);
389
+ return this.cells.slice(liveY * this.cols, (liveY + 1) * this.cols);
390
+ }
391
+ }
@@ -0,0 +1,70 @@
1
+ import { run } from "../platform/commands.js";
2
+
3
+ export function shellSplit(input) {
4
+ const args = [];
5
+ let current = "";
6
+ let quote = null;
7
+ let escaped = false;
8
+ let started = false;
9
+
10
+ for (const ch of String(input)) {
11
+ if (escaped) {
12
+ if (ch === 'n') current += '\n';
13
+ else if (ch === 't') current += '\t';
14
+ else if (ch === 'r') current += '\r';
15
+ else current += ch;
16
+ escaped = false;
17
+ started = true;
18
+ continue;
19
+ }
20
+ if (ch === "\\" && quote !== "'") {
21
+ escaped = true;
22
+ started = true;
23
+ continue;
24
+ }
25
+ if (quote) {
26
+ if (ch === quote) { quote = null; }
27
+ else { current += ch; }
28
+ continue;
29
+ }
30
+ if (ch === "'" || ch === '"') {
31
+ quote = ch;
32
+ started = true;
33
+ continue;
34
+ }
35
+ if (/\s/.test(ch)) {
36
+ if (started) {
37
+ args.push(current);
38
+ current = "";
39
+ started = false;
40
+ }
41
+ continue;
42
+ }
43
+ current += ch;
44
+ started = true;
45
+ }
46
+
47
+ if (escaped) current += "\\";
48
+ if (quote) throw new Error(`Unclosed ${quote} quote`);
49
+ if (started) args.push(current);
50
+ return args;
51
+ }
52
+
53
+ export async function execCommand(name, args = []) {
54
+ const result = await run([name, ...args.map(String)], { allowFailure: true });
55
+ const output = result.stdout + result.stderr;
56
+ return [output, result.ok ? null : `${name} exited with ${result.code}`];
57
+ }
58
+
59
+ export async function runCommand(input) {
60
+ const args = shellSplit(input);
61
+ if (args.length === 0) return ["", "No arguments"];
62
+ return execCommand(args[0], args.slice(1));
63
+ }
64
+
65
+ export function runBackgroundShell(input) {
66
+ return async () => {
67
+ const [output, error] = await runCommand(input);
68
+ return error ? `${error}: ${output}` : output;
69
+ };
70
+ }