jsmdcui 0.3.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 (258) hide show
  1. package/CHANGELOG.md +76 -0
  2. package/LICENSE +22 -0
  3. package/README.md +285 -0
  4. package/package.json +17 -0
  5. package/project_structure.txt +35 -0
  6. package/runmd.mjs +240 -0
  7. package/runtime/colorschemes/atom-dark.micro +33 -0
  8. package/runtime/colorschemes/bubblegum.micro +31 -0
  9. package/runtime/colorschemes/cmc-16.micro +47 -0
  10. package/runtime/colorschemes/cmc-tc.micro +43 -0
  11. package/runtime/colorschemes/darcula.micro +34 -0
  12. package/runtime/colorschemes/default.micro +1 -0
  13. package/runtime/colorschemes/dracula-tc.micro +49 -0
  14. package/runtime/colorschemes/dukedark-tc.micro +38 -0
  15. package/runtime/colorschemes/dukelight-tc.micro +38 -0
  16. package/runtime/colorschemes/dukeubuntu-tc.micro +38 -0
  17. package/runtime/colorschemes/geany.micro +29 -0
  18. package/runtime/colorschemes/gotham.micro +29 -0
  19. package/runtime/colorschemes/gruvbox-tc.micro +29 -0
  20. package/runtime/colorschemes/gruvbox.micro +26 -0
  21. package/runtime/colorschemes/material-tc.micro +36 -0
  22. package/runtime/colorschemes/monokai-dark.micro +28 -0
  23. package/runtime/colorschemes/monokai.micro +34 -0
  24. package/runtime/colorschemes/one-dark.micro +39 -0
  25. package/runtime/colorschemes/railscast.micro +37 -0
  26. package/runtime/colorschemes/simple.micro +33 -0
  27. package/runtime/colorschemes/solarized-tc.micro +31 -0
  28. package/runtime/colorschemes/solarized.micro +30 -0
  29. package/runtime/colorschemes/sunny-day.micro +29 -0
  30. package/runtime/colorschemes/twilight.micro +40 -0
  31. package/runtime/colorschemes/zenburn.micro +30 -0
  32. package/runtime/help/actions.md +161 -0
  33. package/runtime/help/cdp.md +119 -0
  34. package/runtime/help/colors.md +421 -0
  35. package/runtime/help/commands.md +161 -0
  36. package/runtime/help/copypaste.md +149 -0
  37. package/runtime/help/defaultkeys.md +148 -0
  38. package/runtime/help/help.md +285 -0
  39. package/runtime/help/keybindings.md +355 -0
  40. package/runtime/help/linter.md +90 -0
  41. package/runtime/help/options.md +709 -0
  42. package/runtime/help/plugins.md +544 -0
  43. package/runtime/help/tutorial.md +112 -0
  44. package/runtime/jsplugins/cdp/cdp-server.js +1161 -0
  45. package/runtime/jsplugins/cdp/cdp.js +192 -0
  46. package/runtime/jsplugins/chapter/chapter.js +108 -0
  47. package/runtime/jsplugins/diff/diff.js +46 -0
  48. package/runtime/jsplugins/example/example.js +110 -0
  49. package/runtime/jsplugins/linter/linter.js +281 -0
  50. package/runtime/plugins/autoclose/autoclose.lua +75 -0
  51. package/runtime/plugins/ftoptions/ftoptions.lua +17 -0
  52. package/runtime/plugins/literate/README.md +5 -0
  53. package/runtime/plugins/literate/literate.lua +55 -0
  54. package/runtime/plugins/status/help/status.md +21 -0
  55. package/runtime/plugins/status/status.lua +62 -0
  56. package/runtime/syntax/LICENSE +22 -0
  57. package/runtime/syntax/PowerShell.yaml +128 -0
  58. package/runtime/syntax/README.md +63 -0
  59. package/runtime/syntax/ada.yaml +43 -0
  60. package/runtime/syntax/apacheconf.yaml +59 -0
  61. package/runtime/syntax/arduino.yaml +101 -0
  62. package/runtime/syntax/asciidoc.yaml +51 -0
  63. package/runtime/syntax/asm.yaml +123 -0
  64. package/runtime/syntax/ats.yaml +99 -0
  65. package/runtime/syntax/awk.yaml +44 -0
  66. package/runtime/syntax/b.yaml +87 -0
  67. package/runtime/syntax/bat.yaml +57 -0
  68. package/runtime/syntax/c.yaml +60 -0
  69. package/runtime/syntax/caddyfile.yaml +23 -0
  70. package/runtime/syntax/cake.yaml +7 -0
  71. package/runtime/syntax/clojure.yaml +38 -0
  72. package/runtime/syntax/cmake.yaml +42 -0
  73. package/runtime/syntax/coffeescript.yaml +56 -0
  74. package/runtime/syntax/colortest.yaml +19 -0
  75. package/runtime/syntax/conky.yaml +17 -0
  76. package/runtime/syntax/cpp.yaml +91 -0
  77. package/runtime/syntax/crontab.yaml +36 -0
  78. package/runtime/syntax/crystal.yaml +72 -0
  79. package/runtime/syntax/csharp.yaml +52 -0
  80. package/runtime/syntax/css.yaml +44 -0
  81. package/runtime/syntax/csx.yaml +8 -0
  82. package/runtime/syntax/cuda.yaml +68 -0
  83. package/runtime/syntax/cython.yaml +52 -0
  84. package/runtime/syntax/d.yaml +121 -0
  85. package/runtime/syntax/dart.yaml +46 -0
  86. package/runtime/syntax/default.yaml +10 -0
  87. package/runtime/syntax/dockerfile.yaml +36 -0
  88. package/runtime/syntax/dot.yaml +29 -0
  89. package/runtime/syntax/elixir.yaml +30 -0
  90. package/runtime/syntax/elm.yaml +38 -0
  91. package/runtime/syntax/erb.yaml +42 -0
  92. package/runtime/syntax/erlang.yaml +45 -0
  93. package/runtime/syntax/fish.yaml +48 -0
  94. package/runtime/syntax/forth.yaml +34 -0
  95. package/runtime/syntax/fortran.yaml +64 -0
  96. package/runtime/syntax/freebsd-kernel.yaml +14 -0
  97. package/runtime/syntax/fsharp.yaml +48 -0
  98. package/runtime/syntax/gdscript.yaml +61 -0
  99. package/runtime/syntax/gemini.yaml +19 -0
  100. package/runtime/syntax/gentoo-ebuild.yaml +48 -0
  101. package/runtime/syntax/gentoo-etc-portage.yaml +23 -0
  102. package/runtime/syntax/git-commit.yaml +35 -0
  103. package/runtime/syntax/git-config.yaml +14 -0
  104. package/runtime/syntax/git-rebase-todo.yaml +19 -0
  105. package/runtime/syntax/gleam.yaml +69 -0
  106. package/runtime/syntax/glsl.yaml +26 -0
  107. package/runtime/syntax/gnuplot.yaml +15 -0
  108. package/runtime/syntax/go.yaml +62 -0
  109. package/runtime/syntax/godoc.yaml +17 -0
  110. package/runtime/syntax/golo.yaml +73 -0
  111. package/runtime/syntax/gomod.yaml +31 -0
  112. package/runtime/syntax/graphql.yaml +47 -0
  113. package/runtime/syntax/groff.yaml +30 -0
  114. package/runtime/syntax/groovy.yaml +111 -0
  115. package/runtime/syntax/haml.yaml +16 -0
  116. package/runtime/syntax/hare.yaml +52 -0
  117. package/runtime/syntax/haskell.yaml +52 -0
  118. package/runtime/syntax/hc.yaml +52 -0
  119. package/runtime/syntax/html.yaml +70 -0
  120. package/runtime/syntax/html4.yaml +25 -0
  121. package/runtime/syntax/html5.yaml +25 -0
  122. package/runtime/syntax/ini.yaml +23 -0
  123. package/runtime/syntax/inputrc.yaml +14 -0
  124. package/runtime/syntax/java.yaml +37 -0
  125. package/runtime/syntax/javascript.yaml +124 -0
  126. package/runtime/syntax/jinja2.yaml +19 -0
  127. package/runtime/syntax/json.yaml +39 -0
  128. package/runtime/syntax/jsonnet.yaml +92 -0
  129. package/runtime/syntax/julia.yaml +57 -0
  130. package/runtime/syntax/justfile.yaml +40 -0
  131. package/runtime/syntax/keymap.yaml +27 -0
  132. package/runtime/syntax/kickstart.yaml +16 -0
  133. package/runtime/syntax/kotlin.yaml +66 -0
  134. package/runtime/syntax/kvlang.yaml +67 -0
  135. package/runtime/syntax/ledger.yaml +14 -0
  136. package/runtime/syntax/lfe.yaml +17 -0
  137. package/runtime/syntax/lilypond.yaml +26 -0
  138. package/runtime/syntax/lisp.yaml +17 -0
  139. package/runtime/syntax/log.yaml +92 -0
  140. package/runtime/syntax/lua.yaml +111 -0
  141. package/runtime/syntax/mail.yaml +25 -0
  142. package/runtime/syntax/makefile.yaml +38 -0
  143. package/runtime/syntax/man.yaml +12 -0
  144. package/runtime/syntax/markdown.yaml +49 -0
  145. package/runtime/syntax/mc.yaml +23 -0
  146. package/runtime/syntax/meson.yaml +51 -0
  147. package/runtime/syntax/micro.yaml +34 -0
  148. package/runtime/syntax/mpdconf.yaml +13 -0
  149. package/runtime/syntax/msbuild.yaml +6 -0
  150. package/runtime/syntax/nanorc.yaml +16 -0
  151. package/runtime/syntax/nftables.yaml +30 -0
  152. package/runtime/syntax/nginx.yaml +22 -0
  153. package/runtime/syntax/nim.yaml +27 -0
  154. package/runtime/syntax/nix.yaml +32 -0
  155. package/runtime/syntax/nu.yaml +114 -0
  156. package/runtime/syntax/objc.yaml +60 -0
  157. package/runtime/syntax/ocaml.yaml +43 -0
  158. package/runtime/syntax/octave.yaml +83 -0
  159. package/runtime/syntax/odin.yaml +64 -0
  160. package/runtime/syntax/pascal.yaml +45 -0
  161. package/runtime/syntax/patch.yaml +14 -0
  162. package/runtime/syntax/peg.yaml +16 -0
  163. package/runtime/syntax/perl.yaml +58 -0
  164. package/runtime/syntax/php.yaml +60 -0
  165. package/runtime/syntax/pkg-config.yaml +12 -0
  166. package/runtime/syntax/po.yaml +12 -0
  167. package/runtime/syntax/pony.yaml +37 -0
  168. package/runtime/syntax/pov.yaml +21 -0
  169. package/runtime/syntax/privoxy-action.yaml +14 -0
  170. package/runtime/syntax/privoxy-config.yaml +10 -0
  171. package/runtime/syntax/privoxy-filter.yaml +12 -0
  172. package/runtime/syntax/proto.yaml +40 -0
  173. package/runtime/syntax/prql.yaml +84 -0
  174. package/runtime/syntax/puppet.yaml +22 -0
  175. package/runtime/syntax/python2.yaml +60 -0
  176. package/runtime/syntax/python3.yaml +80 -0
  177. package/runtime/syntax/r.yaml +32 -0
  178. package/runtime/syntax/raku.yaml +42 -0
  179. package/runtime/syntax/reST.yaml +18 -0
  180. package/runtime/syntax/renpy.yaml +15 -0
  181. package/runtime/syntax/rpmspec.yaml +43 -0
  182. package/runtime/syntax/ruby.yaml +73 -0
  183. package/runtime/syntax/rust.yaml +78 -0
  184. package/runtime/syntax/sage.yaml +60 -0
  185. package/runtime/syntax/scad.yaml +53 -0
  186. package/runtime/syntax/scala.yaml +33 -0
  187. package/runtime/syntax/sed.yaml +13 -0
  188. package/runtime/syntax/sh.yaml +69 -0
  189. package/runtime/syntax/sls.yaml +15 -0
  190. package/runtime/syntax/smalltalk.yaml +55 -0
  191. package/runtime/syntax/solidity.yaml +41 -0
  192. package/runtime/syntax/sql.yaml +35 -0
  193. package/runtime/syntax/stata.yaml +67 -0
  194. package/runtime/syntax/svelte.yaml +27 -0
  195. package/runtime/syntax/swift.yaml +103 -0
  196. package/runtime/syntax/systemd.yaml +16 -0
  197. package/runtime/syntax/tcl.yaml +18 -0
  198. package/runtime/syntax/terraform.yaml +87 -0
  199. package/runtime/syntax/tex.yaml +32 -0
  200. package/runtime/syntax/toml.yaml +56 -0
  201. package/runtime/syntax/twig.yaml +55 -0
  202. package/runtime/syntax/typescript.yaml +49 -0
  203. package/runtime/syntax/v.yaml +80 -0
  204. package/runtime/syntax/vala.yaml +26 -0
  205. package/runtime/syntax/verilog.yaml +60 -0
  206. package/runtime/syntax/vhdl.yaml +37 -0
  207. package/runtime/syntax/vi.yaml +31 -0
  208. package/runtime/syntax/vue.yaml +64 -0
  209. package/runtime/syntax/xml.yaml +37 -0
  210. package/runtime/syntax/xresources.yaml +14 -0
  211. package/runtime/syntax/yaml.yaml +34 -0
  212. package/runtime/syntax/yum.yaml +12 -0
  213. package/runtime/syntax/zig.yaml +52 -0
  214. package/runtime/syntax/zscript.yaml +72 -0
  215. package/runtime/syntax/zsh.yaml +52 -0
  216. package/single-exe/README.md +80 -0
  217. package/single-exe/assetsHelper.js +90 -0
  218. package/single-exe/assetsLoader.mjs +85 -0
  219. package/single-exe/compiled.js +149 -0
  220. package/single-exe/entry.mjs +9 -0
  221. package/single-exe/packAssets.sh +7 -0
  222. package/src/buffer/backup.js +160 -0
  223. package/src/buffer/buffer.js +126 -0
  224. package/src/buffer/fixed3-codec.js +140 -0
  225. package/src/buffer/loc.js +38 -0
  226. package/src/buffer/message.js +29 -0
  227. package/src/config/clean.js +172 -0
  228. package/src/config/colorscheme.js +99 -0
  229. package/src/config/config.js +122 -0
  230. package/src/config/defaults.js +109 -0
  231. package/src/cui/rpc.mjs +259 -0
  232. package/src/cui/server.mjs +116 -0
  233. package/src/display/ansi-style.js +60 -0
  234. package/src/highlight/highlighter.js +243 -0
  235. package/src/highlight/parser.js +225 -0
  236. package/src/index.js +8009 -0
  237. package/src/lua/engine.js +71 -0
  238. package/src/platform/archive.js +50 -0
  239. package/src/platform/clipboard.js +278 -0
  240. package/src/platform/commands.js +137 -0
  241. package/src/plugins/js-bridge.js +983 -0
  242. package/src/plugins/manager.js +674 -0
  243. package/src/runtime/assets.js +90 -0
  244. package/src/runtime/compiled.js +25 -0
  245. package/src/runtime/encodings.js +10 -0
  246. package/src/runtime/registry.js +134 -0
  247. package/src/screen/cell-buffer.js +81 -0
  248. package/src/screen/events.js +263 -0
  249. package/src/screen/screen.js +142 -0
  250. package/src/screen/vt100.js +571 -0
  251. package/src/shell/shell.js +70 -0
  252. package/testapp.md +55 -0
  253. package/tests/backup.test.js +133 -0
  254. package/tests/cmphex3b64.js +95 -0
  255. package/tests/pty-demo.js +492 -0
  256. package/tests/wv-client.js +96 -0
  257. package/tui +5 -0
  258. package/wui +5 -0
