@gkoreli/ghostty-vt-js 0.3.2
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/LICENSE +21 -0
- package/README.md +89 -0
- package/THIRD_PARTY_NOTICES.md +45 -0
- package/dist/browser-terminal/buffer.d.ts +93 -0
- package/dist/browser-terminal/buffer.d.ts.map +1 -0
- package/dist/browser-terminal/buffer.js +337 -0
- package/dist/browser-terminal/buffer.js.map +1 -0
- package/dist/browser-terminal/encoding.d.ts +17 -0
- package/dist/browser-terminal/encoding.d.ts.map +1 -0
- package/dist/browser-terminal/encoding.js +22 -0
- package/dist/browser-terminal/encoding.js.map +1 -0
- package/dist/browser-terminal/event-emitter.d.ts +8 -0
- package/dist/browser-terminal/event-emitter.d.ts.map +1 -0
- package/dist/browser-terminal/event-emitter.js +27 -0
- package/dist/browser-terminal/event-emitter.js.map +1 -0
- package/dist/browser-terminal/frame-hold.d.ts +13 -0
- package/dist/browser-terminal/frame-hold.d.ts.map +1 -0
- package/dist/browser-terminal/frame-hold.js +17 -0
- package/dist/browser-terminal/frame-hold.js.map +1 -0
- package/dist/browser-terminal/geometry.d.ts +229 -0
- package/dist/browser-terminal/geometry.d.ts.map +1 -0
- package/dist/browser-terminal/geometry.js +299 -0
- package/dist/browser-terminal/geometry.js.map +1 -0
- package/dist/browser-terminal/ghostty.d.ts +478 -0
- package/dist/browser-terminal/ghostty.d.ts.map +1 -0
- package/dist/browser-terminal/ghostty.js +1212 -0
- package/dist/browser-terminal/ghostty.js.map +1 -0
- package/dist/browser-terminal/gpu/adapter.d.ts +70 -0
- package/dist/browser-terminal/gpu/adapter.d.ts.map +1 -0
- package/dist/browser-terminal/gpu/adapter.js +260 -0
- package/dist/browser-terminal/gpu/adapter.js.map +1 -0
- package/dist/browser-terminal/gpu/core/canvas-glyph-rasterizer.d.ts +33 -0
- package/dist/browser-terminal/gpu/core/canvas-glyph-rasterizer.d.ts.map +1 -0
- package/dist/browser-terminal/gpu/core/canvas-glyph-rasterizer.js +159 -0
- package/dist/browser-terminal/gpu/core/canvas-glyph-rasterizer.js.map +1 -0
- package/dist/browser-terminal/gpu/core/context-loss-watchdog.d.ts +35 -0
- package/dist/browser-terminal/gpu/core/context-loss-watchdog.d.ts.map +1 -0
- package/dist/browser-terminal/gpu/core/context-loss-watchdog.js +73 -0
- package/dist/browser-terminal/gpu/core/context-loss-watchdog.js.map +1 -0
- package/dist/browser-terminal/gpu/core/frame.d.ts +68 -0
- package/dist/browser-terminal/gpu/core/frame.d.ts.map +1 -0
- package/dist/browser-terminal/gpu/core/frame.js +167 -0
- package/dist/browser-terminal/gpu/core/frame.js.map +1 -0
- package/dist/browser-terminal/gpu/core/glyph-atlas.d.ts +136 -0
- package/dist/browser-terminal/gpu/core/glyph-atlas.d.ts.map +1 -0
- package/dist/browser-terminal/gpu/core/glyph-atlas.js +281 -0
- package/dist/browser-terminal/gpu/core/glyph-atlas.js.map +1 -0
- package/dist/browser-terminal/gpu/core/procedural-shapes.d.ts +24 -0
- package/dist/browser-terminal/gpu/core/procedural-shapes.d.ts.map +1 -0
- package/dist/browser-terminal/gpu/core/procedural-shapes.js +316 -0
- package/dist/browser-terminal/gpu/core/procedural-shapes.js.map +1 -0
- package/dist/browser-terminal/gpu/core/webgl2-renderer.d.ts +107 -0
- package/dist/browser-terminal/gpu/core/webgl2-renderer.d.ts.map +1 -0
- package/dist/browser-terminal/gpu/core/webgl2-renderer.js +698 -0
- package/dist/browser-terminal/gpu/core/webgl2-renderer.js.map +1 -0
- package/dist/browser-terminal/gpu/create-renderer.d.ts +15 -0
- package/dist/browser-terminal/gpu/create-renderer.d.ts.map +1 -0
- package/dist/browser-terminal/gpu/create-renderer.js +33 -0
- package/dist/browser-terminal/gpu/create-renderer.js.map +1 -0
- package/dist/browser-terminal/index.d.ts +47 -0
- package/dist/browser-terminal/index.d.ts.map +1 -0
- package/dist/browser-terminal/index.js +75 -0
- package/dist/browser-terminal/index.js.map +1 -0
- package/dist/browser-terminal/input-handler.d.ts +262 -0
- package/dist/browser-terminal/input-handler.d.ts.map +1 -0
- package/dist/browser-terminal/input-handler.js +978 -0
- package/dist/browser-terminal/input-handler.js.map +1 -0
- package/dist/browser-terminal/interfaces.d.ts +230 -0
- package/dist/browser-terminal/interfaces.d.ts.map +1 -0
- package/dist/browser-terminal/interfaces.js +5 -0
- package/dist/browser-terminal/interfaces.js.map +1 -0
- package/dist/browser-terminal/link-detector.d.ts +76 -0
- package/dist/browser-terminal/link-detector.d.ts.map +1 -0
- package/dist/browser-terminal/link-detector.js +165 -0
- package/dist/browser-terminal/link-detector.js.map +1 -0
- package/dist/browser-terminal/providers/osc8-link-provider.d.ts +56 -0
- package/dist/browser-terminal/providers/osc8-link-provider.d.ts.map +1 -0
- package/dist/browser-terminal/providers/osc8-link-provider.js +204 -0
- package/dist/browser-terminal/providers/osc8-link-provider.js.map +1 -0
- package/dist/browser-terminal/providers/url-regex-provider.d.ts +68 -0
- package/dist/browser-terminal/providers/url-regex-provider.d.ts.map +1 -0
- package/dist/browser-terminal/providers/url-regex-provider.js +107 -0
- package/dist/browser-terminal/providers/url-regex-provider.js.map +1 -0
- package/dist/browser-terminal/renderer-interface.d.ts +59 -0
- package/dist/browser-terminal/renderer-interface.d.ts.map +1 -0
- package/dist/browser-terminal/renderer-interface.js +2 -0
- package/dist/browser-terminal/renderer-interface.js.map +1 -0
- package/dist/browser-terminal/renderer.d.ts +254 -0
- package/dist/browser-terminal/renderer.d.ts.map +1 -0
- package/dist/browser-terminal/renderer.js +905 -0
- package/dist/browser-terminal/renderer.js.map +1 -0
- package/dist/browser-terminal/scrollbar-overlay.d.ts +45 -0
- package/dist/browser-terminal/scrollbar-overlay.d.ts.map +1 -0
- package/dist/browser-terminal/scrollbar-overlay.js +89 -0
- package/dist/browser-terminal/scrollbar-overlay.js.map +1 -0
- package/dist/browser-terminal/selection-manager.d.ts +180 -0
- package/dist/browser-terminal/selection-manager.d.ts.map +1 -0
- package/dist/browser-terminal/selection-manager.js +913 -0
- package/dist/browser-terminal/selection-manager.js.map +1 -0
- package/dist/browser-terminal/terminal.d.ts +423 -0
- package/dist/browser-terminal/terminal.d.ts.map +1 -0
- package/dist/browser-terminal/terminal.js +1754 -0
- package/dist/browser-terminal/terminal.js.map +1 -0
- package/dist/browser-terminal/types.d.ts +595 -0
- package/dist/browser-terminal/types.d.ts.map +1 -0
- package/dist/browser-terminal/types.js +467 -0
- package/dist/browser-terminal/types.js.map +1 -0
- package/dist/browser-terminal/viewport-scroller.d.ts +94 -0
- package/dist/browser-terminal/viewport-scroller.d.ts.map +1 -0
- package/dist/browser-terminal/viewport-scroller.js +159 -0
- package/dist/browser-terminal/viewport-scroller.js.map +1 -0
- package/dist/browser-terminal/vt/effects.d.ts +114 -0
- package/dist/browser-terminal/vt/effects.d.ts.map +1 -0
- package/dist/browser-terminal/vt/effects.js +143 -0
- package/dist/browser-terminal/vt/effects.js.map +1 -0
- package/dist/browser-terminal/vt/enums.d.ts +155 -0
- package/dist/browser-terminal/vt/enums.d.ts.map +1 -0
- package/dist/browser-terminal/vt/enums.js +161 -0
- package/dist/browser-terminal/vt/enums.js.map +1 -0
- package/dist/browser-terminal/vt/mouse.d.ts +166 -0
- package/dist/browser-terminal/vt/mouse.d.ts.map +1 -0
- package/dist/browser-terminal/vt/mouse.js +259 -0
- package/dist/browser-terminal/vt/mouse.js.map +1 -0
- package/dist/browser-terminal/vt/paste.d.ts +53 -0
- package/dist/browser-terminal/vt/paste.d.ts.map +1 -0
- package/dist/browser-terminal/vt/paste.js +89 -0
- package/dist/browser-terminal/vt/paste.js.map +1 -0
- package/dist/browser-terminal/vt/render-state.d.ts +208 -0
- package/dist/browser-terminal/vt/render-state.d.ts.map +1 -0
- package/dist/browser-terminal/vt/render-state.js +776 -0
- package/dist/browser-terminal/vt/render-state.js.map +1 -0
- package/dist/browser-terminal/vt/style.d.ts +54 -0
- package/dist/browser-terminal/vt/style.d.ts.map +1 -0
- package/dist/browser-terminal/vt/style.js +92 -0
- package/dist/browser-terminal/vt/style.js.map +1 -0
- package/dist/browser-terminal/wasm-loader.d.ts +39 -0
- package/dist/browser-terminal/wasm-loader.d.ts.map +1 -0
- package/dist/browser-terminal/wasm-loader.js +73 -0
- package/dist/browser-terminal/wasm-loader.js.map +1 -0
- package/dist/browser-terminal/write-buffer.d.ts +40 -0
- package/dist/browser-terminal/write-buffer.d.ts.map +1 -0
- package/dist/browser-terminal/write-buffer.js +144 -0
- package/dist/browser-terminal/write-buffer.js.map +1 -0
- package/dist/protocol/index.d.ts +135 -0
- package/dist/protocol/index.d.ts.map +1 -0
- package/dist/protocol/index.js +76 -0
- package/dist/protocol/index.js.map +1 -0
- package/dist/react/index.d.ts +59 -0
- package/dist/react/index.d.ts.map +1 -0
- package/dist/react/index.js +146 -0
- package/dist/react/index.js.map +1 -0
- package/dist/server/index.d.ts +130 -0
- package/dist/server/index.d.ts.map +1 -0
- package/dist/server/index.js +191 -0
- package/dist/server/index.js.map +1 -0
- package/dist/terminal-screen-emulator/emulator.d.ts +66 -0
- package/dist/terminal-screen-emulator/emulator.d.ts.map +1 -0
- package/dist/terminal-screen-emulator/emulator.js +286 -0
- package/dist/terminal-screen-emulator/emulator.js.map +1 -0
- package/dist/terminal-screen-emulator/index.d.ts +82 -0
- package/dist/terminal-screen-emulator/index.d.ts.map +1 -0
- package/dist/terminal-screen-emulator/index.js +92 -0
- package/dist/terminal-screen-emulator/index.js.map +1 -0
- package/dist/terminal-screen-emulator/types.d.ts +382 -0
- package/dist/terminal-screen-emulator/types.d.ts.map +1 -0
- package/dist/terminal-screen-emulator/types.js +91 -0
- package/dist/terminal-screen-emulator/types.js.map +1 -0
- package/dist/terminal-screen-emulator/wasm-loader.d.ts +71 -0
- package/dist/terminal-screen-emulator/wasm-loader.d.ts.map +1 -0
- package/dist/terminal-screen-emulator/wasm-loader.js +162 -0
- package/dist/terminal-screen-emulator/wasm-loader.js.map +1 -0
- package/dist/themes/index.d.ts +6 -0
- package/dist/themes/index.d.ts.map +1 -0
- package/dist/themes/index.js +51 -0
- package/dist/themes/index.js.map +1 -0
- package/dist/wasm/alloc.d.ts +170 -0
- package/dist/wasm/alloc.d.ts.map +1 -0
- package/dist/wasm/alloc.js +307 -0
- package/dist/wasm/alloc.js.map +1 -0
- package/dist/wasm/compile.d.ts +73 -0
- package/dist/wasm/compile.d.ts.map +1 -0
- package/dist/wasm/compile.js +82 -0
- package/dist/wasm/compile.js.map +1 -0
- package/dist/wasm/exports.d.ts +234 -0
- package/dist/wasm/exports.d.ts.map +1 -0
- package/dist/wasm/exports.js +15 -0
- package/dist/wasm/exports.js.map +1 -0
- package/dist/wasm/fn-table.d.ts +119 -0
- package/dist/wasm/fn-table.d.ts.map +1 -0
- package/dist/wasm/fn-table.js +168 -0
- package/dist/wasm/fn-table.js.map +1 -0
- package/dist/wasm/memory.d.ts +59 -0
- package/dist/wasm/memory.d.ts.map +1 -0
- package/dist/wasm/memory.js +89 -0
- package/dist/wasm/memory.js.map +1 -0
- package/dist/wasm/type-layouts.d.ts +61 -0
- package/dist/wasm/type-layouts.d.ts.map +1 -0
- package/dist/wasm/type-layouts.js +32 -0
- package/dist/wasm/type-layouts.js.map +1 -0
- package/package.json +94 -0
- package/wasm/ghostty-vt.wasm +0 -0
|
@@ -0,0 +1,905 @@
|
|
|
1
|
+
// Portions originally derived from coder/ghostty-web (MIT — see ./LICENSE):
|
|
2
|
+
// https://github.com/coder/ghostty-web/blob/6a1a50df5b4f6b34d1b1de10fad3a0fc811bfbc0/lib/renderer.ts
|
|
3
|
+
// Substantially rewritten since; this file is ours and does not track that project.
|
|
4
|
+
import { CellFlags } from './types.js';
|
|
5
|
+
// ============================================================================
|
|
6
|
+
// Default Theme
|
|
7
|
+
// ============================================================================
|
|
8
|
+
/**
|
|
9
|
+
* Default font stack: locally installed Nerd Fonts first, generic monospace
|
|
10
|
+
* last.
|
|
11
|
+
*
|
|
12
|
+
* Terminal content routinely contains Private Use Area glyphs — eza/lsd file
|
|
13
|
+
* icons, powerline prompt separators (p10k, starship), devicons — that
|
|
14
|
+
* generic `monospace` cannot shape; they render as tofu. Canvas `fillText`
|
|
15
|
+
* falls back through the stack PER GLYPH, so listing Nerd Fonts costs nothing
|
|
16
|
+
* when absent and lights up icons when any one of them is installed — which
|
|
17
|
+
* on a developer box running those tools is nearly always. Cell metrics are
|
|
18
|
+
* measured with the same stack (geometry.ts), so measurement and paint can't
|
|
19
|
+
* disagree. Consumers with a bundled webfont can still override via
|
|
20
|
+
* `fontFamily`.
|
|
21
|
+
*/
|
|
22
|
+
export const DEFAULT_FONT_FAMILY = "'MesloLGS NF', 'MesloLGS Nerd Font', 'JetBrainsMono Nerd Font', "
|
|
23
|
+
+ "'FiraCode Nerd Font', 'Hack Nerd Font', 'CaskaydiaCove Nerd Font', "
|
|
24
|
+
+ "'Symbols Nerd Font', ui-monospace, Menlo, Consolas, monospace";
|
|
25
|
+
export const DEFAULT_THEME = {
|
|
26
|
+
foreground: '#d4d4d4',
|
|
27
|
+
// Black, matching the wasm terminal's own built-in default. This constant is
|
|
28
|
+
// pushed INTO the wasm terminal (one authority for 'default background', so
|
|
29
|
+
// grid and gutter can never disagree) — keeping it identical to what the
|
|
30
|
+
// engine would choose alone means adopting the resolved default is not a
|
|
31
|
+
// visual change for anyone.
|
|
32
|
+
background: '#000000',
|
|
33
|
+
cursor: '#ffffff',
|
|
34
|
+
cursorAccent: '#000000',
|
|
35
|
+
// Selection colors: solid colors that replace cell bg/fg when selected
|
|
36
|
+
// Using Ghostty's approach: selection bg = default fg, selection fg = default bg
|
|
37
|
+
selectionBackground: '#d4d4d4',
|
|
38
|
+
selectionForeground: '#000000',
|
|
39
|
+
black: '#000000',
|
|
40
|
+
red: '#cd3131',
|
|
41
|
+
green: '#0dbc79',
|
|
42
|
+
yellow: '#e5e510',
|
|
43
|
+
blue: '#2472c8',
|
|
44
|
+
magenta: '#bc3fbc',
|
|
45
|
+
cyan: '#11a8cd',
|
|
46
|
+
white: '#e5e5e5',
|
|
47
|
+
brightBlack: '#666666',
|
|
48
|
+
brightRed: '#f14c4c',
|
|
49
|
+
brightGreen: '#23d18b',
|
|
50
|
+
brightYellow: '#f5f543',
|
|
51
|
+
brightBlue: '#3b8eea',
|
|
52
|
+
brightMagenta: '#d670d6',
|
|
53
|
+
brightCyan: '#29b8db',
|
|
54
|
+
brightWhite: '#ffffff',
|
|
55
|
+
};
|
|
56
|
+
// ============================================================================
|
|
57
|
+
// Helpers
|
|
58
|
+
// ============================================================================
|
|
59
|
+
/**
|
|
60
|
+
* Parse a CSS color string (`#rgb`, `#rrggbb`, or `rgb(r, g, b)`) into 0–255
|
|
61
|
+
* RGB channels. Used to compare cell colors against the theme background so we
|
|
62
|
+
* can skip per-cell fills for cells matching the default. Returns `{0,0,0}` if
|
|
63
|
+
* the input is unrecognized — defensive: theme strings are well-controlled.
|
|
64
|
+
*/
|
|
65
|
+
function parseCssColorToRgb(color) {
|
|
66
|
+
if (color.startsWith('#')) {
|
|
67
|
+
let hex = color.slice(1);
|
|
68
|
+
if (hex.length === 3) {
|
|
69
|
+
hex = hex[0] + hex[0] + hex[1] + hex[1] + hex[2] + hex[2];
|
|
70
|
+
}
|
|
71
|
+
const v = Number.parseInt(hex, 16);
|
|
72
|
+
if (Number.isNaN(v))
|
|
73
|
+
return { r: 0, g: 0, b: 0 };
|
|
74
|
+
return { r: (v >> 16) & 0xff, g: (v >> 8) & 0xff, b: v & 0xff };
|
|
75
|
+
}
|
|
76
|
+
const m = color.match(/rgb\((\d+),\s*(\d+),\s*(\d+)\)/);
|
|
77
|
+
if (m) {
|
|
78
|
+
return {
|
|
79
|
+
r: Number.parseInt(m[1], 10),
|
|
80
|
+
g: Number.parseInt(m[2], 10),
|
|
81
|
+
b: Number.parseInt(m[3], 10),
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
return { r: 0, g: 0, b: 0 };
|
|
85
|
+
}
|
|
86
|
+
// ============================================================================
|
|
87
|
+
// CanvasRenderer Class
|
|
88
|
+
// ============================================================================
|
|
89
|
+
export class CanvasRenderer {
|
|
90
|
+
backend = 'canvas';
|
|
91
|
+
canvas;
|
|
92
|
+
ctx;
|
|
93
|
+
fontSize;
|
|
94
|
+
fontFamily;
|
|
95
|
+
cursorStyle;
|
|
96
|
+
cursorBlink;
|
|
97
|
+
theme;
|
|
98
|
+
// Pre-parsed RGB form of `theme.background`, used to identify cells whose bg
|
|
99
|
+
// matches the theme default — those cells skip their per-cell fillRect because
|
|
100
|
+
// the line-level fill already covers them. Recomputed in `setTheme()`.
|
|
101
|
+
themeBackgroundRgb;
|
|
102
|
+
devicePixelRatio;
|
|
103
|
+
/** The geometry owner — sole source of cell metrics and the canvas box. */
|
|
104
|
+
geometry;
|
|
105
|
+
palette;
|
|
106
|
+
// Cursor blinking state
|
|
107
|
+
cursorVisible = true;
|
|
108
|
+
cursorBlinkInterval;
|
|
109
|
+
lastCursorPosition = { x: 0, y: 0 };
|
|
110
|
+
// Viewport tracking (for scrolling)
|
|
111
|
+
lastViewportY = 0;
|
|
112
|
+
// Current buffer being rendered (for grapheme lookups)
|
|
113
|
+
currentBuffer = null;
|
|
114
|
+
// Selection manager (for rendering selection)
|
|
115
|
+
selectionManager;
|
|
116
|
+
// Cached selection coordinates for current render pass (viewport-relative)
|
|
117
|
+
currentSelectionCoords = null;
|
|
118
|
+
// Link rendering state
|
|
119
|
+
hoveredHyperlinkId = 0;
|
|
120
|
+
previousHoveredHyperlinkId = 0;
|
|
121
|
+
// Regex link hover tracking (for links without hyperlink_id)
|
|
122
|
+
hoveredLinkRange = null;
|
|
123
|
+
previousHoveredLinkRange = null;
|
|
124
|
+
constructor(canvas, options) {
|
|
125
|
+
this.canvas = canvas;
|
|
126
|
+
const ctx = canvas.getContext('2d', { alpha: true });
|
|
127
|
+
if (!ctx) {
|
|
128
|
+
throw new Error('Failed to get 2D rendering context');
|
|
129
|
+
}
|
|
130
|
+
this.ctx = ctx;
|
|
131
|
+
// Apply options
|
|
132
|
+
this.fontSize = options.fontSize ?? 15;
|
|
133
|
+
this.fontFamily = options.fontFamily ?? DEFAULT_FONT_FAMILY;
|
|
134
|
+
this.geometry = options.geometry;
|
|
135
|
+
this.cursorStyle = options.cursorStyle ?? 'block';
|
|
136
|
+
this.cursorBlink = options.cursorBlink ?? false;
|
|
137
|
+
this.theme = { ...DEFAULT_THEME, ...options.theme };
|
|
138
|
+
this.themeBackgroundRgb = parseCssColorToRgb(this.theme.background);
|
|
139
|
+
this.devicePixelRatio = options.devicePixelRatio ?? window.devicePixelRatio ?? 1;
|
|
140
|
+
// Build color palette (16 ANSI colors)
|
|
141
|
+
this.palette = [
|
|
142
|
+
this.theme.black,
|
|
143
|
+
this.theme.red,
|
|
144
|
+
this.theme.green,
|
|
145
|
+
this.theme.yellow,
|
|
146
|
+
this.theme.blue,
|
|
147
|
+
this.theme.magenta,
|
|
148
|
+
this.theme.cyan,
|
|
149
|
+
this.theme.white,
|
|
150
|
+
this.theme.brightBlack,
|
|
151
|
+
this.theme.brightRed,
|
|
152
|
+
this.theme.brightGreen,
|
|
153
|
+
this.theme.brightYellow,
|
|
154
|
+
this.theme.brightBlue,
|
|
155
|
+
this.theme.brightMagenta,
|
|
156
|
+
this.theme.brightCyan,
|
|
157
|
+
this.theme.brightWhite,
|
|
158
|
+
];
|
|
159
|
+
// Measure font metrics
|
|
160
|
+
// Setup cursor blinking if enabled
|
|
161
|
+
if (this.cursorBlink) {
|
|
162
|
+
this.startCursorBlink();
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
// ==========================================================================
|
|
166
|
+
// Font Metrics Measurement
|
|
167
|
+
// ==========================================================================
|
|
168
|
+
/** Cell metrics, read from the geometry owner (never measured here). */
|
|
169
|
+
get metrics() {
|
|
170
|
+
return this.geometry.metrics;
|
|
171
|
+
}
|
|
172
|
+
// ==========================================================================
|
|
173
|
+
// Color Conversion
|
|
174
|
+
// ==========================================================================
|
|
175
|
+
rgbToCSS(r, g, b) {
|
|
176
|
+
return `rgb(${r}, ${g}, ${b})`;
|
|
177
|
+
}
|
|
178
|
+
// ==========================================================================
|
|
179
|
+
// Canvas Sizing
|
|
180
|
+
// ==========================================================================
|
|
181
|
+
/**
|
|
182
|
+
* Resize canvas to fit terminal dimensions
|
|
183
|
+
*/
|
|
184
|
+
resize(cols, rows) {
|
|
185
|
+
// The canvas box is computed once, by the geometry owner (the geometry-authority design): CSS px
|
|
186
|
+
// for layout, integer device px for the backing store. `cols`/`rows` are
|
|
187
|
+
// accepted for the dirty-tracking comparison below but the box is NOT
|
|
188
|
+
// re-derived here — that duplicate derivation is what let the canvas and
|
|
189
|
+
// the grid disagree.
|
|
190
|
+
const box = this.geometry.canvasBox();
|
|
191
|
+
// The canvas covers the WHOLE pane; the grid is painted inset within it
|
|
192
|
+
// (box.gridOrigin*). Consumers just size the host 100%/100% — there is no
|
|
193
|
+
// CSS margin to manage and no second background to keep in sync.
|
|
194
|
+
this.canvas.style.width = `${box.cssWidth}px`;
|
|
195
|
+
this.canvas.style.height = `${box.cssHeight}px`;
|
|
196
|
+
// Backing store in integer device pixels (fractional values are truncated
|
|
197
|
+
// by the browser, which shifts every cell after the truncation).
|
|
198
|
+
this.canvas.width = box.deviceWidth;
|
|
199
|
+
this.canvas.height = box.deviceHeight;
|
|
200
|
+
// Base transform = dpr scale + grid origin, applied ONCE here. Every cell
|
|
201
|
+
// draw call below stays in grid space (x * cellWidth) and lands in the
|
|
202
|
+
// right place; nothing else in this class knows the inset exists.
|
|
203
|
+
// (Setting canvas.width/height reset the context, so this is authoritative.)
|
|
204
|
+
this.ctx.setTransform(this.devicePixelRatio, 0, 0, this.devicePixelRatio, Math.round(box.gridOriginX * this.devicePixelRatio), Math.round(box.gridOriginY * this.devicePixelRatio));
|
|
205
|
+
// Set text rendering properties for crisp text
|
|
206
|
+
this.ctx.textBaseline = 'alphabetic';
|
|
207
|
+
this.ctx.textAlign = 'left';
|
|
208
|
+
// Fill the whole surface (including the inset gutter) with the theme
|
|
209
|
+
// background — in surface space, so temporarily drop the grid transform.
|
|
210
|
+
this.ctx.save();
|
|
211
|
+
this.ctx.setTransform(1, 0, 0, 1, 0, 0);
|
|
212
|
+
this.ctx.fillStyle = this.theme.background;
|
|
213
|
+
this.ctx.fillRect(0, 0, this.canvas.width, this.canvas.height);
|
|
214
|
+
this.ctx.restore();
|
|
215
|
+
}
|
|
216
|
+
// ==========================================================================
|
|
217
|
+
// Main Rendering
|
|
218
|
+
// ==========================================================================
|
|
219
|
+
/**
|
|
220
|
+
* Render the terminal buffer to canvas
|
|
221
|
+
*/
|
|
222
|
+
render(buffer, forceAll = false, viewportY = 0, scrollbackProvider, scrollbarOpacity = 1) {
|
|
223
|
+
// Store buffer reference for grapheme lookups in renderCell
|
|
224
|
+
this.currentBuffer = buffer;
|
|
225
|
+
// Frame contract: explicit `beginFrame()` re-snapshots the render state so
|
|
226
|
+
// every read below this point (cursor, dims, dirty flags, lines) sees the
|
|
227
|
+
// same point in time. Mirrors `ghostty_render_state_update(state, term)`
|
|
228
|
+
// (`render.h`). Falls back to a no-op for legacy IRenderable shapes.
|
|
229
|
+
buffer.beginFrame?.();
|
|
230
|
+
const cursor = buffer.getCursor();
|
|
231
|
+
const dims = buffer.getDimensions();
|
|
232
|
+
const scrollbackLength = scrollbackProvider ? scrollbackProvider.getScrollbackLength() : 0;
|
|
233
|
+
// Check if buffer needs full redraw (e.g., screen change between normal/alternate)
|
|
234
|
+
if (buffer.needsFullRedraw?.()) {
|
|
235
|
+
forceAll = true;
|
|
236
|
+
}
|
|
237
|
+
// Resize if the backing store disagrees with the geometry owner's box.
|
|
238
|
+
// Compared against canvasBox() — NOT re-derived from the grid — because the
|
|
239
|
+
// surface covers the whole pane, not just the grid (a grid-based formula
|
|
240
|
+
// here would force a resize on every frame and fight the inset design).
|
|
241
|
+
const box = this.geometry.canvasBox();
|
|
242
|
+
const needsResize = this.canvas.width !== box.deviceWidth || this.canvas.height !== box.deviceHeight;
|
|
243
|
+
if (needsResize) {
|
|
244
|
+
this.resize(dims.cols, dims.rows);
|
|
245
|
+
forceAll = true; // Force full render after resize
|
|
246
|
+
}
|
|
247
|
+
// Force re-render when viewport changes (scrolling)
|
|
248
|
+
if (viewportY !== this.lastViewportY) {
|
|
249
|
+
forceAll = true;
|
|
250
|
+
this.lastViewportY = viewportY;
|
|
251
|
+
}
|
|
252
|
+
this.withGridClip(dims, () => {
|
|
253
|
+
// Check if cursor position changed or if blinking (need to redraw cursor line)
|
|
254
|
+
const cursorMoved = cursor.x !== this.lastCursorPosition.x || cursor.y !== this.lastCursorPosition.y;
|
|
255
|
+
if (cursorMoved || this.cursorBlink) {
|
|
256
|
+
// Mark cursor lines as needing redraw
|
|
257
|
+
if (!forceAll && !buffer.isRowDirty(cursor.y)) {
|
|
258
|
+
// Need to redraw cursor line
|
|
259
|
+
const line = buffer.getLine(cursor.y);
|
|
260
|
+
if (line) {
|
|
261
|
+
this.renderLine(line, cursor.y, dims.cols);
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
if (cursorMoved && this.lastCursorPosition.y !== cursor.y) {
|
|
265
|
+
// Also redraw old cursor line if cursor moved to different line
|
|
266
|
+
if (!forceAll && !buffer.isRowDirty(this.lastCursorPosition.y)) {
|
|
267
|
+
const line = buffer.getLine(this.lastCursorPosition.y);
|
|
268
|
+
if (line) {
|
|
269
|
+
this.renderLine(line, this.lastCursorPosition.y, dims.cols);
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
// Check if we need to redraw selection-related lines
|
|
275
|
+
const hasSelection = this.selectionManager && this.selectionManager.hasSelection();
|
|
276
|
+
const selectionRows = new Set();
|
|
277
|
+
// Cache selection coordinates for use during cell rendering
|
|
278
|
+
// This is used by isInSelection() to determine if a cell needs selection colors
|
|
279
|
+
this.currentSelectionCoords = hasSelection ? this.selectionManager.getSelectionCoords() : null;
|
|
280
|
+
// Mark current selection rows for redraw (includes programmatic selections)
|
|
281
|
+
if (this.currentSelectionCoords) {
|
|
282
|
+
const coords = this.currentSelectionCoords;
|
|
283
|
+
for (let row = coords.startRow; row <= coords.endRow; row++) {
|
|
284
|
+
selectionRows.add(row);
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
// Always mark dirty selection rows for redraw (to clear old overlay)
|
|
288
|
+
if (this.selectionManager) {
|
|
289
|
+
const dirtyRows = this.selectionManager.getDirtySelectionRows();
|
|
290
|
+
if (dirtyRows.size > 0) {
|
|
291
|
+
for (const row of dirtyRows) {
|
|
292
|
+
selectionRows.add(row);
|
|
293
|
+
}
|
|
294
|
+
// Clear the dirty rows tracking after marking for redraw
|
|
295
|
+
this.selectionManager.clearDirtySelectionRows();
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
// Track rows with hyperlinks that need redraw when hover changes
|
|
299
|
+
const hyperlinkRows = new Set();
|
|
300
|
+
const hyperlinkChanged = this.hoveredHyperlinkId !== this.previousHoveredHyperlinkId;
|
|
301
|
+
// Field comparison, not JSON.stringify — this runs at 60fps and
|
|
302
|
+
// stringifying two objects per frame is pure allocation churn.
|
|
303
|
+
const a = this.hoveredLinkRange;
|
|
304
|
+
const b = this.previousHoveredLinkRange;
|
|
305
|
+
const linkRangeChanged = (a === null) !== (b === null) ||
|
|
306
|
+
(a !== null && b !== null &&
|
|
307
|
+
(a.startX !== b.startX || a.startY !== b.startY || a.endX !== b.endX || a.endY !== b.endY));
|
|
308
|
+
if (hyperlinkChanged) {
|
|
309
|
+
// Find rows containing the old or new hovered hyperlink
|
|
310
|
+
// Must check the correct buffer based on viewportY (scrollback vs screen)
|
|
311
|
+
for (let y = 0; y < dims.rows; y++) {
|
|
312
|
+
let line = null;
|
|
313
|
+
// Same logic as rendering: fetch from scrollback or screen
|
|
314
|
+
if (viewportY > 0) {
|
|
315
|
+
if (y < viewportY && scrollbackProvider) {
|
|
316
|
+
// This row is from scrollback
|
|
317
|
+
// Floor viewportY for array access (handles fractional values during smooth scroll)
|
|
318
|
+
const scrollbackOffset = scrollbackLength - Math.floor(viewportY) + y;
|
|
319
|
+
line = scrollbackProvider.getScrollbackLine(scrollbackOffset);
|
|
320
|
+
}
|
|
321
|
+
else {
|
|
322
|
+
// This row is from visible screen
|
|
323
|
+
const screenRow = y - Math.floor(viewportY);
|
|
324
|
+
line = buffer.getLine(screenRow);
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
else {
|
|
328
|
+
// At bottom - fetch from visible screen
|
|
329
|
+
line = buffer.getLine(y);
|
|
330
|
+
}
|
|
331
|
+
if (line) {
|
|
332
|
+
for (const cell of line) {
|
|
333
|
+
if (cell.hyperlink_id === this.hoveredHyperlinkId ||
|
|
334
|
+
cell.hyperlink_id === this.previousHoveredHyperlinkId) {
|
|
335
|
+
hyperlinkRows.add(y);
|
|
336
|
+
break; // Found hyperlink in this row
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
// Update previous state
|
|
342
|
+
this.previousHoveredHyperlinkId = this.hoveredHyperlinkId;
|
|
343
|
+
}
|
|
344
|
+
// Track rows affected by link range changes (for regex URLs)
|
|
345
|
+
if (linkRangeChanged) {
|
|
346
|
+
// Add rows from old range
|
|
347
|
+
if (this.previousHoveredLinkRange) {
|
|
348
|
+
for (let y = this.previousHoveredLinkRange.startY; y <= this.previousHoveredLinkRange.endY; y++) {
|
|
349
|
+
hyperlinkRows.add(y);
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
// Add rows from new range
|
|
353
|
+
if (this.hoveredLinkRange) {
|
|
354
|
+
for (let y = this.hoveredLinkRange.startY; y <= this.hoveredLinkRange.endY; y++) {
|
|
355
|
+
hyperlinkRows.add(y);
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
this.previousHoveredLinkRange = this.hoveredLinkRange;
|
|
359
|
+
}
|
|
360
|
+
// Track if anything was actually rendered
|
|
361
|
+
let anyLinesRendered = false;
|
|
362
|
+
// Determine which rows need rendering.
|
|
363
|
+
// We also include adjacent rows (above and below) for each dirty row to handle
|
|
364
|
+
// glyph overflow - tall glyphs like Devanagari vowel signs can extend into
|
|
365
|
+
// adjacent rows' visual space.
|
|
366
|
+
const rowsToRender = new Set();
|
|
367
|
+
for (let y = 0; y < dims.rows; y++) {
|
|
368
|
+
// When scrolled, always force render all lines since we're showing scrollback
|
|
369
|
+
const needsRender = viewportY > 0
|
|
370
|
+
? true
|
|
371
|
+
: forceAll || buffer.isRowDirty(y) || selectionRows.has(y) || hyperlinkRows.has(y);
|
|
372
|
+
if (needsRender) {
|
|
373
|
+
rowsToRender.add(y);
|
|
374
|
+
// Include adjacent rows to handle glyph overflow
|
|
375
|
+
if (y > 0)
|
|
376
|
+
rowsToRender.add(y - 1);
|
|
377
|
+
if (y < dims.rows - 1)
|
|
378
|
+
rowsToRender.add(y + 1);
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
// Render each line
|
|
382
|
+
for (let y = 0; y < dims.rows; y++) {
|
|
383
|
+
if (!rowsToRender.has(y)) {
|
|
384
|
+
continue;
|
|
385
|
+
}
|
|
386
|
+
anyLinesRendered = true;
|
|
387
|
+
// Fetch line from scrollback or visible screen
|
|
388
|
+
let line = null;
|
|
389
|
+
if (viewportY > 0) {
|
|
390
|
+
// Scrolled up - need to fetch from scrollback + visible screen
|
|
391
|
+
// When scrolled up N lines, we want to show:
|
|
392
|
+
// - Scrollback lines (from the end) + visible screen lines
|
|
393
|
+
// Check if this row should come from scrollback or visible screen
|
|
394
|
+
if (y < viewportY && scrollbackProvider) {
|
|
395
|
+
// This row is from scrollback (upper part of viewport)
|
|
396
|
+
// Get from end of scrollback buffer
|
|
397
|
+
// Floor viewportY for array access (handles fractional values during smooth scroll)
|
|
398
|
+
const scrollbackOffset = scrollbackLength - Math.floor(viewportY) + y;
|
|
399
|
+
line = scrollbackProvider.getScrollbackLine(scrollbackOffset);
|
|
400
|
+
}
|
|
401
|
+
else {
|
|
402
|
+
// This row is from visible screen (lower part of viewport)
|
|
403
|
+
const screenRow = viewportY > 0 ? y - Math.floor(viewportY) : y;
|
|
404
|
+
line = buffer.getLine(screenRow);
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
else {
|
|
408
|
+
// At bottom - fetch from visible screen
|
|
409
|
+
line = buffer.getLine(y);
|
|
410
|
+
}
|
|
411
|
+
if (line) {
|
|
412
|
+
this.renderLine(line, y, dims.cols);
|
|
413
|
+
}
|
|
414
|
+
}
|
|
415
|
+
// Selection highlighting is now integrated into renderCellBackground/renderCellText
|
|
416
|
+
// No separate overlay pass needed - this fixes z-order issues with complex glyphs
|
|
417
|
+
// Link underlines are drawn during cell rendering (see renderCell)
|
|
418
|
+
// Render cursor (only if we're at the bottom, not scrolled)
|
|
419
|
+
if (viewportY === 0 && cursor.visible && this.cursorVisible) {
|
|
420
|
+
this.renderCursor(cursor.x, cursor.y);
|
|
421
|
+
}
|
|
422
|
+
});
|
|
423
|
+
// Render scrollbar if scrolled or scrollback exists (with opacity for fade effect)
|
|
424
|
+
if (scrollbackProvider && scrollbarOpacity > 0) {
|
|
425
|
+
this.renderScrollbar(viewportY, scrollbackLength, dims.rows, scrollbarOpacity);
|
|
426
|
+
}
|
|
427
|
+
// Update last cursor position
|
|
428
|
+
this.lastCursorPosition = { x: cursor.x, y: cursor.y };
|
|
429
|
+
// Frame contract: `endFrame()` clears global + per-row dirty flags. Even
|
|
430
|
+
// after a full redraw — without this the next snapshot would still report
|
|
431
|
+
// FULL/dirty rows and we'd never trip the incremental fast path.
|
|
432
|
+
if (buffer.endFrame) {
|
|
433
|
+
buffer.endFrame();
|
|
434
|
+
}
|
|
435
|
+
else {
|
|
436
|
+
buffer.clearDirty();
|
|
437
|
+
}
|
|
438
|
+
}
|
|
439
|
+
/**
|
|
440
|
+
* Render a single line using two-pass approach:
|
|
441
|
+
* 1. First pass: Draw all cell backgrounds
|
|
442
|
+
* 2. Second pass: Draw all cell text and decorations
|
|
443
|
+
*
|
|
444
|
+
* This two-pass approach is necessary for proper rendering of complex scripts
|
|
445
|
+
* like Devanagari where diacritics (like vowel sign ि) can extend LEFT of the
|
|
446
|
+
* base character into the previous cell's visual area. If we draw backgrounds
|
|
447
|
+
* and text in a single pass (cell by cell), the background of cell N would
|
|
448
|
+
* cover any left-extending portions of graphemes from cell N-1.
|
|
449
|
+
*/
|
|
450
|
+
/**
|
|
451
|
+
* Run `draw` with the context clipped to the grid rect (grid space).
|
|
452
|
+
*
|
|
453
|
+
* Glyphs legitimately overhang their cells (antialiasing, italic overhang,
|
|
454
|
+
* tall scripts). Inside the grid the adjacent-row redraw convention repaints
|
|
455
|
+
* that spill, but at the grid's outer edge there is no adjacent row — the
|
|
456
|
+
* spill lands in the gutter where nothing ever repaints it: permanent ghost
|
|
457
|
+
* specks along the inset boundary. The old grid-sized canvas clipped these
|
|
458
|
+
* by construction; the pane-sized surface must do it explicitly.
|
|
459
|
+
*
|
|
460
|
+
* Surface-space drawing (background fill, the scrollbar, which lives in the
|
|
461
|
+
* gutter) must run OUTSIDE this bracket or it would be clipped away.
|
|
462
|
+
*/
|
|
463
|
+
withGridClip(dims, draw) {
|
|
464
|
+
this.ctx.save();
|
|
465
|
+
this.ctx.beginPath();
|
|
466
|
+
this.ctx.rect(0, 0, dims.cols * this.metrics.width, dims.rows * this.metrics.height);
|
|
467
|
+
this.ctx.clip();
|
|
468
|
+
try {
|
|
469
|
+
draw();
|
|
470
|
+
}
|
|
471
|
+
finally {
|
|
472
|
+
this.ctx.restore();
|
|
473
|
+
}
|
|
474
|
+
}
|
|
475
|
+
renderLine(line, y, cols) {
|
|
476
|
+
const lineY = y * this.metrics.height;
|
|
477
|
+
const lineWidth = cols * this.metrics.width;
|
|
478
|
+
// Fill the line with the theme background — source-over, NO clearRect.
|
|
479
|
+
// Line rects are fractional (cell height × row), so both clearRect and
|
|
480
|
+
// fillRect antialias their edges. clearRect PARTIALLY clears the boundary
|
|
481
|
+
// pixel row (alpha < 1) and the following fill repaints it with partial
|
|
482
|
+
// alpha: partial × partial never returns to opaque, leaving a translucent
|
|
483
|
+
// seam at every line boundary — invisible on a dark page, glaring the
|
|
484
|
+
// moment the element behind the canvas has a color. The theme background
|
|
485
|
+
// is opaque and the surface was filled opaque at resize, so a plain
|
|
486
|
+
// composite fully covers; nothing here may punch alpha out of the surface.
|
|
487
|
+
this.ctx.fillStyle = this.theme.background;
|
|
488
|
+
this.ctx.fillRect(0, lineY, lineWidth, this.metrics.height);
|
|
489
|
+
// PASS 1: Draw all cell backgrounds first
|
|
490
|
+
// This ensures all backgrounds are painted before any text, allowing text
|
|
491
|
+
// to "bleed" across cell boundaries without being covered by adjacent backgrounds
|
|
492
|
+
for (let x = 0; x < line.length; x++) {
|
|
493
|
+
const cell = line[x];
|
|
494
|
+
if (cell.width === 0)
|
|
495
|
+
continue; // Skip spacer cells for wide characters
|
|
496
|
+
this.renderCellBackground(cell, x, y);
|
|
497
|
+
}
|
|
498
|
+
// PASS 2: Draw all cell text and decorations
|
|
499
|
+
// Now text can safely extend beyond cell boundaries (for complex scripts)
|
|
500
|
+
for (let x = 0; x < line.length; x++) {
|
|
501
|
+
const cell = line[x];
|
|
502
|
+
if (cell.width === 0)
|
|
503
|
+
continue; // Skip spacer cells for wide characters
|
|
504
|
+
this.renderCellText(cell, x, y);
|
|
505
|
+
}
|
|
506
|
+
}
|
|
507
|
+
/**
|
|
508
|
+
* Render a cell's background only (Pass 1 of two-pass rendering)
|
|
509
|
+
* Selection highlighting is integrated here to avoid z-order issues with
|
|
510
|
+
* complex glyphs (like Devanagari) that extend outside their cell bounds.
|
|
511
|
+
*/
|
|
512
|
+
renderCellBackground(cell, x, y) {
|
|
513
|
+
const cellX = x * this.metrics.width;
|
|
514
|
+
const cellY = y * this.metrics.height;
|
|
515
|
+
const cellWidth = this.metrics.width * cell.width;
|
|
516
|
+
// Check if this cell is selected
|
|
517
|
+
const isSelected = this.isInSelection(x, y);
|
|
518
|
+
if (isSelected) {
|
|
519
|
+
// Draw selection background (solid color, not overlay)
|
|
520
|
+
this.ctx.fillStyle = this.theme.selectionBackground;
|
|
521
|
+
this.ctx.fillRect(cellX, cellY, cellWidth, this.metrics.height);
|
|
522
|
+
return; // Selection background replaces cell background
|
|
523
|
+
}
|
|
524
|
+
// Extract background color and handle inverse
|
|
525
|
+
let bg_r = cell.bg_r, bg_g = cell.bg_g, bg_b = cell.bg_b;
|
|
526
|
+
if (cell.flags & CellFlags.INVERSE) {
|
|
527
|
+
// When inverted, background becomes foreground
|
|
528
|
+
bg_r = cell.fg_r;
|
|
529
|
+
bg_g = cell.fg_g;
|
|
530
|
+
bg_b = cell.fg_b;
|
|
531
|
+
}
|
|
532
|
+
// Skip the per-cell fill when the cell already matches the theme bg —
|
|
533
|
+
// the line-level theme background drawn earlier covers it. We compare
|
|
534
|
+
// against the parsed theme bg (`themeBgRgb`) rather than hardcoded
|
|
535
|
+
// `0,0,0`, so this works for both light and dark themes.
|
|
536
|
+
const themeBgRgb = this.themeBackgroundRgb;
|
|
537
|
+
const isDefaultBg = bg_r === themeBgRgb.r && bg_g === themeBgRgb.g && bg_b === themeBgRgb.b;
|
|
538
|
+
if (!isDefaultBg) {
|
|
539
|
+
this.ctx.fillStyle = this.rgbToCSS(bg_r, bg_g, bg_b);
|
|
540
|
+
this.ctx.fillRect(cellX, cellY, cellWidth, this.metrics.height);
|
|
541
|
+
}
|
|
542
|
+
}
|
|
543
|
+
/**
|
|
544
|
+
* Render a cell's text and decorations (Pass 2 of two-pass rendering)
|
|
545
|
+
* Selection foreground color is applied here to match the selection background.
|
|
546
|
+
*/
|
|
547
|
+
renderCellText(cell, x, y, colorOverride) {
|
|
548
|
+
const cellX = x * this.metrics.width;
|
|
549
|
+
const cellY = y * this.metrics.height;
|
|
550
|
+
const cellWidth = this.metrics.width * cell.width;
|
|
551
|
+
// Skip rendering if invisible
|
|
552
|
+
if (cell.flags & CellFlags.INVISIBLE) {
|
|
553
|
+
return;
|
|
554
|
+
}
|
|
555
|
+
// Check if this cell is selected
|
|
556
|
+
const isSelected = this.isInSelection(x, y);
|
|
557
|
+
// Set text style
|
|
558
|
+
let fontStyle = '';
|
|
559
|
+
if (cell.flags & CellFlags.ITALIC)
|
|
560
|
+
fontStyle += 'italic ';
|
|
561
|
+
if (cell.flags & CellFlags.BOLD)
|
|
562
|
+
fontStyle += 'bold ';
|
|
563
|
+
this.ctx.font = `${fontStyle}${this.fontSize}px ${this.fontFamily}`;
|
|
564
|
+
// Set text color - use override, selection foreground, or normal color
|
|
565
|
+
if (colorOverride) {
|
|
566
|
+
this.ctx.fillStyle = colorOverride;
|
|
567
|
+
}
|
|
568
|
+
else if (isSelected) {
|
|
569
|
+
this.ctx.fillStyle = this.theme.selectionForeground;
|
|
570
|
+
}
|
|
571
|
+
else {
|
|
572
|
+
// Extract colors and handle inverse
|
|
573
|
+
let fg_r = cell.fg_r, fg_g = cell.fg_g, fg_b = cell.fg_b;
|
|
574
|
+
if (cell.flags & CellFlags.INVERSE) {
|
|
575
|
+
// When inverted, foreground becomes background
|
|
576
|
+
fg_r = cell.bg_r;
|
|
577
|
+
fg_g = cell.bg_g;
|
|
578
|
+
fg_b = cell.bg_b;
|
|
579
|
+
}
|
|
580
|
+
this.ctx.fillStyle = this.rgbToCSS(fg_r, fg_g, fg_b);
|
|
581
|
+
}
|
|
582
|
+
// Apply faint effect
|
|
583
|
+
if (cell.flags & CellFlags.FAINT) {
|
|
584
|
+
this.ctx.globalAlpha = 0.5;
|
|
585
|
+
}
|
|
586
|
+
// Draw text
|
|
587
|
+
const textX = cellX;
|
|
588
|
+
const textY = cellY + this.metrics.baseline;
|
|
589
|
+
// Get the character to render - use grapheme lookup for complex scripts
|
|
590
|
+
let char;
|
|
591
|
+
if (cell.grapheme_len > 0 && this.currentBuffer?.getGraphemeString) {
|
|
592
|
+
// Cell has additional codepoints - get full grapheme cluster
|
|
593
|
+
char = this.currentBuffer.getGraphemeString(y, x);
|
|
594
|
+
}
|
|
595
|
+
else {
|
|
596
|
+
// Simple cell - single codepoint
|
|
597
|
+
char = String.fromCodePoint(cell.codepoint || 32); // Default to space if null
|
|
598
|
+
}
|
|
599
|
+
this.ctx.fillText(char, textX, textY);
|
|
600
|
+
// Reset alpha
|
|
601
|
+
if (cell.flags & CellFlags.FAINT) {
|
|
602
|
+
this.ctx.globalAlpha = 1.0;
|
|
603
|
+
}
|
|
604
|
+
// Draw underline
|
|
605
|
+
if (cell.flags & CellFlags.UNDERLINE) {
|
|
606
|
+
const underlineY = cellY + this.metrics.baseline + 2;
|
|
607
|
+
this.ctx.strokeStyle = this.ctx.fillStyle;
|
|
608
|
+
this.ctx.lineWidth = 1;
|
|
609
|
+
this.ctx.beginPath();
|
|
610
|
+
this.ctx.moveTo(cellX, underlineY);
|
|
611
|
+
this.ctx.lineTo(cellX + cellWidth, underlineY);
|
|
612
|
+
this.ctx.stroke();
|
|
613
|
+
}
|
|
614
|
+
// Draw strikethrough
|
|
615
|
+
if (cell.flags & CellFlags.STRIKETHROUGH) {
|
|
616
|
+
const strikeY = cellY + this.metrics.height / 2;
|
|
617
|
+
this.ctx.strokeStyle = this.ctx.fillStyle;
|
|
618
|
+
this.ctx.lineWidth = 1;
|
|
619
|
+
this.ctx.beginPath();
|
|
620
|
+
this.ctx.moveTo(cellX, strikeY);
|
|
621
|
+
this.ctx.lineTo(cellX + cellWidth, strikeY);
|
|
622
|
+
this.ctx.stroke();
|
|
623
|
+
}
|
|
624
|
+
// Draw hyperlink underline (for OSC8 hyperlinks)
|
|
625
|
+
if (cell.hyperlink_id > 0) {
|
|
626
|
+
const isHovered = cell.hyperlink_id === this.hoveredHyperlinkId;
|
|
627
|
+
// Only show underline when hovered (cleaner look)
|
|
628
|
+
if (isHovered) {
|
|
629
|
+
const underlineY = cellY + this.metrics.baseline + 2;
|
|
630
|
+
this.ctx.strokeStyle = '#4A90E2'; // Blue underline on hover
|
|
631
|
+
this.ctx.lineWidth = 1;
|
|
632
|
+
this.ctx.beginPath();
|
|
633
|
+
this.ctx.moveTo(cellX, underlineY);
|
|
634
|
+
this.ctx.lineTo(cellX + cellWidth, underlineY);
|
|
635
|
+
this.ctx.stroke();
|
|
636
|
+
}
|
|
637
|
+
}
|
|
638
|
+
// Draw regex link underline (for plain text URLs)
|
|
639
|
+
if (this.hoveredLinkRange) {
|
|
640
|
+
const range = this.hoveredLinkRange;
|
|
641
|
+
// Check if this cell is within the hovered link range
|
|
642
|
+
const isInRange = (y === range.startY && x >= range.startX && (y < range.endY || x <= range.endX)) ||
|
|
643
|
+
(y > range.startY && y < range.endY) ||
|
|
644
|
+
(y === range.endY && x <= range.endX && (y > range.startY || x >= range.startX));
|
|
645
|
+
if (isInRange) {
|
|
646
|
+
const underlineY = cellY + this.metrics.baseline + 2;
|
|
647
|
+
this.ctx.strokeStyle = '#4A90E2'; // Blue underline on hover
|
|
648
|
+
this.ctx.lineWidth = 1;
|
|
649
|
+
this.ctx.beginPath();
|
|
650
|
+
this.ctx.moveTo(cellX, underlineY);
|
|
651
|
+
this.ctx.lineTo(cellX + cellWidth, underlineY);
|
|
652
|
+
this.ctx.stroke();
|
|
653
|
+
}
|
|
654
|
+
}
|
|
655
|
+
}
|
|
656
|
+
/**
|
|
657
|
+
* Render cursor
|
|
658
|
+
*/
|
|
659
|
+
renderCursor(x, y) {
|
|
660
|
+
const cursorX = x * this.metrics.width;
|
|
661
|
+
const cursorY = y * this.metrics.height;
|
|
662
|
+
this.ctx.fillStyle = this.theme.cursor;
|
|
663
|
+
switch (this.cursorStyle) {
|
|
664
|
+
case 'block':
|
|
665
|
+
// Full cell block
|
|
666
|
+
this.ctx.fillRect(cursorX, cursorY, this.metrics.width, this.metrics.height);
|
|
667
|
+
// Re-draw character under cursor with cursorAccent color
|
|
668
|
+
{
|
|
669
|
+
const line = this.currentBuffer?.getLine(y);
|
|
670
|
+
if (line?.[x]) {
|
|
671
|
+
this.ctx.save();
|
|
672
|
+
this.ctx.beginPath();
|
|
673
|
+
this.ctx.rect(cursorX, cursorY, this.metrics.width, this.metrics.height);
|
|
674
|
+
this.ctx.clip();
|
|
675
|
+
this.renderCellText(line[x], x, y, this.theme.cursorAccent);
|
|
676
|
+
this.ctx.restore();
|
|
677
|
+
}
|
|
678
|
+
}
|
|
679
|
+
break;
|
|
680
|
+
case 'underline':
|
|
681
|
+
// Underline at bottom of cell
|
|
682
|
+
const underlineHeight = Math.max(2, Math.floor(this.metrics.height * 0.15));
|
|
683
|
+
this.ctx.fillRect(cursorX, cursorY + this.metrics.height - underlineHeight, this.metrics.width, underlineHeight);
|
|
684
|
+
break;
|
|
685
|
+
case 'bar':
|
|
686
|
+
// Vertical bar at left of cell
|
|
687
|
+
const barWidth = Math.max(2, Math.floor(this.metrics.width * 0.15));
|
|
688
|
+
this.ctx.fillRect(cursorX, cursorY, barWidth, this.metrics.height);
|
|
689
|
+
break;
|
|
690
|
+
}
|
|
691
|
+
}
|
|
692
|
+
// ==========================================================================
|
|
693
|
+
// Cursor Blinking
|
|
694
|
+
// ==========================================================================
|
|
695
|
+
startCursorBlink() {
|
|
696
|
+
// xterm.js uses ~530ms blink interval
|
|
697
|
+
this.cursorBlinkInterval = window.setInterval(() => {
|
|
698
|
+
this.cursorVisible = !this.cursorVisible;
|
|
699
|
+
// Note: Render loop should redraw cursor line automatically
|
|
700
|
+
}, 530);
|
|
701
|
+
}
|
|
702
|
+
stopCursorBlink() {
|
|
703
|
+
if (this.cursorBlinkInterval !== undefined) {
|
|
704
|
+
clearInterval(this.cursorBlinkInterval);
|
|
705
|
+
this.cursorBlinkInterval = undefined;
|
|
706
|
+
}
|
|
707
|
+
this.cursorVisible = true;
|
|
708
|
+
}
|
|
709
|
+
// ==========================================================================
|
|
710
|
+
// Public API
|
|
711
|
+
// ==========================================================================
|
|
712
|
+
/**
|
|
713
|
+
* Update theme colors
|
|
714
|
+
*/
|
|
715
|
+
setTheme(theme) {
|
|
716
|
+
this.theme = { ...DEFAULT_THEME, ...theme };
|
|
717
|
+
this.themeBackgroundRgb = parseCssColorToRgb(this.theme.background);
|
|
718
|
+
// Rebuild palette
|
|
719
|
+
this.palette = [
|
|
720
|
+
this.theme.black,
|
|
721
|
+
this.theme.red,
|
|
722
|
+
this.theme.green,
|
|
723
|
+
this.theme.yellow,
|
|
724
|
+
this.theme.blue,
|
|
725
|
+
this.theme.magenta,
|
|
726
|
+
this.theme.cyan,
|
|
727
|
+
this.theme.white,
|
|
728
|
+
this.theme.brightBlack,
|
|
729
|
+
this.theme.brightRed,
|
|
730
|
+
this.theme.brightGreen,
|
|
731
|
+
this.theme.brightYellow,
|
|
732
|
+
this.theme.brightBlue,
|
|
733
|
+
this.theme.brightMagenta,
|
|
734
|
+
this.theme.brightCyan,
|
|
735
|
+
this.theme.brightWhite,
|
|
736
|
+
];
|
|
737
|
+
}
|
|
738
|
+
/**
|
|
739
|
+
* Update font size
|
|
740
|
+
*/
|
|
741
|
+
setFontSize(size) {
|
|
742
|
+
this.fontSize = size;
|
|
743
|
+
}
|
|
744
|
+
/**
|
|
745
|
+
* Update font family
|
|
746
|
+
*/
|
|
747
|
+
setFontFamily(family) {
|
|
748
|
+
this.fontFamily = family;
|
|
749
|
+
}
|
|
750
|
+
/**
|
|
751
|
+
* Update cursor style
|
|
752
|
+
*/
|
|
753
|
+
setCursorStyle(style) {
|
|
754
|
+
this.cursorStyle = style;
|
|
755
|
+
}
|
|
756
|
+
/**
|
|
757
|
+
* Enable/disable cursor blinking
|
|
758
|
+
*/
|
|
759
|
+
setCursorBlink(enabled) {
|
|
760
|
+
if (enabled && !this.cursorBlink) {
|
|
761
|
+
this.cursorBlink = true;
|
|
762
|
+
this.startCursorBlink();
|
|
763
|
+
}
|
|
764
|
+
else if (!enabled && this.cursorBlink) {
|
|
765
|
+
this.cursorBlink = false;
|
|
766
|
+
this.stopCursorBlink();
|
|
767
|
+
}
|
|
768
|
+
}
|
|
769
|
+
/**
|
|
770
|
+
* Get current font metrics
|
|
771
|
+
*/
|
|
772
|
+
/**
|
|
773
|
+
* Render scrollbar (Phase 2)
|
|
774
|
+
* Shows scroll position and allows click/drag interaction
|
|
775
|
+
* @param opacity Opacity level (0-1) for fade in/out effect
|
|
776
|
+
*/
|
|
777
|
+
renderScrollbar(viewportY, scrollbackLength, visibleRows, opacity = 1) {
|
|
778
|
+
const ctx = this.ctx;
|
|
779
|
+
// The scrollbar belongs to the SURFACE (it hugs the pane's right edge),
|
|
780
|
+
// not to the grid — draw it in surface space, outside the grid transform.
|
|
781
|
+
ctx.save();
|
|
782
|
+
ctx.setTransform(this.devicePixelRatio, 0, 0, this.devicePixelRatio, 0, 0);
|
|
783
|
+
const canvasHeight = this.canvas.height / this.devicePixelRatio;
|
|
784
|
+
const canvasWidth = this.canvas.width / this.devicePixelRatio;
|
|
785
|
+
// Scrollbar dimensions
|
|
786
|
+
const scrollbarWidth = 8;
|
|
787
|
+
const scrollbarX = canvasWidth - scrollbarWidth - 4;
|
|
788
|
+
const scrollbarPadding = 4;
|
|
789
|
+
const scrollbarTrackHeight = canvasHeight - scrollbarPadding * 2;
|
|
790
|
+
// Reset the strip with an opaque background fill (covers the faded thumb
|
|
791
|
+
// without clearRect — punching alpha leaves translucent seams at the
|
|
792
|
+
// strip's antialiased edges, same defect as the per-line path).
|
|
793
|
+
ctx.fillStyle = this.theme.background;
|
|
794
|
+
ctx.fillRect(scrollbarX - 2, 0, scrollbarWidth + 6, canvasHeight);
|
|
795
|
+
// Don't draw scrollbar if fully transparent or no scrollback
|
|
796
|
+
if (opacity <= 0 || scrollbackLength === 0) {
|
|
797
|
+
ctx.restore();
|
|
798
|
+
return;
|
|
799
|
+
}
|
|
800
|
+
// Calculate scrollbar thumb size and position
|
|
801
|
+
const totalLines = scrollbackLength + visibleRows;
|
|
802
|
+
const thumbHeight = Math.max(20, (visibleRows / totalLines) * scrollbarTrackHeight);
|
|
803
|
+
// Position: 0 = at bottom, scrollbackLength = at top
|
|
804
|
+
const scrollPosition = viewportY / scrollbackLength; // 0 to 1
|
|
805
|
+
const thumbY = scrollbarPadding + (scrollbarTrackHeight - thumbHeight) * (1 - scrollPosition);
|
|
806
|
+
// Draw scrollbar track (subtle background) with opacity
|
|
807
|
+
ctx.fillStyle = `rgba(128, 128, 128, ${0.1 * opacity})`;
|
|
808
|
+
ctx.fillRect(scrollbarX, scrollbarPadding, scrollbarWidth, scrollbarTrackHeight);
|
|
809
|
+
// Draw scrollbar thumb with opacity
|
|
810
|
+
const isScrolled = viewportY > 0;
|
|
811
|
+
const baseOpacity = isScrolled ? 0.5 : 0.3;
|
|
812
|
+
ctx.fillStyle = `rgba(128, 128, 128, ${baseOpacity * opacity})`;
|
|
813
|
+
ctx.fillRect(scrollbarX, thumbY, scrollbarWidth, thumbHeight);
|
|
814
|
+
ctx.restore();
|
|
815
|
+
}
|
|
816
|
+
getMetrics() {
|
|
817
|
+
return { ...this.metrics };
|
|
818
|
+
}
|
|
819
|
+
/**
|
|
820
|
+
* Get canvas element (needed by SelectionManager)
|
|
821
|
+
*/
|
|
822
|
+
getCanvas() {
|
|
823
|
+
return this.canvas;
|
|
824
|
+
}
|
|
825
|
+
/**
|
|
826
|
+
* Set selection manager (for rendering selection)
|
|
827
|
+
*/
|
|
828
|
+
setSelectionManager(manager) {
|
|
829
|
+
this.selectionManager = manager;
|
|
830
|
+
}
|
|
831
|
+
/**
|
|
832
|
+
* Check if a cell at (x, y) is within the current selection.
|
|
833
|
+
* Uses cached selection coordinates for performance.
|
|
834
|
+
*/
|
|
835
|
+
isInSelection(x, y) {
|
|
836
|
+
const sel = this.currentSelectionCoords;
|
|
837
|
+
if (!sel)
|
|
838
|
+
return false;
|
|
839
|
+
const { startCol, startRow, endCol, endRow } = sel;
|
|
840
|
+
// Single line selection
|
|
841
|
+
if (startRow === endRow) {
|
|
842
|
+
return y === startRow && x >= startCol && x <= endCol;
|
|
843
|
+
}
|
|
844
|
+
// Multi-line selection
|
|
845
|
+
if (y === startRow) {
|
|
846
|
+
// First line: from startCol to end of line
|
|
847
|
+
return x >= startCol;
|
|
848
|
+
}
|
|
849
|
+
else if (y === endRow) {
|
|
850
|
+
// Last line: from start of line to endCol
|
|
851
|
+
return x <= endCol;
|
|
852
|
+
}
|
|
853
|
+
else if (y > startRow && y < endRow) {
|
|
854
|
+
// Middle lines: entire line is selected
|
|
855
|
+
return true;
|
|
856
|
+
}
|
|
857
|
+
return false;
|
|
858
|
+
}
|
|
859
|
+
/**
|
|
860
|
+
* Set the currently hovered hyperlink ID for rendering underlines
|
|
861
|
+
*/
|
|
862
|
+
setHoveredHyperlinkId(hyperlinkId) {
|
|
863
|
+
this.hoveredHyperlinkId = hyperlinkId;
|
|
864
|
+
}
|
|
865
|
+
/**
|
|
866
|
+
* Set the currently hovered link range for rendering underlines (for regex-detected URLs)
|
|
867
|
+
* Pass null to clear the hover state
|
|
868
|
+
*/
|
|
869
|
+
setHoveredLinkRange(range) {
|
|
870
|
+
this.hoveredLinkRange = range;
|
|
871
|
+
}
|
|
872
|
+
/**
|
|
873
|
+
* Get character cell width (for coordinate conversion)
|
|
874
|
+
*/
|
|
875
|
+
get charWidth() {
|
|
876
|
+
return this.metrics.width;
|
|
877
|
+
}
|
|
878
|
+
/**
|
|
879
|
+
* Get character cell height (for coordinate conversion)
|
|
880
|
+
*/
|
|
881
|
+
get charHeight() {
|
|
882
|
+
return this.metrics.height;
|
|
883
|
+
}
|
|
884
|
+
/**
|
|
885
|
+
* Clear entire canvas
|
|
886
|
+
*/
|
|
887
|
+
clear() {
|
|
888
|
+
// Whole-surface operation → surface space, not grid space.
|
|
889
|
+
this.ctx.save();
|
|
890
|
+
this.ctx.setTransform(1, 0, 0, 1, 0, 0);
|
|
891
|
+
// clearRect first because fillRect composites rather than replaces,
|
|
892
|
+
// so transparent/translucent backgrounds wouldn't clear previous content.
|
|
893
|
+
this.ctx.clearRect(0, 0, this.canvas.width, this.canvas.height);
|
|
894
|
+
this.ctx.fillStyle = this.theme.background;
|
|
895
|
+
this.ctx.fillRect(0, 0, this.canvas.width, this.canvas.height);
|
|
896
|
+
this.ctx.restore();
|
|
897
|
+
}
|
|
898
|
+
/**
|
|
899
|
+
* Cleanup resources
|
|
900
|
+
*/
|
|
901
|
+
dispose() {
|
|
902
|
+
this.stopCursorBlink();
|
|
903
|
+
}
|
|
904
|
+
}
|
|
905
|
+
//# sourceMappingURL=renderer.js.map
|