MCPcopy
hub / github.com/xtermjs/xterm.js / selectionText

Method selectionText

src/browser/services/SelectionService.ts:203–262  ·  view source on GitHub ↗

* Gets the text currently selected.

()

Source from the content-addressed store, hash-verified

201 * Gets the text currently selected.
202 */
203 public get selectionText(): string {
204 const start = this._model.finalSelectionStart;
205 const end = this._model.finalSelectionEnd;
206 if (!start || !end) {
207 return '';
208 }
209
210 const buffer = this._bufferService.buffer;
211 const result: string[] = [];
212
213 if (this._activeSelectionMode === SelectionMode.COLUMN) {
214 // Ignore zero width selections
215 if (start[0] === end[0]) {
216 return '';
217 }
218
219 // For column selection it's not enough to rely on final selection's swapping of reversed
220 // values, it also needs the x coordinates to swap independently of the y coordinate is needed
221 const startCol = start[0] < end[0] ? start[0] : end[0];
222 const endCol = start[0] < end[0] ? end[0] : start[0];
223 for (let i = start[1]; i <= end[1]; i++) {
224 const lineText = buffer.translateBufferLineToString(i, true, startCol, endCol);
225 result.push(lineText);
226 }
227 } else {
228 // Get first row
229 const startRowEndCol = start[1] === end[1] ? end[0] : undefined;
230 result.push(buffer.translateBufferLineToString(start[1], true, start[0], startRowEndCol));
231
232 // Get middle rows
233 for (let i = start[1] + 1; i <= end[1] - 1; i++) {
234 const bufferLine = buffer.lines.get(i);
235 const lineText = buffer.translateBufferLineToString(i, true);
236 if (bufferLine?.isWrapped) {
237 result[result.length - 1] += lineText;
238 } else {
239 result.push(lineText);
240 }
241 }
242
243 // Get final row
244 if (start[1] !== end[1]) {
245 const bufferLine = buffer.lines.get(end[1]);
246 const lineText = buffer.translateBufferLineToString(end[1], true, 0, end[0]);
247 if (bufferLine && bufferLine!.isWrapped) {
248 result[result.length - 1] += lineText;
249 } else {
250 result.push(lineText);
251 }
252 }
253 }
254
255 // Format string by replacing non-breaking space chars with regular spaces
256 // and joining the array into a multi-line string.
257 const formattedResult = result.map(line => {
258 return line.replace(ALL_NON_BREAKING_SPACE_REGEX, ' ');
259 }).join(Browser.isWindows ? '\r\n' : '\n');
260

Callers

nothing calls this directly

Calls 5

joinMethod · 0.80
pushMethod · 0.65
getMethod · 0.65
mapMethod · 0.65

Tested by

no test coverage detected