From bc35f28f512ec5eadb2cf1bff107686b4bbeed36 Mon Sep 17 00:00:00 2001 From: Philip Olson Date: Thu, 22 Feb 2018 17:07:35 -0600 Subject: [PATCH] Paste newlines as \r Fix issue where non-windows OS terminals don't paste newlines correctly into misbehaving programs. As noted in JetBrains/jediterm#136, the most practical solution is for terminals to handle this in their paste logic. In a perfect world, all terminal programs would implement bracketed paste mode properly, but that's a lot to ask. - translate newlines to \r which is what terminal programs expect --- src/handlers/Clipboard.test.ts | 18 ++++++++++++------ src/handlers/Clipboard.ts | 9 +++------ 2 files changed, 15 insertions(+), 12 deletions(-) diff --git a/src/handlers/Clipboard.test.ts b/src/handlers/Clipboard.test.ts index d35e5fa4..174f6efb 100644 --- a/src/handlers/Clipboard.test.ts +++ b/src/handlers/Clipboard.test.ts @@ -8,13 +8,19 @@ import * as Terminal from '../Terminal'; import * as Clipboard from './Clipboard'; describe('evaluatePastedTextProcessing', () => { - it('should replace carriage return + line feed with line feed on windows', () => { - const pastedText = 'foo\r\nbar\r\n'; - const processedText = Clipboard.prepareTextForTerminal(pastedText, false); - const windowsProcessedText = Clipboard.prepareTextForTerminal(pastedText, true); + it('should replace carriage return and/or line feed with carriage return', () => { + const pastedText = { + unix: 'foo\nbar\n', + windows: 'foo\r\nbar\r\n' + }; - assert.equal(processedText, 'foo\r\nbar\r\n'); - assert.equal(windowsProcessedText, 'foo\rbar\r'); + const processedText = { + unix: Clipboard.prepareTextForTerminal(pastedText.unix), + windows: Clipboard.prepareTextForTerminal(pastedText.windows) + }; + + assert.equal(processedText.unix, 'foo\rbar\r'); + assert.equal(processedText.windows, 'foo\rbar\r'); }); it('should bracket pasted text in bracketedPasteMode', () => { const pastedText = 'foo bar'; diff --git a/src/handlers/Clipboard.ts b/src/handlers/Clipboard.ts index 7ac97714..23925007 100644 --- a/src/handlers/Clipboard.ts +++ b/src/handlers/Clipboard.ts @@ -18,11 +18,8 @@ declare var window: IWindow; * Prepares text to be pasted into the terminal by normalizing the line endings * @param text The pasted text that needs processing before inserting into the terminal */ -export function prepareTextForTerminal(text: string, isMSWindows: boolean): string { - if (isMSWindows) { - return text.replace(/\r?\n/g, '\r'); - } - return text; +export function prepareTextForTerminal(text: string): string { + return text.replace(/\r?\n/g, '\r'); } /** @@ -62,7 +59,7 @@ export function pasteHandler(ev: ClipboardEvent, term: ITerminal): void { let text: string; let dispatchPaste = function(text: string): void { - text = prepareTextForTerminal(text, term.browser.isMSWindows); + text = prepareTextForTerminal(text); text = bracketTextForPaste(text, term.bracketedPasteMode); term.handler(text); term.textarea.value = '';