Debian Codegraph @

node/@xterm/xterm/src/browser/input/MoveToCell.ts

raw
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 currentRow >= 0 &&
204 currentRow < bufferService.buffer.lines.length) {
205 currentCol += forward ? 1 : -1;
206
207 if (forward && currentCol > bufferService.cols - 1) {
208 bufferStr += bufferService.buffer.translateBufferLineToString(
209 currentRow, false, startCol, currentCol
210 );
211 currentCol = 0;
212 startCol = 0;
213 currentRow++;
214 } else if (!forward && currentCol < 0) {
215 bufferStr += bufferService.buffer.translateBufferLineToString(
216 currentRow, false, 0, startCol + 1
217 );
218 currentCol = bufferService.cols - 1;
219 startCol = currentCol;
220 currentRow--;
221 }
222 }
223
224 return bufferStr + bufferService.buffer.translateBufferLineToString(
225 currentRow, false, startCol, currentCol
226 );
227 }
228
229 /**
230 * Constructs the escape sequence for clicking an arrow
231 * @param direction The direction to move
232 */
233 function sequence(direction: Direction, applicationCursor: boolean): string {
234 const mod = applicationCursor ? 'O' : '[';
235 return C0.ESC + mod + direction;
236 }
237
238 /**
239 * Returns a string repeated a given number of times
240 * Polyfill from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/repeat
241 * @param count The number of times to repeat the string
242 * @param str The string that is to be repeated
243 */
244 function repeat(count: number, str: string): string {
245 count = Math.floor(count);
246 let rpt = '';
247 for (let i = 0; i < count; i++) {
248 rpt += str;
249 }
250 return rpt;
251 }