devel/Get-ChildItemColor debug.ps1
$OriginalForegroundColor = $Host.UI.RawUI.ForegroundColor if ([System.Enum]::IsDefined([System.ConsoleColor], 1) -eq "False") { $OriginalForegroundColor = "Gray" } $CompressedList = @(".7z", ".gz", ".rar", ".tar", ".zip") $ExecutableList = @(".exe", ".bat", ".cmd", ".py", ".pl", ".ps1", ".psm1", ".vbs", ".rb", ".reg", ".fsx", ".sh") $DllPdbList = @(".dll", ".pdb") $TextList = @(".csv", ".log", ".markdown", ".rst", ".txt") $ConfigsList = @(".cfg", ".conf", ".config", ".ini", ".json") $ColorTable = @{} $ColorTable.Add('Default', $OriginalForegroundColor) $ColorTable.Add('Directory', "Green") ForEach ($Extension in $CompressedList) { $ColorTable.Add($Extension, "Yellow") } ForEach ($Extension in $ExecutableList) { $ColorTable.Add($Extension, "Blue") } ForEach ($Extension in $TextList) { $ColorTable.Add($Extension, "Cyan") } ForEach ($Extension in $DllPdbList) { $ColorTable.Add($Extension, "DarkGreen") } ForEach ($Extension in $ConfigsList) { $ColorTable.Add($Extension, "DarkYellow") } Function Get-Color($Item) { $Key = 'Default' If ($Item.GetType().Name -eq 'DirectoryInfo') { $Key = 'Directory' } Else { If ($Item.PSobject.Properties.Name -contains "Extension") { If ($ColorTable.ContainsKey($Item.Extension)) { $Key = $Item.Extension } } } $Color = $ColorTable[$Key] Return $Color } $Path = "~" $Force = $false $nnl = $True $Expression = "Get-ChildItem -Path `"$Path`" $Args" if ($Force) {$Expression += " -Force"} $Items = Invoke-Expression $Expression $lnStr = $Items | Select-Object Name | Sort-Object { "$_".Length } -Descending | Select-Object -First 1 $len = $lnStr.Name.Length $width = $Host.UI.RawUI.WindowSize.Width $cols = If ($len) {[math]::Floor(($width + 1) / ($len + 2))} Else {1} if (!$cols) {$cols = 1} $i = 0 $pad = [math]::Ceiling(($width + 2) / $cols) - 3 ForEach ($Item in $Items) { If ($Item.PSobject.Properties.Name -contains "PSParentPath") { If ($Item.PSParentPath -match "FileSystem") { $ParentType = "Directory" $ParentName = $Item.PSParentPath.Replace("Microsoft.PowerShell.Core\FileSystem::", "") } ElseIf ($Item.PSParentPath -match "Registry") { $ParentType = "Hive" $ParentName = $Item.PSParentPath.Replace("Microsoft.PowerShell.Core\Registry::", "") } } Else { $ParentType = "" $ParentName = "" $LastParentName = $ParentName } $Color = Get-Color $Item If ($LastParentName -ne $ParentName) { If($i -ne 0 -AND $Host.UI.RawUI.CursorPosition.X -ne 0){ # conditionally add an empty line Write-Host "" } Write-Host -Fore $OriginalForegroundColor ("`n $($ParentType): $ParentName`n") } $nnl = ++$i % $cols -ne 0 # truncate the item name $toWrite = $Item.Name If ($toWrite.length -gt $pad) { $toWrite = $toWrite.Substring(0, $pad - 3) + "..." } Write-Host ("{0,-$pad}" -f $toWrite) -Fore $Color -NoNewLine:$nnl If ($nnl) { Write-Host " " -NoNewLine } $LastParentName = $ParentName If ($Item.Name.Substring(0, 4) -eq "cpub") { Write-Host "Breaking!" Break } } If ($nnl) { # conditionally add an empty line Write-Host "" Write-Host "" } |