@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,373 @@
1
+ /**
2
+ * Copyright (c) 2019 The xterm.js authors. All rights reserved.
3
+ * @license MIT
4
+ */
5
+
6
+ import { IEvent, IEventEmitter } from 'common/EventEmitter';
7
+ import { IBuffer, IBufferSet } from 'common/buffer/Types';
8
+ import { IDecPrivateModes, ICoreMouseEvent, CoreMouseEncoding, ICoreMouseProtocol, CoreMouseEventType, ICharset, IWindowOptions, IModes, IAttributeData, ScrollSource, IDisposable, IColor, CursorStyle, CursorInactiveStyle, IOscLinkData } from 'common/Types';
9
+ import { createDecorator } from 'common/services/ServiceRegistry';
10
+ import { IDecorationOptions, IDecoration, ILinkHandler, IWindowsPty, ILogger } from '@xterm/xterm';
11
+
12
+ export const IBufferService = createDecorator<IBufferService>('BufferService');
13
+ export interface IBufferService {
14
+ serviceBrand: undefined;
15
+
16
+ readonly cols: number;
17
+ readonly rows: number;
18
+ readonly buffer: IBuffer;
19
+ readonly buffers: IBufferSet;
20
+ isUserScrolling: boolean;
21
+ onResize: IEvent<{ cols: number, rows: number }>;
22
+ onScroll: IEvent<number>;
23
+ scroll(eraseAttr: IAttributeData, isWrapped?: boolean): void;
24
+ scrollLines(disp: number, suppressScrollEvent?: boolean, source?: ScrollSource): void;
25
+ resize(cols: number, rows: number): void;
26
+ reset(): void;
27
+ }
28
+
29
+ export const ICoreMouseService = createDecorator<ICoreMouseService>('CoreMouseService');
30
+ export interface ICoreMouseService {
31
+ activeProtocol: string;
32
+ activeEncoding: string;
33
+ areMouseEventsActive: boolean;
34
+ addProtocol(name: string, protocol: ICoreMouseProtocol): void;
35
+ addEncoding(name: string, encoding: CoreMouseEncoding): void;
36
+ reset(): void;
37
+
38
+ /**
39
+ * Triggers a mouse event to be sent.
40
+ *
41
+ * Returns true if the event passed all protocol restrictions and a report
42
+ * was sent, otherwise false. The return value may be used to decide whether
43
+ * the default event action in the bowser component should be omitted.
44
+ *
45
+ * Note: The method will change values of the given event object
46
+ * to fullfill protocol and encoding restrictions.
47
+ */
48
+ triggerMouseEvent(event: ICoreMouseEvent): boolean;
49
+
50
+ /**
51
+ * Event to announce changes in mouse tracking.
52
+ */
53
+ onProtocolChange: IEvent<CoreMouseEventType>;
54
+
55
+ /**
56
+ * Human readable version of mouse events.
57
+ */
58
+ explainEvents(events: CoreMouseEventType): { [event: string]: boolean };
59
+ }
60
+
61
+ export const ICoreService = createDecorator<ICoreService>('CoreService');
62
+ export interface ICoreService {
63
+ serviceBrand: undefined;
64
+
65
+ /**
66
+ * Initially the cursor will not be visible until the first time the terminal
67
+ * is focused.
68
+ */
69
+ isCursorInitialized: boolean;
70
+ isCursorHidden: boolean;
71
+
72
+ readonly modes: IModes;
73
+ readonly decPrivateModes: IDecPrivateModes;
74
+
75
+ readonly onData: IEvent<string>;
76
+ readonly onUserInput: IEvent<void>;
77
+ readonly onBinary: IEvent<string>;
78
+ readonly onRequestScrollToBottom: IEvent<void>;
79
+
80
+ reset(): void;
81
+
82
+ /**
83
+ * Triggers the onData event in the public API.
84
+ * @param data The data that is being emitted.
85
+ * @param wasUserInput Whether the data originated from the user (as opposed to
86
+ * resulting from parsing incoming data). When true this will also:
87
+ * - Scroll to the bottom of the buffer if option scrollOnUserInput is true.
88
+ * - Fire the `onUserInput` event (so selection can be cleared).
89
+ */
90
+ triggerDataEvent(data: string, wasUserInput?: boolean): void;
91
+
92
+ /**
93
+ * Triggers the onBinary event in the public API.
94
+ * @param data The data that is being emitted.
95
+ */
96
+ triggerBinaryEvent(data: string): void;
97
+ }
98
+
99
+ export const ICharsetService = createDecorator<ICharsetService>('CharsetService');
100
+ export interface ICharsetService {
101
+ serviceBrand: undefined;
102
+
103
+ charset: ICharset | undefined;
104
+ readonly glevel: number;
105
+
106
+ reset(): void;
107
+
108
+ /**
109
+ * Set the G level of the terminal.
110
+ * @param g
111
+ */
112
+ setgLevel(g: number): void;
113
+
114
+ /**
115
+ * Set the charset for the given G level of the terminal.
116
+ * @param g
117
+ * @param charset
118
+ */
119
+ setgCharset(g: number, charset: ICharset | undefined): void;
120
+ }
121
+
122
+ export interface IServiceIdentifier<T> {
123
+ (...args: any[]): void;
124
+ type: T;
125
+ }
126
+
127
+ export interface IBrandedService {
128
+ serviceBrand: undefined;
129
+ }
130
+
131
+ type GetLeadingNonServiceArgs<TArgs extends any[]> = TArgs extends [] ? []
132
+ : TArgs extends [...infer TFirst, infer TLast] ? TLast extends IBrandedService ? GetLeadingNonServiceArgs<TFirst> : TArgs
133
+ : never;
134
+
135
+ export const IInstantiationService = createDecorator<IInstantiationService>('InstantiationService');
136
+ export interface IInstantiationService {
137
+ serviceBrand: undefined;
138
+
139
+ setService<T>(id: IServiceIdentifier<T>, instance: T): void;
140
+ getService<T>(id: IServiceIdentifier<T>): T | undefined;
141
+ createInstance<Ctor extends new (...args: any[]) => any, R extends InstanceType<Ctor>>(t: Ctor, ...args: GetLeadingNonServiceArgs<ConstructorParameters<Ctor>>): R;
142
+ }
143
+
144
+ export enum LogLevelEnum {
145
+ TRACE = 0,
146
+ DEBUG = 1,
147
+ INFO = 2,
148
+ WARN = 3,
149
+ ERROR = 4,
150
+ OFF = 5
151
+ }
152
+
153
+ export const ILogService = createDecorator<ILogService>('LogService');
154
+ export interface ILogService {
155
+ serviceBrand: undefined;
156
+
157
+ readonly logLevel: LogLevelEnum;
158
+
159
+ trace(message: any, ...optionalParams: any[]): void;
160
+ debug(message: any, ...optionalParams: any[]): void;
161
+ info(message: any, ...optionalParams: any[]): void;
162
+ warn(message: any, ...optionalParams: any[]): void;
163
+ error(message: any, ...optionalParams: any[]): void;
164
+ }
165
+
166
+ export const IOptionsService = createDecorator<IOptionsService>('OptionsService');
167
+ export interface IOptionsService {
168
+ serviceBrand: undefined;
169
+
170
+ /**
171
+ * Read only access to the raw options object, this is an internal-only fast path for accessing
172
+ * single options without any validation as we trust TypeScript to enforce correct usage
173
+ * internally.
174
+ */
175
+ readonly rawOptions: Required<ITerminalOptions>;
176
+
177
+ /**
178
+ * Options as exposed through the public API, this property uses getters and setters with
179
+ * validation which makes it safer but slower. {@link rawOptions} should be used for pretty much
180
+ * all internal usage for performance reasons.
181
+ */
182
+ readonly options: Required<ITerminalOptions>;
183
+
184
+ /**
185
+ * Adds an event listener for when any option changes.
186
+ */
187
+ readonly onOptionChange: IEvent<keyof ITerminalOptions>;
188
+
189
+ /**
190
+ * Adds an event listener for when a specific option changes, this is a convenience method that is
191
+ * preferred over {@link onOptionChange} when only a single option is being listened to.
192
+ */
193
+ // eslint-disable-next-line @typescript-eslint/naming-convention
194
+ onSpecificOptionChange<T extends keyof ITerminalOptions>(key: T, listener: (arg1: Required<ITerminalOptions>[T]) => any): IDisposable;
195
+
196
+ /**
197
+ * Adds an event listener for when a set of specific options change, this is a convenience method
198
+ * that is preferred over {@link onOptionChange} when multiple options are being listened to and
199
+ * handled the same way.
200
+ */
201
+ // eslint-disable-next-line @typescript-eslint/naming-convention
202
+ onMultipleOptionChange(keys: (keyof ITerminalOptions)[], listener: () => any): IDisposable;
203
+ }
204
+
205
+ export type FontWeight = 'normal' | 'bold' | '100' | '200' | '300' | '400' | '500' | '600' | '700' | '800' | '900' | number;
206
+ export type LogLevel = 'trace' | 'debug' | 'info' | 'warn' | 'error' | 'off';
207
+
208
+ export interface ITerminalOptions {
209
+ allowProposedApi?: boolean;
210
+ allowTransparency?: boolean;
211
+ altClickMovesCursor?: boolean;
212
+ cols?: number;
213
+ convertEol?: boolean;
214
+ cursorBlink?: boolean;
215
+ cursorStyle?: CursorStyle;
216
+ cursorWidth?: number;
217
+ cursorInactiveStyle?: CursorInactiveStyle;
218
+ customGlyphs?: boolean;
219
+ disableStdin?: boolean;
220
+ documentOverride?: any | null;
221
+ drawBoldTextInBrightColors?: boolean;
222
+ fastScrollModifier?: 'none' | 'alt' | 'ctrl' | 'shift';
223
+ fastScrollSensitivity?: number;
224
+ fontSize?: number;
225
+ fontFamily?: string;
226
+ fontWeight?: FontWeight;
227
+ fontWeightBold?: FontWeight;
228
+ ignoreBracketedPasteMode?: boolean;
229
+ letterSpacing?: number;
230
+ lineHeight?: number;
231
+ linkHandler?: ILinkHandler | null;
232
+ logLevel?: LogLevel;
233
+ logger?: ILogger | null;
234
+ macOptionIsMeta?: boolean;
235
+ macOptionClickForcesSelection?: boolean;
236
+ minimumContrastRatio?: number;
237
+ rightClickSelectsWord?: boolean;
238
+ rows?: number;
239
+ screenReaderMode?: boolean;
240
+ scrollback?: number;
241
+ scrollOnUserInput?: boolean;
242
+ scrollSensitivity?: number;
243
+ smoothScrollDuration?: number;
244
+ tabStopWidth?: number;
245
+ theme?: ITheme;
246
+ windowsMode?: boolean;
247
+ windowsPty?: IWindowsPty;
248
+ windowOptions?: IWindowOptions;
249
+ wordSeparator?: string;
250
+ overviewRulerWidth?: number;
251
+
252
+ [key: string]: any;
253
+ cancelEvents: boolean;
254
+ termName: string;
255
+ }
256
+
257
+ export interface ITheme {
258
+ foreground?: string;
259
+ background?: string;
260
+ cursor?: string;
261
+ cursorAccent?: string;
262
+ selectionForeground?: string;
263
+ selectionBackground?: string;
264
+ selectionInactiveBackground?: string;
265
+ black?: string;
266
+ red?: string;
267
+ green?: string;
268
+ yellow?: string;
269
+ blue?: string;
270
+ magenta?: string;
271
+ cyan?: string;
272
+ white?: string;
273
+ brightBlack?: string;
274
+ brightRed?: string;
275
+ brightGreen?: string;
276
+ brightYellow?: string;
277
+ brightBlue?: string;
278
+ brightMagenta?: string;
279
+ brightCyan?: string;
280
+ brightWhite?: string;
281
+ extendedAnsi?: string[];
282
+ }
283
+
284
+ export const IOscLinkService = createDecorator<IOscLinkService>('OscLinkService');
285
+ export interface IOscLinkService {
286
+ serviceBrand: undefined;
287
+ /**
288
+ * Registers a link to the service, returning the link ID. The link data is managed by this
289
+ * service and will be freed when this current cursor position is trimmed off the buffer.
290
+ */
291
+ registerLink(linkData: IOscLinkData): number;
292
+ /**
293
+ * Adds a line to a link if needed.
294
+ */
295
+ addLineToLink(linkId: number, y: number): void;
296
+ /** Get the link data associated with a link ID. */
297
+ getLinkData(linkId: number): IOscLinkData | undefined;
298
+ }
299
+
300
+ /*
301
+ * Width and Grapheme_Cluster_Break properties of a character as a bit mask.
302
+ *
303
+ * bit 0: shouldJoin - should combine with preceding character.
304
+ * bit 1..2: wcwidth - see UnicodeCharWidth.
305
+ * bit 3..31: class of character (currently only 4 bits are used).
306
+ * This is used to determined grapheme clustering - i.e. which codepoints
307
+ * are to be combined into a single compound character.
308
+ *
309
+ * Use the UnicodeService static function createPropertyValue to create a
310
+ * UnicodeCharProperties; use extractShouldJoin, extractWidth, and
311
+ * extractCharKind to extract the components.
312
+ */
313
+ export type UnicodeCharProperties = number;
314
+
315
+ /**
316
+ * Width in columns of a character.
317
+ * In a CJK context, "half-width" characters (such as Latin) are width 1,
318
+ * while "full-width" characters (such as Kanji) are 2 columns wide.
319
+ * Combining characters (such as accents) are width 0.
320
+ */
321
+ export type UnicodeCharWidth = 0 | 1 | 2;
322
+
323
+ export const IUnicodeService = createDecorator<IUnicodeService>('UnicodeService');
324
+ export interface IUnicodeService {
325
+ serviceBrand: undefined;
326
+ /** Register an Unicode version provider. */
327
+ register(provider: IUnicodeVersionProvider): void;
328
+ /** Registered Unicode versions. */
329
+ readonly versions: string[];
330
+ /** Currently active version. */
331
+ activeVersion: string;
332
+ /** Event triggered, when activate version changed. */
333
+ readonly onChange: IEvent<string>;
334
+
335
+ /**
336
+ * Unicode version dependent
337
+ */
338
+ wcwidth(codepoint: number): UnicodeCharWidth;
339
+ getStringCellWidth(s: string): number;
340
+ /**
341
+ * Return character width and type for grapheme clustering.
342
+ * If preceding != 0, it is the return code from the previous character;
343
+ * in that case the result specifies if the characters should be joined.
344
+ */
345
+ charProperties(codepoint: number, preceding: UnicodeCharProperties): UnicodeCharProperties;
346
+ }
347
+
348
+ export interface IUnicodeVersionProvider {
349
+ readonly version: string;
350
+ wcwidth(ucs: number): UnicodeCharWidth;
351
+ charProperties(codepoint: number, preceding: UnicodeCharProperties): UnicodeCharProperties;
352
+ }
353
+
354
+ export const IDecorationService = createDecorator<IDecorationService>('DecorationService');
355
+ export interface IDecorationService extends IDisposable {
356
+ serviceBrand: undefined;
357
+ readonly decorations: IterableIterator<IInternalDecoration>;
358
+ readonly onDecorationRegistered: IEvent<IInternalDecoration>;
359
+ readonly onDecorationRemoved: IEvent<IInternalDecoration>;
360
+ registerDecoration(decorationOptions: IDecorationOptions): IDecoration | undefined;
361
+ reset(): void;
362
+ /**
363
+ * Trigger a callback over the decoration at a cell (in no particular order). This uses a callback
364
+ * instead of an iterator as it's typically used in hot code paths.
365
+ */
366
+ forEachDecorationAtCell(x: number, line: number, layer: 'bottom' | 'top' | undefined, callback: (decoration: IInternalDecoration) => void): void;
367
+ }
368
+ export interface IInternalDecoration extends IDecoration {
369
+ readonly options: IDecorationOptions;
370
+ readonly backgroundColorRGB: IColor | undefined;
371
+ readonly foregroundColorRGB: IColor | undefined;
372
+ readonly onRenderEmitter: IEventEmitter<HTMLElement>;
373
+ }
@@ -0,0 +1,111 @@
1
+ /**
2
+ * Copyright (c) 2019 The xterm.js authors. All rights reserved.
3
+ * @license MIT
4
+ */
5
+
6
+ import { EventEmitter } from 'common/EventEmitter';
7
+ import { UnicodeV6 } from 'common/input/UnicodeV6';
8
+ import { IUnicodeService, IUnicodeVersionProvider, UnicodeCharProperties, UnicodeCharWidth } from 'common/services/Services';
9
+
10
+ export class UnicodeService implements IUnicodeService {
11
+ public serviceBrand: any;
12
+
13
+ private _providers: {[key: string]: IUnicodeVersionProvider} = Object.create(null);
14
+ private _active: string = '';
15
+ private _activeProvider: IUnicodeVersionProvider;
16
+
17
+ private readonly _onChange = new EventEmitter<string>();
18
+ public readonly onChange = this._onChange.event;
19
+
20
+ public static extractShouldJoin(value: UnicodeCharProperties): boolean {
21
+ return (value & 1) !== 0;
22
+ }
23
+ public static extractWidth(value: UnicodeCharProperties): UnicodeCharWidth {
24
+ return ((value >> 1) & 0x3) as UnicodeCharWidth;
25
+ }
26
+ public static extractCharKind(value: UnicodeCharProperties): number {
27
+ return value >> 3;
28
+ }
29
+ public static createPropertyValue(state: number, width: number, shouldJoin: boolean = false): UnicodeCharProperties {
30
+ return ((state & 0xffffff) << 3) | ((width & 3) << 1) | (shouldJoin?1:0);
31
+ }
32
+
33
+ constructor() {
34
+ const defaultProvider = new UnicodeV6();
35
+ this.register(defaultProvider);
36
+ this._active = defaultProvider.version;
37
+ this._activeProvider = defaultProvider;
38
+ }
39
+
40
+ public dispose(): void {
41
+ this._onChange.dispose();
42
+ }
43
+
44
+ public get versions(): string[] {
45
+ return Object.keys(this._providers);
46
+ }
47
+
48
+ public get activeVersion(): string {
49
+ return this._active;
50
+ }
51
+
52
+ public set activeVersion(version: string) {
53
+ if (!this._providers[version]) {
54
+ throw new Error(`unknown Unicode version "${version}"`);
55
+ }
56
+ this._active = version;
57
+ this._activeProvider = this._providers[version];
58
+ this._onChange.fire(version);
59
+ }
60
+
61
+ public register(provider: IUnicodeVersionProvider): void {
62
+ this._providers[provider.version] = provider;
63
+ }
64
+
65
+ /**
66
+ * Unicode version dependent interface.
67
+ */
68
+ public wcwidth(num: number): UnicodeCharWidth {
69
+ return this._activeProvider.wcwidth(num);
70
+ }
71
+
72
+ public getStringCellWidth(s: string): number {
73
+ let result = 0;
74
+ let precedingInfo = 0;
75
+ const length = s.length;
76
+ for (let i = 0; i < length; ++i) {
77
+ let code = s.charCodeAt(i);
78
+ // surrogate pair first
79
+ if (0xD800 <= code && code <= 0xDBFF) {
80
+ if (++i >= length) {
81
+ // this should not happen with strings retrieved from
82
+ // Buffer.translateToString as it converts from UTF-32
83
+ // and therefore always should contain the second part
84
+ // for any other string we still have to handle it somehow:
85
+ // simply treat the lonely surrogate first as a single char (UCS-2 behavior)
86
+ return result + this.wcwidth(code);
87
+ }
88
+ const second = s.charCodeAt(i);
89
+ // convert surrogate pair to high codepoint only for valid second part (UTF-16)
90
+ // otherwise treat them independently (UCS-2 behavior)
91
+ if (0xDC00 <= second && second <= 0xDFFF) {
92
+ code = (code - 0xD800) * 0x400 + second - 0xDC00 + 0x10000;
93
+ } else {
94
+ result += this.wcwidth(second);
95
+ }
96
+ }
97
+ const currentInfo = this.charProperties(code, precedingInfo);
98
+ let chWidth = UnicodeService.extractWidth(currentInfo);
99
+ if (UnicodeService.extractShouldJoin(currentInfo)) {
100
+ chWidth -= UnicodeService.extractWidth(precedingInfo);
101
+ }
102
+ result += chWidth;
103
+ precedingInfo = currentInfo;
104
+ }
105
+ return result;
106
+ }
107
+
108
+ public charProperties(codepoint: number, preceding: UnicodeCharProperties): UnicodeCharProperties {
109
+ return this._activeProvider.charProperties(codepoint, preceding);
110
+ }
111
+ }
@@ -0,0 +1,136 @@
1
+ /**
2
+ * Copyright (c) 2014 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 { DEFAULT_ATTR_DATA } from 'common/buffer/BufferLine';
25
+ import { IBuffer } from 'common/buffer/Types';
26
+ import { CoreTerminal } from 'common/CoreTerminal';
27
+ import { EventEmitter, forwardEvent } from 'common/EventEmitter';
28
+ import { IMarker, ITerminalOptions, ScrollSource } from 'common/Types';
29
+
30
+ export class Terminal extends CoreTerminal {
31
+ private readonly _onBell = this.register(new EventEmitter<void>());
32
+ public readonly onBell = this._onBell.event;
33
+ private readonly _onCursorMove = this.register(new EventEmitter<void>());
34
+ public readonly onCursorMove = this._onCursorMove.event;
35
+ private readonly _onTitleChange = this.register(new EventEmitter<string>());
36
+ public readonly onTitleChange = this._onTitleChange.event;
37
+ private readonly _onA11yCharEmitter = this.register(new EventEmitter<string>());
38
+ public readonly onA11yChar = this._onA11yCharEmitter.event;
39
+ private readonly _onA11yTabEmitter = this.register(new EventEmitter<number>());
40
+ public readonly onA11yTab = this._onA11yTabEmitter.event;
41
+
42
+ constructor(
43
+ options: ITerminalOptions = {}
44
+ ) {
45
+ super(options);
46
+
47
+ this._setup();
48
+
49
+ // Setup InputHandler listeners
50
+ this.register(this._inputHandler.onRequestBell(() => this.bell()));
51
+ this.register(this._inputHandler.onRequestReset(() => this.reset()));
52
+ this.register(forwardEvent(this._inputHandler.onCursorMove, this._onCursorMove));
53
+ this.register(forwardEvent(this._inputHandler.onTitleChange, this._onTitleChange));
54
+ this.register(forwardEvent(this._inputHandler.onA11yChar, this._onA11yCharEmitter));
55
+ this.register(forwardEvent(this._inputHandler.onA11yTab, this._onA11yTabEmitter));
56
+ }
57
+
58
+ /**
59
+ * Convenience property to active buffer.
60
+ */
61
+ public get buffer(): IBuffer {
62
+ return this.buffers.active;
63
+ }
64
+
65
+ // TODO: Support paste here?
66
+
67
+ public get markers(): IMarker[] {
68
+ return this.buffer.markers;
69
+ }
70
+
71
+ public addMarker(cursorYOffset: number): IMarker | undefined {
72
+ // Disallow markers on the alt buffer
73
+ if (this.buffer !== this.buffers.normal) {
74
+ return;
75
+ }
76
+
77
+ return this.buffer.addMarker(this.buffer.ybase + this.buffer.y + cursorYOffset);
78
+ }
79
+
80
+ public bell(): void {
81
+ this._onBell.fire();
82
+ }
83
+
84
+ /**
85
+ * Resizes the terminal.
86
+ *
87
+ * @param x The number of columns to resize to.
88
+ * @param y The number of rows to resize to.
89
+ */
90
+ public resize(x: number, y: number): void {
91
+ if (x === this.cols && y === this.rows) {
92
+ return;
93
+ }
94
+
95
+ super.resize(x, y);
96
+ }
97
+
98
+ /**
99
+ * Clear the entire buffer, making the prompt line the new first line.
100
+ */
101
+ public clear(): void {
102
+ if (this.buffer.ybase === 0 && this.buffer.y === 0) {
103
+ // Don't clear if it's already clear
104
+ return;
105
+ }
106
+ this.buffer.lines.set(0, this.buffer.lines.get(this.buffer.ybase + this.buffer.y)!);
107
+ this.buffer.lines.length = 1;
108
+ this.buffer.ydisp = 0;
109
+ this.buffer.ybase = 0;
110
+ this.buffer.y = 0;
111
+ for (let i = 1; i < this.rows; i++) {
112
+ this.buffer.lines.push(this.buffer.getBlankLine(DEFAULT_ATTR_DATA));
113
+ }
114
+ this._onScroll.fire({ position: this.buffer.ydisp, source: ScrollSource.TERMINAL });
115
+ }
116
+
117
+ /**
118
+ * Reset terminal.
119
+ * Note: Calling this directly from JS is synchronous but does not clear
120
+ * input buffers and does not reset the parser, thus the terminal will
121
+ * continue to apply pending input data.
122
+ * If you need in band reset (synchronous with input data) consider
123
+ * using DECSTR (soft reset, CSI ! p) or RIS instead (hard reset, ESC c).
124
+ */
125
+ public reset(): void {
126
+ /**
127
+ * Since _setup handles a full terminal creation, we have to carry forward
128
+ * a few things that should not reset.
129
+ */
130
+ this.options.rows = this.rows;
131
+ this.options.cols = this.cols;
132
+
133
+ this._setup();
134
+ super.reset();
135
+ }
136
+ }