Skip to content

Instantly share code, notes, and snippets.

@chklauser
Created January 31, 2022 22:13
Show Gist options
  • Save chklauser/9f2d64b434434afe2374113974d6de88 to your computer and use it in GitHub Desktop.
Save chklauser/9f2d64b434434afe2374113974d6de88 to your computer and use it in GitHub Desktop.
Powershell version of my favorite custom bash function: 'lk' (look; shows a thing; decides how to show it based on the path/file extension)
New-Module -Name Lk -ScriptBlock {
[Reflection.Assembly]::LoadWithPartialName('System.IO.Compression.FileSystem')
if (-not (Get-Command -ErrorAction SilentlyContinue rich)) {
Write-Error '`rich` not installed. See https://github.com/Textualize/rich-cli'
}
if (-not (Get-Command -ErrorAction SilentlyContinue less)) {
Write-Error '`less` not installed. `scoop install less` maybe?'
}
function Show-Item {
param(
[Parameter(ValueFromPipeline,Position=0,ValueFromRemainingArguments)]
[SupportsWildcards()]
[ValidateNotNullOrEmpty()]
[string[]]
$Paths = "."
)
$lessOpts='--use-color','--mouse','--quit-if-one-screen','--raw-control-chars','--force'
foreach ($path in $Paths) {
foreach($item in (Get-Item -Path $path)) {
if ($item.PSIsContainer) {
Get-ChildItem -Path $path
} else {
switch -wildcard ($item.Name) {
"*.json" {
$t = New-TemporaryFile
rich --json --force-terminal -- "$($item.FullName)" | set-content -Encoding utf8 -LiteralPath $t.FullName
less @lessOpts -- $t.FullName
$t | Remove-Item -ErrorAction SilentlyContinue
}
"*.md" {
$t = New-TemporaryFile
rich --markdown --force-terminal -- "$($item.FullName)" | set-content -Encoding utf8 -LiteralPath $t.FullName
less @lessOpts -- $t.FullName
$t | Remove-Item -ErrorAction SilentlyContinue
}
"*.cs" {
$t = New-TemporaryFile
rich --lexer csharp --force-terminal -- "$($item.FullName)" | set-content -Encoding utf8 -LiteralPath $t.FullName
less @lessOpts -- $t.FullName
$t | Remove-Item -ErrorAction SilentlyContinue
}
"*.js" {
$t = New-TemporaryFile
rich --lexer javascript --force-terminal -- "$($item.FullName)" | set-content -Encoding utf8 -LiteralPath $t.FullName
less @lessOpts -- $t.FullName
$t | Remove-Item -ErrorAction SilentlyContinue
}
"*.ts" {
$t = New-TemporaryFile
rich --lexer typescript --force-terminal -- "$($item.FullName)" | set-content -Encoding utf8 -LiteralPath $t.FullName
less @lessOpts -- $t.FullName
$t | Remove-Item -ErrorAction SilentlyContinue
}
"*.html" {
$t = New-TemporaryFile
rich --lexer html --force-terminal -- "$($item.FullName)" | set-content -Encoding utf8 -LiteralPath $t.FullName
less @lessOpts -- $t.FullName
$t | Remove-Item -ErrorAction SilentlyContinue
}
"*.xml" {
$t = New-TemporaryFile
rich --lexer xml --force-terminal -- "$($item.FullName)" | set-content -Encoding utf8 -LiteralPath $t.FullName
less @lessOpts -- $t.FullName
$t | Remove-Item -ErrorAction SilentlyContinue
}
"*.sql" {
$t = New-TemporaryFile
rich --lexer sql --force-terminal -- "$($item.FullName)" | set-content -Encoding utf8 -LiteralPath $t.FullName
less @lessOpts -- $t.FullName
$t | Remove-Item -ErrorAction SilentlyContinue
}
"*.zip" {
rich --rule -- "$($item.Name)"
[IO.Compression.ZipFile]::OpenRead($item.FullName).Entries | format-table -Property FullName,Length -RepeatHeader
}
default {
less @lessOpts -- "$($item.FullName)"
}
}
}
}
}
}
} | Import-Module
New-Alias -Name lk -Value Show-Item
lk @args
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment