@xterm/xterm 5.4.0-beta.1

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 (108) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +235 -0
  3. package/css/xterm.css +209 -0
  4. package/lib/xterm.js +2 -0
  5. package/lib/xterm.js.map +1 -0
  6. package/package.json +101 -0
  7. package/src/browser/AccessibilityManager.ts +278 -0
  8. package/src/browser/Clipboard.ts +93 -0
  9. package/src/browser/ColorContrastCache.ts +34 -0
  10. package/src/browser/Lifecycle.ts +33 -0
  11. package/src/browser/Linkifier2.ts +416 -0
  12. package/src/browser/LocalizableStrings.ts +12 -0
  13. package/src/browser/OscLinkProvider.ts +128 -0
  14. package/src/browser/RenderDebouncer.ts +83 -0
  15. package/src/browser/Terminal.ts +1317 -0
  16. package/src/browser/TimeBasedDebouncer.ts +86 -0
  17. package/src/browser/Types.d.ts +181 -0
  18. package/src/browser/Viewport.ts +401 -0
  19. package/src/browser/decorations/BufferDecorationRenderer.ts +134 -0
  20. package/src/browser/decorations/ColorZoneStore.ts +117 -0
  21. package/src/browser/decorations/OverviewRulerRenderer.ts +218 -0
  22. package/src/browser/input/CompositionHelper.ts +246 -0
  23. package/src/browser/input/Mouse.ts +54 -0
  24. package/src/browser/input/MoveToCell.ts +249 -0
  25. package/src/browser/public/Terminal.ts +260 -0
  26. package/src/browser/renderer/dom/DomRenderer.ts +509 -0
  27. package/src/browser/renderer/dom/DomRendererRowFactory.ts +526 -0
  28. package/src/browser/renderer/dom/WidthCache.ts +160 -0
  29. package/src/browser/renderer/shared/CellColorResolver.ts +137 -0
  30. package/src/browser/renderer/shared/CharAtlasCache.ts +96 -0
  31. package/src/browser/renderer/shared/CharAtlasUtils.ts +75 -0
  32. package/src/browser/renderer/shared/Constants.ts +14 -0
  33. package/src/browser/renderer/shared/CursorBlinkStateManager.ts +146 -0
  34. package/src/browser/renderer/shared/CustomGlyphs.ts +687 -0
  35. package/src/browser/renderer/shared/DevicePixelObserver.ts +41 -0
  36. package/src/browser/renderer/shared/README.md +1 -0
  37. package/src/browser/renderer/shared/RendererUtils.ts +58 -0
  38. package/src/browser/renderer/shared/SelectionRenderModel.ts +91 -0
  39. package/src/browser/renderer/shared/TextureAtlas.ts +1082 -0
  40. package/src/browser/renderer/shared/Types.d.ts +173 -0
  41. package/src/browser/selection/SelectionModel.ts +144 -0
  42. package/src/browser/selection/Types.d.ts +15 -0
  43. package/src/browser/services/CharSizeService.ts +102 -0
  44. package/src/browser/services/CharacterJoinerService.ts +339 -0
  45. package/src/browser/services/CoreBrowserService.ts +137 -0
  46. package/src/browser/services/MouseService.ts +46 -0
  47. package/src/browser/services/RenderService.ts +279 -0
  48. package/src/browser/services/SelectionService.ts +1031 -0
  49. package/src/browser/services/Services.ts +147 -0
  50. package/src/browser/services/ThemeService.ts +237 -0
  51. package/src/common/CircularList.ts +241 -0
  52. package/src/common/Clone.ts +23 -0
  53. package/src/common/Color.ts +357 -0
  54. package/src/common/CoreTerminal.ts +284 -0
  55. package/src/common/EventEmitter.ts +78 -0
  56. package/src/common/InputHandler.ts +3461 -0
  57. package/src/common/Lifecycle.ts +108 -0
  58. package/src/common/MultiKeyMap.ts +42 -0
  59. package/src/common/Platform.ts +44 -0
  60. package/src/common/SortedList.ts +118 -0
  61. package/src/common/TaskQueue.ts +166 -0
  62. package/src/common/TypedArrayUtils.ts +17 -0
  63. package/src/common/Types.d.ts +553 -0
  64. package/src/common/WindowsMode.ts +27 -0
  65. package/src/common/buffer/AttributeData.ts +196 -0
  66. package/src/common/buffer/Buffer.ts +654 -0
  67. package/src/common/buffer/BufferLine.ts +524 -0
  68. package/src/common/buffer/BufferRange.ts +13 -0
  69. package/src/common/buffer/BufferReflow.ts +223 -0
  70. package/src/common/buffer/BufferSet.ts +134 -0
  71. package/src/common/buffer/CellData.ts +94 -0
  72. package/src/common/buffer/Constants.ts +149 -0
  73. package/src/common/buffer/Marker.ts +43 -0
  74. package/src/common/buffer/Types.d.ts +52 -0
  75. package/src/common/data/Charsets.ts +256 -0
  76. package/src/common/data/EscapeSequences.ts +153 -0
  77. package/src/common/input/Keyboard.ts +398 -0
  78. package/src/common/input/TextDecoder.ts +346 -0
  79. package/src/common/input/UnicodeV6.ts +145 -0
  80. package/src/common/input/WriteBuffer.ts +246 -0
  81. package/src/common/input/XParseColor.ts +80 -0
  82. package/src/common/parser/Constants.ts +58 -0
  83. package/src/common/parser/DcsParser.ts +192 -0
  84. package/src/common/parser/EscapeSequenceParser.ts +792 -0
  85. package/src/common/parser/OscParser.ts +238 -0
  86. package/src/common/parser/Params.ts +229 -0
  87. package/src/common/parser/Types.d.ts +275 -0
  88. package/src/common/public/AddonManager.ts +53 -0
  89. package/src/common/public/BufferApiView.ts +35 -0
  90. package/src/common/public/BufferLineApiView.ts +29 -0
  91. package/src/common/public/BufferNamespaceApi.ts +36 -0
  92. package/src/common/public/ParserApi.ts +37 -0
  93. package/src/common/public/UnicodeApi.ts +27 -0
  94. package/src/common/services/BufferService.ts +151 -0
  95. package/src/common/services/CharsetService.ts +34 -0
  96. package/src/common/services/CoreMouseService.ts +318 -0
  97. package/src/common/services/CoreService.ts +87 -0
  98. package/src/common/services/DecorationService.ts +140 -0
  99. package/src/common/services/InstantiationService.ts +85 -0
  100. package/src/common/services/LogService.ts +124 -0
  101. package/src/common/services/OptionsService.ts +202 -0
  102. package/src/common/services/OscLinkService.ts +115 -0
  103. package/src/common/services/ServiceRegistry.ts +49 -0
  104. package/src/common/services/Services.ts +373 -0
  105. package/src/common/services/UnicodeService.ts +111 -0
  106. package/src/headless/Terminal.ts +136 -0
  107. package/src/headless/public/Terminal.ts +195 -0
  108. package/typings/xterm.d.ts +1857 -0
@@ -0,0 +1,357 @@
1
+ /**
2
+ * Copyright (c) 2019 The xterm.js authors. All rights reserved.
3
+ * @license MIT
4
+ */
5
+
6
+ import { isNode } from 'common/Platform';
7
+ import { IColor, IColorRGB } from 'common/Types';
8
+
9
+ let $r = 0;
10
+ let $g = 0;
11
+ let $b = 0;
12
+ let $a = 0;
13
+
14
+ export const NULL_COLOR: IColor = {
15
+ css: '#00000000',
16
+ rgba: 0
17
+ };
18
+
19
+ /**
20
+ * Helper functions where the source type is "channels" (individual color channels as numbers).
21
+ */
22
+ export namespace channels {
23
+ export function toCss(r: number, g: number, b: number, a?: number): string {
24
+ if (a !== undefined) {
25
+ return `#${toPaddedHex(r)}${toPaddedHex(g)}${toPaddedHex(b)}${toPaddedHex(a)}`;
26
+ }
27
+ return `#${toPaddedHex(r)}${toPaddedHex(g)}${toPaddedHex(b)}`;
28
+ }
29
+
30
+ export function toRgba(r: number, g: number, b: number, a: number = 0xFF): number {
31
+ // Note: The aggregated number is RGBA32 (BE), thus needs to be converted to ABGR32
32
+ // on LE systems, before it can be used for direct 32-bit buffer writes.
33
+ // >>> 0 forces an unsigned int
34
+ return (r << 24 | g << 16 | b << 8 | a) >>> 0;
35
+ }
36
+ }
37
+
38
+ /**
39
+ * Helper functions where the source type is `IColor`.
40
+ */
41
+ export namespace color {
42
+ export function blend(bg: IColor, fg: IColor): IColor {
43
+ $a = (fg.rgba & 0xFF) / 255;
44
+ if ($a === 1) {
45
+ return {
46
+ css: fg.css,
47
+ rgba: fg.rgba
48
+ };
49
+ }
50
+ const fgR = (fg.rgba >> 24) & 0xFF;
51
+ const fgG = (fg.rgba >> 16) & 0xFF;
52
+ const fgB = (fg.rgba >> 8) & 0xFF;
53
+ const bgR = (bg.rgba >> 24) & 0xFF;
54
+ const bgG = (bg.rgba >> 16) & 0xFF;
55
+ const bgB = (bg.rgba >> 8) & 0xFF;
56
+ $r = bgR + Math.round((fgR - bgR) * $a);
57
+ $g = bgG + Math.round((fgG - bgG) * $a);
58
+ $b = bgB + Math.round((fgB - bgB) * $a);
59
+ const css = channels.toCss($r, $g, $b);
60
+ const rgba = channels.toRgba($r, $g, $b);
61
+ return { css, rgba };
62
+ }
63
+
64
+ export function isOpaque(color: IColor): boolean {
65
+ return (color.rgba & 0xFF) === 0xFF;
66
+ }
67
+
68
+ export function ensureContrastRatio(bg: IColor, fg: IColor, ratio: number): IColor | undefined {
69
+ const result = rgba.ensureContrastRatio(bg.rgba, fg.rgba, ratio);
70
+ if (!result) {
71
+ return undefined;
72
+ }
73
+ return rgba.toColor(
74
+ (result >> 24 & 0xFF),
75
+ (result >> 16 & 0xFF),
76
+ (result >> 8 & 0xFF)
77
+ );
78
+ }
79
+
80
+ export function opaque(color: IColor): IColor {
81
+ const rgbaColor = (color.rgba | 0xFF) >>> 0;
82
+ [$r, $g, $b] = rgba.toChannels(rgbaColor);
83
+ return {
84
+ css: channels.toCss($r, $g, $b),
85
+ rgba: rgbaColor
86
+ };
87
+ }
88
+
89
+ export function opacity(color: IColor, opacity: number): IColor {
90
+ $a = Math.round(opacity * 0xFF);
91
+ [$r, $g, $b] = rgba.toChannels(color.rgba);
92
+ return {
93
+ css: channels.toCss($r, $g, $b, $a),
94
+ rgba: channels.toRgba($r, $g, $b, $a)
95
+ };
96
+ }
97
+
98
+ export function multiplyOpacity(color: IColor, factor: number): IColor {
99
+ $a = color.rgba & 0xFF;
100
+ return opacity(color, ($a * factor) / 0xFF);
101
+ }
102
+
103
+ export function toColorRGB(color: IColor): IColorRGB {
104
+ return [(color.rgba >> 24) & 0xFF, (color.rgba >> 16) & 0xFF, (color.rgba >> 8) & 0xFF];
105
+ }
106
+ }
107
+
108
+ /**
109
+ * Helper functions where the source type is "css" (string: '#rgb', '#rgba', '#rrggbb',
110
+ * '#rrggbbaa').
111
+ */
112
+ export namespace css {
113
+ let $ctx: CanvasRenderingContext2D | undefined;
114
+ let $litmusColor: CanvasGradient | undefined;
115
+ if (!isNode) {
116
+ // This is guaranteed to run in the first window, so document should be correct
117
+ const canvas = document.createElement('canvas');
118
+ canvas.width = 1;
119
+ canvas.height = 1;
120
+ const ctx = canvas.getContext('2d', {
121
+ willReadFrequently: true
122
+ });
123
+ if (ctx) {
124
+ $ctx = ctx;
125
+ $ctx.globalCompositeOperation = 'copy';
126
+ $litmusColor = $ctx.createLinearGradient(0, 0, 1, 1);
127
+ }
128
+ }
129
+
130
+ /**
131
+ * Converts a css string to an IColor, this should handle all valid CSS color strings and will
132
+ * throw if it's invalid. The ideal format to use is `#rrggbb[aa]` as it's the fastest to parse.
133
+ *
134
+ * Only `#rgb[a]`, `#rrggbb[aa]`, `rgb()` and `rgba()` formats are supported when run in a Node
135
+ * environment.
136
+ */
137
+ export function toColor(css: string): IColor {
138
+ // Formats: #rgb[a] and #rrggbb[aa]
139
+ if (css.match(/#[\da-f]{3,8}/i)) {
140
+ switch (css.length) {
141
+ case 4: { // #rgb
142
+ $r = parseInt(css.slice(1, 2).repeat(2), 16);
143
+ $g = parseInt(css.slice(2, 3).repeat(2), 16);
144
+ $b = parseInt(css.slice(3, 4).repeat(2), 16);
145
+ return rgba.toColor($r, $g, $b);
146
+ }
147
+ case 5: { // #rgba
148
+ $r = parseInt(css.slice(1, 2).repeat(2), 16);
149
+ $g = parseInt(css.slice(2, 3).repeat(2), 16);
150
+ $b = parseInt(css.slice(3, 4).repeat(2), 16);
151
+ $a = parseInt(css.slice(4, 5).repeat(2), 16);
152
+ return rgba.toColor($r, $g, $b, $a);
153
+ }
154
+ case 7: // #rrggbb
155
+ return {
156
+ css,
157
+ rgba: (parseInt(css.slice(1), 16) << 8 | 0xFF) >>> 0
158
+ };
159
+ case 9: // #rrggbbaa
160
+ return {
161
+ css,
162
+ rgba: parseInt(css.slice(1), 16) >>> 0
163
+ };
164
+ }
165
+ }
166
+
167
+ // Formats: rgb() or rgba()
168
+ const rgbaMatch = css.match(/rgba?\(\s*(\d{1,3})\s*,\s*(\d{1,3})\s*,\s*(\d{1,3})\s*(,\s*(0|1|\d?\.(\d+))\s*)?\)/);
169
+ if (rgbaMatch) {
170
+ $r = parseInt(rgbaMatch[1]);
171
+ $g = parseInt(rgbaMatch[2]);
172
+ $b = parseInt(rgbaMatch[3]);
173
+ $a = Math.round((rgbaMatch[5] === undefined ? 1 : parseFloat(rgbaMatch[5])) * 0xFF);
174
+ return rgba.toColor($r, $g, $b, $a);
175
+ }
176
+
177
+ // Validate the context is available for canvas-based color parsing
178
+ if (!$ctx || !$litmusColor) {
179
+ throw new Error('css.toColor: Unsupported css format');
180
+ }
181
+
182
+ // Validate the color using canvas fillStyle
183
+ // See https://html.spec.whatwg.org/multipage/canvas.html#fill-and-stroke-styles
184
+ $ctx.fillStyle = $litmusColor;
185
+ $ctx.fillStyle = css;
186
+ if (typeof $ctx.fillStyle !== 'string') {
187
+ throw new Error('css.toColor: Unsupported css format');
188
+ }
189
+
190
+ $ctx.fillRect(0, 0, 1, 1);
191
+ [$r, $g, $b, $a] = $ctx.getImageData(0, 0, 1, 1).data;
192
+
193
+ // Validate the color is non-transparent as color hue gets lost when drawn to the canvas
194
+ if ($a !== 0xFF) {
195
+ throw new Error('css.toColor: Unsupported css format');
196
+ }
197
+
198
+ // Extract the color from the canvas' fillStyle property which exposes the color value in rgba()
199
+ // format
200
+ // See https://html.spec.whatwg.org/multipage/canvas.html#serialisation-of-a-color
201
+ return {
202
+ rgba: channels.toRgba($r, $g, $b, $a),
203
+ css
204
+ };
205
+ }
206
+ }
207
+
208
+ /**
209
+ * Helper functions where the source type is "rgb" (number: 0xrrggbb).
210
+ */
211
+ export namespace rgb {
212
+ /**
213
+ * Gets the relative luminance of an RGB color, this is useful in determining the contrast ratio
214
+ * between two colors.
215
+ * @param rgb The color to use.
216
+ * @see https://www.w3.org/TR/WCAG20/#relativeluminancedef
217
+ */
218
+ export function relativeLuminance(rgb: number): number {
219
+ return relativeLuminance2(
220
+ (rgb >> 16) & 0xFF,
221
+ (rgb >> 8 ) & 0xFF,
222
+ (rgb ) & 0xFF);
223
+ }
224
+
225
+ /**
226
+ * Gets the relative luminance of an RGB color, this is useful in determining the contrast ratio
227
+ * between two colors.
228
+ * @param r The red channel (0x00 to 0xFF).
229
+ * @param g The green channel (0x00 to 0xFF).
230
+ * @param b The blue channel (0x00 to 0xFF).
231
+ * @see https://www.w3.org/TR/WCAG20/#relativeluminancedef
232
+ */
233
+ export function relativeLuminance2(r: number, g: number, b: number): number {
234
+ const rs = r / 255;
235
+ const gs = g / 255;
236
+ const bs = b / 255;
237
+ const rr = rs <= 0.03928 ? rs / 12.92 : Math.pow((rs + 0.055) / 1.055, 2.4);
238
+ const rg = gs <= 0.03928 ? gs / 12.92 : Math.pow((gs + 0.055) / 1.055, 2.4);
239
+ const rb = bs <= 0.03928 ? bs / 12.92 : Math.pow((bs + 0.055) / 1.055, 2.4);
240
+ return rr * 0.2126 + rg * 0.7152 + rb * 0.0722;
241
+ }
242
+ }
243
+
244
+ /**
245
+ * Helper functions where the source type is "rgba" (number: 0xrrggbbaa).
246
+ */
247
+ export namespace rgba {
248
+ /**
249
+ * Given a foreground color and a background color, either increase or reduce the luminance of the
250
+ * foreground color until the specified contrast ratio is met. If pure white or black is hit
251
+ * without the contrast ratio being met, go the other direction using the background color as the
252
+ * foreground color and take either the first or second result depending on which has the higher
253
+ * contrast ratio.
254
+ *
255
+ * `undefined` will be returned if the contrast ratio is already met.
256
+ *
257
+ * @param bgRgba The background color in rgba format.
258
+ * @param fgRgba The foreground color in rgba format.
259
+ * @param ratio The contrast ratio to achieve.
260
+ */
261
+ export function ensureContrastRatio(bgRgba: number, fgRgba: number, ratio: number): number | undefined {
262
+ const bgL = rgb.relativeLuminance(bgRgba >> 8);
263
+ const fgL = rgb.relativeLuminance(fgRgba >> 8);
264
+ const cr = contrastRatio(bgL, fgL);
265
+ if (cr < ratio) {
266
+ if (fgL < bgL) {
267
+ const resultA = reduceLuminance(bgRgba, fgRgba, ratio);
268
+ const resultARatio = contrastRatio(bgL, rgb.relativeLuminance(resultA >> 8));
269
+ if (resultARatio < ratio) {
270
+ const resultB = increaseLuminance(bgRgba, fgRgba, ratio);
271
+ const resultBRatio = contrastRatio(bgL, rgb.relativeLuminance(resultB >> 8));
272
+ return resultARatio > resultBRatio ? resultA : resultB;
273
+ }
274
+ return resultA;
275
+ }
276
+ const resultA = increaseLuminance(bgRgba, fgRgba, ratio);
277
+ const resultARatio = contrastRatio(bgL, rgb.relativeLuminance(resultA >> 8));
278
+ if (resultARatio < ratio) {
279
+ const resultB = reduceLuminance(bgRgba, fgRgba, ratio);
280
+ const resultBRatio = contrastRatio(bgL, rgb.relativeLuminance(resultB >> 8));
281
+ return resultARatio > resultBRatio ? resultA : resultB;
282
+ }
283
+ return resultA;
284
+ }
285
+ return undefined;
286
+ }
287
+
288
+ export function reduceLuminance(bgRgba: number, fgRgba: number, ratio: number): number {
289
+ // This is a naive but fast approach to reducing luminance as converting to
290
+ // HSL and back is expensive
291
+ const bgR = (bgRgba >> 24) & 0xFF;
292
+ const bgG = (bgRgba >> 16) & 0xFF;
293
+ const bgB = (bgRgba >> 8) & 0xFF;
294
+ let fgR = (fgRgba >> 24) & 0xFF;
295
+ let fgG = (fgRgba >> 16) & 0xFF;
296
+ let fgB = (fgRgba >> 8) & 0xFF;
297
+ let cr = contrastRatio(rgb.relativeLuminance2(fgR, fgG, fgB), rgb.relativeLuminance2(bgR, bgG, bgB));
298
+ while (cr < ratio && (fgR > 0 || fgG > 0 || fgB > 0)) {
299
+ // Reduce by 10% until the ratio is hit
300
+ fgR -= Math.max(0, Math.ceil(fgR * 0.1));
301
+ fgG -= Math.max(0, Math.ceil(fgG * 0.1));
302
+ fgB -= Math.max(0, Math.ceil(fgB * 0.1));
303
+ cr = contrastRatio(rgb.relativeLuminance2(fgR, fgG, fgB), rgb.relativeLuminance2(bgR, bgG, bgB));
304
+ }
305
+ return (fgR << 24 | fgG << 16 | fgB << 8 | 0xFF) >>> 0;
306
+ }
307
+
308
+ export function increaseLuminance(bgRgba: number, fgRgba: number, ratio: number): number {
309
+ // This is a naive but fast approach to increasing luminance as converting to
310
+ // HSL and back is expensive
311
+ const bgR = (bgRgba >> 24) & 0xFF;
312
+ const bgG = (bgRgba >> 16) & 0xFF;
313
+ const bgB = (bgRgba >> 8) & 0xFF;
314
+ let fgR = (fgRgba >> 24) & 0xFF;
315
+ let fgG = (fgRgba >> 16) & 0xFF;
316
+ let fgB = (fgRgba >> 8) & 0xFF;
317
+ let cr = contrastRatio(rgb.relativeLuminance2(fgR, fgG, fgB), rgb.relativeLuminance2(bgR, bgG, bgB));
318
+ while (cr < ratio && (fgR < 0xFF || fgG < 0xFF || fgB < 0xFF)) {
319
+ // Increase by 10% until the ratio is hit
320
+ fgR = Math.min(0xFF, fgR + Math.ceil((255 - fgR) * 0.1));
321
+ fgG = Math.min(0xFF, fgG + Math.ceil((255 - fgG) * 0.1));
322
+ fgB = Math.min(0xFF, fgB + Math.ceil((255 - fgB) * 0.1));
323
+ cr = contrastRatio(rgb.relativeLuminance2(fgR, fgG, fgB), rgb.relativeLuminance2(bgR, bgG, bgB));
324
+ }
325
+ return (fgR << 24 | fgG << 16 | fgB << 8 | 0xFF) >>> 0;
326
+ }
327
+
328
+ // FIXME: Move this to channels NS?
329
+ export function toChannels(value: number): [number, number, number, number] {
330
+ return [(value >> 24) & 0xFF, (value >> 16) & 0xFF, (value >> 8) & 0xFF, value & 0xFF];
331
+ }
332
+
333
+ export function toColor(r: number, g: number, b: number, a?: number): IColor {
334
+ return {
335
+ css: channels.toCss(r, g, b, a),
336
+ rgba: channels.toRgba(r, g, b, a)
337
+ };
338
+ }
339
+ }
340
+
341
+ export function toPaddedHex(c: number): string {
342
+ const s = c.toString(16);
343
+ return s.length < 2 ? '0' + s : s;
344
+ }
345
+
346
+ /**
347
+ * Gets the contrast ratio between two relative luminance values.
348
+ * @param l1 The first relative luminance.
349
+ * @param l2 The first relative luminance.
350
+ * @see https://www.w3.org/TR/WCAG20/#contrast-ratiodef
351
+ */
352
+ export function contrastRatio(l1: number, l2: number): number {
353
+ if (l1 < l2) {
354
+ return (l2 + 0.05) / (l1 + 0.05);
355
+ }
356
+ return (l1 + 0.05) / (l2 + 0.05);
357
+ }
@@ -0,0 +1,284 @@
1
+ /**
2
+ * Copyright (c) 2014-2020 The xterm.js authors. All rights reserved.
3
+ * Copyright (c) 2012-2013, Christopher Jeffrey (MIT License)
4
+ * @license MIT
5
+ *
6
+ * Originally forked from (with the author's permission):
7
+ * Fabrice Bellard's javascript vt100 for jslinux:
8
+ * http://bellard.org/jslinux/
9
+ * Copyright (c) 2011 Fabrice Bellard
10
+ * The original design remains. The terminal itself
11
+ * has been extended to include xterm CSI codes, among
12
+ * other features.
13
+ *
14
+ * Terminal Emulation References:
15
+ * http://vt100.net/
16
+ * http://invisible-island.net/xterm/ctlseqs/ctlseqs.txt
17
+ * http://invisible-island.net/xterm/ctlseqs/ctlseqs.html
18
+ * http://invisible-island.net/vttest/
19
+ * http://www.inwap.com/pdp10/ansicode.txt
20
+ * http://linux.die.net/man/4/console_codes
21
+ * http://linux.die.net/man/7/urxvt
22
+ */
23
+
24
+ import { Disposable, MutableDisposable, toDisposable } from 'common/Lifecycle';
25
+ import { IInstantiationService, IOptionsService, IBufferService, ILogService, ICharsetService, ICoreService, ICoreMouseService, IUnicodeService, LogLevelEnum, ITerminalOptions, IOscLinkService } from 'common/services/Services';
26
+ import { InstantiationService } from 'common/services/InstantiationService';
27
+ import { LogService } from 'common/services/LogService';
28
+ import { BufferService, MINIMUM_COLS, MINIMUM_ROWS } from 'common/services/BufferService';
29
+ import { OptionsService } from 'common/services/OptionsService';
30
+ import { IDisposable, IAttributeData, ICoreTerminal, IScrollEvent, ScrollSource } from 'common/Types';
31
+ import { CoreService } from 'common/services/CoreService';
32
+ import { EventEmitter, IEvent, forwardEvent } from 'common/EventEmitter';
33
+ import { CoreMouseService } from 'common/services/CoreMouseService';
34
+ import { UnicodeService } from 'common/services/UnicodeService';
35
+ import { CharsetService } from 'common/services/CharsetService';
36
+ import { updateWindowsModeWrappedState } from 'common/WindowsMode';
37
+ import { IFunctionIdentifier, IParams } from 'common/parser/Types';
38
+ import { IBufferSet } from 'common/buffer/Types';
39
+ import { InputHandler } from 'common/InputHandler';
40
+ import { WriteBuffer } from 'common/input/WriteBuffer';
41
+ import { OscLinkService } from 'common/services/OscLinkService';
42
+
43
+ // Only trigger this warning a single time per session
44
+ let hasWriteSyncWarnHappened = false;
45
+
46
+ export abstract class CoreTerminal extends Disposable implements ICoreTerminal {
47
+ protected readonly _instantiationService: IInstantiationService;
48
+ protected readonly _bufferService: IBufferService;
49
+ protected readonly _logService: ILogService;
50
+ protected readonly _charsetService: ICharsetService;
51
+ protected readonly _oscLinkService: IOscLinkService;
52
+
53
+ public readonly coreMouseService: ICoreMouseService;
54
+ public readonly coreService: ICoreService;
55
+ public readonly unicodeService: IUnicodeService;
56
+ public readonly optionsService: IOptionsService;
57
+
58
+ protected _inputHandler: InputHandler;
59
+ private _writeBuffer: WriteBuffer;
60
+ private _windowsWrappingHeuristics = this.register(new MutableDisposable());
61
+
62
+ private readonly _onBinary = this.register(new EventEmitter<string>());
63
+ public readonly onBinary = this._onBinary.event;
64
+ private readonly _onData = this.register(new EventEmitter<string>());
65
+ public readonly onData = this._onData.event;
66
+ protected _onLineFeed = this.register(new EventEmitter<void>());
67
+ public readonly onLineFeed = this._onLineFeed.event;
68
+ private readonly _onResize = this.register(new EventEmitter<{ cols: number, rows: number }>());
69
+ public readonly onResize = this._onResize.event;
70
+ protected readonly _onWriteParsed = this.register(new EventEmitter<void>());
71
+ public readonly onWriteParsed = this._onWriteParsed.event;
72
+
73
+ /**
74
+ * Internally we track the source of the scroll but this is meaningless outside the library so
75
+ * it's filtered out.
76
+ */
77
+ protected _onScrollApi?: EventEmitter<number, void>;
78
+ protected _onScroll = this.register(new EventEmitter<IScrollEvent, void>());
79
+ public get onScroll(): IEvent<number, void> {
80
+ if (!this._onScrollApi) {
81
+ this._onScrollApi = this.register(new EventEmitter<number, void>());
82
+ this._onScroll.event(ev => {
83
+ this._onScrollApi?.fire(ev.position);
84
+ });
85
+ }
86
+ return this._onScrollApi.event;
87
+ }
88
+
89
+ public get cols(): number { return this._bufferService.cols; }
90
+ public get rows(): number { return this._bufferService.rows; }
91
+ public get buffers(): IBufferSet { return this._bufferService.buffers; }
92
+ public get options(): Required<ITerminalOptions> { return this.optionsService.options; }
93
+ public set options(options: ITerminalOptions) {
94
+ for (const key in options) {
95
+ this.optionsService.options[key] = options[key];
96
+ }
97
+ }
98
+
99
+ constructor(
100
+ options: Partial<ITerminalOptions>
101
+ ) {
102
+ super();
103
+
104
+ // Setup and initialize services
105
+ this._instantiationService = new InstantiationService();
106
+ this.optionsService = this.register(new OptionsService(options));
107
+ this._instantiationService.setService(IOptionsService, this.optionsService);
108
+ this._bufferService = this.register(this._instantiationService.createInstance(BufferService));
109
+ this._instantiationService.setService(IBufferService, this._bufferService);
110
+ this._logService = this.register(this._instantiationService.createInstance(LogService));
111
+ this._instantiationService.setService(ILogService, this._logService);
112
+ this.coreService = this.register(this._instantiationService.createInstance(CoreService));
113
+ this._instantiationService.setService(ICoreService, this.coreService);
114
+ this.coreMouseService = this.register(this._instantiationService.createInstance(CoreMouseService));
115
+ this._instantiationService.setService(ICoreMouseService, this.coreMouseService);
116
+ this.unicodeService = this.register(this._instantiationService.createInstance(UnicodeService));
117
+ this._instantiationService.setService(IUnicodeService, this.unicodeService);
118
+ this._charsetService = this._instantiationService.createInstance(CharsetService);
119
+ this._instantiationService.setService(ICharsetService, this._charsetService);
120
+ this._oscLinkService = this._instantiationService.createInstance(OscLinkService);
121
+ this._instantiationService.setService(IOscLinkService, this._oscLinkService);
122
+
123
+ // Register input handler and handle/forward events
124
+ this._inputHandler = this.register(new InputHandler(this._bufferService, this._charsetService, this.coreService, this._logService, this.optionsService, this._oscLinkService, this.coreMouseService, this.unicodeService));
125
+ this.register(forwardEvent(this._inputHandler.onLineFeed, this._onLineFeed));
126
+ this.register(this._inputHandler);
127
+
128
+ // Setup listeners
129
+ this.register(forwardEvent(this._bufferService.onResize, this._onResize));
130
+ this.register(forwardEvent(this.coreService.onData, this._onData));
131
+ this.register(forwardEvent(this.coreService.onBinary, this._onBinary));
132
+ this.register(this.coreService.onRequestScrollToBottom(() => this.scrollToBottom()));
133
+ this.register(this.coreService.onUserInput(() => this._writeBuffer.handleUserInput()));
134
+ this.register(this.optionsService.onMultipleOptionChange(['windowsMode', 'windowsPty'], () => this._handleWindowsPtyOptionChange()));
135
+ this.register(this._bufferService.onScroll(event => {
136
+ this._onScroll.fire({ position: this._bufferService.buffer.ydisp, source: ScrollSource.TERMINAL });
137
+ this._inputHandler.markRangeDirty(this._bufferService.buffer.scrollTop, this._bufferService.buffer.scrollBottom);
138
+ }));
139
+ this.register(this._inputHandler.onScroll(event => {
140
+ this._onScroll.fire({ position: this._bufferService.buffer.ydisp, source: ScrollSource.TERMINAL });
141
+ this._inputHandler.markRangeDirty(this._bufferService.buffer.scrollTop, this._bufferService.buffer.scrollBottom);
142
+ }));
143
+
144
+ // Setup WriteBuffer
145
+ this._writeBuffer = this.register(new WriteBuffer((data, promiseResult) => this._inputHandler.parse(data, promiseResult)));
146
+ this.register(forwardEvent(this._writeBuffer.onWriteParsed, this._onWriteParsed));
147
+ }
148
+
149
+ public write(data: string | Uint8Array, callback?: () => void): void {
150
+ this._writeBuffer.write(data, callback);
151
+ }
152
+
153
+ /**
154
+ * Write data to terminal synchonously.
155
+ *
156
+ * This method is unreliable with async parser handlers, thus should not
157
+ * be used anymore. If you need blocking semantics on data input consider
158
+ * `write` with a callback instead.
159
+ *
160
+ * @deprecated Unreliable, will be removed soon.
161
+ */
162
+ public writeSync(data: string | Uint8Array, maxSubsequentCalls?: number): void {
163
+ if (this._logService.logLevel <= LogLevelEnum.WARN && !hasWriteSyncWarnHappened) {
164
+ this._logService.warn('writeSync is unreliable and will be removed soon.');
165
+ hasWriteSyncWarnHappened = true;
166
+ }
167
+ this._writeBuffer.writeSync(data, maxSubsequentCalls);
168
+ }
169
+
170
+ public resize(x: number, y: number): void {
171
+ if (isNaN(x) || isNaN(y)) {
172
+ return;
173
+ }
174
+
175
+ x = Math.max(x, MINIMUM_COLS);
176
+ y = Math.max(y, MINIMUM_ROWS);
177
+
178
+ this._bufferService.resize(x, y);
179
+ }
180
+
181
+ /**
182
+ * Scroll the terminal down 1 row, creating a blank line.
183
+ * @param eraseAttr The attribute data to use the for blank line.
184
+ * @param isWrapped Whether the new line is wrapped from the previous line.
185
+ */
186
+ public scroll(eraseAttr: IAttributeData, isWrapped: boolean = false): void {
187
+ this._bufferService.scroll(eraseAttr, isWrapped);
188
+ }
189
+
190
+ /**
191
+ * Scroll the display of the terminal
192
+ * @param disp The number of lines to scroll down (negative scroll up).
193
+ * @param suppressScrollEvent Don't emit the scroll event as scrollLines. This is used to avoid
194
+ * unwanted events being handled by the viewport when the event was triggered from the viewport
195
+ * originally.
196
+ * @param source Which component the event came from.
197
+ */
198
+ public scrollLines(disp: number, suppressScrollEvent?: boolean, source?: ScrollSource): void {
199
+ this._bufferService.scrollLines(disp, suppressScrollEvent, source);
200
+ }
201
+
202
+ public scrollPages(pageCount: number): void {
203
+ this.scrollLines(pageCount * (this.rows - 1));
204
+ }
205
+
206
+ public scrollToTop(): void {
207
+ this.scrollLines(-this._bufferService.buffer.ydisp);
208
+ }
209
+
210
+ public scrollToBottom(): void {
211
+ this.scrollLines(this._bufferService.buffer.ybase - this._bufferService.buffer.ydisp);
212
+ }
213
+
214
+ public scrollToLine(line: number): void {
215
+ const scrollAmount = line - this._bufferService.buffer.ydisp;
216
+ if (scrollAmount !== 0) {
217
+ this.scrollLines(scrollAmount);
218
+ }
219
+ }
220
+
221
+ /** Add handler for ESC escape sequence. See xterm.d.ts for details. */
222
+ public registerEscHandler(id: IFunctionIdentifier, callback: () => boolean | Promise<boolean>): IDisposable {
223
+ return this._inputHandler.registerEscHandler(id, callback);
224
+ }
225
+
226
+ /** Add handler for DCS escape sequence. See xterm.d.ts for details. */
227
+ public registerDcsHandler(id: IFunctionIdentifier, callback: (data: string, param: IParams) => boolean | Promise<boolean>): IDisposable {
228
+ return this._inputHandler.registerDcsHandler(id, callback);
229
+ }
230
+
231
+ /** Add handler for CSI escape sequence. See xterm.d.ts for details. */
232
+ public registerCsiHandler(id: IFunctionIdentifier, callback: (params: IParams) => boolean | Promise<boolean>): IDisposable {
233
+ return this._inputHandler.registerCsiHandler(id, callback);
234
+ }
235
+
236
+ /** Add handler for OSC escape sequence. See xterm.d.ts for details. */
237
+ public registerOscHandler(ident: number, callback: (data: string) => boolean | Promise<boolean>): IDisposable {
238
+ return this._inputHandler.registerOscHandler(ident, callback);
239
+ }
240
+
241
+ protected _setup(): void {
242
+ this._handleWindowsPtyOptionChange();
243
+ }
244
+
245
+ public reset(): void {
246
+ this._inputHandler.reset();
247
+ this._bufferService.reset();
248
+ this._charsetService.reset();
249
+ this.coreService.reset();
250
+ this.coreMouseService.reset();
251
+ }
252
+
253
+
254
+ private _handleWindowsPtyOptionChange(): void {
255
+ let value = false;
256
+ const windowsPty = this.optionsService.rawOptions.windowsPty;
257
+ if (windowsPty && windowsPty.buildNumber !== undefined && windowsPty.buildNumber !== undefined) {
258
+ value = !!(windowsPty.backend === 'conpty' && windowsPty.buildNumber < 21376);
259
+ } else if (this.optionsService.rawOptions.windowsMode) {
260
+ value = true;
261
+ }
262
+ if (value) {
263
+ this._enableWindowsWrappingHeuristics();
264
+ } else {
265
+ this._windowsWrappingHeuristics.clear();
266
+ }
267
+ }
268
+
269
+ protected _enableWindowsWrappingHeuristics(): void {
270
+ if (!this._windowsWrappingHeuristics.value) {
271
+ const disposables: IDisposable[] = [];
272
+ disposables.push(this.onLineFeed(updateWindowsModeWrappedState.bind(null, this._bufferService)));
273
+ disposables.push(this.registerCsiHandler({ final: 'H' }, () => {
274
+ updateWindowsModeWrappedState(this._bufferService);
275
+ return false;
276
+ }));
277
+ this._windowsWrappingHeuristics.value = toDisposable(() => {
278
+ for (const d of disposables) {
279
+ d.dispose();
280
+ }
281
+ });
282
+ }
283
+ }
284
+ }