Skip to content

Commit 40b121c

Browse files
committed
Merge branch 'release/v3.2.1'
2 parents bb886d7 + 49f9299 commit 40b121c

File tree

8 files changed

+107
-96
lines changed

8 files changed

+107
-96
lines changed

README.org

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,14 @@ by [[http://thepowershellguy.com/][the PowerShell Guy]], and [[https://github.co
88

99
It provides two main functionalities:
1010

11-
1. Provide ~Get-ChildItemColor~, which adds coloring to the output of
11+
1. ~Get-ChildItemColor~, which adds coloring to the output of
1212
~Get-ChildItem~, this function supports pipelines (it is pipeline-aware, so
1313
it does not touch the output when it is being used in a pipeline).
14-
2. It provides =Get-ChildItemColorFormatWide=, which uses =Write-Host= to
15-
output coloring. This is because =Get-ChildItemColor | Format-Wide= does
16-
not allow multiple colors in one line. As a result, pipeline does not work
17-
with =Get-ChildItemColorFormatWide=.
14+
2. =Get-ChildItemColorFormatWide=, which is colored version of
15+
=Get-ChildItemColor | Format-Wide=. This uses =Write-Host= to output
16+
coloring. This is because =Get-ChildItemColor | Format-Wide= does not allow
17+
multiple colors in one line. As a result, pipeline does not work with
18+
=Get-ChildItemColorFormatWide=.
1819

1920
Note that as of v3.0.0, it no longer overloads ~Out-Default~, and thus ~Get-ChildItem~'s
2021
output will not be touched. Users should use ~Get-ChildItemColor~ instead.
@@ -29,9 +30,7 @@ output will not be touched. Users should use ~Get-ChildItemColor~ instead.
2930
PowerShellGet is required, which is included in Windows 10 and WMF5. If you
3031
are using PowerShell V3 or V4, you will need to install [[https://www.microsoft.com/en-us/download/details.aspx?id=49186][PowerShellGet]].
3132

32-
Then, you can run =Install-Module -AllowClobber Get-ChildItemColor=. Note that
33-
you need =-AllowClobber= option so =Get-ChildItemColor= may override the
34-
existing command =Out-Default=.
33+
Then, you can run =Install-Module Get-ChildItemColor=.
3534
** Install from GitHub
3635
After cloning the repo, you can put files in =/src= folder into
3736
=Get-ChildItemColor= folder under your =PSModulePath=
@@ -111,6 +110,8 @@ $Global:GetChildItemColorVerticalSpace = 1
111110
* Authors
112111
- [[http://github.com/joonro][Joon Ro]].
113112
* Changelog
113+
** v3.2.1
114+
- Refactoring.
114115
** v3.2.0
115116
- Make ~Get-ChildItemColor~ a proxy function of ~Get-ChildItem~, so
116117
~Get-ChildItemColor~ supports all the parameters of the original function.

src/FileInfo.ps1

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,28 +17,28 @@ function Write-FileLength {
1717

1818
# Outputs a line of a DirectoryInfo or FileInfo
1919
function Write-Color-LS {
20-
param ([string]$Color = "White", $Item)
20+
param ([string]$color = "White", $item)
2121

22-
Write-host ("{0,-7} " -f $Item.mode) -NoNewline
23-
Write-host ("{0,25} " -f ([String]::Format("{0,10} {1,8}", $Item.LastWriteTime.ToString("d"), $Item.LastWriteTime.ToString("t")))) -NoNewline
24-
Write-host ("{0,10} " -f (Write-FileLength $Item.length)) -NoNewline
25-
Write-host ("{0}" -f $Item.name) -ForegroundColor $Color
22+
Write-host ("{0,-7} " -f $item.mode) -NoNewline
23+
Write-host ("{0,25} " -f ([String]::Format("{0,10} {1,8}", $item.LastWriteTime.ToString("d"), $item.LastWriteTime.ToString("t")))) -NoNewline
24+
Write-host ("{0,10} " -f (Write-FileLength $item.length)) -NoNewline
25+
Write-host ("{0}" -f $item.name) -ForegroundColor $color
2626
}
2727

2828
function FileInfo {
2929
param (
3030
[Parameter(Mandatory=$True, Position=1)]
31-
$Item
31+
$item
3232
)
3333

34-
$ParentName = $Item.PSParentPath.Replace("Microsoft.PowerShell.Core\FileSystem::", "")
34+
$parentName = $item.PSParentPath.Replace("Microsoft.PowerShell.Core\FileSystem::", "")
3535

36-
If ($Script:LastParentName -ne $ParentName -or $Script:ShowHeader) {
37-
$Color = $GetChildItemColorTable.File['Directory']
36+
if ($script:LastParentName -ne $ParentName -or $script:ShowHeader) {
37+
$color = $GetChildItemColorTable.File['Directory']
3838

3939
Write-Host
4040
Write-Host " Directory: " -noNewLine
41-
Write-Host " $($ParentName)`n" -ForegroundColor $Color
41+
Write-Host " $($parentName)`n" -ForegroundColor $color
4242

4343
For ($l=1; $l -lt $GetChildItemColorVerticalSpace; $l++) {
4444
Write-Host ""
@@ -47,12 +47,12 @@ function FileInfo {
4747
Write-Host "Mode LastWriteTime Length Name"
4848
Write-Host "---- ------------- ------ ----"
4949

50-
$Script:ShowHeader = $False
50+
$script:ShowHeader = $False
5151
}
5252

53-
$Color = Get-FileColor $Item
53+
$color = Get-FileColor $item
5454

55-
Write-Color-LS $Color $Item
55+
Write-Color-LS $color $item
5656

57-
$Script:LastParentName = $ParentName
57+
$Script:LastParentName = $parentName
5858
}

src/Get-ChildItemColor.psd1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
RootModule = 'Get-ChildItemColor.psm1'
1111

1212
# Version number of this module.
13-
ModuleVersion = '3.2.0'
13+
ModuleVersion = '3.2.1'
1414

1515
# Supported PSEditions
1616
# CompatiblePSEditions = @()

src/Get-ChildItemColor.psm1

Lines changed: 62 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,25 @@ $Global:GetChildItemColorVerticalSpace = 1
55

66
. "$PSScriptRoot\Get-ChildItemColorTable.ps1"
77

8-
Function Get-FileColor($Item) {
8+
function Get-FileColor($Item) {
99
$Key = 'Default'
1010

11-
If ([bool]($Item.Attributes -band [IO.FileAttributes]::ReparsePoint)) {
11+
if ([bool]($Item.Attributes -band [IO.FileAttributes]::ReparsePoint)) {
1212
$Key = 'Symlink'
13-
} ElseIf ($Item.GetType().Name -eq 'DirectoryInfo') {
13+
} elseif ($Item.GetType().Name -eq 'DirectoryInfo') {
1414
$Key = 'Directory'
15-
} ElseIf ($Item.PSobject.Properties.Name -contains "Extension") {
15+
} elseif ($Item.PSobject.Properties.Name -contains "Extension") {
1616
If ($GetChildItemColorTable.File.ContainsKey($Item.Extension)) {
1717
$Key = $Item.Extension
1818
}
1919
}
2020

2121
$Color = $GetChildItemColorTable.File[$Key]
22-
Return $Color
22+
return $Color
2323
}
2424

25-
Function Get-ChildItemColorFormatWide {
26-
Param(
25+
function Get-ChildItemColorFormatWide {
26+
param(
2727
[string]$Path = "",
2828
[switch]$Force,
2929
[switch]$HideHeader,
@@ -34,45 +34,45 @@ Function Get-ChildItemColorFormatWide {
3434

3535
$Expression = "Get-ChildItem -Path `"$Path`" $Args"
3636

37-
If ($Force) {$Expression += " -Force"}
37+
if ($Force) {$Expression += " -Force"}
3838

3939
$Items = Invoke-Expression $Expression
4040

4141
$lnStr = $Items | Select-Object Name | Sort-Object { LengthInBufferCells("$_") } -Descending | Select-Object -First 1
4242
$len = LengthInBufferCells($lnStr.Name)
4343
$width = $Host.UI.RawUI.WindowSize.Width
44-
$cols = If ($len) {[math]::Floor(($width + 1) / ($len + 2))} Else {1}
44+
$cols = if ($len) {[math]::Floor(($width + 1) / ($len + 2))} else {1}
4545
if (!$cols) {$cols = 1}
4646

4747
$i = 0
4848
$pad = [math]::Ceiling(($width + 2) / $cols) - 3
4949

50-
ForEach ($Item in $Items) {
51-
If ($Item.PSobject.Properties.Name -contains "PSParentPath") {
52-
If ($Item.PSParentPath -match "FileSystem") {
50+
foreach ($Item in $Items) {
51+
if ($Item.PSobject.Properties.Name -contains "PSParentPath") {
52+
if ($Item.PSParentPath -match "FileSystem") {
5353
$ParentType = "Directory"
5454
$ParentName = $Item.PSParentPath.Replace("Microsoft.PowerShell.Core\FileSystem::", "")
55-
} ElseIf ($Item.PSParentPath -match "Registry") {
55+
} elseif ($Item.PSParentPath -match "Registry") {
5656
$ParentType = "Hive"
5757
$ParentName = $Item.PSParentPath.Replace("Microsoft.PowerShell.Core\Registry::", "")
5858
}
59-
} Else {
59+
} else {
6060
$ParentType = ""
6161
$ParentName = ""
6262
$LastParentName = $ParentName
6363
}
6464

65-
If ($i -eq 0 -and $HideHeader) {
65+
if ($i -eq 0 -and $HideHeader) {
6666
Write-Host ""
6767
}
6868

6969
# write header
70-
If ($LastParentName -ne $ParentName -and -not $HideHeader) {
71-
If ($i -ne 0 -AND $Host.UI.RawUI.CursorPosition.X -ne 0){ # conditionally add an empty line
70+
if ($LastParentName -ne $ParentName -and -not $HideHeader) {
71+
if ($i -ne 0 -AND $Host.UI.RawUI.CursorPosition.X -ne 0){ # conditionally add an empty line
7272
Write-Host ""
7373
}
7474

75-
For ($l=1; $l -le $GetChildItemColorVerticalSpace; $l++) {
75+
for ($l=1; $l -le $GetChildItemColorVerticalSpace; $l++) {
7676
Write-Host ""
7777
}
7878

@@ -81,7 +81,7 @@ Function Get-ChildItemColorFormatWide {
8181
$Color = $GetChildItemColorTable.File['Directory']
8282
Write-Host -Fore $Color " $ParentName"
8383

84-
For ($l=1; $l -le $GetChildItemColorVerticalSpace; $l++) {
84+
for ($l=1; $l -le $GetChildItemColorVerticalSpace; $l++) {
8585
Write-Host ""
8686
}
8787
}
@@ -91,32 +91,32 @@ Function Get-ChildItemColorFormatWide {
9191
# truncate the item name
9292
$toWrite = $Item.Name
9393

94-
If ($TrailingSlashDirectory -and $Item.GetType().Name -eq 'DirectoryInfo') {
94+
if ($TrailingSlashDirectory -and $Item.GetType().Name -eq 'DirectoryInfo') {
9595
$toWrite += '\'
9696
}
9797

9898
$itemLength = LengthInBufferCells($toWrite)
99-
If ($itemLength -gt $pad) {
99+
if ($itemLength -gt $pad) {
100100
$toWrite = (CutString $toWrite $pad)
101101
$itemLength = LengthInBufferCells($toWrite)
102102
}
103103

104-
$Color = Get-FileColor $Item
104+
$color = Get-FileColor $Item
105105
$widePad = $pad - ($itemLength - $toWrite.Length)
106-
Write-Host ("{0,-$widePad}" -f $toWrite) -Fore $Color -NoNewLine:$nnl
106+
Write-Host ("{0,-$widePad}" -f $toWrite) -Fore $color -NoNewLine:$nnl
107107

108-
If ($nnl) {
108+
if ($nnl) {
109109
Write-Host " " -NoNewLine
110110
}
111111

112112
$LastParentName = $ParentName
113113
}
114114

115-
For ($l=1; $l -lt $GetChildItemColorVerticalSpace; $l++) {
115+
for ($l=1; $l -lt $GetChildItemColorVerticalSpace; $l++) {
116116
Write-Host ""
117117
}
118118

119-
If ($nnl) { # conditionally add an empty line
119+
if ($nnl) { # conditionally add an empty line
120120
Write-Host ""
121121
}
122122
}
@@ -129,69 +129,69 @@ Add-Type -assemblyname System.ServiceProcess
129129
. "$PSScriptRoot\MatchInfo.ps1"
130130
. "$PSScriptRoot\ProcessInfo.ps1"
131131

132-
$Script:ShowHeader=$True
132+
$script:ShowHeader=$True
133133

134-
Function Out-ChildItemColor {
134+
function Out-ChildItemColor {
135135
[CmdletBinding(HelpUri='http://go.microsoft.com/fwlink/?LinkID=113362', RemotingCapability='None')]
136136
param(
137137
[switch] ${Transcript},
138138
[Parameter(Position=0, ValueFromPipeline=$True)] [psobject] ${InputObject}
139139
)
140140

141-
Begin {
142-
Try {
143-
For ($l=1; $l -lt $GetChildItemColorVerticalSpace; $l++) {
141+
begin {
142+
try {
143+
for ($l=1; $l -lt $GetChildItemColorVerticalSpace; $l++) {
144144
Write-Host ""
145145
}
146146

147147
$outBuffer = $null
148-
If ($PSBoundParameters.TryGetValue('OutBuffer', [ref]$outBuffer)) {
148+
if ($PSBoundParameters.TryGetValue('OutBuffer', [ref]$outBuffer)) {
149149
$PSBoundParameters['OutBuffer'] = 1
150150
}
151151
$wrappedCmd = $ExecutionContext.InvokeCommand.GetCommand('Microsoft.PowerShell.Core\Out-Default', [System.Management.Automation.CommandTypes]::Cmdlet)
152152
$scriptCmd = {& $wrappedCmd @PSBoundParameters }
153153

154154
$steppablePipeline = $scriptCmd.GetSteppablePipeline()
155155
$steppablePipeline.Begin($PSCmdlet)
156-
} Catch {
157-
Throw
156+
} catch {
157+
throw
158158
}
159159
}
160160

161-
Process {
162-
Try {
163-
If (($_ -is [System.IO.DirectoryInfo]) -or ($_ -is [System.IO.FileInfo])) {
161+
process {
162+
try {
163+
if (($_ -is [System.IO.DirectoryInfo]) -or ($_ -is [System.IO.FileInfo])) {
164164
FileInfo $_
165165
$_ = $Null
166166
}
167167

168-
ElseIf ($_ -is [System.ServiceProcess.ServiceController]) {
168+
elseif ($_ -is [System.ServiceProcess.ServiceController]) {
169169
ServiceController $_
170170
$_ = $Null
171171
}
172172

173-
ElseIf ($_ -is [Microsoft.Powershell.Commands.MatchInfo]) {
173+
elseif ($_ -is [Microsoft.Powershell.Commands.MatchInfo]) {
174174
MatchInfo $_
175175
$_ = $null
176176
}
177-
Else {
177+
else {
178178
$steppablePipeline.Process($_)
179179
}
180-
} Catch {
181-
Throw
180+
} catch {
181+
throw
182182
}
183183
}
184184

185-
End {
186-
Try {
187-
For ($l=1; $l -le $GetChildItemColorVerticalSpace; $l++) {
185+
end {
186+
try {
187+
for ($l=1; $l -le $GetChildItemColorVerticalSpace; $l++) {
188188
Write-Host ""
189189
}
190190

191-
$Script:ShowHeader=$true
191+
$script:ShowHeader=$true
192192
$steppablePipeline.End()
193-
} Catch {
194-
Throw
193+
} catch {
194+
throw
195195
}
196196
}
197197
<#
@@ -202,7 +202,7 @@ Function Out-ChildItemColor {
202202
#>
203203
}
204204

205-
Function Get-ChildItemColor {
205+
function Get-ChildItemColor {
206206
[CmdletBinding(DefaultParameterSetName='Items', HelpUri='https://go.microsoft.com/fwlink/?LinkID=2096492')]
207207
param(
208208
[Parameter(ParameterSetName='Items', Position=0, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)]
@@ -276,6 +276,13 @@ begin
276276
$wrappedCmd = $ExecutionContext.InvokeCommand.GetCommand('Microsoft.PowerShell.Management\Get-ChildItem', [System.Management.Automation.CommandTypes]::Cmdlet)
277277
$scriptCmd = {& $wrappedCmd @PSBoundParameters }
278278

279+
$ifPipeline = $PSCmdlet.MyInvocation.Line -Match '\|'
280+
281+
if ($ifPipeline) {
282+
$steppablePipeline = $scriptCmd.GetSteppablePipeline($myInvocation.CommandOrigin)
283+
$steppablePipeline.Begin($PSCmdlet)
284+
}
285+
279286
} catch {
280287
throw
281288
}
@@ -286,9 +293,9 @@ process
286293
try {
287294
$items = $scriptCmd.invoke()
288295

289-
If ($PSCmdlet.MyInvocation.Line -Match '\|') { # pipeline is used
290-
$items
291-
} Else {
296+
if ($ifPipeline) {
297+
$steppablePipeline.Process($_)
298+
} else {
292299
$items | Out-ChildItemColor
293300
}
294301
} catch {
@@ -299,6 +306,9 @@ process
299306
end
300307
{
301308
try {
309+
if ($ifPipeline) {
310+
$steppablePipeline.End()
311+
}
302312
} catch {
303313
throw
304314
}

0 commit comments

Comments
 (0)