-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft2.ps1
326 lines (295 loc) · 12.4 KB
/
ft2.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
<#
.SYNOPSIS
Formats a collection of objects into a neatly rendered table with automatic column wrapping.
.DESCRIPTION
Format-Table2 takes objects from the pipeline and examines all their properties to determine column widths,
alignments, and the appropriate table layout. It then constructs and outputs a table with borders. If the table
is wider than the host buffer, the columns are split into multiple blocks with specified repeat columns appearing
on each block.
.PARAMETER InputObject
The objects to be formatted as a table. This parameter accepts pipeline input.
.PARAMETER RepeatColumns
An array of column names that should be repeated in each wrapped block.
If not specified, the first discovered column is repeated by default.
.EXAMPLE
Get-Process | Select-Object -First 3 | Format-Table2
Retrieves the first 3 processes and displays them in a formatted table. If the total table width exceeds the host's
buffer width, columns are wrapped and the first discovered column is repeated for each block.
.EXAMPLE
Get-Process | Select-Object -First 3 | Format-Table2 -RepeatColumns "Name", "Id"
In this example, both "Name" and "Id" columns are repeated on every wrapped block of the table output.
.NOTES
The function dynamically calculates optimal column widths based on both header labels and cell content,
ensuring that numeric values are right-aligned while textual values are left-aligned. It leverages
efficient collection types and .NET string builders to boost performance. However, despite these
optimizations, the function may not scale well for very large datasets, so please use it with caution
in high-volume scenarios.
AUTHOR: Terry Yang
https://github.com/0x7FFFFFFFFFFFFFFF/Format-Table2
#>
function Format-Table2 {
[CmdletBinding()]
[Alias("ft2")]
param(
[Parameter(Mandatory = $true, ValueFromPipeline = $true)][PSObject]$InputObject,
# Which column(s) should be repeated on every wrapped block.
# If not provided, by default the first discovered column is used.
[string[]]$RepeatColumns = @()
)
begin {
# Number of spaces on each side of cell content.
$Padding = 1
# Use a generic list for efficient accumulation of input objects.
$data = [System.Collections.Generic.List[PSObject]]::new()
# Cache the newline value.
$nl = [Environment]::NewLine
# Helper: Returns $true if a string can be parsed as a number.
function is_numeric {
param([string]$s)
$dummy = 0.0
return [double]::TryParse($s, [System.Globalization.NumberStyles]::Number,
[System.Globalization.CultureInfo]::InvariantCulture, [ref]$dummy)
}
# An ordered dictionary mapping column names to column details.
# Each detail is stored as a hashtable so its values can be updated.
# Keys include: Header, MaxLen, and AllNumeric.
$colInfo = [ordered]@{}
}
process {
$data.Add($InputObject)
foreach ($prop in $InputObject.PSObject.Properties) {
$name = $prop.Name
if (-not $colInfo.Contains($name)) { # Using Contains (not ContainsKey) for an ordered dictionary.
$colInfo[$name] = @{
Header = $name
MaxLen = $name.Length
AllNumeric = $true
}
}
if ($prop.Value -ne $null) {
$s = $prop.Value.ToString() -replace "\t", " "
}
else {
$s = ""
}
if ($s.Length -gt $colInfo[$name]['MaxLen']) {
$colInfo[$name]['MaxLen'] = $s.Length
}
if ($s -ne "" -and -not (is_numeric $s)) {
$colInfo[$name]['AllNumeric'] = $false
}
}
}
end {
if ($data.Count -eq 0) { return }
# Finalize each column's width and alignment.
foreach ($key in $colInfo.Keys) {
$info = $colInfo[$key]
$info['Width'] = $info['MaxLen'] + (2 * $Padding)
$info['Alignment'] = if ($info['AllNumeric']) { "Right" } else { "Left" }
}
# Preserve natural (first encountered) order.
$orderedColumns = @($colInfo.Keys)
# Process repeat columns: if none specified, default to the first discovered column.
if ($RepeatColumns.Count -eq 0) {
$RepeatColumns = @($orderedColumns[0])
}
else {
$RepeatColumns = $RepeatColumns | Where-Object { $orderedColumns -contains $_ }
if ($RepeatColumns.Count -eq 0) {
$RepeatColumns = @($orderedColumns[0])
}
}
# Cache available width from the host.
$bufferWidth = $Host.UI.RawUI.BufferSize.Width
# Helper: Compute total block width for specified columns.
function get_block_width {
param([string[]]$Cols)
$sum = 0
foreach ($c in $Cols) {
$sum += $colInfo[$c]['Width']
}
# Each column adds a vertical border; add one more than the number of columns.
return $sum + ($Cols.Count + 1)
}
# Define border characters.
$bc = @{
TopLeft = "+"
TopMid = "+"
TopRight = "+"
Fill = "-"
MidLeft = "+"
MidMid = "+"
MidRight = "+"
BottomLeft = "+"
BottomMid = "+"
BottomRight = "+"
Vertical = "|"
}
# Function: Generate a table block given a list of columns.
function generate_table_block {
param([string[]]$Cols)
$sb = [System.Text.StringBuilder]::new()
# Top border.
$lineTopSB = [System.Text.StringBuilder]::new()
$lineTopSB.Append($bc.TopLeft) | Out-Null
foreach ($col in $Cols) {
$width = $colInfo[$col]['Width']
$lineTopSB.Append([string]::new($bc.Fill, $width)) | Out-Null
$lineTopSB.Append($bc.TopMid) | Out-Null
}
if ($lineTopSB.Length -gt 1) {
$lineTopStr = $lineTopSB.ToString()
$lineTop = $lineTopStr.Substring(0, $lineTopStr.Length - 1) + $bc.TopRight
}
else {
$lineTop = $lineTopSB.ToString() + $bc.TopRight
}
$sb.AppendLine($lineTop) | Out-Null
# Header row (center the header).
$headerSB = [System.Text.StringBuilder]::new()
$headerSB.Append($bc.Vertical) | Out-Null
foreach ($col in $Cols) {
$info = $colInfo[$col]
$content = $info['Header']
$totalPad = $info['Width'] - $content.Length
$leftPad = [int]([math]::Floor($totalPad / 2))
$rightPad = $totalPad - $leftPad
$cell = (" " * $leftPad) + $content + (" " * $rightPad)
$headerSB.Append($cell) | Out-Null
$headerSB.Append($bc.Vertical) | Out-Null
}
$sb.AppendLine($headerSB.ToString()) | Out-Null
# Middle border.
$lineMidSB = [System.Text.StringBuilder]::new()
$lineMidSB.Append($bc.MidLeft) | Out-Null
foreach ($col in $Cols) {
$width = $colInfo[$col]['Width']
$lineMidSB.Append([string]::new($bc.Fill, $width)) | Out-Null
$lineMidSB.Append($bc.MidMid) | Out-Null
}
if ($lineMidSB.Length -gt 1) {
$lineMidStr = $lineMidSB.ToString()
$lineMid = $lineMidStr.Substring(0, $lineMidStr.Length - 1) + $bc.MidRight
}
else {
$lineMid = $lineMidSB.ToString() + $bc.MidRight
}
$sb.AppendLine($lineMid) | Out-Null
# Data rows.
foreach ($obj in $data) {
$rowSB = [System.Text.StringBuilder]::new()
$rowSB.Append($bc.Vertical) | Out-Null
foreach ($col in $Cols) {
$info = $colInfo[$col]
$val = ""
if ($obj.PSObject.Properties[$col]) {
$temp = $obj.$col
if ($temp -ne $null) {
$val = $temp.ToString() -replace "\t", " "
}
}
$contentWidth = [Math]::Max(0, $info['Width'] - (2 * $Padding))
if ($info['Alignment'] -eq "Right") {
$cellContent = $val.PadLeft($contentWidth)
}
else {
$cellContent = $val.PadRight($contentWidth)
}
$cell = (" " * $Padding) + $cellContent + (" " * $Padding)
$rowSB.Append($cell) | Out-Null
$rowSB.Append($bc.Vertical) | Out-Null
}
$sb.AppendLine($rowSB.ToString()) | Out-Null
}
# Bottom border.
$lineBottomSB = [System.Text.StringBuilder]::new()
$lineBottomSB.Append($bc.BottomLeft) | Out-Null
foreach ($col in $Cols) {
$width = $colInfo[$col]['Width']
$lineBottomSB.Append([string]::new($bc.Fill, $width)) | Out-Null
$lineBottomSB.Append($bc.BottomMid) | Out-Null
}
if ($lineBottomSB.Length -gt 1) {
$lineBottomStr = $lineBottomSB.ToString()
$lineBottom = $lineBottomStr.Substring(0, $lineBottomStr.Length - 1) + $bc.BottomRight
}
else {
$lineBottom = $lineBottomSB.ToString() + $bc.BottomRight
}
$sb.AppendLine($lineBottom) | Out-Null
return $sb.ToString().TrimEnd()
}
# -------------------------------
# Determine column blocks that fit within the available width.
# Block 1 is built from the natural column order.
# -------------------------------
$blocks = @()
$block1 = @()
$i = 0
while ($i -lt $orderedColumns.Count) {
$trial = $block1 + $orderedColumns[$i]
if ((get_block_width -Cols $trial) -le $bufferWidth) {
$block1 = $trial
$i++
}
else {
# Force add if no columns have been added to $block1 (to ensure progress)
if ($block1.Count -eq 0) {
$block1 = $trial
$i++
}
break
}
}
$blocks += ,$block1
# Remaining columns (if any) for subsequent blocks.
if ($i -lt $orderedColumns.Count) {
$remaining = $orderedColumns[$i..($orderedColumns.Count - 1)]
}
else {
$remaining = @()
}
# For subsequent blocks, prepend the repeat columns and add as many remaining columns as fit.
while ($remaining.Count -gt 0) {
$block = @()
foreach ($p in $RepeatColumns) {
if ($block -notcontains $p) {
$block += $p
}
}
$j = 0
while ($j -lt $remaining.Count) {
$trial = $block + $remaining[$j]
if ((get_block_width -Cols $trial) -le $bufferWidth) {
$block += $remaining[$j]
$j++
}
else {
# If no column could be added from the remaining list, force add one so progress is made.
if ($j -eq 0) {
$block += $remaining[$j]
$j++
}
break
}
}
$blocks += ,$block
if ($j -lt $remaining.Count) {
$remaining = $remaining[$j..($remaining.Count - 1)]
}
else {
$remaining = @()
}
}
# -------------------------------
# Generate full output (each block as one table).
# -------------------------------
$outputSB = [System.Text.StringBuilder]::new()
foreach ($blk in $blocks) {
$outputSB.AppendLine((generate_table_block -Cols $blk)) | Out-Null
$outputSB.AppendLine() | Out-Null
}
Write-Output $outputSB.ToString().TrimEnd()
}
}