@@ -0,0 +1,142 @@
1
+ import process from "node:process";
2
+ import { styleToAnsi } from "../display/ansi-style.js";
3
+ import { CellBuffer } from "./cell-buffer.js";
4
+ import { DISABLE_MOUSE, DISABLE_PASTE, ENABLE_MOUSE, ENABLE_PASTE, ResizeEvent } from "./events.js";
5
+
6
+ const CURSOR_SHAPE_SEQUENCE = {
7
+ default: "\x1b[0 q",
8
+ "blinking-block": "\x1b[1 q",
9
+ block: "\x1b[2 q",
10
+ "blinking-underline": "\x1b[3 q",
11
+ underline: "\x1b[4 q",
12
+ "blinking-bar": "\x1b[5 q",
13
+ bar: "\x1b[6 q",
14
+ };
15
+
16
+ export function cursorShapeSequence(shape) {
17
+ return CURSOR_SHAPE_SEQUENCE[shape] ?? CURSOR_SHAPE_SEQUENCE.block;
18
+ }
19
+
20
+ export class Screen {
21
+ constructor({ mouse = true } = {}) {
22
+ this.mouse = mouse;
23
+ this.cols = process.stdout.columns || 80;
24
+ this.rows = process.stdout.rows || 24;
25
+ this.cells = new CellBuffer(this.cols, this.rows);
26
+ this.previous = null;
27
+ this.cursor = null;
28
+ this.cursorVisible = false;
29
+ }
30
+
31
+ init() {
32
+ this.write("\x1b[?1049h\x1b[?25l");
33
+ if (this.mouse) this.write(ENABLE_MOUSE);
34
+ this.write(ENABLE_PASTE);
35
+ }
36
+
37
+ fini() {
38
+ if (this.mouse) this.write(DISABLE_MOUSE);
39
+ this.write(DISABLE_PASTE);
40
+ this.write("\x1b[0 q\x1b[?25h\x1b[?1049l\x1b[0m");
41
+ }
42
+
43
+ SetContent(x, y, ch, combining = [], style = null) {
44
+ this.cells.setContent(x, y, ch, style, combining);
45
+ }
46
+
47
+ setContent(x, y, ch, style = null, combining = []) {
48
+ this.SetContent(x, y, ch, combining, style);
49
+ }
50
+
51
+ setFillerContent(x, y, style = null) {
52
+ this.cells.setFiller(x, y, style);
53
+ }
54
+
55
+ GetContent(x, y) {
56
+ return this.cells.getContent(x, y);
57
+ }
58
+
59
+ getContent(x, y) {
60
+ return this.GetContent(x, y);
61
+ }
62
+
63
+ Fill(ch = " ", style = null) {
64
+ this.cells.fill(ch, style);
65
+ }
66
+
67
+ fill(ch = " ", style = null) {
68
+ this.Fill(ch, style);
69
+ }
70
+
71
+ Show() {
72
+ const changes = this.cells.diff(this.previous);
73
+ let out = "\x1b[?25l";
74
+ let activeStyleKey = null;
75
+ for (const { x, y, cell } of changes) {
76
+ if (cell.filler) continue; // right-half of a wide char; the base cell covers this column
77
+ // If this is a wide char (next cell is its filler), clear the filler column with
78
+ // default style first. On narrow-emoji terminals this leaves a default-bg blank
79
+ // at the right-half column instead of stale cursor-line / syntax background, so
80
+ // the area next to the glyph doesn't look like a colored block "covering" it.
81
+ // On wide-emoji terminals the glyph's right half overwrites the blank harmlessly.
82
+ const nextCell = this.cells.getContent(x + 1, y);
83
+ if (nextCell?.filler) {
84
+ out += this.move(y + 1, x + 2);
85
+ out += styleToAnsi({});
86
+ activeStyleKey = "";
87
+ out += " ";
88
+ }
89
+ out += this.move(y + 1, x + 1);
90
+ if (cell.styleKey !== activeStyleKey) {
91
+ out += styleToAnsi(cell.style ?? {});
92
+ activeStyleKey = cell.styleKey;
93
+ }
94
+ out += cell.ch + cell.combining.join("");
95
+ }
96
+ out += "\x1b[0m";
97
+ if (this.cursor && this.cursorVisible) {
98
+ out += cursorShapeSequence(this.cursor.shape) + this.move(this.cursor.y + 1, this.cursor.x + 1) + "\x1b[?25h";
99
+ } else out += "\x1b[?25l";
100
+ this.write(out);
101
+ this.previous = this.cells.clone();
102
+ }
103
+
104
+ show() {
105
+ this.Show();
106
+ }
107
+
108
+ setCursor(x, y, visible = true, shape = "block") {
109
+ this.cursor = { x, y, shape };
110
+ this.cursorVisible = visible;
111
+ }
112
+
113
+ hideCursor() {
114
+ this.cursorVisible = false;
115
+ this.write("\x1b[?25l");
116
+ }
117
+
118
+ showCursor() {
119
+ this.cursorVisible = true;
120
+ this.write("\x1b[?25h");
121
+ }
122
+
123
+ move(row, col) {
124
+ return `\x1b[${row};${col}H`;
125
+ }
126
+
127
+ clearToEndOfLine() {
128
+ return "\x1b[K";
129
+ }
130
+
131
+ updateSize() {
132
+ this.cols = process.stdout.columns || this.cols;
133
+ this.rows = process.stdout.rows || this.rows;
134
+ this.cells.resize(this.cols, this.rows);
135
+ this.previous = null;
136
+ return new ResizeEvent(this.cols, this.rows);
137
+ }
138
+
139
+ write(data) {
140
+ process.stdout.write(data);
141
+ }
142
+ }
@@ -0,0 +1,571 @@
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: " ", combining: [], filler: false, 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
+ function isZeroWidthCodePoint(cp) {
28
+ return (
29
+ cp === 0x200D ||
30
+ (cp >= 0x0300 && cp <= 0x036F) ||
31
+ (cp >= 0x1AB0 && cp <= 0x1AFF) ||
32
+ (cp >= 0x1DC0 && cp <= 0x1DFF) ||
33
+ (cp >= 0x20D0 && cp <= 0x20FF) ||
34
+ (cp >= 0xFE00 && cp <= 0xFE0F) ||
35
+ (cp >= 0xFE20 && cp <= 0xFE2F) ||
36
+ (cp >= 0xE0100 && cp <= 0xE01EF)
37
+ );
38
+ }
39
+
40
+ function isWideCodePoint(cp) {
41
+ if (cp < 0x1100) return false;
42
+ return (
43
+ cp <= 0x115F ||
44
+ cp === 0x2329 || cp === 0x232A ||
45
+ (cp >= 0x2E80 && cp <= 0x303E) ||
46
+ (cp >= 0x3040 && cp <= 0x33FF) ||
47
+ (cp >= 0x3400 && cp <= 0x4DBF) ||
48
+ (cp >= 0x4E00 && cp <= 0xA4C6) ||
49
+ (cp >= 0xA960 && cp <= 0xA97C) ||
50
+ (cp >= 0xAC00 && cp <= 0xD7A3) ||
51
+ (cp >= 0xF900 && cp <= 0xFAFF) ||
52
+ (cp >= 0xFE10 && cp <= 0xFE19) ||
53
+ (cp >= 0xFE30 && cp <= 0xFE4F) ||
54
+ (cp >= 0xFF01 && cp <= 0xFF60) ||
55
+ (cp >= 0xFFE0 && cp <= 0xFFE6) ||
56
+ (cp >= 0x1B000 && cp <= 0x1B0FF) ||
57
+ (cp >= 0x1F004 && cp <= 0x1F0CF) ||
58
+ (cp >= 0x1F18F && cp <= 0x1F19A) ||
59
+ (cp >= 0x1F200 && cp <= 0x1F2FF) ||
60
+ (cp >= 0x1F300 && cp <= 0x1FAFF) ||
61
+ (cp >= 0x20000 && cp <= 0x2FFFD) ||
62
+ (cp >= 0x30000 && cp <= 0x3FFFD)
63
+ );
64
+ }
65
+
66
+ function charWidth(ch) {
67
+ if (!ch) return 0;
68
+ const cp = ch.codePointAt(0);
69
+ if (cp < 32 || (cp >= 0x7f && cp < 0xa0) || isZeroWidthCodePoint(cp)) return 0;
70
+ return isWideCodePoint(cp) ? 2 : 1;
71
+ }
72
+
73
+ export class VT100 {
74
+ constructor(cols, rows) {
75
+ this.cols = Math.max(1, cols);
76
+ this.rows = Math.max(1, rows);
77
+ this.cells = [];
78
+ this.cx = 0;
79
+ this.cy = 0;
80
+ this.wrapPending = false;
81
+ this.savedCursor = { x: 0, y: 0 };
82
+ this.scrollTop = 0;
83
+ this.scrollBottom = this.rows - 1;
84
+ this.sgr = { fg: "default", bg: "default", bold: false, italic: false, underline: false, reverse: false };
85
+ this.pending = "";
86
+ // scrollback
87
+ this.scrollback = []; // oldest first, each entry is a cols-length cell array
88
+ this.maxScrollback = 500;
89
+ this.scrollOffset = 0; // 0 = live view; n = n rows scrolled back into history
90
+ // mouse reporting: set by the application via ?1000h / ?1002h / ?1003h
91
+ this.mouseMode = false;
92
+ this.keyboardProtocolFlags = 0;
93
+ this.keyboardProtocolStack = [];
94
+ this.modifyOtherKeys = 0;
95
+ this.formatOtherKeys = 0;
96
+ this._initCells();
97
+ }
98
+
99
+ _initCells() {
100
+ this.cells = Array.from({ length: this.cols * this.rows }, () => blankCell());
101
+ }
102
+
103
+ _idx(x, y) { return y * this.cols + x; }
104
+
105
+ _cell(x, y) {
106
+ if (x < 0 || x >= this.cols || y < 0 || y >= this.rows) return null;
107
+ return this.cells[this._idx(x, y)];
108
+ }
109
+
110
+ _copyStyleTo(cell) {
111
+ cell.fg = this.sgr.fg;
112
+ cell.bg = this.sgr.bg;
113
+ cell.bold = this.sgr.bold;
114
+ cell.italic = this.sgr.italic;
115
+ cell.underline = this.sgr.underline;
116
+ cell.reverse = this.sgr.reverse;
117
+ }
118
+
119
+ _clearCellRaw(x, y) {
120
+ const cell = this._cell(x, y);
121
+ if (cell) Object.assign(cell, blankCell());
122
+ }
123
+
124
+ _breakWideAt(x, y) {
125
+ const cell = this._cell(x, y);
126
+ if (!cell) return;
127
+ if (cell.filler) this._clearCellRaw(x - 1, y);
128
+ if (x + 1 < this.cols && this._cell(x + 1, y)?.filler) this._clearCellRaw(x + 1, y);
129
+ }
130
+
131
+ _setCell(x, y, ch, width = 1) {
132
+ if (width === 0) {
133
+ const targetX = this.cx > 0 ? this.cx - 1 : 0;
134
+ const cell = this._cell(targetX, this.cy);
135
+ if (cell && !cell.filler) cell.combining.push(ch);
136
+ return;
137
+ }
138
+ if (width > 1 && x >= this.cols - 1) {
139
+ this.cx = 0;
140
+ this._lineFeed();
141
+ x = this.cx;
142
+ y = this.cy;
143
+ }
144
+ this._breakWideAt(x, y);
145
+ if (width > 1) this._breakWideAt(x + 1, y);
146
+ const cell = this._cell(x, y);
147
+ if (!cell) return;
148
+ cell.ch = ch;
149
+ cell.combining = [];
150
+ cell.filler = false;
151
+ this._copyStyleTo(cell);
152
+ if (width > 1) {
153
+ const filler = this._cell(x + 1, y);
154
+ if (filler) {
155
+ Object.assign(filler, blankCell());
156
+ filler.filler = true;
157
+ this._copyStyleTo(filler);
158
+ }
159
+ }
160
+ }
161
+
162
+ _clearCell(x, y) {
163
+ this._breakWideAt(x, y);
164
+ this._clearCellRaw(x, y);
165
+ }
166
+
167
+ _clearLineFrom(x, y) {
168
+ for (let i = x; i < this.cols; i++) this._clearCell(i, y);
169
+ }
170
+
171
+ _clearLineTo(x, y) {
172
+ for (let i = 0; i <= x; i++) this._clearCell(i, y);
173
+ }
174
+
175
+ _clearLine(y) {
176
+ for (let i = 0; i < this.cols; i++) this._clearCell(i, y);
177
+ }
178
+
179
+ _scrollUp(n = 1) {
180
+ for (let count = 0; count < n; count++) {
181
+ // Save the line scrolling off the top into scrollback history
182
+ const row = [];
183
+ for (let x = 0; x < this.cols; x++) row.push({ ...this.cells[this._idx(x, this.scrollTop)] });
184
+ this.scrollback.push(row);
185
+ if (this.scrollback.length > this.maxScrollback) this.scrollback.shift();
186
+
187
+ for (let y = this.scrollTop; y < this.scrollBottom; y++) {
188
+ for (let x = 0; x < this.cols; x++) {
189
+ this.cells[this._idx(x, y)] = { ...this.cells[this._idx(x, y + 1)] };
190
+ }
191
+ }
192
+ this._clearLine(this.scrollBottom);
193
+ }
194
+ }
195
+
196
+ // Adjust scrollback view. delta > 0 = scroll back (older), delta < 0 = scroll forward (newer).
197
+ scroll(delta) {
198
+ this.scrollOffset = Math.max(0, Math.min(this.scrollback.length, this.scrollOffset + delta));
199
+ }
200
+
201
+ _scrollDown(n = 1, fromRow = this.scrollTop) {
202
+ for (let count = 0; count < n; count++) {
203
+ for (let y = Math.min(this.scrollBottom, this.rows - 2); y >= fromRow; y--) {
204
+ for (let x = 0; x < this.cols; x++) {
205
+ this.cells[this._idx(x, y + 1)] = { ...this.cells[this._idx(x, y)] };
206
+ }
207
+ }
208
+ this._clearLine(fromRow);
209
+ }
210
+ }
211
+
212
+ _lineFeed() {
213
+ this.wrapPending = false;
214
+ if (this.cy < this.scrollBottom) {
215
+ this.cy++;
216
+ } else {
217
+ this._scrollUp(1);
218
+ }
219
+ }
220
+
221
+ _moveCursor(x, y) {
222
+ this.cx = Math.min(this.cols - 1, Math.max(0, x));
223
+ this.cy = Math.min(this.rows - 1, Math.max(0, y));
224
+ this.wrapPending = false;
225
+ }
226
+
227
+ // Feed a chunk of terminal output. Returns array of response strings to send back.
228
+ feed(text) {
229
+ const data = this.pending + text;
230
+ this.pending = "";
231
+ const responses = [];
232
+ let i = 0;
233
+
234
+ while (i < data.length) {
235
+ const ch = data[i];
236
+ const code = data.charCodeAt(i);
237
+
238
+ if (ch === "\x1b") {
239
+ if (i + 1 >= data.length) { this.pending = data.slice(i); break; }
240
+ const next = data[i + 1];
241
+
242
+ if (next === "[") {
243
+ // CSI
244
+ const end = findCSIEnd(data, i + 2);
245
+ if (end < 0) { this.pending = data.slice(i); break; }
246
+ const params = data.slice(i + 2, end);
247
+ const final = data[end];
248
+ const resp = this._handleCSI(params, final);
249
+ if (resp) responses.push(resp);
250
+ i = end + 1;
251
+
252
+ } else if (next === "]") {
253
+ // OSC — find ST (\x1b\\) or BEL (\x07)
254
+ const bel = data.indexOf("\x07", i + 2);
255
+ const st = data.indexOf("\x1b\\", i + 2);
256
+ if (bel < 0 && st < 0) { this.pending = data.slice(i); break; }
257
+ const endOSC = (bel >= 0 && (st < 0 || bel < st)) ? bel : st + 1;
258
+ i = endOSC + 1;
259
+
260
+ } else if (next === "7") {
261
+ this.savedCursor = { x: this.cx, y: this.cy }; i += 2;
262
+ } else if (next === "8") {
263
+ this._moveCursor(this.savedCursor.x, this.savedCursor.y); i += 2;
264
+ } else if (next === "M") {
265
+ // Reverse index
266
+ if (this.cy === this.scrollTop) this._scrollDown(1);
267
+ else this._moveCursor(this.cx, this.cy - 1);
268
+ i += 2;
269
+ } else if (next === "(" || next === ")" || next === "*" || next === "+") {
270
+ i += 3; // charset designation, skip designator
271
+ } else if (next === "c") {
272
+ // Full reset
273
+ this._initCells(); this.cx = this.cy = 0;
274
+ this.wrapPending = false;
275
+ this.sgr = { fg: "default", bg: "default", bold: false, italic: false, underline: false, reverse: false };
276
+ this.scrollTop = 0; this.scrollBottom = this.rows - 1;
277
+ i += 2;
278
+ } else if (next === "=") {
279
+ i += 2; // application keypad mode, ignore
280
+ } else if (next === ">") {
281
+ i += 2; // normal keypad mode, ignore
282
+ } else {
283
+ i += 2; // unknown two-char escape
284
+ }
285
+
286
+ } else if (ch === "\r") {
287
+ this.cx = 0; this.wrapPending = false; i++;
288
+ } else if (ch === "\n" || ch === "\x0b" || ch === "\x0c") {
289
+ this._lineFeed(); i++;
290
+ } else if (ch === "\b") {
291
+ if (this.wrapPending) this.wrapPending = false;
292
+ else if (this.cx > 0) this.cx--;
293
+ i++;
294
+ } else if (ch === "\t") {
295
+ this.cx = Math.min(this.cols - 1, (Math.floor(this.cx / 8) + 1) * 8); this.wrapPending = false; i++;
296
+ } else if (ch === "\x07") {
297
+ i++; // Bell: ignore
298
+ } else if (ch === "\x0e" || ch === "\x0f") {
299
+ i++; // SO/SI charset switch, ignore
300
+ } else if (code >= 0x20) {
301
+ // Printable
302
+ const cp = data.codePointAt(i);
303
+ const rune = String.fromCodePoint(cp);
304
+ const width = charWidth(rune);
305
+ if (width > 0 && this.wrapPending) {
306
+ this.cx = 0;
307
+ this._lineFeed();
308
+ }
309
+ this._setCell(this.cx, this.cy, rune, width);
310
+ if (width > 0) {
311
+ const nextX = this.cx + width;
312
+ if (nextX >= this.cols) {
313
+ this.cx = this.cols - 1;
314
+ this.wrapPending = true;
315
+ } else {
316
+ this.cx = nextX;
317
+ this.wrapPending = false;
318
+ }
319
+ }
320
+ i += cp > 0xFFFF ? 2 : 1;
321
+ } else {
322
+ i++; // other control: skip
323
+ }
324
+ }
325
+
326
+ return responses;
327
+ }
328
+
329
+ _handleCSI(params, final) {
330
+ if (final === "u" && /^(?:[?<>]=?|=)/.test(params)) {
331
+ return this._handleKeyboardProtocol(params);
332
+ }
333
+
334
+ // Check for private mode prefix
335
+ const isPrivate = params.startsWith("?");
336
+ const raw = isPrivate ? params.slice(1) : params;
337
+ const parts = raw === "" ? [0] : raw.split(";").map(p => p === "" ? 0 : Number(p));
338
+ const p1 = parts[0] ?? 0;
339
+ const p2 = parts[1] ?? 0;
340
+
341
+ switch (final) {
342
+ case "A": this._moveCursor(this.cx, Math.max(this.scrollTop, this.cy - Math.max(1, p1))); break;
343
+ case "B": this._moveCursor(this.cx, Math.min(this.scrollBottom, this.cy + Math.max(1, p1))); break;
344
+ case "C": this._moveCursor(Math.min(this.cols - 1, this.cx + Math.max(1, p1)), this.cy); break;
345
+ case "D": this._moveCursor(Math.max(0, this.cx - Math.max(1, p1)), this.cy); break;
346
+ case "E": this._moveCursor(0, Math.min(this.rows - 1, this.cy + Math.max(1, p1))); break;
347
+ case "F": this._moveCursor(0, Math.max(0, this.cy - Math.max(1, p1))); break;
348
+ case "G": this._moveCursor(Math.min(this.cols - 1, Math.max(0, Math.max(1, p1) - 1)), this.cy); break;
349
+ case "H":
350
+ this._moveCursor(Math.max(0, Math.max(1, p2) - 1), Math.max(0, Math.max(1, p1) - 1));
351
+ break;
352
+ case "J":
353
+ if (p1 === 0) {
354
+ this._clearLineFrom(this.cx, this.cy);
355
+ for (let y = this.cy + 1; y < this.rows; y++) this._clearLine(y);
356
+ } else if (p1 === 1) {
357
+ for (let y = 0; y < this.cy; y++) this._clearLine(y);
358
+ this._clearLineTo(this.cx, this.cy);
359
+ } else if (p1 === 2 || p1 === 3) {
360
+ this._initCells(); this.cx = this.cy = 0;
361
+ }
362
+ break;
363
+ case "K":
364
+ if (p1 === 0) this._clearLineFrom(this.cx, this.cy);
365
+ else if (p1 === 1) this._clearLineTo(this.cx, this.cy);
366
+ else if (p1 === 2) this._clearLine(this.cy);
367
+ break;
368
+ case "L": this._scrollDown(Math.max(1, p1), this.cy); break;
369
+ case "M": {
370
+ const n = Math.max(1, p1);
371
+ for (let i = 0; i < n; i++) {
372
+ for (let y = this.cy; y < this.scrollBottom; y++) {
373
+ for (let x = 0; x < this.cols; x++) {
374
+ this.cells[this._idx(x, y)] = { ...this.cells[this._idx(x, y + 1)] };
375
+ }
376
+ }
377
+ this._clearLine(this.scrollBottom);
378
+ }
379
+ break;
380
+ }
381
+ case "P": {
382
+ const n = Math.max(1, p1);
383
+ for (let x = this.cx; x < this.cols - n; x++)
384
+ this.cells[this._idx(x, this.cy)] = { ...this.cells[this._idx(x + n, this.cy)] };
385
+ for (let x = this.cols - n; x < this.cols; x++) this._clearCell(x, this.cy);
386
+ break;
387
+ }
388
+ case "S": this._scrollUp(Math.max(1, p1)); break;
389
+ case "T": this._scrollDown(Math.max(1, p1)); break;
390
+ case "X": {
391
+ const n = Math.max(1, p1);
392
+ for (let x = this.cx; x < Math.min(this.cols, this.cx + n); x++) this._clearCell(x, this.cy);
393
+ break;
394
+ }
395
+ case "d": this._moveCursor(this.cx, Math.min(this.rows - 1, Math.max(0, Math.max(1, p1) - 1))); break;
396
+ case "f":
397
+ if (params.startsWith(">")) this._handleXtermKeyFormat(raw);
398
+ else {
399
+ this._moveCursor(Math.max(0, Math.max(1, p2) - 1), Math.max(0, Math.max(1, p1) - 1));
400
+ }
401
+ break;
402
+ case "m":
403
+ if (params.startsWith(">")) this._handleXtermKeyModifier(raw);
404
+ else this._handleSGR(parts);
405
+ break;
406
+ case "n":
407
+ if (p1 === 6) return `\x1b[${this.cy + 1};${this.cx + 1}R`; // CPR
408
+ if (p1 === 5) return "\x1b[0n"; // device status OK
409
+ break;
410
+ case "r":
411
+ this.scrollTop = Math.max(0, Math.max(1, p1) - 1);
412
+ this.scrollBottom = Math.min(this.rows - 1, (p2 || this.rows) - 1);
413
+ if (this.scrollTop >= this.scrollBottom) { this.scrollTop = 0; this.scrollBottom = this.rows - 1; }
414
+ break;
415
+ case "s": this.savedCursor = { x: this.cx, y: this.cy }; break;
416
+ case "u":
417
+ if (params === "") {
418
+ this._moveCursor(this.savedCursor.x, this.savedCursor.y);
419
+ }
420
+ break;
421
+ case "c":
422
+ if (params === "" || p1 === 0) return "\x1b[?1;2c"; // primary device attributes
423
+ break;
424
+ case "h":
425
+ if (isPrivate) {
426
+ for (const n of parts) {
427
+ if (n === 1000 || n === 1002 || n === 1003 || n === 1006) this.mouseMode = true;
428
+ }
429
+ }
430
+ break;
431
+ case "l":
432
+ if (isPrivate) {
433
+ for (const n of parts) {
434
+ if (n === 1000 || n === 1002 || n === 1003 || n === 1006) this.mouseMode = false;
435
+ }
436
+ }
437
+ break;
438
+ }
439
+ return null;
440
+ }
441
+
442
+ _handleKeyboardProtocol(params) {
443
+ const parseNum = (value, fallback = 0) => {
444
+ const n = Number(String(value ?? "").split(":")[0]);
445
+ return Number.isFinite(n) ? n : fallback;
446
+ };
447
+
448
+ if (params === "?") return `\x1b[?${this.keyboardProtocolFlags}u`;
449
+ if (params.startsWith("=")) {
450
+ const parts = params.slice(1).split(";");
451
+ const flags = parseNum(parts[0], 0);
452
+ const mode = parseNum(parts[1], 1);
453
+ if (mode === 2) this.keyboardProtocolFlags |= flags;
454
+ else if (mode === 3) this.keyboardProtocolFlags &= ~flags;
455
+ else this.keyboardProtocolFlags = flags;
456
+ return null;
457
+ }
458
+ if (params.startsWith(">")) {
459
+ const flags = parseNum(params.slice(1), 0);
460
+ this.keyboardProtocolStack.push(this.keyboardProtocolFlags);
461
+ if (this.keyboardProtocolStack.length > 32) this.keyboardProtocolStack.shift();
462
+ this.keyboardProtocolFlags = flags;
463
+ return null;
464
+ }
465
+ if (params.startsWith("<")) {
466
+ const count = Math.max(1, parseNum(params.slice(1), 1));
467
+ for (let i = 0; i < count; i++) {
468
+ this.keyboardProtocolFlags = this.keyboardProtocolStack.length > 0 ? this.keyboardProtocolStack.pop() : 0;
469
+ }
470
+ return null;
471
+ }
472
+ return null;
473
+ }
474
+
475
+ _handleXtermKeyFormat(raw) {
476
+ const parts = raw.slice(1).split(";").map(p => Number(p));
477
+ const id = parts[0];
478
+ const value = parts[1];
479
+ if (id === 4) this.formatOtherKeys = Number.isFinite(value) ? value : 0;
480
+ }
481
+
482
+ _handleXtermKeyModifier(raw) {
483
+ const parts = raw.slice(1).split(";").map(p => Number(p));
484
+ const id = parts[0];
485
+ const value = parts[1];
486
+ if (id === 4) this.modifyOtherKeys = Number.isFinite(value) ? value : 0;
487
+ }
488
+
489
+ _handleSGR(parts) {
490
+ if (parts.length === 0 || (parts.length === 1 && parts[0] === 0)) {
491
+ this.sgr = { fg: "default", bg: "default", bold: false, italic: false, underline: false, reverse: false };
492
+ return;
493
+ }
494
+ let i = 0;
495
+ while (i < parts.length) {
496
+ const n = parts[i];
497
+ switch (true) {
498
+ case n === 0: this.sgr = { fg: "default", bg: "default", bold: false, italic: false, underline: false, reverse: false }; break;
499
+ case n === 1: this.sgr.bold = true; break;
500
+ case n === 2: break; // dim, ignore
501
+ case n === 3: this.sgr.italic = true; break;
502
+ case n === 4: this.sgr.underline = true; break;
503
+ case n === 7: this.sgr.reverse = true; break;
504
+ case n === 21:
505
+ case n === 22: this.sgr.bold = false; break;
506
+ case n === 23: this.sgr.italic = false; break;
507
+ case n === 24: this.sgr.underline = false; break;
508
+ case n === 27: this.sgr.reverse = false; break;
509
+ case n >= 30 && n <= 37: this.sgr.fg = ANSI_COLORS[n - 30]; break;
510
+ case n === 38: {
511
+ const mode = parts[i + 1];
512
+ if (mode === 5) { this.sgr.fg = parts[i + 2] ?? 0; i += 2; }
513
+ else if (mode === 2) {
514
+ this.sgr.fg = `#${toHex2(parts[i+2])}${toHex2(parts[i+3])}${toHex2(parts[i+4])}`;
515
+ i += 4;
516
+ }
517
+ break;
518
+ }
519
+ case n === 39: this.sgr.fg = "default"; break;
520
+ case n >= 40 && n <= 47: this.sgr.bg = ANSI_COLORS[n - 40]; break;
521
+ case n === 48: {
522
+ const mode = parts[i + 1];
523
+ if (mode === 5) { this.sgr.bg = parts[i + 2] ?? 0; i += 2; }
524
+ else if (mode === 2) {
525
+ this.sgr.bg = `#${toHex2(parts[i+2])}${toHex2(parts[i+3])}${toHex2(parts[i+4])}`;
526
+ i += 4;
527
+ }
528
+ break;
529
+ }
530
+ case n === 49: this.sgr.bg = "default"; break;
531
+ case n >= 90 && n <= 97: this.sgr.fg = ANSI_COLORS[n - 90 + 8]; break;
532
+ case n >= 100 && n <= 107: this.sgr.bg = ANSI_COLORS[n - 100 + 8]; break;
533
+ }
534
+ i++;
535
+ }
536
+ }
537
+
538
+ resize(cols, rows) {
539
+ cols = Math.max(1, cols);
540
+ rows = Math.max(1, rows);
541
+ const newCells = Array.from({ length: cols * rows }, () => blankCell());
542
+ for (let y = 0; y < Math.min(rows, this.rows); y++) {
543
+ for (let x = 0; x < Math.min(cols, this.cols); x++) {
544
+ newCells[y * cols + x] = { ...this.cells[this._idx(x, y)] };
545
+ }
546
+ }
547
+ this.cols = cols;
548
+ this.rows = rows;
549
+ this.cells = newCells;
550
+ this.cx = Math.min(this.cx, cols - 1);
551
+ this.cy = Math.min(this.cy, rows - 1);
552
+ this.wrapPending = false;
553
+ this.scrollTop = 0;
554
+ this.scrollBottom = rows - 1;
555
+ }
556
+
557
+ getRow(y) {
558
+ if (y < 0 || y >= this.rows) return Array.from({ length: this.cols }, blankCell);
559
+ if (this.scrollOffset === 0) {
560
+ return this.cells.slice(y * this.cols, (y + 1) * this.cols);
561
+ }
562
+ // Map view row y to virtual history + live combined buffer
563
+ const histStart = this.scrollback.length - this.scrollOffset;
564
+ const virtualY = histStart + y;
565
+ if (virtualY < 0) return Array.from({ length: this.cols }, blankCell);
566
+ if (virtualY < this.scrollback.length) return this.scrollback[virtualY];
567
+ const liveY = virtualY - this.scrollback.length;
568
+ if (liveY >= this.rows) return Array.from({ length: this.cols }, blankCell);
569
+ return this.cells.slice(liveY * this.cols, (liveY + 1) * this.cols);
570
+ }
571
+ }