Support bracketed paste mode

- As described in https://cirw.in/blog/bracketed-paste
- Example support in vim:
  - https://github.com/ConradIrwin/vim-bracketed-paste/blob/master/plugin/bracketed-paste.vim
  - http://vimdoc.sourceforge.net/htmldoc/term.html#raw-terminal-mode
- Required for e.g. ipython, via python-prompt-toolkit:
  - https://github.com/ipython/ipython
  - https://github.com/jonathanslenders/python-prompt-toolkit

Repro:
- Run `ipython` (version 6.2.1)
- Copy multiple lines of text and paste into ipython prompt, e.g.
```py
print(1)
print(2)
print(3)
```
- Before: first line would paste and run, and subsequent lines are lost
- After: all lines paste into single prompt
This commit is contained in:
Dan Brown
2017-10-30 00:26:50 -07:00
parent 8ece306dbe
commit 1dbcf70cee
7 changed files with 46 additions and 0 deletions
+8
View File
@@ -16,4 +16,12 @@ describe('evaluatePastedTextProcessing', () => {
assert.equal(processedText, 'foo\r\nbar\r\n');
assert.equal(windowsProcessedText, 'foo\rbar\r');
});
it('should bracket pasted text in bracketedPasteMode', () => {
const pastedText = 'foo bar';
const unbracketedText = Clipboard.bracketTextForPaste(pastedText, false);
const bracketedText = Clipboard.bracketTextForPaste(pastedText, true);
assert.equal(unbracketedText, 'foo bar');
assert.equal(bracketedText, '\x1b[200~foo bar\x1b[201~');
});
});
+12
View File
@@ -25,6 +25,17 @@ export function prepareTextForTerminal(text: string, isMSWindows: boolean): stri
return text;
}
/**
* Bracket text for paste, if necessary, as per https://cirw.in/blog/bracketed-paste
* @param text The pasted text to bracket
*/
export function bracketTextForPaste(text: string, bracketedPasteMode: boolean): string {
if (bracketedPasteMode) {
return '\x1b[200~' + text + '\x1b[201~';
}
return text;
}
/**
* Binds copy functionality to the given terminal.
* @param {ClipboardEvent} ev The original copy event to be handled
@@ -52,6 +63,7 @@ export function pasteHandler(ev: ClipboardEvent, term: ITerminal): void {
let dispatchPaste = function(text: string): void {
text = prepareTextForTerminal(text, term.browser.isMSWindows);
text = bracketTextForPaste(text, term.bracketedPasteMode);
term.handler(text);
term.textarea.value = '';
term.emit('paste', text);