From 765dd6929c52b9f79ec6a3909c7a0b73aadc43fa Mon Sep 17 00:00:00 2001 From: Michael Kelley Date: Sat, 19 Oct 2024 14:26:37 -0700 Subject: [PATCH] Add bat highlighting for PowerShell help (#281) --- helpers/PsFzfTabExpansion-Parameter.ps1 | 11 ++++++++++- helpers/PsFzfTabExpansion-Preview.ps1 | 9 +++++++-- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/helpers/PsFzfTabExpansion-Parameter.ps1 b/helpers/PsFzfTabExpansion-Parameter.ps1 index 625a59a..4f77e40 100644 --- a/helpers/PsFzfTabExpansion-Parameter.ps1 +++ b/helpers/PsFzfTabExpansion-Parameter.ps1 @@ -3,6 +3,10 @@ $command = $args[0] $parameter = $args[1] $parameter = $parameter.replace('-', '') +$RunningInWindowsTerminal = [bool]($env:WT_Session) +$IsWindowsCheck = ($PSVersionTable.PSVersion.Major -le 5) -or $IsWindows +$ansiCompatible = $script:RunningInWindowsTerminal -or (-not $script:IsWindowsCheck) + if ([System.Management.Automation.Cmdlet]::CommonParameters.Contains($parameter)) { $tempFile = New-TemporaryFile Get-Help about_CommonParameters | out-file $tempFile @@ -13,5 +17,10 @@ if ([System.Management.Automation.Cmdlet]::CommonParameters.Contains($parameter) Remove-Item $tempFile -ErrorAction SilentlyContinue } else { - Get-Help -Name $Command -Parameter $parameter + if ($ansiCompatible -and $(Get-Command bat -ErrorAction Ignore)) { + Get-Help -Name $Command -Parameter $parameter | bat --language=markdown --color always --style=plain + } + else { + Get-Help -Name $Command -Parameter $parameter + } } diff --git a/helpers/PsFzfTabExpansion-Preview.ps1 b/helpers/PsFzfTabExpansion-Preview.ps1 index 74fc3ed..f6fa91a 100644 --- a/helpers/PsFzfTabExpansion-Preview.ps1 +++ b/helpers/PsFzfTabExpansion-Preview.ps1 @@ -17,7 +17,6 @@ else { } # is directory? if (Test-Path $path -PathType Container) { - Get-ChildItem $path if (Get-Command git -ErrorAction Ignore) { @@ -46,7 +45,13 @@ elseif (Test-Path $path -PathType leaf) { elseif (($cmdResults = Get-Command $Item -ErrorAction Ignore)) { if ($cmdResults) { if ($cmdResults.CommandType -ne 'Application') { - Get-Help $Item + if ($ansiCompatible -and $(Get-Command bat -ErrorAction Ignore)) { + Get-Help $Item | bat --language=markdown --color always --style=plain + } + else { + + } + } else { # just output application location: