Skip to content

Instantly share code, notes, and snippets.

@david-risney
Created May 6, 2022 22:43
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save david-risney/5af2d43f91a36c1b81c38e52c1bf7fda to your computer and use it in GitHub Desktop.
Save david-risney/5af2d43f91a36c1b81c38e52c1bf7fda to your computer and use it in GitHub Desktop.
PowerShell clickable directory listing
function Format-TerminalClickableString {
param(
$Uri,
$DisplayText);
$clickableFormatString = "`e]8;;{0}`e\{1}`e]8;;`e\"
$formattedString = ($clickableFormatString -F ($Uri,$DisplayText));
$formattedString;
}
function Format-TerminalClickableFileInfo {
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseShouldProcessForStateChangingFunctions', '')]
[OutputType([string])]
[CmdletBinding()]
param(
[Parameter(Mandatory, ValueFromPipeline)]
[IO.FileSystemInfo]$FileInfo
)
process {
if (Test-Path function:Format-TerminalIcons) {
$displayText = Format-TerminalIcons $FileInfo;
} else {
$displayText = $FileInfo.Name;
}
Format-TerminalClickableString $FileInfo.FullName $displayText;
}
}
Update-FormatData -PrependPath ~\TerminalClickable.format.ps1xml;
<?xml version="1.0" encoding="utf-8" ?>
<!-- Based on the format.ps1xml file from Terminal-Icons
https://github.com/devblackops/Terminal-Icons/blob/main/Terminal-Icons/Terminal-Icons.format.ps1xml -->
<Configuration>
<SelectionSets>
<SelectionSet>
<Name>FileSystemTypes</Name>
<Types>
<TypeName>System.IO.DirectoryInfo</TypeName>
<TypeName>System.IO.FileInfo</TypeName>
</Types>
</SelectionSet>
</SelectionSets>
<Controls>
<Control>
<Name>FileSystemTypes-GroupingFormat</Name>
<CustomControl>
<CustomEntries>
<CustomEntry>
<CustomItem>
<Frame>
<LeftIndent>4</LeftIndent>
<CustomItem>
<Text AssemblyName="System.Management.Automation" BaseName="FileSystemProviderStrings" ResourceId="DirectoryDisplayGrouping"/>
<ExpressionBinding>
<ScriptBlock>
$_.PSParentPath.Replace("Microsoft.PowerShell.Core\FileSystem::", "")
</ScriptBlock>
</ExpressionBinding>
<NewLine/>
</CustomItem>
</Frame>
</CustomItem>
</CustomEntry>
</CustomEntries>
</CustomControl>
</Control>
</Controls>
<ViewDefinitions>
<View>
<Name>children</Name>
<ViewSelectedBy>
<SelectionSetName>FileSystemTypes</SelectionSetName>
</ViewSelectedBy>
<GroupBy>
<PropertyName>PSParentPath</PropertyName>
<CustomControlName>FileSystemTypes-GroupingFormat</CustomControlName>
</GroupBy>
<TableControl>
<TableHeaders>
<TableColumnHeader>
<Label>Mode</Label>
<Width>7</Width>
<Alignment>left</Alignment>
</TableColumnHeader>
<TableColumnHeader>
<Label>LastWriteTime</Label>
<Width>25</Width>
<Alignment>right</Alignment>
</TableColumnHeader>
<TableColumnHeader>
<Label>Length</Label>
<Width>14</Width>
<Alignment>right</Alignment>
</TableColumnHeader>
<TableColumnHeader>
<Label>Name</Label>
</TableColumnHeader>
</TableHeaders>
<TableRowEntries>
<TableRowEntry>
<Wrap/>
<TableColumnItems>
<TableColumnItem>
<PropertyName>Mode</PropertyName>
</TableColumnItem>
<TableColumnItem>
<ScriptBlock>
[String]::Format('{0,10} {1,8}', $_.LastWriteTime.ToString('d'), $_.LastWriteTime.ToString('t'))
</ScriptBlock>
</TableColumnItem>
<TableColumnItem>
<PropertyName>Length</PropertyName>
</TableColumnItem>
<TableColumnItem>
<ScriptBlock>
Format-TerminalClickableFileInfo $_
</ScriptBlock>
</TableColumnItem>
</TableColumnItems>
</TableRowEntry>
</TableRowEntries>
</TableControl>
</View>
<View>
<Name>children</Name>
<ViewSelectedBy>
<SelectionSetName>FileSystemTypes</SelectionSetName>
</ViewSelectedBy>
<GroupBy>
<PropertyName>PSParentPath</PropertyName>
<CustomControlName>FileSystemTypes-GroupingFormat</CustomControlName>
</GroupBy>
<ListControl>
<ListEntries>
<ListEntry>
<EntrySelectedBy>
<TypeName>System.IO.FileInfo</TypeName>
</EntrySelectedBy>
<ListItems>
<ListItem>
<Label>Name</Label>
<ScriptBlock>
Format-TerminalClickableFileInfo $_
</ScriptBlock>
</ListItem>
<ListItem>
<PropertyName>Length</PropertyName>
</ListItem>
<ListItem>
<PropertyName>CreationTime</PropertyName>
</ListItem>
<ListItem>
<PropertyName>LastWriteTime</PropertyName>
</ListItem>
<ListItem>
<PropertyName>LastAccessTime</PropertyName>
</ListItem>
<ListItem>
<PropertyName>Mode</PropertyName>
</ListItem>
<ListItem>
<PropertyName>LinkType</PropertyName>
</ListItem>
<ListItem>
<Label>Target</Label>
<ScriptBlock>
Format-TerminalClickableFileInfo $_
</ScriptBlock>
</ListItem>
<!-- <ListItem>
<PropertyName>VersionInfo</PropertyName>
</ListItem> -->
</ListItems>
</ListEntry>
<ListEntry>
<ListItems>
<ListItem>
<Label>Name</Label>
<ScriptBlock>
Format-TerminalClickableFileInfo $_
</ScriptBlock>
</ListItem>
<ListItem>
<PropertyName>CreationTime</PropertyName>
</ListItem>
<ListItem>
<PropertyName>LastWriteTime</PropertyName>
</ListItem>
<ListItem>
<PropertyName>LastAccessTime</PropertyName>
</ListItem>
<ListItem>
<PropertyName>Mode</PropertyName>
</ListItem>
<ListItem>
<PropertyName>LinkType</PropertyName>
</ListItem>
<ListItem>
<Label>Target</Label>
<ScriptBlock>
Format-TerminalClickableFileInfo $_
</ScriptBlock>
</ListItem>
</ListItems>
</ListEntry>
</ListEntries>
</ListControl>
</View>
<View>
<Name>children</Name>
<ViewSelectedBy>
<SelectionSetName>FileSystemTypes</SelectionSetName>
</ViewSelectedBy>
<GroupBy>
<PropertyName>PSParentPath</PropertyName>
<CustomControlName>FileSystemTypes-GroupingFormat</CustomControlName>
</GroupBy>
<WideControl>
<WideEntries>
<WideEntry>
<WideItem>
<ScriptBlock>
Format-TerminalClickableFileInfo $_
</ScriptBlock>
</WideItem>
</WideEntry>
<WideEntry>
<EntrySelectedBy>
<TypeName>System.IO.DirectoryInfo</TypeName>
</EntrySelectedBy>
<WideItem>
<ScriptBlock>
Format-TerminalClickableFileInfo $_
</ScriptBlock>
</WideItem>
</WideEntry>
</WideEntries>
</WideControl>
</View>
</ViewDefinitions>
</Configuration>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment