Extension: Pipe output to command palette in Terminal #12074

Open
opened 2026-01-31 03:05:45 +00:00 by claunia · 7 comments
Owner

Originally created by @zadjii-msft on GitHub (Jan 15, 2021).

Okay, this is gonna sound crazy so bear with me. I was switching git branches, and thought, "I really do not know how to use less well, and git branch | $ grep foo each time is annoying".

Then I thought, we've got this great command palette for filtering a list of things.

As an extension, what if we could pipe a list of items to the terminal, and then have the terminal open up the command palette with those items populated in it. Then selecting one of those items would insert that text?

So something like:

git branch | wt -w 0 select-list --prefix "git checkout "

would pipe to the current window, into the command palette. --prefix "git checkout " would indicate "use this text as a prefix for whatever the selected item was".

So hitting enter on one of these items, like "dev/migrie/foo" would SendInput git checkout dev/migrie/foo to the terminal.

This encapsulates a bunch of things:

  • extensions need to be able to add their own subcommands to the commandline parser
    • they also need to be able to parse their own args
  • extensions need to be able to open a command palette FilterableListView with whatever they want (a set of actions in this case. The actions would all be {sendInput(prefix+item), name=item})
Originally created by @zadjii-msft on GitHub (Jan 15, 2021). Okay, this is gonna sound crazy so bear with me. I was switching git branches, and thought, "I really do not know how to use `less` well, and `git branch | $ grep foo` each time is annoying". Then I thought, we've got this great command palette for filtering a list of _things_. _As an extension_, what if we could pipe a list of items to the terminal, and then have the terminal open up the command palette with those items populated in it. Then selecting one of those items would _insert that text_? So something like: ``` git branch | wt -w 0 select-list --prefix "git checkout " ``` would pipe to the current window, into the command palette. `--prefix "git checkout "` would indicate "use this text as a prefix for whatever the selected item was". So hitting enter on one of these items, like "dev/migrie/foo" would `SendInput` `git checkout dev/migrie/foo` to the terminal. This encapsulates a bunch of things: * extensions need to be able to add their own subcommands to the commandline parser * they also need to be able to parse their own args * extensions need to be able to open a ~command palette~ FilterableListView with whatever they want (a set of actions in this case. The actions would all be {sendInput(prefix+item), name=item})
Author
Owner

@DHowett commented on GitHub (Jan 19, 2021):

No, and in an issue that’s not related to Command Prompt is not an appropriate place to ask 😄

@DHowett commented on GitHub (Jan 19, 2021): No, and in an issue that’s not related to Command Prompt is not an appropriate place to ask :smile:
Author
Owner

@zadjii-msft commented on GitHub (Jul 29, 2021):

~Hokay so it's not this simple. wt is a WINDOWS subsystem application, so even when it is piped to, we can't GetStdHandle(STD_IN)/ReadFile the input piped to it.

We'd need the proposed wtc as a helper here. wtc select-list could check if there's input piped to it, and then use the Remoting lib to pass that to the monarch to pass to the active window...~

WAIT what

windowsterminal.exe, when running unpackaged, accepts input from the commandline perfectly fine. But wt doesn't? Is this because there's the wt.exe shim in the middle that's not passing the input through to windowsterminal.exe? Oh it might be...

EDIT:

It definitely was that. If you change the wt.exe shim to bInheritHandles=true, then this works just fine.

@zadjii-msft commented on GitHub (Jul 29, 2021): ~Hokay so it's not this simple. `wt` is a WINDOWS subsystem application, so even when it is piped to, we can't `GetStdHandle(STD_IN)`/`ReadFile` the input piped to it. We'd need the proposed `wtc` as a helper here. `wtc select-list` could check if there's input piped to it, and then use the Remoting lib to pass that to the monarch to pass to the active window...~ WAIT what `windowsterminal.exe`, when running unpackaged, accepts input from the commandline perfectly fine. But `wt` doesn't? Is this because there's the `wt.exe` shim in the middle that's not passing the input through to `windowsterminal.exe`? Oh it might be... EDIT: It definitely was that. If you change the wt.exe shim to `bInheritHandles=true`, then this works just fine.
Author
Owner

@zadjii-msft commented on GitHub (Jul 29, 2021):

hackathon week is my favorite week

select-list-000

@zadjii-msft commented on GitHub (Jul 29, 2021): hackathon week is my favorite week ![select-list-000](https://user-images.githubusercontent.com/18356694/127558472-7e0e5ae9-a20e-4a6a-8a6c-4199ba68d722.gif)
Author
Owner

@zadjii-msft commented on GitHub (Jul 29, 2021):

Remaining TODOS:

  • generic todos throughout code - resources, etc.
  • after selecting/dismissing this menu, return to the normal list of commands in the palette. Right now it leaves them behind!
  • Maybe don't overload the Command ctor like I did?
  • add a -t,--trim parameter, to remove whitespace on either side of the command
  • add short form, sl (?)
  • If select-list is the only action, and -w wasn't provided, then default to -w 0, and DONT ADD new-tab. So dir | wt sl will by default run in the current terminal
  • add a --suffix arg as well
  • Ignore empty lines entirely.
  • trim \rs off too. Don't want to be executing these things immediately!
    • maybe add a commandline arg to append the enter so the user can just execute immediately?
  • --icon to set an icon for the commands? That seems wack though. If only we could font-fallback to nerd-fonts for their git symbol.
  • Make sure that this can't actually get parsed by json. Or should it?
  • Should wt sl --option "foo" --option "bar" etc work as expected? Like, should we actually allow that param on the commandline, and only use Stdin when it's actually there? (When would this be useful...?)
    • wt sl -o cmd -o wsl -o pwsh --prefix "wt -w 0 nt"? something like that?

<unbelievably wack idea>

sendInput("dir /b /ad | wt sl --prefix cd") bound to a key is fun. Maybe next year I'll think of a way to auto-bind that as a nested command, so you can navigate your directory tree with the command palette lol.

Maybe that would work as a combo with #9994 and #5970

{
  "command": {
    "action": "executeActions",
    "subcommand": "cd",
    "actions": [
	  { "action":"sendInput", "input": "dir /b /ad | wt sl --prefix cd --suffix \"; wt cd\"\n"}
	]
  },
  "name": "change directory..."
},

so that would cause wt cd to input dir /b /ad | wt sl --prefix cd --suffix "; wt cd"\n to the terminal. We'd open up the command palette with the list of directories, and upon selecting one, we'd navigate to that directory, and immediately do another wt cd, bringing up the menu again. Hitting esc would dismiss this palette.

@zadjii-msft commented on GitHub (Jul 29, 2021): Remaining TODOS: * [ ] generic todos throughout code - resources, etc. * [ ] after selecting/dismissing this menu, return to the normal list of commands in the palette. Right now it leaves them behind! * [ ] Maybe _don't_ overload the `Command` ctor like I did? * [ ] add a `-t,--trim` parameter, to remove whitespace on either side of the command * [ ] add short form, `sl` (?) * [ ] If `select-list` is the only action, and `-w` wasn't provided, then _default to `-w 0`_, and DONT ADD `new-tab`. So `dir | wt sl` will by default run in the current terminal * [ ] add a `--suffix` arg as well * [ ] Ignore empty lines entirely. * [ ] trim `\r`s off too. Don't want to be executing these things immediately! * [ ] maybe add a commandline arg to append the <kbd>enter</kbd> so the user can just execute immediately? * [ ] `--icon` to set an icon for the commands? That seems wack though. If only we could font-fallback to nerd-fonts for their git symbol. * [ ] Make sure that this can't actually get parsed by json. Or should it? * [ ] Should `wt sl --option "foo" --option "bar"` etc work as expected? Like, should we actually allow that param on the commandline, and only use Stdin when it's actually there? (When would this be useful...?) * `wt sl -o cmd -o wsl -o pwsh --prefix "wt -w 0 nt"`? something like that? <hr> \<unbelievably wack idea> `sendInput("dir /b /ad | wt sl --prefix cd")` bound to a key is fun. Maybe next year I'll think of a way to auto-bind that as a nested command, so you can navigate your directory tree with the command palette lol. Maybe that would work as a combo with #9994 and #5970 ```json { "command": { "action": "executeActions", "subcommand": "cd", "actions": [ { "action":"sendInput", "input": "dir /b /ad | wt sl --prefix cd --suffix \"; wt cd\"\n"} ] }, "name": "change directory..." }, ``` so that would cause `wt cd` to input `dir /b /ad | wt sl --prefix cd --suffix "; wt cd"\n` to the terminal. We'd open up the command palette with the list of directories, and upon selecting one, we'd navigate to that directory, and _immediately do another `wt cd`_, bringing up the menu again. Hitting esc would dismiss this palette.
Author
Owner

@cpriest commented on GitHub (Jul 30, 2021):

Just a thought on this. fzf already does this very well and works everywhere.

@cpriest commented on GitHub (Jul 30, 2021): Just a thought on this. fzf already does this very well and works everywhere.
Author
Owner

@zadjii-msft commented on GitHub (Aug 2, 2021):

@cpriest Yea, I was mostly just messing around with this to see if this particular combination of things would work directly in the Terminal. It was more of an experiment than anything else. Almost more as a thought experiment more than anything else, but having the actual prototype in front of me was fun to play with and see what else we could do with something similar.

@zadjii-msft commented on GitHub (Aug 2, 2021): @cpriest Yea, I was mostly just messing around with this to see if this particular combination of things would work directly in the Terminal. It was more of an experiment than anything else. Almost more as a thought experiment more than anything else, but having the actual prototype in front of me was fun to play with and see what else we could do with something similar.
Author
Owner

@zadjii-msft commented on GitHub (Aug 3, 2023):

As a possibly related combination of wack ideas:

AS mentioned in #4719

Another really useful plugin would be a Text Editor that could be used for profiles and scripts. The data would be piped to/from the Console for reading and saving

wack idea:

type foo.txt | wt open-scratch

where open-scratch will read from stdin.

Though, it does seem easier to just

wt edit foo.txt

This all might be fully impossible with the new process model v3 as of 1.18. The wt that's run is a different process than the current one, so it'd be hard to plumb the HANDLE to the right place...

@zadjii-msft commented on GitHub (Aug 3, 2023): As a possibly related combination of wack ideas: AS mentioned in #4719 > Another really useful plugin would be a Text Editor that could be used for profiles and scripts. The data would be piped to/from the Console for reading and saving wack idea: ```type foo.txt | wt open-scratch``` where `open-scratch` will read from stdin. Though, it does seem easier to just ``` wt edit foo.txt ``` --- This all might be _fully_ impossible with the new process model v3 as of 1.18. The wt that's run is a different process than the current one, so it'd be hard to plumb the HANDLE to the right place...
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/terminal#12074