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

Added new command for sending custom sequence to terminal #56962

merged 7 commits into from
Sep 8, 2018

Conversation

njkevlani
Copy link
Contributor

Fixes #56024
We can set keybindings for sending custom key sequences to terminal. Sequence is sent to current active terminal. Terminal is not created if it does not exist.

Keybinding can be create as:

"key": "ctrl+shift+alt+p",
"command": "workbench.action.terminal.sendSequence",
"args": {"text": TEXT}

Some examples of args are:
"args": {"text": "ctrl+c ls ctrl+j"}
"args": {"text": "ctrl+c python -m http.server \n"}
"args": {"text": "ctrl+c up enter"}
"args": {"text": "ctrl+c up \\x0a"}
"args": {"text": "ctrl+c up \\X0A"}

Currently supported sequence type:

  1. Hexcode
  2. Key strokes with ctrl modifier
  3. enter
  4. Arrow keys
  5. Escape chars like \n

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 text = args.text.split(' ');
var terminalText;
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.

return;
}
const text = args.text.split(' ');
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.

}
}

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.


function prepareTerminaText(text: string): string {
// 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

}

// 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 ?

@njkevlani
Copy link
Contributor Author

njkevlani commented Sep 1, 2018

@Tyriar
One thing that I noticed is that, if we pass "args": {"text": "\u0003\u001b[A\n"}, it works without any preprocessing. That is in this case we can directly call terminalInstance.sendText(args.text, false)

This is because this code takes care of unicode chars and convert them to appropriate string.
If we add same type of rules for hex chars, we can make code much cleaner :)

@Tyriar
Copy link
Member

Tyriar commented Sep 4, 2018

@njkevlani oh you mean it works with \u formatting currently? That should be good enough.

@njkevlani
Copy link
Contributor Author

Currently, this implementation works with args similar to following examples.

"args": {"text": "\u0003\r\u001b[A\n"}
"args": {"text": "\u0003\rpython -m http.server\n"}

@Tyriar
Copy link
Member

Tyriar commented Sep 8, 2018

FYI you can workaround the extra \r if you use mac with the new platform-specific keybindings:

[
	{
		"key": "ctrl+q",
		"command": "workbench.action.terminal.sendSequence",
		"args": { "text": "\u0003\u001b[A\n" },
		"when": "isMac"
	},
	{
		"key": "ctrl+q",
		"command": "workbench.action.terminal.sendSequence",
		"args": { "text": "\u0003\r\u001b[A\n" },
		"when": "isLinux"
	}
]

@Tyriar Tyriar added this to the September 2018 milestone Sep 8, 2018
@Tyriar
Copy link
Member

Tyriar commented Sep 8, 2018

@njkevlani thanks for the contribution, this should land in Monday's Insiders build.

@Tyriar Tyriar merged commit 4bfc0a6 into microsoft:master Sep 8, 2018
@Tyriar
Copy link
Member

Tyriar commented Sep 8, 2018

@njkevlani check out microsoft/vscode-docs@b532d2a for the documentation I added on this.

@njkevlani
Copy link
Contributor Author

Thanks @Tyriar for accepting the PR :)

@njkevlani njkevlani deleted the feature-56024 branch September 9, 2018 06:22
@ArturoDent
Copy link

ArturoDent commented Mar 25, 2019

I am a little confused about usage of sendSequence. I set up a simple cursor left snippet:

{
  "key": "alt+x",
  "command": "workbench.action.terminal.sendSequence",  // working code
  "args": { "text": "a987654321\u001b5\u001b[D" }    // moves cursor back 5 spaces non-destructively
}

Note the 5 had to be escaped and placed in front of the [D.

The above works but is not what I expected, I thought the below would work:

"args": { "text": "a987654321u001b[5D" } // doesn't work HAS TYPO

"args": { "text": "a987654321\u001b[5D" } // doesn't work TYPO FIXED

But this later form is what I have seen used consistently in my research.

Is the working code how it must be done in vscode + powershell + W10(fast ring)?

@Tyriar
Copy link
Member

Tyriar commented Mar 26, 2019

"args": { "text": "a987654321u001b[5D" } // doesn't work

That won't work because you don't have a \ in front of the u, so you're just printing a bunch of characters

@Tyriar
Copy link
Member

Tyriar commented Mar 26, 2019

Also on Windows things could be different, you're sending the text directly to the shell, so the shell needs to support whatever you're sending. Refer to their documentation for that.

@ArturoDent
Copy link

ArturoDent commented Mar 28, 2019

Of course "args": { "text": "a987654321u001b[5D" } // doesn't work was just a typo. Fixed above. Does not work. Literally prints: a987654321[5D

The documentation https://code.visualstudio.com/docs/editor/integrated-terminal#_send-text-from-a-keybinding just gives two citations to exterm info that would lead one to believe "args": { "text": "a987654321\u001b[5D" } is the proper way to do it

And since the vscode documentation gives this example:

"args": { "text": "\u001b[1;5D\u007f" }

which works as is for me (in powershell on W10) I am still at a loss to explain why "args": { "text": "a987654321\u001b[5D" } doesn't?

@Tyriar
Copy link
Member

Tyriar commented Mar 28, 2019

The fact that it's powershell is the problem, you're sending sequences that bash expects to powershell and it doesn't know how to handle it. You would need to consult the powershell documentation on whether it's even possible to send a sequence to do this.

@github-actions github-actions bot locked and limited conversation to collaborators Mar 27, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support keybindings for sending custom sequences to the terminal
4 participants