microsoft / terminal

The new Windows Terminal and the original Windows console host, all in the same place!
MIT License
95.17k stars 8.26k forks source link

Extension: Pipe output to command palette in Terminal #8797

Open zadjii-msft opened 3 years ago

zadjii-msft commented 3 years ago

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:

DHowett commented 3 years ago

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

zadjii-msft commented 3 years ago

~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 3 years ago

hackathon week is my favorite week

select-list-000

zadjii-msft commented 3 years ago

Remaining TODOS:


\

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.

cpriest commented 3 years ago

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

zadjii-msft commented 3 years ago

@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 1 year ago

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...