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

Commands like get-childitem could emit hyperlinked clickable directory entries using OSC sequences when hosted by windows terminal #18098

Open
ironyman opened this issue Sep 15, 2022 · 10 comments
Labels
Issue-Enhancement the issue is more of a feature request than a bug Needs-Triage The issue is new and needs to be triaged by a work group. WG-Engine-Format

Comments

@ironyman
Copy link

Summary of the new feature / enhancement

Hi all,

There was some discussion about this in the terminal project.

microsoft/terminal#5001 (comment)

Using this feature supported by windows terminal

https://gist.github.com/egmontkob/eb114294efbcd5adb1944c9f3cb5feda

To summarize, when a user runs dir, it would be super awesome to able to shellexecute the listings from dir by clicking on them, which we could do using the OSC sequence from the that gist.

Thoughts?

Proposed technical implementation details (optional)

No response

@ironyman ironyman added Issue-Enhancement the issue is more of a feature request than a bug Needs-Triage The issue is new and needs to be triaged by a work group. labels Sep 15, 2022
@237dmitry
Copy link

Something like this?

Get-Childitem C:\ -Directory | Foreach-Object {

    Write-Host "  `e[34m`e]8;;file://$($_.FullName)`e\$($_.Name)`e]8;;`e[0m"
}

hl

@dkaszews
Copy link
Contributor

Does it allow executing arbitrary commands? That is, what will happen if you click on the link to a file or folder? Will it be always be run by default application, same as with Start-Process or is it possible to e.g. execute Get-ChildItem when clicking on the directory?

@237dmitry
Copy link

237dmitry commented Sep 15, 2022

what will happen if you click on the link to a file or folder?

I haven't tested much. The behavior is as if you click a file or a folder with the mouse in Explorer.
PS. Latest Windows Terminal Preview. Оn linux (Tilix terminal), the links look ugly and nothing happens. I use i3wm, maybe that's the problem.

@dkaszews
Copy link
Contributor

Too bad, that pretty much kills all utility to me, as it also won't do anything useful over SSH.

@237dmitry
Copy link

237dmitry commented Sep 15, 2022

On Linux works too, it opens files and folders in default applications according mime-types except executables or desktop-files:

Get-Childitem /some/dir | Foreach-Object {
 
      Write-Host "  `e[4;34m`e]8;;file://$($_.FullName)`e`\$($_.Name)`e]8;;`e`\`e[0m"
 }

PS. This is correct for Windows too.

@ironyman
Copy link
Author

@237dmitry exactly like that!

@dkaszews it would only execute things with something like start-process, I think the parent process will be the terminal and not powershell though.

I would like for it to be able to execute commands as well but that's beyond the capabilities of this OSC sequence.

@jhoneill
Copy link

jhoneill commented Sep 16, 2022

@ironyman there are at least 2 reasons why this can't be the default.
First I don't think PowerShell has way to learn which ANSI sequences are and aren't supported,
Second making sense of it in a remote session is problematic.

However you can customize this to suit yourself and load the customization from your profile. To do this

  1. copy C:\Windows\System32\WindowsPowerShell\v1.0\FileSystem.format.ps1xml to convenient location : I used file.psm1xml in my profile directory.
  2. Open the copy in your favourite editor.
  3. Do a one-by-one search and replace of
<PropertyName>Name</PropertyName>

with

 <ScriptBlock>"$([char]27)[96m$([char]27)]8;;file://$($_.FullName)$([char]27)`\$($_.Name)$([char]27)]8;;$([char]27)`\$([char]27)[0m"</ScriptBlock>

I'm making the links cyan (96) for good contrast and using $([char]27) instead of `e because that will work on PowerShell 5 as well as 7
As you do the replacements if they are in a <ListItem> add <Label>Name</Label> inside the same item at the same level as the script block. If they are in a table look for the corresponding TableColumnHeader it may be <TableColumnHeader/> if so replace it with <TableColumnHeader><Label>Name</Label></TableColumnHeader>

  1. Save the file and load it with Update-FormatData -PrependPath .\file.ps1xml (use your chosen file name here) for me it looks like this
    image
    (proving it works with 5 as well !)

  2. Make any adjustments and run Update-FormatData no need for -PrependPath.

  3. You can (conditionally) run Update-FormatData -PrependPath ... from your profile.

I'm happy to post my version as a gist.

@RReverser
Copy link

@jhoneill That's pretty amazing, thanks. Maybe it would be possible to somehow ship this as a PowerShell module so that it could be installed via PSGet?

@jhoneill
Copy link

@RReverser Thanks over the years I have posted multiple replacement formatting files - not just for files. It certainly is possible to have a module which only loads formatting, but everyone seems to want something different. Some key people on the team like to splash colour around, I find that makes things harder to read - and helpfully I can manage that via PsStyle. Some people want links, some want different date format or thousand separators in numbers, or narrow columns or 101 other things, so there isn't one solution to please everyone - hence I keep helping people to customize it. Note that the Export-FormatData doesn't work too well for file so starting from an existing format file is recommended in this case

@RReverser
Copy link

For me being able to install it via PSGet or another package manager is mostly about reproducible installs - if I reinstall Windows on a new machine at some point, I'd love to be able to easily get familiar environment via winget+PSGet+custom config files.

The scenarios like described above are easier to lose / forget how I got that formatting in the first place, hence wondering if it can be automated.

It looks like a fairly straightforward copy + find-replace (or XML manipulation) + import, so it seems it could be put into a module in a way that would still allow it to compose well with most other formattings you mentioned? (except those that add colors I guess)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Issue-Enhancement the issue is more of a feature request than a bug Needs-Triage The issue is new and needs to be triaged by a work group. WG-Engine-Format
Projects
None yet
Development

No branches or pull requests

6 participants