Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions PesterExplorer/Private/Get-ListPanel.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ function Get-ListPanel {
$List,
[string]
$SelectedItem,
[string]$SelectedPane = "list"
[string]$SelectedPane = "list",
[int]$ScrollOffset = 0,
[int]$ListHeight = 0
)
$paneColor = if($SelectedPane -ne "list") {
# If the selected pane is not preview, return an empty panel
Expand All @@ -44,7 +46,19 @@ function Get-ListPanel {
StemColor = [Spectre.Console.Color]::Grey
LeafColor = [Spectre.Console.Color]::White
}
$results = $List | ForEach-Object {
# Calculate the visible window. Panel borders and potential ellipsis rows consume 4 rows.
$visibleCount = if ($ListHeight -gt 4) { $ListHeight - 4 } else { $List.Count }
$startIndex = [Math]::Max(0, [Math]::Min($ScrollOffset, $List.Count - 1))
$endIndex = [Math]::Min($startIndex + $visibleCount - 1, $List.Count - 1)
$showEllipsisTop = $startIndex -gt 0
$showEllipsisBottom = $endIndex -lt ($List.Count - 1)
$visibleList = if ($List.Count -gt 0) { @($List[$startIndex..$endIndex]) } else { @() }

$results = @()
if ($showEllipsisTop) {
$results += Write-SpectreHost "[grey]...[/]" -PassThru | Format-SpectrePadded -Padding 0
}
$results += $visibleList | ForEach-Object {
$name = $_
if($name -eq '..') {
# This is a parent item, so we show it as a folder
Expand Down Expand Up @@ -87,6 +101,9 @@ function Get-ListPanel {
}
}
}
if ($showEllipsisBottom) {
$results += Write-SpectreHost "[grey]...[/]" -PassThru | Format-SpectrePadded -Padding 0
}
$results |
Format-SpectreRows |
Format-SpectrePanel -Header "[white]List[/]" -Expand -Color $paneColor
Expand Down
17 changes: 16 additions & 1 deletion PesterExplorer/Public/Show-PesterResult.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,15 @@ function Show-PesterResult {
$object = $PesterResult
$selectedPane = 'list'
$scrollPosition = 0
$listScrollOffset = 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"

# Handle input
Expand All @@ -106,7 +108,7 @@ function Show-PesterResult {
$scrollPosition = 0
} elseif ($lastKeyPressed.Key -eq "PageUp") {
$currentIndex = $list.IndexOf($selectedItem)
$newIndex = [Math]::Max($currentIndex - 10, $list.Count - 1)
$newIndex = [Math]::Max($currentIndex - 10, 0)
$selectedItem = $list[$newIndex]
$scrollPosition = 0
} elseif ($lastKeyPressed.Key -eq "Home") {
Expand Down Expand Up @@ -135,6 +137,7 @@ function Show-PesterResult {
$list = [array]$items.Keys
$selectedItem = $list[0]
$scrollPosition = 0
$listScrollOffset = 0
} elseif ($lastKeyPressed.Key -eq "Escape") {
# Move up via Esc key
if($stack.Count -eq 0) {
Expand All @@ -146,7 +149,17 @@ function Show-PesterResult {
$list = [array]$items.Keys
$selectedItem = $list[0]
$scrollPosition = 0
$listScrollOffset = 0
}
# Ensure the selected item is always within the visible viewport
$selectedIndex = $list.IndexOf($selectedItem)
$visibleCount = if ($listHeight -gt 4) { $listHeight - 4 } else { $list.Count }
if ($selectedIndex -lt $listScrollOffset) {
$listScrollOffset = $selectedIndex
} elseif ($selectedIndex -ge ($listScrollOffset + $visibleCount)) {
$listScrollOffset = $selectedIndex - $visibleCount + 1
}
if ($listScrollOffset -lt 0) { $listScrollOffset = 0 }
}
else {
#region Preview Navigation
Expand Down Expand Up @@ -175,6 +188,8 @@ function Show-PesterResult {
List = $list
SelectedItem = $selectedItem
SelectedPane = $selectedPane
ScrollOffset = $listScrollOffset
ListHeight = $listHeight
}
$listPanel = Get-ListPanel @getListPanelSplat

Expand Down
113 changes: 113 additions & 0 deletions tests/Get-ListPanel.tests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
Describe 'Get-ListPanel' {
BeforeAll {
. (Join-Path $PSScriptRoot 'Helpers.ps1')
$manifest = Import-PowerShellDataFile -Path $env:BHPSModuleManifest

Check warning on line 4 in tests/Get-ListPanel.tests.ps1

View workflow job for this annotation

GitHub Actions / ci / Run Linters

Unknown word (BHPS) Suggestions: (baps, bops, bhp, BHP, bps)
$outputDir = Join-Path -Path $env:BHProjectPath -ChildPath 'Output'
$outputModDir = Join-Path -Path $outputDir -ChildPath $env:BHProjectName
$outputModVerDir = Join-Path -Path $outputModDir -ChildPath $manifest.ModuleVersion
$outputModVerManifest = Join-Path -Path $outputModVerDir -ChildPath "$($env:BHProjectName).psd1"

# Remove all versions of the module from the session. Pester can't handle multiple versions.
Get-Module $env:BHProjectName | Remove-Module -Force -ErrorAction Ignore
Import-Module -Name $outputModVerManifest -Verbose:$false -ErrorAction Stop

InModuleScope $env:BHProjectName {
$script:tenItems = @('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j')

# Viewport scrolling panel: ScrollOffset=2, ListHeight=8
# visibleCount = 8-4 = 4; items c,d,e,f visible; top and bottom ellipsis shown
$script:scrolledPanel = Get-ListPanel `
-List $script:tenItems `
-SelectedItem 'c' `
-ScrollOffset 2 `
-ListHeight 8
$script:scrolledText = global:Get-RenderedText -Panel $script:scrolledPanel

# Bottom-of-list panel: ScrollOffset=6, ListHeight=20
# visibleCount = 16; items g,h,i,j visible; top ellipsis only
$script:bottomPanel = Get-ListPanel `
-List $script:tenItems `
-SelectedItem 'j' `
-ScrollOffset 6 `
-ListHeight 20
$script:bottomText = global:Get-RenderedText -Panel $script:bottomPanel
}
}

It 'should return a Spectre.Console.Panel object' {
InModuleScope $env:BHProjectName {
$panel = Get-ListPanel -List @('item1', 'item2') -SelectedItem 'item1'
$panel.GetType().ToString() | Should -BeExactly 'Spectre.Console.Panel'
}
}

It 'should show all items when list fits within available height' {
InModuleScope $env:BHProjectName {
$panel = Get-ListPanel -List $script:tenItems -SelectedItem 'a' -ListHeight 20
$rendered = global:Get-RenderedText -Panel $panel
foreach ($item in $script:tenItems) {
$rendered | Should -BeLike "*$item*"
}
}
}

It 'should show all items when ListHeight is 0 (default, no constraint)' {
InModuleScope $env:BHProjectName {
$panel = Get-ListPanel -List $script:tenItems -SelectedItem 'a'
$rendered = global:Get-RenderedText -Panel $panel
foreach ($item in $script:tenItems) {
$rendered | Should -BeLike "*$item*"
}
}
}

It 'should show only items within the visible window when scrolled' {
InModuleScope $env:BHProjectName {
$script:scrolledText | Should -BeLike '*c*'
$script:scrolledText | Should -BeLike '*d*'
$script:scrolledText | Should -BeLike '*e*'
$script:scrolledText | Should -BeLike '*f*'
}
}

It 'should show top ellipsis when items are scrolled above the window' {
InModuleScope $env:BHProjectName {
$script:scrolledText | Should -BeLike '*...*'
}
}

It 'should show bottom ellipsis when items are below the window' {
InModuleScope $env:BHProjectName {
# Both top and bottom ellipsis are present since items a-b are above and g-j below
$script:scrolledText | Should -BeLike '*...*'
}
}

It 'should show visible items when scrolled to the bottom' {
InModuleScope $env:BHProjectName {
$script:bottomText | Should -BeLike '*g*'
$script:bottomText | Should -BeLike '*j*'
}
}

It 'should show top ellipsis when scrolled to the bottom' {
InModuleScope $env:BHProjectName {
$script:bottomText | Should -BeLike '*...*'
}
}

It 'should highlight the selected item in the rendered output' {
InModuleScope $env:BHProjectName {
$panel = Get-ListPanel -List @('alpha', 'beta', 'gamma') -SelectedItem 'beta'
$rendered = global:Get-RenderedText -Panel $panel
$rendered | Should -BeLike '*beta*'
}
}

It 'should return a Panel when list pane is not selected' {
InModuleScope $env:BHProjectName {
$panel = Get-ListPanel -List @('item1') -SelectedItem 'item1' -SelectedPane 'preview'
$panel.GetType().ToString() | Should -BeExactly 'Spectre.Console.Panel'
}
}
}
2 changes: 1 addition & 1 deletion tests/Help.tests.ps1
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Taken with love from @juneb_get_help (https://raw.githubusercontent.com/juneb/PesterTDD/master/Module.Help.Tests.ps1)

Check warning on line 1 in tests/Help.tests.ps1

View workflow job for this annotation

GitHub Actions / ci / Run Linters

Unknown word (juneb) Suggestions: (june, junes, June, Junes, jube)

BeforeDiscovery {

Expand All @@ -9,7 +9,7 @@
$params | Where-Object { $_.Name -notin $commonParameters } | Sort-Object -Property Name -Unique
}

$manifest = Import-PowerShellDataFile -Path $env:BHPSModuleManifest

Check warning on line 12 in tests/Help.tests.ps1

View workflow job for this annotation

GitHub Actions / ci / Run Linters

Unknown word (BHPS) Suggestions: (baps, bops, bhp, BHP, bps)
$outputDir = Join-Path -Path $env:BHProjectPath -ChildPath 'Output'
$outputModDir = Join-Path -Path $outputDir -ChildPath $env:BHProjectName
$outputModVerDir = Join-Path -Path $outputModDir -ChildPath $manifest.ModuleVersion
Expand Down Expand Up @@ -108,7 +108,7 @@

# Shouldn't find extra parameters in help.
It "finds help parameter in code: <_>" {
$_ -in $parameterNames | Should -Be $true
$_ -in $commandParameterNames | Should -Be $true
}
}
}
Loading