From 2d7b331bc5c5a4c9fe2b1e4c29bd6c8d1b1f05a2 Mon Sep 17 00:00:00 2001 From: Joon Ro Date: Mon, 6 Jun 2022 22:13:47 +0900 Subject: [PATCH 1/5] Change function name --- src/Get-ChildItemColor.psm1 | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Get-ChildItemColor.psm1 b/src/Get-ChildItemColor.psm1 index c8237d2..9b525cb 100644 --- a/src/Get-ChildItemColor.psm1 +++ b/src/Get-ChildItemColor.psm1 @@ -142,7 +142,7 @@ Add-Type -assemblyname System.ServiceProcess $Script:ShowHeader=$True -Function Out-Default { +Function Out-DefaultColor { [CmdletBinding(HelpUri='http://go.microsoft.com/fwlink/?LinkID=113362', RemotingCapability='None')] param( [switch] ${Transcript}, @@ -213,4 +213,4 @@ Function Out-Default { #> } -Export-ModuleMember -Function Out-Default, 'Get-*' +Export-ModuleMember -Function Out-DefaultColor, 'Get-*' From d651f922462d98934c1beff565c931bbbb80d39e Mon Sep 17 00:00:00 2001 From: Joon Ro Date: Tue, 7 Jun 2022 16:47:58 +0900 Subject: [PATCH 2/5] Make pipeline-aware Get-ChildItemColor --- README.org | 27 ++++++++---------------- src/Get-ChildItemColor.psd1 | 2 +- src/Get-ChildItemColor.psm1 | 41 ++++++++++++++++++++----------------- 3 files changed, 31 insertions(+), 39 deletions(-) diff --git a/README.org b/README.org index 71e65f2..44ba281 100644 --- a/README.org +++ b/README.org @@ -8,22 +8,13 @@ by [[http://thepowershellguy.com/][the PowerShell Guy]], and [[https://github.co It provides two main functionalities: -1. Directly provide coloring of ~Get-ChildItem~ output by modifying - ~Out-Default~. Once the module is imported, ~Get-ChildItem~'s output will - be automatically colored. It does support pipeline (e.g., ~Get-ChildItem | - grep ".git"~). Also, now the directory name and the headers of its output - have consistent colors. This is an adaptation of [[https://github.com/Davlind/PSColor][PSColor]]. +1. Provide ~Get-ChildItemColor~, which adds coloring to the output of + ~Get-ChildItem~, this function supports pipelines (it is pipeline-aware, so + it does not touch the output when it is being used in a pipeline). 2. It provides =Get-ChildItemColorFormatWide=, which uses =Write-Host= to output coloring. This is because =Get-ChildItemColor | Format-Wide= does - not allow multiple colors in one line. As a result, pipeline does not work with - =Get-ChildItemColorFormatWide=. - -It also provides ~Get-ChildItemColor~, which just changes -=$Host.UI.RawUI.ForegroundColor= and keep the item object intact. This was the -implementation before v2.0.0, and it does support pipeline. (e.g., -~Get-ChildItemColor | grep ".git"~). The main shortcoming of this approach is -that the directory name and the headers of its output have inconsistent -colors. + not allow multiple colors in one line. As a result, pipeline does not work + with =Get-ChildItemColorFormatWide=. * Screenshot: ** Get-ChildItem (Colorized) @@ -54,10 +45,8 @@ When you import the module: Import-Module Get-ChildItemColor #+end_src -it provides a proxy function for =Output-Default=, so =Get-ChildItem='s output -will be automatically colored. In addition, it provides two functions, -=Get-ChildItemColorFormatWide= and =Get-ChildItemColor= (the latter is -unlikely to be useful, but remained intact just in case). +it provides two functions, =Get-ChildItemColorFormatWide= and +=Get-ChildItemColor=. You can add aliases to these functions for convenience. For example, I have the following in my profile[fn:pathProfile] (please do not put this into ISE @@ -67,7 +56,7 @@ profile[fn:pathProfileISE] as it does not work in ISE): If (-Not (Test-Path Variable:PSise)) { # Only run this in the console and not in the ISE Import-Module Get-ChildItemColor - Set-Alias l Get-ChildItem -option AllScope + Set-Alias l Get-ChildItemColor -option AllScope Set-Alias ls Get-ChildItemColorFormatWide -option AllScope [-HideHeader] } #+end_src diff --git a/src/Get-ChildItemColor.psd1 b/src/Get-ChildItemColor.psd1 index 9e09cba..51b8e81 100644 --- a/src/Get-ChildItemColor.psd1 +++ b/src/Get-ChildItemColor.psd1 @@ -70,7 +70,7 @@ Description = 'Get-ChildItemColor provides colored versions of Get-ChildItem Cmd FunctionsToExport = @( 'Get-ChildItemColor', 'Get-ChildItemColorFormatWide' - 'Out-Default' + 'Out-ChildItemColor' ) # Cmdlets to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no cmdlets to export. diff --git a/src/Get-ChildItemColor.psm1 b/src/Get-ChildItemColor.psm1 index 9b525cb..a040c01 100644 --- a/src/Get-ChildItemColor.psm1 +++ b/src/Get-ChildItemColor.psm1 @@ -22,23 +22,6 @@ Function Get-FileColor($Item) { Return $Color } -Function Get-ChildItemColor { - Param( - [string]$Path = "" - ) - $Expression = "Get-ChildItem -Path `"$Path`" $Args" - - $Items = Invoke-Expression $Expression - - ForEach ($Item in $Items) { - $Color = Get-FileColor $Item - - $Host.UI.RawUI.ForegroundColor = $Color - $Item - $Host.UI.RawUI.ForegroundColor = $OriginalForegroundColor - } -} - Function Get-ChildItemColorFormatWide { Param( [string]$Path = "", @@ -142,7 +125,7 @@ Add-Type -assemblyname System.ServiceProcess $Script:ShowHeader=$True -Function Out-DefaultColor { +Function Out-ChildItemColor { [CmdletBinding(HelpUri='http://go.microsoft.com/fwlink/?LinkID=113362', RemotingCapability='None')] param( [switch] ${Transcript}, @@ -213,4 +196,24 @@ Function Out-DefaultColor { #> } -Export-ModuleMember -Function Out-DefaultColor, 'Get-*' +Function Get-ChildItemColor { + [CmdletBinding()] + Param( + [string]$Path = "" + ) + + Begin { + $Expression = "Get-ChildItem -Path `"$Path`" $Args" + $Items = Invoke-Expression $Expression + } + + Process { + If ($PSCmdlet.MyInvocation.Line -Match '\|') { # pipeline is used + $Items + } Else { + $Items | Out-ChildItemColor + } + } +} + +Export-ModuleMember -Function Out-ChildItemColor, 'Get-*' From f02bd345a6af715b82b7477c90ac4c1594c4ea67 Mon Sep 17 00:00:00 2001 From: Joon Ro Date: Tue, 7 Jun 2022 16:53:51 +0900 Subject: [PATCH 3/5] Add TrailingSlashDirectory switch --- README.org | 11 +++++++---- src/Get-ChildItemColor.psm1 | 8 +++++++- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/README.org b/README.org index 44ba281..bacd51b 100644 --- a/README.org +++ b/README.org @@ -57,14 +57,17 @@ If (-Not (Test-Path Variable:PSise)) { # Only run this in the console and not i Import-Module Get-ChildItemColor Set-Alias l Get-ChildItemColor -option AllScope - Set-Alias ls Get-ChildItemColorFormatWide -option AllScope [-HideHeader] + Set-Alias ls Get-ChildItemColorFormatWide -option AllScope [-HideHeader] [-TrailingSlashDirectory] } #+end_src So =l= yields colored output of =Get-ChildItem= and =ls= yields colored output -of =Get-ChildItem | Format-Wide= equivalent. There is an optional -~-HideHeader~ switch which will supress printing of headers (path on top) for -~Get-ChildItemColorFormatWide~ when specified. +of =Get-ChildItem | Format-Wide= equivalent. + +~Get-ChildItemColorFormatWide~ has the following optional switches: + +- -HideHeader :: supress printing of headers (path on top). +- -TrailingSlashDirectory :: add a trailing slash to directory names. [fn:pathProfile] ~$Home\[My ]Documents\PowerShell\Profile.ps1~ or ~$Home\[My ]Documents\WindowsPowerShell\Profile.ps1~ diff --git a/src/Get-ChildItemColor.psm1 b/src/Get-ChildItemColor.psm1 index a040c01..21b238f 100644 --- a/src/Get-ChildItemColor.psm1 +++ b/src/Get-ChildItemColor.psm1 @@ -26,7 +26,8 @@ Function Get-ChildItemColorFormatWide { Param( [string]$Path = "", [switch]$Force, - [switch]$HideHeader + [switch]$HideHeader, + [switch]$TrailingSlashDirectory ) $nnl = $True @@ -89,6 +90,11 @@ Function Get-ChildItemColorFormatWide { # truncate the item name $toWrite = $Item.Name + + If ($TrailingSlashDirectory -and $Item.GetType().Name -eq 'DirectoryInfo') { + $toWrite += '\' + } + $itemLength = LengthInBufferCells($toWrite) If ($itemLength -gt $pad) { $toWrite = (CutString $toWrite $pad) From e282fadfcfc92cbf79f5010f6a366005c82f4a9e Mon Sep 17 00:00:00 2001 From: Joon Ro Date: Tue, 7 Jun 2022 17:00:07 +0900 Subject: [PATCH 4/5] Add instructions and changelog associated with v3.0.0 --- README.org | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/README.org b/README.org index bacd51b..9703673 100644 --- a/README.org +++ b/README.org @@ -16,6 +16,9 @@ It provides two main functionalities: not allow multiple colors in one line. As a result, pipeline does not work with =Get-ChildItemColorFormatWide=. +Note that as of v3.0.0, it no longer overloads ~Out-Default~, and thus ~Get-ChildItem~'s +output will not be touched. Users should use ~Get-ChildItemColor~ instead. + * Screenshot: ** Get-ChildItem (Colorized) [[file:./screenshots/Get-ChildItem.png]] @@ -110,6 +113,10 @@ $Global:GetChildItemColorVerticalSpace = 1 * Authors - [[http://github.com/joonro][Joon Ro]]. * Changelog +** v3.0.0 +- ~Get-ChildItemColor~ is pipeline-aware and only adds color when it is not + being used as a part of a pipeline. It no longer overloads ~Out-Default~. ([[https://github.com/joonro/Get-ChildItemColor/issues/31][#31]]) +- Add ~TrailingSlashDirectory~ switch to ~Get-ChildItemColorFormatWide~ ([[https://github.com/joonro/Get-ChildItemColor/issues/37][#37]]) ** v2.4.0 - Add ~HideHeader~ switch to ~Get-ChildItemColorFormatWide~ ([[https://github.com/joonro/Get-ChildItemColor/issues/29][#29]]) ** v2.3.0 From 572f44b7035ec1f2557d1aae348b6883689180d4 Mon Sep 17 00:00:00 2001 From: Joon Ro Date: Tue, 7 Jun 2022 17:01:32 +0900 Subject: [PATCH 5/5] Update version number --- src/Get-ChildItemColor.psd1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Get-ChildItemColor.psd1 b/src/Get-ChildItemColor.psd1 index 51b8e81..65c8a5c 100644 --- a/src/Get-ChildItemColor.psd1 +++ b/src/Get-ChildItemColor.psd1 @@ -10,7 +10,7 @@ RootModule = 'Get-ChildItemColor.psm1' # Version number of this module. -ModuleVersion = '2.4.0' +ModuleVersion = '3.0.0' # Supported PSEditions # CompatiblePSEditions = @()