-
Notifications
You must be signed in to change notification settings - Fork 0
/
Format-TextTable.ps1
138 lines (114 loc) · 3.65 KB
/
Format-TextTable.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
function ConvertTo-TextTableBody {
[CmdletBinding()]
[Alias('wrapBody')]
param (
[Parameter(Mandatory,
ValueFromPipeline,
ValueFromPipelineByPropertyName,
Position = 0
)]
[object[]] $Header,
[Parameter(
Mandatory,
ValueFromPipeline,
ValueFromPipelineByPropertyName,
Position = 1
)]
[Collections.Generic.List[psobject]] $Line,
[Parameter(
Mandatory,
ValueFromPipeline,
ValueFromPipelineByPropertyName,
Position = 2
)]
[hashtable] $Length
)
process {
foreach ($L in $Line) {
Write-Verbose "Wrapping: [$L]"
$IsStart = $true
foreach ($Head in $Header) {
$HeaderName = $Head.Name
Write-Verbose $L
Write-Verbose $HeaderName
$Res = $L.$HeaderName.ToString()
$PadDir = $Head.TypeNameOfValue -eq 'System.Int32' ? 'PadLeft' : 'PadRight'
$PadAmount = $Length[$HeaderName]
if ($IsStart) {
$ResultLine = '|'
$IsStart = $false
}
$ResultLine += ' {0} |' -f $Res.$PadDir($PadAmount, ' ')
Write-Verbose "So far: $ResultLine"
}
$ResultLine
}
}
}
function Format-TextTable {
[CmdletBinding()]
[Alias('fmt')]
param (
[Parameter(
Mandatory,
ValueFromPipeline,
ValueFromPipelineByPropertyName,
Position = 0
)]
[psobject[]] $Object
)
begin {
$Capture = [Collections.Generic.List[psobject]]::new()
$PropLength = [ordered] @{}
}
process {
foreach ($Obj in $Object) {
Write-Verbose "Capturing [$Obj]"
$Capture.Add($Obj)
}
}
end {
Write-Verbose "Parsing the headers from [$($Capture[0])]"
$Header = $Capture[0].psobject.properties | Where-Object MemberType -eq 'NoteProperty'
Write-Verbose "Setting initial length to property name length"
foreach ($Head in $Header) {
$HeaderName = $Head.Name
$PropLength[$HeaderName] = $HeaderName.length
}
Write-Verbose "Enumerating collection, in case value length is greater than property name"
foreach ($Head in $Header) {
$HeaderName = $Head.Name
foreach ($Cap in $Capture) {
$NewLen = $Cap.$HeaderName.ToString().Length
$CurrLen = $PropLength[$HeaderName]
$PropLength[$HeaderName] = [Math]::Max($CurrLen, $NewLen)
}
}
Write-Verbose "Creating break line"
$IsStart = $true
foreach ($Len in $PropLength.GetEnumerator()) {
$Delim = $IsStart ? '|' : '+'
$Dash = '-' * ($Len.Value + 2)
$BreakLine += '{0}{1}' -f $Delim, $Dash
$IsStart = $false
}
$BreakLine += '|'
Write-Verbose "Generating Text Table"
$TextTableBodyParams = @{
Header = $Header
Length = $PropLength
}
$IsStart = $true
foreach ($Head in $Header) {
$HeaderName = $Head.Name
if ($IsStart) {$HeaderLine += '|'}
$HeaderLine += ' {0} |' -f $HeaderName.PadRight($PropLength[$HeaderName], ' ')
$IsStart = $false
}
$BreakLine
$HeaderLine
$BreakLine
ConvertTo-TextTableBody @TextTableBodyParams -Line $Capture
$BreakLine
}
}