-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathShow-PesterResult.ps1
More file actions
251 lines (238 loc) · 11.6 KB
/
Show-PesterResult.ps1
File metadata and controls
251 lines (238 loc) · 11.6 KB
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
function Show-PesterResult {
<#
.SYNOPSIS
Open a TUI to explore the Pester result object.
.DESCRIPTION
Show a Pester result in a TUI (Text User Interface) using Spectre.Console.
This function builds a layout with a header, a list of items, and a preview panel.
.PARAMETER PesterResult
The Pester result object to display. This should be a Pester Run object.
.PARAMETER NoShortcutPanel
If specified, the shortcut panel will not be displayed at the bottom of the TUI.
.EXAMPLE
$pesterResult = Invoke-Pester -Path "path\to\tests.ps1" -PassThru
Show-PesterResult -PesterResult $pesterResult
This example runs Pester tests and opens a TUI to explore the results.
#>
[CmdletBinding()]
[OutputType([void])]
[Diagnostics.CodeAnalysis.SuppressMessageAttribute(
'PSReviewUnusedParameter',
'PesterResult',
Justification='This is actually used in the script block.'
)]
param (
[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[Pester.Run]
$PesterResult,
[switch]
$NoShortcutPanel
)
# Build and show the TUI
$rows = @(
# Row 1
(
New-SpectreLayout -Name "header" -MinimumSize 5 -Ratio 1 -Data ("empty")
),
# Row 2
(
New-SpectreLayout -Name "content" -Ratio 10 -Columns @(
(
New-SpectreLayout -Name "list" -Ratio 1 -Data "empty"
),
(
New-SpectreLayout -Name "preview" -Ratio 4 -Data "empty"
)
)
)
)
if(-not $NoShortcutPanel) {
$rows += (
# Row 3
(
New-SpectreLayout -Name "footer" -Ratio 1 -MinimumSize 1 -Data (
Get-ShortcutKeyPanel
)
)
)
}
$layout = New-SpectreLayout -Name "root" -Rows $rows
# Start live rendering the layout
Invoke-SpectreLive -Data $layout -ScriptBlock {
param (
[Spectre.Console.LiveDisplayContext] $Context
)
#region Initial State
$items = Get-ListFromObject -Object $PesterResult
Write-Debug "Items: $($items.Keys -join ', ')"
$list = [array]$items.Keys
$selectedItem = $list[0]
$stack = [System.Collections.Stack]::new()
$object = $PesterResult
$selectedPane = 'list'
$scrollPosition = 0
$listScrollPosition = 0
#endregion Initial State
while ($true) {
# Check the layout sizes
$sizes = $layout | Get-SpectreLayoutSizes
$previewHeight = $sizes["preview"].Height
$previewWidth = $sizes["preview"].Width
$listHeight = $sizes["list"].Height
Write-Debug "Preview size: $previewWidth x $previewHeight"
Write-Debug "List height: $listHeight"
# Handle input
$lastKeyPressed = Get-LastKeyPressed
if ($null -ne $lastKeyPressed) {
#region List Navigation
if($selectedPane -eq 'list') {
if ($lastKeyPressed.Key -in @("j", "DownArrow")) {
$selectedItem = $list[($list.IndexOf($selectedItem) + 1) % $list.Count]
$scrollPosition = 0
# Update list scroll position to keep selected item visible
$selectedIndex = $list.IndexOf($selectedItem)
if ($listHeight -gt 0 -and $list.Count -gt $listHeight) {
# If selected item is below visible area, scroll down
if ($selectedIndex -ge ($listScrollPosition + $listHeight - 1)) {
$listScrollPosition = $selectedIndex - $listHeight + 2
}
# If selected item is above visible area, scroll up
elseif ($selectedIndex -lt $listScrollPosition) {
$listScrollPosition = $selectedIndex
}
# Ensure scroll position is within bounds
$listScrollPosition = [Math]::Max(0, [Math]::Min($listScrollPosition, $list.Count - $listHeight))
}
} elseif ($lastKeyPressed.Key -in @("k", "UpArrow")) {
$selectedItem = $list[($list.IndexOf($selectedItem) - 1 + $list.Count) % $list.Count]
$scrollPosition = 0
# Update list scroll position to keep selected item visible
$selectedIndex = $list.IndexOf($selectedItem)
if ($listHeight -gt 0 -and $list.Count -gt $listHeight) {
# If selected item is above visible area, scroll up
if ($selectedIndex -lt $listScrollPosition) {
$listScrollPosition = $selectedIndex
}
# If selected item is below visible area, scroll down
elseif ($selectedIndex -ge ($listScrollPosition + $listHeight - 1)) {
$listScrollPosition = $selectedIndex - $listHeight + 2
}
# Ensure scroll position is within bounds
$listScrollPosition = [Math]::Max(0, [Math]::Min($listScrollPosition, $list.Count - $listHeight))
}
} elseif ($lastKeyPressed.Key -eq "PageDown") {
$currentIndex = $list.IndexOf($selectedItem)
$newIndex = [Math]::Min($currentIndex + 10, $list.Count - 1)
$selectedItem = $list[$newIndex]
$scrollPosition = 0
# Update list scroll position to keep selected item visible
if ($listHeight -gt 0 -and $list.Count -gt $listHeight) {
$listScrollPosition = [Math]::Max(0, [Math]::Min($newIndex - [Math]::Floor($listHeight / 2), $list.Count - $listHeight))
}
} elseif ($lastKeyPressed.Key -eq "PageUp") {
$currentIndex = $list.IndexOf($selectedItem)
$newIndex = [Math]::Max($currentIndex - 10, 0)
$selectedItem = $list[$newIndex]
$scrollPosition = 0
# Update list scroll position to keep selected item visible
if ($listHeight -gt 0 -and $list.Count -gt $listHeight) {
$listScrollPosition = [Math]::Max(0, [Math]::Min($newIndex - [Math]::Floor($listHeight / 2), $list.Count - $listHeight))
}
} elseif ($lastKeyPressed.Key -eq "Home") {
$selectedItem = $list[0]
$scrollPosition = 0
$listScrollPosition = 0
} elseif ($lastKeyPressed.Key -eq "End") {
$selectedItem = $list[-1]
$scrollPosition = 0
# Scroll to bottom to show last item
if ($listHeight -gt 0 -and $list.Count -gt $listHeight) {
$listScrollPosition = $list.Count - $listHeight
} else {
$listScrollPosition = 0
}
} elseif ($lastKeyPressed.Key -in @("Tab", "RightArrow", "l")) {
$selectedPane = 'preview'
} elseif ($lastKeyPressed.Key -eq "Enter") {
<# Recurse into Pester Object #>
if($items.Item($selectedItem).GetType().Name -eq "Test") {
# This is a test. We don't want to go deeper.
}
if($selectedItem -like '*..*') {
# Move up one via selecting ..
$object = $stack.Pop()
Write-Debug "Popped item from stack: $($object.Name)"
} else {
Write-Debug "Pushing item into stack: $($items.Item($selectedItem).Name)"
$stack.Push($object)
$object = $items.Item($selectedItem)
}
$items = Get-ListFromObject -Object $object
$list = [array]$items.Keys
$selectedItem = $list[0]
$scrollPosition = 0
$listScrollPosition = 0
} elseif ($lastKeyPressed.Key -eq "Escape") {
# Move up via Esc key
if($stack.Count -eq 0) {
# This is the top level. Exit the loop.
return
}
$object = $stack.Pop()
$items = Get-ListFromObject -Object $object
$list = [array]$items.Keys
$selectedItem = $list[0]
$scrollPosition = 0
$listScrollPosition = 0
}
}
else {
#region Preview Navigation
if ($lastKeyPressed.Key -in "Escape", "Tab", "LeftArrow", "h") {
$selectedPane = 'list'
} elseif ($lastKeyPressed.Key -eq "Down") {
# Scroll down in the preview panel
$scrollPosition = $ScrollPosition + 1
} elseif ($lastKeyPressed.Key -eq "Up") {
# Scroll up in the preview panel
$scrollPosition = $ScrollPosition - 1
} elseif ($lastKeyPressed.Key -eq "PageDown") {
# Scroll down by a page in the preview panel
$scrollPosition = $ScrollPosition + 1
} elseif ($lastKeyPressed.Key -eq "PageUp") {
# Scroll up by a page in the preview panel
$scrollPosition = $ScrollPosition - 1
}
#endregion Preview Navigation
}
}
# Generate new data
$titlePanel = Get-TitlePanel -Item $object
$getListPanelSplat = @{
List = $list
SelectedItem = $selectedItem
SelectedPane = $selectedPane
ListScrollPosition = $listScrollPosition
ListHeight = $listHeight
}
$listPanel = Get-ListPanel @getListPanelSplat
$getPreviewPanelSplat = @{
Items = $items
SelectedItem = $selectedItem
ScrollPosition = $scrollPosition
PreviewHeight = $previewHeight
PreviewWidth = $previewWidth
SelectedPane = $selectedPane
}
$previewPanel = Get-PreviewPanel @getPreviewPanelSplat
# Update layout
$layout["header"].Update($titlePanel) | Out-Null
$layout["list"].Update($listPanel) | Out-Null
$layout["preview"].Update($previewPanel) | Out-Null
# Draw changes
$Context.Refresh()
Start-Sleep -Milliseconds 100
}
}
}