@@ -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' )]
207207param (
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
299306end
300307{
301308 try {
309+ if ($ifPipeline ) {
310+ $steppablePipeline.End ()
311+ }
302312 } catch {
303313 throw
304314 }
0 commit comments