Skip to content

Commit 2a43165

Browse files
committed
feat(Panels): ✨ enhance navigation and layout for list and preview panels
* Added support for scrolling in the preview panel. * Improved navigation with additional key bindings for list selection. * Updated `Get-ListPanel` and `Get-PreviewPanel` to accept new parameters for better layout control. * Removed unused class `A` from the project.
1 parent 549250c commit 2a43165

File tree

6 files changed

+129
-52
lines changed

6 files changed

+129
-52
lines changed

PesterExplorer/Classes/classA.ps1

Lines changed: 0 additions & 3 deletions
This file was deleted.

PesterExplorer/PesterExplorer.psm1

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
#require -Module PwshSpectreConsole
12
# Dot source public/private functions
23
$classes = @(Get-ChildItem -Path (Join-Path -Path $PSScriptRoot -ChildPath 'Classes/*.ps1') -Recurse -ErrorAction Stop)
3-
$public = @(Get-ChildItem -Path (Join-Path -Path $PSScriptRoot -ChildPath 'Public/*.ps1') -Recurse -ErrorAction Stop)
4+
$public = @(Get-ChildItem -Path (Join-Path -Path $PSScriptRoot -ChildPath 'Public/*.ps1') -Recurse -ErrorAction Stop)
45
$private = @(Get-ChildItem -Path (Join-Path -Path $PSScriptRoot -ChildPath 'Private/*.ps1') -Recurse -ErrorAction Stop)
56
foreach ($import in @($classes + $public + $private)) {
67
try {

PesterExplorer/Private/Get-ListPanel.ps1

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,15 @@ function Get-ListPanel {
2929
[array]
3030
$List,
3131
[string]
32-
$SelectedItem
32+
$SelectedItem,
33+
[string]$SelectedPane = "list"
3334
)
35+
$paneColor = if($SelectedPane -ne "list") {
36+
# If the selected pane is not preview, return an empty panel
37+
"blue"
38+
} else {
39+
"white"
40+
}
3441
$unselectedStyle = @{
3542
RootColor = [Spectre.Console.Color]::Grey
3643
SeparatorColor = [Spectre.Console.Color]::Grey
@@ -82,5 +89,5 @@ function Get-ListPanel {
8289
}
8390
$results |
8491
Format-SpectreRows |
85-
Format-SpectrePanel -Header "[white]List[/]" -Expand
92+
Format-SpectrePanel -Header "[white]List[/]" -Expand $paneColor
8693
}

PesterExplorer/Private/Get-PreviewPanel.ps1

Lines changed: 39 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -47,22 +47,41 @@ function Get-PreviewPanel {
4747
#>
4848
[CmdletBinding()]
4949
param (
50+
[Parameter(Mandatory)]
51+
[ValidateNotNullOrEmpty()]
5052
[hashtable]
5153
$Items,
54+
[Parameter(Mandatory)]
5255
[string]
53-
$SelectedItem
56+
$SelectedItem,
57+
[Parameter()]
58+
[int]
59+
$ScrollPosition = 0,
60+
[Parameter()]
61+
[ValidateNotNull()]
62+
$PreviewHeight,
63+
[Parameter()]
64+
[ValidateNotNull()]
65+
$PreviewWidth,
66+
[string]$SelectedPane = "list"
5467
)
68+
$paneColor = if($SelectedPane -ne "preview") {
69+
# If the selected pane is not preview, return an empty panel
70+
"blue"
71+
} else {
72+
"white"
73+
}
5574
if($SelectedItem -like "*..") {
5675
$formatSpectreAlignedSplat = @{
5776
HorizontalAlignment = 'Center'
5877
VerticalAlignment = 'Middle'
5978
}
6079
return "[grey]Please select an item.[/]" |
6180
Format-SpectreAligned @formatSpectreAlignedSplat |
62-
Format-SpectrePanel -Header "[white]Preview[/]" -Expand
81+
Format-SpectrePanel -Header "[white]Preview[/]" -Expand -Color $paneColor
6382
}
6483
$object = $Items.Item($SelectedItem)
65-
$result = @()
84+
$rows = @()
6685
# SelectedItem can be a few different types:
6786
# - A Pester object (Run, Container, Block, Test)
6887

@@ -134,17 +153,23 @@ function Get-PreviewPanel {
134153
$errorRecords += $_ |
135154
Format-SpectreException -ExceptionFormat ShortenEverything
136155
}
137-
$formatSpectrePanelSplat = @{
138-
Header = "Errors"
139-
Border = "Rounded"
140-
Color = "Red"
141-
}
142-
$result += $errorRecords |
143-
Format-SpectreRows |
144-
Format-SpectrePanel @formatSpectrePanelSplat
156+
$rows += $errorRecords | Format-SpectreRows | Format-SpectrePanel -Header "Errors" -Border "Rounded" -Color "Red"
157+
}
158+
159+
$formatSpectrePanelSplat = @{
160+
Header = "[white]Preview[/]"
161+
Width = $PreviewWidth
162+
Height = $PreviewHeight
163+
Color = $paneColor
164+
}
165+
166+
$formatScrollableSpectrePanelSplat = @{
167+
Height = $PreviewHeight
168+
Width = $PreviewWidth
169+
ScrollPosition = $ScrollPosition
170+
PanelSplat = $formatSpectrePanelSplat
171+
Data = $($rows | Format-SpectreRows)
145172
}
146173

147-
return $result |
148-
Format-SpectreGrid |
149-
Format-SpectrePanel -Header "[white]Preview[/]" -Expand
174+
return $(Format-ScrollableSpectrePanel @formatScrollableSpectrePanelSplat)
150175
}

PesterExplorer/Private/Get-ShortcutKeyPanel.ps1

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,12 @@ function Get-ShortcutKeyPanel {
2121
[CmdletBinding()]
2222
$shortcutKeys = @(
2323
"Up, Down - Navigate",
24+
"Home, End - Jump to Top/Bottom",
25+
"PageUp, PageDown - Scroll",
2426
"Enter - Explore",
27+
"Tab - Switch Panel",
2528
"Esc - Back",
2629
"Ctrl+C - Exit"
27-
# TODO: Add a key to jump to the test
2830
)
2931
$formatSpectreAlignedSplat = @{
3032
HorizontalAlignment = 'Center'

PesterExplorer/Public/Show-PesterResult.ps1

Lines changed: 76 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -76,53 +76,98 @@ function Show-PesterResult {
7676
$selectedItem = $list[0]
7777
$stack = [System.Collections.Stack]::new()
7878
$object = $PesterResult
79+
$selectedPane = 'list'
80+
$scrollPosition = 0
7981
#endregion Initial State
8082

8183
while ($true) {
84+
# Check the layout sizes
85+
$sizes = $layout | Get-SpectreLayoutSizes
86+
$previewHeight = $sizes["preview"].Height
87+
$previewWidth = $sizes["preview"].Width
88+
Write-Debug "Preview size: $previewWidth x $previewHeight"
89+
8290
# Handle input
8391
$lastKeyPressed = Get-LastKeyPressed
84-
# ToDo: Add support for scrolling the right panel
92+
# ToDo: Add vim navigation keys
8593
if ($null -ne $lastKeyPressed) {
86-
if ($lastKeyPressed.Key -eq "DownArrow") {
87-
$selectedItem = $list[($list.IndexOf($selectedItem) + 1) % $list.Count]
88-
} elseif ($lastKeyPressed.Key -eq "UpArrow") {
89-
$selectedItem = $list[($list.IndexOf($selectedItem) - 1 + $list.Count) % $list.Count]
90-
} elseif ($lastKeyPressed.Key -eq "Enter") {
91-
<# Recurse into Pester Object #>
92-
if($selectedItem -like '*..*') {
93-
# Move up one via selecting ..
94-
$object = $stack.Pop()
95-
Write-Debug "Popped item from stack: $($object.Name)"
96-
} else {
97-
Write-Debug "Pushing item into stack: $($items.Item($selectedItem).Name)"
98-
99-
$stack.Push($object)
100-
$object = $items.Item($selectedItem)
101-
if($object.GetType().Name -eq "Test") {
94+
#region List Navigation
95+
if($selectedPane -eq 'list') {
96+
if ($lastKeyPressed.Key -eq "DownArrow") {
97+
$selectedItem = $list[($list.IndexOf($selectedItem) + 1) % $list.Count]
98+
} elseif ($lastKeyPressed.Key -eq "UpArrow") {
99+
$selectedItem = $list[($list.IndexOf($selectedItem) - 1 + $list.Count) % $list.Count]
100+
} elseif ($lastKeyPressed.Key -eq "PageDown") {
101+
$currentIndex = $list.IndexOf($selectedItem)
102+
$newIndex = [Math]::Min($currentIndex + 10, $list.Count - 1)
103+
$selectedItem = $list[$newIndex]
104+
} elseif ($lastKeyPressed.Key -eq "PageUp") {
105+
$currentIndex = $list.IndexOf($selectedItem)
106+
$newIndex = [Math]::Max($currentIndex - 10, $list.Count - 1)
107+
$selectedItem = $list[$newIndex]
108+
} elseif ($lastKeyPressed.Key -eq "Home") {
109+
$selectedItem = $list[0]
110+
} elseif ($lastKeyPressed.Key -eq "End") {
111+
$selectedItem = $list[-1]
112+
} elseif ($lastKeyPressed.Key -in @("Tab", "RightArrow")) {
113+
$selectedPane = 'preview'
114+
} elseif ($lastKeyPressed.Key -eq "Enter") {
115+
<# Recurse into Pester Object #>
116+
if($items.Item($selectedItem).GetType().Name -eq "Test") {
102117
# This is a test. We don't want to go deeper.
118+
}
119+
if($selectedItem -like '*..*') {
120+
# Move up one via selecting ..
103121
$object = $stack.Pop()
122+
Write-Debug "Popped item from stack: $($object.Name)"
123+
} else {
124+
Write-Debug "Pushing item into stack: $($items.Item($selectedItem).Name)"
125+
$stack.Push($object)
126+
$object = $items.Item($selectedItem)
104127
}
128+
$items = Get-ListFromObject -Object $object
129+
$list = [array]$items.Keys
130+
$selectedItem = $list[0]
131+
$scrollPosition = 0
132+
} elseif ($lastKeyPressed.Key -eq "Escape") {
133+
# Move up via Esc key
134+
if($stack.Count -eq 0) {
135+
# This is the top level. Exit the loop.
136+
return
137+
}
138+
$object = $stack.Pop()
139+
$items = Get-ListFromObject -Object $object
140+
$list = [array]$items.Keys
141+
$selectedItem = $list[0]
142+
$scrollPosition = 0
105143
}
106-
$items = Get-ListFromObject -Object $object
107-
$list = [array]$items.Keys
108-
$selectedItem = $list[0]
109-
} elseif ($lastKeyPressed.Key -eq "Escape") {
110-
# Move up via Esc key
111-
if($stack.Count -eq 0) {
112-
# This is the top level. Exit the loop.
113-
return
144+
}
145+
else {
146+
#region Preview Navigation
147+
# ToDo: Add support for scrolling the right panel
148+
if ($lastKeyPressed.Key -in "Escape", "Tab", "LeftArrow", "RightArrow") {
149+
$selectedPane = 'list'
150+
} elseif ($lastKeyPressed.Key -eq "Down") {
151+
# Scroll down in the preview panel
152+
$scrollPosition = $ScrollPosition + 1
153+
} elseif ($lastKeyPressed.Key -eq "Up") {
154+
# Scroll up in the preview panel
155+
$scrollPosition = $ScrollPosition - 1
156+
} elseif ($lastKeyPressed.Key -eq "PageDown") {
157+
# Scroll down by a page in the preview panel
158+
$scrollPosition = $ScrollPosition + 10
159+
} elseif ($lastKeyPressed.Key -eq "PageUp") {
160+
# Scroll up by a page in the preview panel
161+
$scrollPosition = $ScrollPosition - 10
114162
}
115-
$object = $stack.Pop()
116-
$items = Get-ListFromObject -Object $object
117-
$list = [array]$items.Keys
118-
$selectedItem = $list[0]
163+
#endregion Preview Navigation
119164
}
120165
}
121166

122167
# Generate new data
123168
$titlePanel = Get-TitlePanel -Item $object
124-
$listPanel = Get-ListPanel -List $list -SelectedItem $selectedItem
125-
$previewPanel = Get-PreviewPanel -Items $items -SelectedItem $selectedItem
169+
$listPanel = Get-ListPanel -List $list -SelectedItem $selectedItem -SelectedPane $selectedPane
170+
$previewPanel = Get-PreviewPanel -Items $items -SelectedItem $selectedItem -ScrollPosition $scrollPosition -PreviewHeight $previewHeight -PreviewWidth $previewWidth -SelectedPane $selectedPane
126171

127172
# Update layout
128173
$layout["header"].Update($titlePanel) | Out-Null

0 commit comments

Comments
 (0)