From 05bd2e77c5cdbdad720e79326ab7d919a7517ab4 Mon Sep 17 00:00:00 2001 From: Daniel Imms <2193314+Tyriar@users.noreply.github.com> Date: Tue, 1 Feb 2022 07:01:48 -0800 Subject: [PATCH] Polish multi-line paste code --- .../terminal/browser/terminalInstance.ts | 46 +++++++++---------- 1 file changed, 23 insertions(+), 23 deletions(-) diff --git a/src/vs/workbench/contrib/terminal/browser/terminalInstance.ts b/src/vs/workbench/contrib/terminal/browser/terminalInstance.ts index 9e9d70ffb278b..4db17be1f84a7 100644 --- a/src/vs/workbench/contrib/terminal/browser/terminalInstance.ts +++ b/src/vs/workbench/contrib/terminal/browser/terminalInstance.ts @@ -1061,38 +1061,38 @@ export class TerminalInstance extends Disposable implements ITerminalInstance { } private async _shouldPasteText(text: string): Promise { - const textForLines = text.split(/(\r?\n)/); + const textForLines = text.split(/\r?\n/); let confirmation: IConfirmationResult; // If the clipboard has only one line, no prompt will be triggered if (textForLines.length === 1 || !this._configurationService.getValue(TerminalSettingId.EnableMultiLinePasteWarning)) { return true; - } else { - const message = nls.localize('confirmMoveTrashMessageFilesAndDirectories', "Are you sure you want to paste the following {0} lines to the terminal?", text.split(/\r\n|\n|\r/).length); + } - const displayItemsCount = 3; - const ellipsis = '…'; + const displayItemsCount = 3; + const maxPreviewLineLength = 30; - let detail = textForLines - .slice(0, displayItemsCount) - .map(s => s.length >= 30 ? s.slice(0, 30) + ellipsis : s) - .join(''); - if (textForLines.length > displayItemsCount && !detail.endsWith(ellipsis)) { - detail += ellipsis; - } - const primaryButton = nls.localize({ key: 'multiLinePasteButton', comment: ['&& denotes a mnemonic'] }, "&&Paste"); - - confirmation = await this._dialogService.confirm({ - type: 'question', - message: message, - detail: detail, - primaryButton: primaryButton, - checkbox: { - label: nls.localize('doNotAskAgain', "Do not ask me again") - } - }); + let detail = 'Preview:'; + for (let i = 0; i < Math.min(textForLines.length, displayItemsCount); i++) { + const line = textForLines[i]; + const cleanedLine = line.length > maxPreviewLineLength ? `${line.slice(0, maxPreviewLineLength)}…` : line; + detail += `\n${cleanedLine}`; } + if (textForLines.length > displayItemsCount) { + detail += `\n…`; + } + + confirmation = await this._dialogService.confirm({ + type: 'question', + message: nls.localize('confirmMoveTrashMessageFilesAndDirectories', "Are you sure you want to paste {0} lines of text into the terminal?", textForLines.length), + detail, + primaryButton: nls.localize({ key: 'multiLinePasteButton', comment: ['&& denotes a mnemonic'] }, "&&Paste"), + checkbox: { + label: nls.localize('doNotAskAgain', "Do not ask me again") + } + }); + if (confirmation.confirmed && confirmation.checkboxChecked) { await this._configurationService.updateValue(TerminalSettingId.EnableMultiLinePasteWarning, false); }