@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,246 @@
1
+ /**
2
+ * Copyright (c) 2016 The xterm.js authors. All rights reserved.
3
+ * @license MIT
4
+ */
5
+
6
+ import { IRenderService } from 'browser/services/Services';
7
+ import { IBufferService, ICoreService, IOptionsService } from 'common/services/Services';
8
+ import { C0 } from 'common/data/EscapeSequences';
9
+
10
+ interface IPosition {
11
+ start: number;
12
+ end: number;
13
+ }
14
+
15
+ /**
16
+ * Encapsulates the logic for handling compositionstart, compositionupdate and compositionend
17
+ * events, displaying the in-progress composition to the UI and forwarding the final composition
18
+ * to the handler.
19
+ */
20
+ export class CompositionHelper {
21
+ /**
22
+ * Whether input composition is currently happening, eg. via a mobile keyboard, speech input or
23
+ * IME. This variable determines whether the compositionText should be displayed on the UI.
24
+ */
25
+ private _isComposing: boolean;
26
+ public get isComposing(): boolean { return this._isComposing; }
27
+
28
+ /**
29
+ * The position within the input textarea's value of the current composition.
30
+ */
31
+ private _compositionPosition: IPosition;
32
+
33
+ /**
34
+ * Whether a composition is in the process of being sent, setting this to false will cancel any
35
+ * in-progress composition.
36
+ */
37
+ private _isSendingComposition: boolean;
38
+
39
+ /**
40
+ * Data already sent due to keydown event.
41
+ */
42
+ private _dataAlreadySent: string;
43
+
44
+ constructor(
45
+ private readonly _textarea: HTMLTextAreaElement,
46
+ private readonly _compositionView: HTMLElement,
47
+ @IBufferService private readonly _bufferService: IBufferService,
48
+ @IOptionsService private readonly _optionsService: IOptionsService,
49
+ @ICoreService private readonly _coreService: ICoreService,
50
+ @IRenderService private readonly _renderService: IRenderService
51
+ ) {
52
+ this._isComposing = false;
53
+ this._isSendingComposition = false;
54
+ this._compositionPosition = { start: 0, end: 0 };
55
+ this._dataAlreadySent = '';
56
+ }
57
+
58
+ /**
59
+ * Handles the compositionstart event, activating the composition view.
60
+ */
61
+ public compositionstart(): void {
62
+ this._isComposing = true;
63
+ this._compositionPosition.start = this._textarea.value.length;
64
+ this._compositionView.textContent = '';
65
+ this._dataAlreadySent = '';
66
+ this._compositionView.classList.add('active');
67
+ }
68
+
69
+ /**
70
+ * Handles the compositionupdate event, updating the composition view.
71
+ * @param ev The event.
72
+ */
73
+ public compositionupdate(ev: Pick<CompositionEvent, 'data'>): void {
74
+ this._compositionView.textContent = ev.data;
75
+ this.updateCompositionElements();
76
+ setTimeout(() => {
77
+ this._compositionPosition.end = this._textarea.value.length;
78
+ }, 0);
79
+ }
80
+
81
+ /**
82
+ * Handles the compositionend event, hiding the composition view and sending the composition to
83
+ * the handler.
84
+ */
85
+ public compositionend(): void {
86
+ this._finalizeComposition(true);
87
+ }
88
+
89
+ /**
90
+ * Handles the keydown event, routing any necessary events to the CompositionHelper functions.
91
+ * @param ev The keydown event.
92
+ * @returns Whether the Terminal should continue processing the keydown event.
93
+ */
94
+ public keydown(ev: KeyboardEvent): boolean {
95
+ if (this._isComposing || this._isSendingComposition) {
96
+ if (ev.keyCode === 229) {
97
+ // Continue composing if the keyCode is the "composition character"
98
+ return false;
99
+ }
100
+ if (ev.keyCode === 16 || ev.keyCode === 17 || ev.keyCode === 18) {
101
+ // Continue composing if the keyCode is a modifier key
102
+ return false;
103
+ }
104
+ // Finish composition immediately. This is mainly here for the case where enter is
105
+ // pressed and the handler needs to be triggered before the command is executed.
106
+ this._finalizeComposition(false);
107
+ }
108
+
109
+ if (ev.keyCode === 229) {
110
+ // If the "composition character" is used but gets to this point it means a non-composition
111
+ // character (eg. numbers and punctuation) was pressed when the IME was active.
112
+ this._handleAnyTextareaChanges();
113
+ return false;
114
+ }
115
+
116
+ return true;
117
+ }
118
+
119
+ /**
120
+ * Finalizes the composition, resuming regular input actions. This is called when a composition
121
+ * is ending.
122
+ * @param waitForPropagation Whether to wait for events to propagate before sending
123
+ * the input. This should be false if a non-composition keystroke is entered before the
124
+ * compositionend event is triggered, such as enter, so that the composition is sent before
125
+ * the command is executed.
126
+ */
127
+ private _finalizeComposition(waitForPropagation: boolean): void {
128
+ this._compositionView.classList.remove('active');
129
+ this._isComposing = false;
130
+
131
+ if (!waitForPropagation) {
132
+ // Cancel any delayed composition send requests and send the input immediately.
133
+ this._isSendingComposition = false;
134
+ const input = this._textarea.value.substring(this._compositionPosition.start, this._compositionPosition.end);
135
+ this._coreService.triggerDataEvent(input, true);
136
+ } else {
137
+ // Make a deep copy of the composition position here as a new compositionstart event may
138
+ // fire before the setTimeout executes.
139
+ const currentCompositionPosition = {
140
+ start: this._compositionPosition.start,
141
+ end: this._compositionPosition.end
142
+ };
143
+
144
+ // Since composition* events happen before the changes take place in the textarea on most
145
+ // browsers, use a setTimeout with 0ms time to allow the native compositionend event to
146
+ // complete. This ensures the correct character is retrieved.
147
+ // This solution was used because:
148
+ // - The compositionend event's data property is unreliable, at least on Chromium
149
+ // - The last compositionupdate event's data property does not always accurately describe
150
+ // the character, a counter example being Korean where an ending consonsant can move to
151
+ // the following character if the following input is a vowel.
152
+ this._isSendingComposition = true;
153
+ setTimeout(() => {
154
+ // Ensure that the input has not already been sent
155
+ if (this._isSendingComposition) {
156
+ this._isSendingComposition = false;
157
+ let input;
158
+ // Add length of data already sent due to keydown event,
159
+ // otherwise input characters can be duplicated. (Issue #3191)
160
+ currentCompositionPosition.start += this._dataAlreadySent.length;
161
+ if (this._isComposing) {
162
+ // Use the end position to get the string if a new composition has started.
163
+ input = this._textarea.value.substring(currentCompositionPosition.start, currentCompositionPosition.end);
164
+ } else {
165
+ // Don't use the end position here in order to pick up any characters after the
166
+ // composition has finished, for example when typing a non-composition character
167
+ // (eg. 2) after a composition character.
168
+ input = this._textarea.value.substring(currentCompositionPosition.start);
169
+ }
170
+ if (input.length > 0) {
171
+ this._coreService.triggerDataEvent(input, true);
172
+ }
173
+ }
174
+ }, 0);
175
+ }
176
+ }
177
+
178
+ /**
179
+ * Apply any changes made to the textarea after the current event chain is allowed to complete.
180
+ * This should be called when not currently composing but a keydown event with the "composition
181
+ * character" (229) is triggered, in order to allow non-composition text to be entered when an
182
+ * IME is active.
183
+ */
184
+ private _handleAnyTextareaChanges(): void {
185
+ const oldValue = this._textarea.value;
186
+ setTimeout(() => {
187
+ // Ignore if a composition has started since the timeout
188
+ if (!this._isComposing) {
189
+ const newValue = this._textarea.value;
190
+
191
+ const diff = newValue.replace(oldValue, '');
192
+
193
+ this._dataAlreadySent = diff;
194
+
195
+ if (newValue.length > oldValue.length) {
196
+ this._coreService.triggerDataEvent(diff, true);
197
+ } else if (newValue.length < oldValue.length) {
198
+ this._coreService.triggerDataEvent(`${C0.DEL}`, true);
199
+ } else if ((newValue.length === oldValue.length) && (newValue !== oldValue)) {
200
+ this._coreService.triggerDataEvent(newValue, true);
201
+ }
202
+
203
+ }
204
+ }, 0);
205
+ }
206
+
207
+ /**
208
+ * Positions the composition view on top of the cursor and the textarea just below it (so the
209
+ * IME helper dialog is positioned correctly).
210
+ * @param dontRecurse Whether to use setTimeout to recursively trigger another update, this is
211
+ * necessary as the IME events across browsers are not consistently triggered.
212
+ */
213
+ public updateCompositionElements(dontRecurse?: boolean): void {
214
+ if (!this._isComposing) {
215
+ return;
216
+ }
217
+
218
+ if (this._bufferService.buffer.isCursorInViewport) {
219
+ const cursorX = Math.min(this._bufferService.buffer.x, this._bufferService.cols - 1);
220
+
221
+ const cellHeight = this._renderService.dimensions.css.cell.height;
222
+ const cursorTop = this._bufferService.buffer.y * this._renderService.dimensions.css.cell.height;
223
+ const cursorLeft = cursorX * this._renderService.dimensions.css.cell.width;
224
+
225
+ this._compositionView.style.left = cursorLeft + 'px';
226
+ this._compositionView.style.top = cursorTop + 'px';
227
+ this._compositionView.style.height = cellHeight + 'px';
228
+ this._compositionView.style.lineHeight = cellHeight + 'px';
229
+ this._compositionView.style.fontFamily = this._optionsService.rawOptions.fontFamily;
230
+ this._compositionView.style.fontSize = this._optionsService.rawOptions.fontSize + 'px';
231
+ // Sync the textarea to the exact position of the composition view so the IME knows where the
232
+ // text is.
233
+ const compositionViewBounds = this._compositionView.getBoundingClientRect();
234
+ this._textarea.style.left = cursorLeft + 'px';
235
+ this._textarea.style.top = cursorTop + 'px';
236
+ // Ensure the text area is at least 1x1, otherwise certain IMEs may break
237
+ this._textarea.style.width = Math.max(compositionViewBounds.width, 1) + 'px';
238
+ this._textarea.style.height = Math.max(compositionViewBounds.height, 1) + 'px';
239
+ this._textarea.style.lineHeight = compositionViewBounds.height + 'px';
240
+ }
241
+
242
+ if (!dontRecurse) {
243
+ setTimeout(() => this.updateCompositionElements(true), 0);
244
+ }
245
+ }
246
+ }
@@ -0,0 +1,54 @@
1
+ /**
2
+ * Copyright (c) 2017 The xterm.js authors. All rights reserved.
3
+ * @license MIT
4
+ */
5
+
6
+ export function getCoordsRelativeToElement(window: Pick<Window, 'getComputedStyle'>, event: {clientX: number, clientY: number}, element: HTMLElement): [number, number] {
7
+ const rect = element.getBoundingClientRect();
8
+ const elementStyle = window.getComputedStyle(element);
9
+ const leftPadding = parseInt(elementStyle.getPropertyValue('padding-left'));
10
+ const topPadding = parseInt(elementStyle.getPropertyValue('padding-top'));
11
+ return [
12
+ event.clientX - rect.left - leftPadding,
13
+ event.clientY - rect.top - topPadding
14
+ ];
15
+ }
16
+
17
+ /**
18
+ * Gets coordinates within the terminal for a particular mouse event. The result
19
+ * is returned as an array in the form [x, y] instead of an object as it's a
20
+ * little faster and this function is used in some low level code.
21
+ * @param window The window object the element belongs to.
22
+ * @param event The mouse event.
23
+ * @param element The terminal's container element.
24
+ * @param colCount The number of columns in the terminal.
25
+ * @param rowCount The number of rows n the terminal.
26
+ * @param hasValidCharSize Whether there is a valid character size available.
27
+ * @param cssCellWidth The cell width device pixel render dimensions.
28
+ * @param cssCellHeight The cell height device pixel render dimensions.
29
+ * @param isSelection Whether the request is for the selection or not. This will
30
+ * apply an offset to the x value such that the left half of the cell will
31
+ * select that cell and the right half will select the next cell.
32
+ */
33
+ export function getCoords(window: Pick<Window, 'getComputedStyle'>, event: Pick<MouseEvent, 'clientX' | 'clientY'>, element: HTMLElement, colCount: number, rowCount: number, hasValidCharSize: boolean, cssCellWidth: number, cssCellHeight: number, isSelection?: boolean): [number, number] | undefined {
34
+ // Coordinates cannot be measured if there are no valid
35
+ if (!hasValidCharSize) {
36
+ return undefined;
37
+ }
38
+
39
+ const coords = getCoordsRelativeToElement(window, event, element);
40
+ if (!coords) {
41
+ return undefined;
42
+ }
43
+
44
+ coords[0] = Math.ceil((coords[0] + (isSelection ? cssCellWidth / 2 : 0)) / cssCellWidth);
45
+ coords[1] = Math.ceil(coords[1] / cssCellHeight);
46
+
47
+ // Ensure coordinates are within the terminal viewport. Note that selections
48
+ // need an addition point of precision to cover the end point (as characters
49
+ // cover half of one char and half of the next).
50
+ coords[0] = Math.min(Math.max(coords[0], 1), colCount + (isSelection ? 1 : 0));
51
+ coords[1] = Math.min(Math.max(coords[1], 1), rowCount);
52
+
53
+ return coords;
54
+ }
@@ -0,0 +1,249 @@
1
+ /**
2
+ * Copyright (c) 2018 The xterm.js authors. All rights reserved.
3
+ * @license MIT
4
+ */
5
+
6
+ import { C0 } from 'common/data/EscapeSequences';
7
+ import { IBufferService } from 'common/services/Services';
8
+
9
+ const enum Direction {
10
+ UP = 'A',
11
+ DOWN = 'B',
12
+ RIGHT = 'C',
13
+ LEFT = 'D'
14
+ }
15
+
16
+ /**
17
+ * Concatenates all the arrow sequences together.
18
+ * Resets the starting row to an unwrapped row, moves to the requested row,
19
+ * then moves to requested col.
20
+ */
21
+ export function moveToCellSequence(targetX: number, targetY: number, bufferService: IBufferService, applicationCursor: boolean): string {
22
+ const startX = bufferService.buffer.x;
23
+ const startY = bufferService.buffer.y;
24
+
25
+ // The alt buffer should try to navigate between rows
26
+ if (!bufferService.buffer.hasScrollback) {
27
+ return resetStartingRow(startX, startY, targetX, targetY, bufferService, applicationCursor) +
28
+ moveToRequestedRow(startY, targetY, bufferService, applicationCursor) +
29
+ moveToRequestedCol(startX, startY, targetX, targetY, bufferService, applicationCursor);
30
+ }
31
+
32
+ // Only move horizontally for the normal buffer
33
+ let direction;
34
+ if (startY === targetY) {
35
+ direction = startX > targetX ? Direction.LEFT : Direction.RIGHT;
36
+ return repeat(Math.abs(startX - targetX), sequence(direction, applicationCursor));
37
+ }
38
+ direction = startY > targetY ? Direction.LEFT : Direction.RIGHT;
39
+ const rowDifference = Math.abs(startY - targetY);
40
+ const cellsToMove = colsFromRowEnd(startY > targetY ? targetX : startX, bufferService) +
41
+ (rowDifference - 1) * bufferService.cols + 1 /* wrap around 1 row */ +
42
+ colsFromRowBeginning(startY > targetY ? startX : targetX, bufferService);
43
+ return repeat(cellsToMove, sequence(direction, applicationCursor));
44
+ }
45
+
46
+ /**
47
+ * Find the number of cols from a row beginning to a col.
48
+ */
49
+ function colsFromRowBeginning(currX: number, bufferService: IBufferService): number {
50
+ return currX - 1;
51
+ }
52
+
53
+ /**
54
+ * Find the number of cols from a col to row end.
55
+ */
56
+ function colsFromRowEnd(currX: number, bufferService: IBufferService): number {
57
+ return bufferService.cols - currX;
58
+ }
59
+
60
+ /**
61
+ * If the initial position of the cursor is on a row that is wrapped, move the
62
+ * cursor up to the first row that is not wrapped to have accurate vertical
63
+ * positioning.
64
+ */
65
+ function resetStartingRow(startX: number, startY: number, targetX: number, targetY: number, bufferService: IBufferService, applicationCursor: boolean): string {
66
+ if (moveToRequestedRow(startY, targetY, bufferService, applicationCursor).length === 0) {
67
+ return '';
68
+ }
69
+ return repeat(bufferLine(
70
+ startX, startY, startX,
71
+ startY - wrappedRowsForRow(startY, bufferService), false, bufferService
72
+ ).length, sequence(Direction.LEFT, applicationCursor));
73
+ }
74
+
75
+ /**
76
+ * Using the reset starting and ending row, move to the requested row,
77
+ * ignoring wrapped rows
78
+ */
79
+ function moveToRequestedRow(startY: number, targetY: number, bufferService: IBufferService, applicationCursor: boolean): string {
80
+ const startRow = startY - wrappedRowsForRow(startY, bufferService);
81
+ const endRow = targetY - wrappedRowsForRow(targetY, bufferService);
82
+
83
+ const rowsToMove = Math.abs(startRow - endRow) - wrappedRowsCount(startY, targetY, bufferService);
84
+
85
+ return repeat(rowsToMove, sequence(verticalDirection(startY, targetY), applicationCursor));
86
+ }
87
+
88
+ /**
89
+ * Move to the requested col on the ending row
90
+ */
91
+ function moveToRequestedCol(startX: number, startY: number, targetX: number, targetY: number, bufferService: IBufferService, applicationCursor: boolean): string {
92
+ let startRow;
93
+ if (moveToRequestedRow(startY, targetY, bufferService, applicationCursor).length > 0) {
94
+ startRow = targetY - wrappedRowsForRow(targetY, bufferService);
95
+ } else {
96
+ startRow = startY;
97
+ }
98
+
99
+ const endRow = targetY;
100
+ const direction = horizontalDirection(startX, startY, targetX, targetY, bufferService, applicationCursor);
101
+
102
+ return repeat(bufferLine(
103
+ startX, startRow, targetX, endRow,
104
+ direction === Direction.RIGHT, bufferService
105
+ ).length, sequence(direction, applicationCursor));
106
+ }
107
+
108
+ /**
109
+ * Utility functions
110
+ */
111
+
112
+ /**
113
+ * Calculates the number of wrapped rows between the unwrapped starting and
114
+ * ending rows. These rows need to ignored since the cursor skips over them.
115
+ */
116
+ function wrappedRowsCount(startY: number, targetY: number, bufferService: IBufferService): number {
117
+ let wrappedRows = 0;
118
+ const startRow = startY - wrappedRowsForRow(startY, bufferService);
119
+ const endRow = targetY - wrappedRowsForRow(targetY, bufferService);
120
+
121
+ for (let i = 0; i < Math.abs(startRow - endRow); i++) {
122
+ const direction = verticalDirection(startY, targetY) === Direction.UP ? -1 : 1;
123
+ const line = bufferService.buffer.lines.get(startRow + (direction * i));
124
+ if (line?.isWrapped) {
125
+ wrappedRows++;
126
+ }
127
+ }
128
+
129
+ return wrappedRows;
130
+ }
131
+
132
+ /**
133
+ * Calculates the number of wrapped rows that make up a given row.
134
+ * @param currentRow The row to determine how many wrapped rows make it up
135
+ */
136
+ function wrappedRowsForRow(currentRow: number, bufferService: IBufferService): number {
137
+ let rowCount = 0;
138
+ let line = bufferService.buffer.lines.get(currentRow);
139
+ let lineWraps = line?.isWrapped;
140
+
141
+ while (lineWraps && currentRow >= 0 && currentRow < bufferService.rows) {
142
+ rowCount++;
143
+ line = bufferService.buffer.lines.get(--currentRow);
144
+ lineWraps = line?.isWrapped;
145
+ }
146
+
147
+ return rowCount;
148
+ }
149
+
150
+ /**
151
+ * Direction determiners
152
+ */
153
+
154
+ /**
155
+ * Determines if the right or left arrow is needed
156
+ */
157
+ function horizontalDirection(startX: number, startY: number, targetX: number, targetY: number, bufferService: IBufferService, applicationCursor: boolean): Direction {
158
+ let startRow;
159
+ if (moveToRequestedRow(targetX, targetY, bufferService, applicationCursor).length > 0) {
160
+ startRow = targetY - wrappedRowsForRow(targetY, bufferService);
161
+ } else {
162
+ startRow = startY;
163
+ }
164
+
165
+ if ((startX < targetX &&
166
+ startRow <= targetY) || // down/right or same y/right
167
+ (startX >= targetX &&
168
+ startRow < targetY)) { // down/left or same y/left
169
+ return Direction.RIGHT;
170
+ }
171
+ return Direction.LEFT;
172
+ }
173
+
174
+ /**
175
+ * Determines if the up or down arrow is needed
176
+ */
177
+ function verticalDirection(startY: number, targetY: number): Direction {
178
+ return startY > targetY ? Direction.UP : Direction.DOWN;
179
+ }
180
+
181
+ /**
182
+ * Constructs the string of chars in the buffer from a starting row and col
183
+ * to an ending row and col
184
+ * @param startCol The starting column position
185
+ * @param startRow The starting row position
186
+ * @param endCol The ending column position
187
+ * @param endRow The ending row position
188
+ * @param forward Direction to move
189
+ */
190
+ function bufferLine(
191
+ startCol: number,
192
+ startRow: number,
193
+ endCol: number,
194
+ endRow: number,
195
+ forward: boolean,
196
+ bufferService: IBufferService
197
+ ): string {
198
+ let currentCol = startCol;
199
+ let currentRow = startRow;
200
+ let bufferStr = '';
201
+
202
+ while (currentCol !== endCol || currentRow !== endRow) {
203
+ currentCol += forward ? 1 : -1;
204
+
205
+ if (forward && currentCol > bufferService.cols - 1) {
206
+ bufferStr += bufferService.buffer.translateBufferLineToString(
207
+ currentRow, false, startCol, currentCol
208
+ );
209
+ currentCol = 0;
210
+ startCol = 0;
211
+ currentRow++;
212
+ } else if (!forward && currentCol < 0) {
213
+ bufferStr += bufferService.buffer.translateBufferLineToString(
214
+ currentRow, false, 0, startCol + 1
215
+ );
216
+ currentCol = bufferService.cols - 1;
217
+ startCol = currentCol;
218
+ currentRow--;
219
+ }
220
+ }
221
+
222
+ return bufferStr + bufferService.buffer.translateBufferLineToString(
223
+ currentRow, false, startCol, currentCol
224
+ );
225
+ }
226
+
227
+ /**
228
+ * Constructs the escape sequence for clicking an arrow
229
+ * @param direction The direction to move
230
+ */
231
+ function sequence(direction: Direction, applicationCursor: boolean): string {
232
+ const mod = applicationCursor ? 'O' : '[';
233
+ return C0.ESC + mod + direction;
234
+ }
235
+
236
+ /**
237
+ * Returns a string repeated a given number of times
238
+ * Polyfill from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/repeat
239
+ * @param count The number of times to repeat the string
240
+ * @param str The string that is to be repeated
241
+ */
242
+ function repeat(count: number, str: string): string {
243
+ count = Math.floor(count);
244
+ let rpt = '';
245
+ for (let i = 0; i < count; i++) {
246
+ rpt += str;
247
+ }
248
+ return rpt;
249
+ }