Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added new command for sending custom sequence to terminal #56962

Merged
merged 7 commits into from
Sep 8, 2018
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/vs/workbench/parts/terminal/common/terminalCommands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ export const enum TERMINAL_COMMAND_ID {
SELECT_TO_NEXT_COMMAND = 'workbench.action.terminal.selectToNextCommand',
SELECT_TO_PREVIOUS_LINE = 'workbench.action.terminal.selectToPreviousLine',
SELECT_TO_NEXT_LINE = 'workbench.action.terminal.selectToNextLine',
TOGGLE_ESCAPE_SEQUENCE_LOGGING = 'toggleEscapeSequenceLogging'
TOGGLE_ESCAPE_SEQUENCE_LOGGING = 'toggleEscapeSequenceLogging',
SEND_SEQUENCE = 'workbench.action.terminal.sendSequence'
}


Expand All @@ -79,4 +80,4 @@ function registerOpenTerminalAtIndexCommands(): void {
}
});
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { getTerminalDefaultShellUnixLike, getTerminalDefaultShellWindows } from
import { IWorkbenchActionRegistry, Extensions as ActionExtensions } from 'vs/workbench/common/actions';
import { KeyCode, KeyMod } from 'vs/base/common/keyCodes';
import { ContextKeyExpr } from 'vs/platform/contextkey/common/contextkey';
import { KillTerminalAction, ClearSelectionTerminalAction, CopyTerminalSelectionAction, CreateNewTerminalAction, CreateNewInActiveWorkspaceTerminalAction, FocusActiveTerminalAction, FocusNextTerminalAction, FocusPreviousTerminalAction, SelectDefaultShellWindowsTerminalAction, RunSelectedTextInTerminalAction, RunActiveFileInTerminalAction, ScrollDownTerminalAction, ScrollDownPageTerminalAction, ScrollToBottomTerminalAction, ScrollUpTerminalAction, ScrollUpPageTerminalAction, ScrollToTopTerminalAction, TerminalPasteAction, ToggleTerminalAction, ClearTerminalAction, AllowWorkspaceShellTerminalCommand, DisallowWorkspaceShellTerminalCommand, RenameTerminalAction, SelectAllTerminalAction, FocusTerminalFindWidgetAction, HideTerminalFindWidgetAction, DeleteWordLeftTerminalAction, DeleteWordRightTerminalAction, QuickOpenActionTermContributor, QuickOpenTermAction, TERMINAL_PICKER_PREFIX, MoveToLineStartTerminalAction, MoveToLineEndTerminalAction, SplitTerminalAction, SplitInActiveWorkspaceTerminalAction, FocusPreviousPaneTerminalAction, FocusNextPaneTerminalAction, ResizePaneLeftTerminalAction, ResizePaneRightTerminalAction, ResizePaneUpTerminalAction, ResizePaneDownTerminalAction, ScrollToPreviousCommandAction, ScrollToNextCommandAction, SelectToPreviousCommandAction, SelectToNextCommandAction, SelectToPreviousLineAction, SelectToNextLineAction, ToggleEscapeSequenceLoggingAction } from 'vs/workbench/parts/terminal/electron-browser/terminalActions';
import { KillTerminalAction, ClearSelectionTerminalAction, CopyTerminalSelectionAction, CreateNewTerminalAction, CreateNewInActiveWorkspaceTerminalAction, FocusActiveTerminalAction, FocusNextTerminalAction, FocusPreviousTerminalAction, SelectDefaultShellWindowsTerminalAction, RunSelectedTextInTerminalAction, RunActiveFileInTerminalAction, ScrollDownTerminalAction, ScrollDownPageTerminalAction, ScrollToBottomTerminalAction, ScrollUpTerminalAction, ScrollUpPageTerminalAction, ScrollToTopTerminalAction, TerminalPasteAction, ToggleTerminalAction, ClearTerminalAction, AllowWorkspaceShellTerminalCommand, DisallowWorkspaceShellTerminalCommand, RenameTerminalAction, SelectAllTerminalAction, FocusTerminalFindWidgetAction, HideTerminalFindWidgetAction, DeleteWordLeftTerminalAction, DeleteWordRightTerminalAction, QuickOpenActionTermContributor, QuickOpenTermAction, TERMINAL_PICKER_PREFIX, MoveToLineStartTerminalAction, MoveToLineEndTerminalAction, SplitTerminalAction, SplitInActiveWorkspaceTerminalAction, FocusPreviousPaneTerminalAction, FocusNextPaneTerminalAction, ResizePaneLeftTerminalAction, ResizePaneRightTerminalAction, ResizePaneUpTerminalAction, ResizePaneDownTerminalAction, ScrollToPreviousCommandAction, ScrollToNextCommandAction, SelectToPreviousCommandAction, SelectToNextCommandAction, SelectToPreviousLineAction, SelectToNextLineAction, ToggleEscapeSequenceLoggingAction, SendSequenceTerminalCommand } from 'vs/workbench/parts/terminal/electron-browser/terminalActions';
import { Registry } from 'vs/platform/registry/common/platform';
import { ShowAllCommandsAction } from 'vs/workbench/parts/quickopen/browser/commandsHandler';
import { SyncActionDescriptor } from 'vs/platform/actions/common/actions';
Expand Down Expand Up @@ -534,6 +534,12 @@ actionRegistry.registerWorkbenchAction(new SyncActionDescriptor(SelectToPrevious
actionRegistry.registerWorkbenchAction(new SyncActionDescriptor(SelectToNextLineAction, SelectToNextLineAction.ID, SelectToNextLineAction.LABEL), 'Terminal: Select To Next Line', category);
actionRegistry.registerWorkbenchAction(new SyncActionDescriptor(ToggleEscapeSequenceLoggingAction, ToggleEscapeSequenceLoggingAction.ID, ToggleEscapeSequenceLoggingAction.LABEL), 'Terminal: Toggle Escape Sequence Logging', category);

const sendSequenceTerminalCommand = new SendSequenceTerminalCommand({
id: SendSequenceTerminalCommand.ID,
precondition: null
});
sendSequenceTerminalCommand.register();

setupTerminalCommands();
setupTerminalMenu();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,14 @@ import { IQuickOpenService } from 'vs/platform/quickOpen/common/quickOpen';
import { IQuickInputService, IPickOptions, IQuickPickItem } from 'vs/platform/quickinput/common/quickInput';
import { ActionBarContributor } from 'vs/workbench/browser/actions';
import { TerminalEntry } from 'vs/workbench/parts/terminal/browser/terminalQuickOpen';
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
import { IInstantiationService, ServicesAccessor } from 'vs/platform/instantiation/common/instantiation';
import { IContextViewService } from 'vs/platform/contextview/browser/contextView';
import { ICommandService } from 'vs/platform/commands/common/commands';
import { IWorkspaceContextService } from 'vs/platform/workspace/common/workspace';
import { PICK_WORKSPACE_FOLDER_COMMAND_ID } from 'vs/workbench/browser/actions/workspaceCommands';
import { INotificationService } from 'vs/platform/notification/common/notification';
import { TERMINAL_COMMAND_ID } from 'vs/workbench/parts/terminal/common/terminalCommands';
import { Command } from 'vs/editor/browser/editorExtensions';

export const TERMINAL_PICKER_PREFIX = 'term ';

Expand Down Expand Up @@ -227,6 +228,64 @@ export class MoveToLineEndTerminalAction extends BaseSendTextTerminalAction {
}
}

export class SendSequenceTerminalCommand extends Command {
Tyriar marked this conversation as resolved.
Show resolved Hide resolved
public static readonly ID = TERMINAL_COMMAND_ID.SEND_SEQUENCE;
public static readonly LABEL = nls.localize('workbench.action.terminal.sendSequence', "Send Custom Sequence To Terminal");

public runCommand(accessor: ServicesAccessor, args: any): void {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we type args?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

string?

const terminalInstance = accessor.get(ITerminalService).getActiveInstance();
if (!terminalInstance) {
return;
}
const text = args.text.split(' ');
Tyriar marked this conversation as resolved.
Show resolved Hide resolved
var terminalText;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prefer let to var

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay.

for (let i = 0; i < text.length; i++) {
terminalText = ' ' + prepareTerminaText(text[i]);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why the extra space?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to add a space at the end or at the beginning (to separate text or any command).
If we keep a space at the end, it will have an extra space(near terminal prompt) in the new line after execution is over.

terminalInstance.sendText(terminalText, false);
}
}
}

function prepareTerminaText(text: string): string {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Termina typo, also let's make this a private member of SendSequenceTerminalCommand

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay.

// Hexcodes and excape char
if (text.substring(0, 2).toLowerCase() === '\\x') {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs to happen for every character, I suggest an algorithm like this:

for each char
  detect if it's hex. if so translate and "consume" the characters and move the loop to the following character

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By each character, do you mean each space separated word? If so, this function is being called for each space separated word.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we have different ideas for the end solution, you want to pass in a space separated list of characters or words that translate to characters, I think the most flexible way would be to pass in a list of characters. So instead of this:

"args": {"text": "ctrl+c up enter"}

It would be:

"args": {"text": "\x03\x1b[A\x0d"}

I think we should start with this as it supports everything and then see if we need something better. In fact if we support my proposal an extension could provide the nicer interface if needed.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I could see something like this being nice:

"args": {
  "keys": [
    "ctrl+c",
    "up",
    "enter"
  ]
}

But I think it should live in an extension first to see if people find it useful.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On implementation side, implementing this is easy:
"args": {"text": "\x03\x1b[A\x0d"}
For each char, termnalInstance.sendText(String.fromChar(char))
But will it be easy enough for end user to use?
Final decision is yours. Let me know what way should we go :)
BTW, How should we handle [A if we go that way?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry about the delay, had too many notifications 😱

  1. Yes, I would also expect \a, \b, etc. to work as specified in this file: https://github.com/xtermjs/xterm.js/blob/f47f095b8928a6e50ce80f87d7fd11e50cf9a190/src/common/data/EscapeSequences.ts#L25, not sure if they will without special casing
  2. text should allow spaces, using { "text": "hello world" } should send that, including the space
  3. We probably need to use \\x03 in the keybindings.json

Copy link
Contributor Author

@njkevlani njkevlani Aug 30, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Tyriar
I'm stuck on one last thing, that I need some help with,
If I use \\x03 for ctrl+c, it works but then terminalInstance.sendText ignores next single char.

That is, for args: {'text': '\\x03ls\n'}, the output on terminal is bash: s: command not found
It works as expected with args: {'text': '\\x03 ls\n'}

For upArrow key, if I use "args": {"text": "\\x03\\x1b[A\n"} or "args": {"text": "\\x03 \\x1b[A\n"} I'm getting bash: s: command not found on terminal. (last executed command is ls.)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just tested sending directly to the process in the xterm.js demo (what VS Code's terminal is based on) and sending \x03 definitely doesn't eat the next character for me:

screen shot 2018-08-31 at 9 27 45 am

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

screenshot_2018-08-31_23-22-34

Can you please try term._core.handler("\x03ls\n")

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can repro this in Linux, I'm guessing this is expected behavior of bash. For this I would try workaround it by using something like \r to make sure the ^C goes though which apppears to work\x03\rls\n, it adds another line but that's not a big deal.

image

// Valid hex with 2 digits
if (/^\\x[0-9a-f]{2}$/i.test(text)) {
return String.fromCharCode(parseInt(text.substring(2), 16));
}
}

// ctrl modifier with single char
if (text.indexOf('+') >= 0) {
const text1 = text.split('+');
if (text1[0].toLowerCase() === 'ctrl') {
if (text1[1].length === 1) {
return String.fromCharCode(text1[1].toUpperCase().charCodeAt(0) - 64);
}
}
}
// handle enter
if (text.toLowerCase() === 'enter') {
return '\n';
}

// Arrow keys
if (text.toLowerCase() === 'down') {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure these key strings should be handled specially, what if the user wants to send the text "down"?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should I remove all except up ?

return String.fromCharCode(15);
}
if (text.toLowerCase() === 'up') {
return String.fromCharCode(16);
}
if (text.toLowerCase() === 'right') {
return String.fromCharCode(17);
}
if (text.toLowerCase() === 'left') {
return String.fromCharCode(18);
}

return text;
}

export class CreateNewTerminalAction extends Action {

public static readonly ID = TERMINAL_COMMAND_ID.NEW;
Expand Down