Skip to content

Commit

Permalink
Merge branch 'release/v3.0.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
joonro committed Jun 7, 2022
2 parents 8f91d52 + 572f44b commit 9d43ab9
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 44 deletions.
43 changes: 21 additions & 22 deletions README.org
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,16 @@ 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=.
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.
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)
Expand Down Expand Up @@ -54,10 +48,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
Expand All @@ -67,15 +59,18 @@ 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 ls Get-ChildItemColorFormatWide -option AllScope [-HideHeader]
Set-Alias l Get-ChildItemColor -option AllScope
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~

Expand Down Expand Up @@ -118,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
Expand Down
4 changes: 2 additions & 2 deletions src/Get-ChildItemColor.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
RootModule = 'Get-ChildItemColor.psm1'

# Version number of this module.
ModuleVersion = '2.4.0'
ModuleVersion = '3.0.0'

# Supported PSEditions
# CompatiblePSEditions = @()
Expand Down Expand Up @@ -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.
Expand Down
49 changes: 29 additions & 20 deletions src/Get-ChildItemColor.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -22,28 +22,12 @@ 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 = "",
[switch]$Force,
[switch]$HideHeader
[switch]$HideHeader,
[switch]$TrailingSlashDirectory
)

$nnl = $True
Expand Down Expand Up @@ -106,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)
Expand Down Expand Up @@ -142,7 +131,7 @@ Add-Type -assemblyname System.ServiceProcess

$Script:ShowHeader=$True

Function Out-Default {
Function Out-ChildItemColor {
[CmdletBinding(HelpUri='http://go.microsoft.com/fwlink/?LinkID=113362', RemotingCapability='None')]
param(
[switch] ${Transcript},
Expand Down Expand Up @@ -213,4 +202,24 @@ Function Out-Default {
#>
}

Export-ModuleMember -Function Out-Default, '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-*'

0 comments on commit 9d43ab9

Please sign in to comment.