|
| 1 | +function Format-Collection2 ($Value, [switch]$Pretty) { |
| 2 | + $length = 0 |
| 3 | + $o = foreach ($v in $Value) { |
| 4 | + $formatted = Format-Nicely2 -Value $v -Pretty:$Pretty |
| 5 | + $length += $formatted.Length + 1 # 1 is for the separator |
| 6 | + $formatted |
| 7 | + } |
| 8 | + |
| 9 | + $prettyLimit = 50 |
| 10 | + if ($Pretty -and ($length + 3) -gt $prettyLimit) { |
| 11 | + # 3 is for the '@()' |
| 12 | + "@(`n $($o -join ",`n ")`n)" |
| 13 | + } |
| 14 | + else { |
| 15 | + "@($($o -join ', '))" |
| 16 | + } |
| 17 | +} |
| 18 | + |
| 19 | +function Format-Object2 ($Value, $Property, [switch]$Pretty) { |
| 20 | + if ($null -eq $Property) { |
| 21 | + $Property = foreach ($p in $Value.PSObject.Properties) { $p.Name } |
| 22 | + } |
| 23 | + $orderedProperty = foreach ($p in $Property | & $SafeCommands['Sort-Object']) { |
| 24 | + # force the values to be strings for powershell v2 |
| 25 | + "$p" |
| 26 | + } |
| 27 | + |
| 28 | + $valueType = Get-ShortType $Value |
| 29 | + $items = foreach ($p in $orderedProperty) { |
| 30 | + $v = ([PSObject]$Value.$p) |
| 31 | + $f = Format-Nicely2 -Value $v -Pretty:$Pretty |
| 32 | + "$p=$f" |
| 33 | + } |
| 34 | + |
| 35 | + if (0 -eq $Property.Length ) { |
| 36 | + $o = "$valueType{}" |
| 37 | + } |
| 38 | + elseif ($Pretty) { |
| 39 | + $o = "$valueType{`n $($items -join ";`n ");`n}" |
| 40 | + } |
| 41 | + else { |
| 42 | + $o = "$valueType{$($items -join '; ')}" |
| 43 | + } |
| 44 | + |
| 45 | + $o |
| 46 | +} |
| 47 | + |
| 48 | +function Format-String2 ($Value) { |
| 49 | + if ('' -eq $Value) { |
| 50 | + return '<empty>' |
| 51 | + } |
| 52 | + |
| 53 | + "'$Value'" |
| 54 | +} |
| 55 | + |
| 56 | +function Format-Null2 { |
| 57 | + '$null' |
| 58 | +} |
| 59 | + |
| 60 | +function Format-Boolean2 ($Value) { |
| 61 | + '$' + $Value.ToString().ToLower() |
| 62 | +} |
| 63 | + |
| 64 | +function Format-ScriptBlock2 ($Value) { |
| 65 | + '{' + $Value + '}' |
| 66 | +} |
| 67 | + |
| 68 | +function Format-Number2 ($Value) { |
| 69 | + [string]$Value |
| 70 | +} |
| 71 | + |
| 72 | +function Format-Hashtable2 ($Value) { |
| 73 | + $head = '@{' |
| 74 | + $tail = '}' |
| 75 | + |
| 76 | + $entries = foreach ($v in $Value.Keys | & $SafeCommands['Sort-Object']) { |
| 77 | + $formattedValue = Format-Nicely2 $Value.$v |
| 78 | + "$v=$formattedValue" |
| 79 | + } |
| 80 | + |
| 81 | + $head + ( $entries -join '; ') + $tail |
| 82 | +} |
| 83 | + |
| 84 | +function Format-Dictionary2 ($Value) { |
| 85 | + $head = 'Dictionary{' |
| 86 | + $tail = '}' |
| 87 | + |
| 88 | + $entries = foreach ($v in $Value.Keys | & $SafeCommands['Sort-Object'] ) { |
| 89 | + $formattedValue = Format-Nicely2 $Value.$v |
| 90 | + "$v=$formattedValue" |
| 91 | + } |
| 92 | + |
| 93 | + $head + ( $entries -join '; ') + $tail |
| 94 | +} |
| 95 | + |
| 96 | +function Format-Nicely2 ($Value, [switch]$Pretty) { |
| 97 | + if ($null -eq $Value) { |
| 98 | + return Format-Null2 -Value $Value |
| 99 | + } |
| 100 | + |
| 101 | + if ($Value -is [bool]) { |
| 102 | + return Format-Boolean2 -Value $Value |
| 103 | + } |
| 104 | + |
| 105 | + if ($Value -is [string]) { |
| 106 | + return Format-String2 -Value $Value |
| 107 | + } |
| 108 | + |
| 109 | + if ($value -is [type]) { |
| 110 | + return Format-Type2 -Value $Value |
| 111 | + } |
| 112 | + |
| 113 | + if (Is-DecimalNumber -Value $Value) { |
| 114 | + return Format-Number2 -Value $Value |
| 115 | + } |
| 116 | + |
| 117 | + if (Is-ScriptBlock -Value $Value) { |
| 118 | + return Format-ScriptBlock2 -Value $Value |
| 119 | + } |
| 120 | + |
| 121 | + if (Is-Value -Value $Value) { |
| 122 | + return $Value |
| 123 | + } |
| 124 | + |
| 125 | + if (Is-Hashtable -Value $Value) { |
| 126 | + return Format-Hashtable2 -Value $Value |
| 127 | + } |
| 128 | + |
| 129 | + if (Is-Dictionary -Value $Value) { |
| 130 | + return Format-Dictionary2 -Value $Value |
| 131 | + } |
| 132 | + |
| 133 | + if ((Is-DataTable -Value $Value) -or (Is-DataRow -Value $Value)) { |
| 134 | + return Format-DataTable2 -Value $Value -Pretty:$Pretty |
| 135 | + } |
| 136 | + |
| 137 | + if (Is-Collection -Value $Value) { |
| 138 | + return Format-Collection2 -Value $Value -Pretty:$Pretty |
| 139 | + } |
| 140 | + |
| 141 | + Format-Object2 -Value $Value -Property (Get-DisplayProperty2 $Value.GetType()) -Pretty:$Pretty |
| 142 | +} |
| 143 | + |
| 144 | +function Get-DisplayProperty2 ([Type]$Type) { |
| 145 | + # rename to Get-DisplayProperty? |
| 146 | + |
| 147 | + <# some objects are simply too big to show all of their properties, |
| 148 | + so we can create a list of properties to show from an object |
| 149 | + maybe the default info from Get-FormatData could be utilized here somehow |
| 150 | + so we show only stuff that would normally show in format-table view |
| 151 | + leveraging the work PS team already did #> |
| 152 | + |
| 153 | + # this will become more advanced, basically something along the lines of: |
| 154 | + # foreach type, try constructing the type, and if it exists then check if the |
| 155 | + # incoming type is assignable to the current type, if so then return the properties, |
| 156 | + # this way I can specify the map from the most concrete type to the least concrete type |
| 157 | + # and for types that do not exist |
| 158 | + |
| 159 | + $propertyMap = @{ |
| 160 | + 'System.Diagnostics.Process' = 'Id', 'Name' |
| 161 | + } |
| 162 | + |
| 163 | + $propertyMap[$Type.FullName] |
| 164 | +} |
| 165 | + |
| 166 | +function Get-ShortType2 ($Value) { |
| 167 | + if ($null -ne $value) { |
| 168 | + Format-Type2 $Value.GetType() |
| 169 | + } |
| 170 | + else { |
| 171 | + Format-Type2 $null |
| 172 | + } |
| 173 | +} |
| 174 | + |
| 175 | +function Format-Type2 ([Type]$Value) { |
| 176 | + if ($null -eq $Value) { |
| 177 | + return '[null]' |
| 178 | + } |
| 179 | + |
| 180 | + $type = [string]$Value |
| 181 | + |
| 182 | + $typeFormatted = $type ` |
| 183 | + -replace "^System\." ` |
| 184 | + -replace "^Management\.Automation\.PSCustomObject$", "PSObject" ` |
| 185 | + -replace "^PSCustomObject$", "PSObject" ` |
| 186 | + -replace "^Object\[\]$", "collection" ` |
| 187 | + |
| 188 | + "[$($typeFormatted)]" |
| 189 | +} |
| 190 | + |
| 191 | +function Format-DataTable2 ($Value) { |
| 192 | + return "$Value" |
| 193 | +} |
| 194 | + |
0 commit comments