Skip to content

Commit

Permalink
Polish multi-line paste code
Browse files Browse the repository at this point in the history
  • Loading branch information
Tyriar committed Feb 1, 2022
1 parent 0bb6cac commit 05bd2e7
Showing 1 changed file with 23 additions and 23 deletions.
46 changes: 23 additions & 23 deletions src/vs/workbench/contrib/terminal/browser/terminalInstance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1061,38 +1061,38 @@ export class TerminalInstance extends Disposable implements ITerminalInstance {
}

private async _shouldPasteText(text: string): Promise<boolean> {
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<boolean>(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);
}
Expand Down

0 comments on commit 05bd2e7

Please sign in to comment.