@@ -54,8 +54,6 @@ function Get-PreviewPanel {
5454 [Parameter (Mandatory )]
5555 [string ]
5656 $SelectedItem ,
57- [Parameter ()]
58- [int ]
5957 $ScrollPosition = 0 ,
6058 [Parameter ()]
6159 [ValidateNotNull ()]
@@ -65,6 +63,7 @@ function Get-PreviewPanel {
6563 $PreviewWidth ,
6664 [string ]$SelectedPane = " list"
6765 )
66+ Write-Debug " Get-PreviewPanel called with SelectedItem: $SelectedItem , ScrollPosition: $ScrollPosition "
6867 $paneColor = if ($SelectedPane -ne " preview" ) {
6968 # If the selected pane is not preview, return an empty panel
7069 " blue"
@@ -81,95 +80,139 @@ function Get-PreviewPanel {
8180 Format-SpectrePanel - Header " [white]Preview[/]" - Expand - Color $paneColor
8281 }
8382 $object = $Items.Item ($SelectedItem )
84- $rows = @ ()
83+ $results = @ ()
8584 # SelectedItem can be a few different types:
8685 # - A Pester object (Run, Container, Block, Test)
8786
8887 # region Breakdown
8988 # Skip if the object is null or they are all zero.
9089 if (
90+ $null -ne $object.PassedCount -and
91+ $null -ne $object.InconclusiveCount -and
92+ $null -ne $object.SkippedCount -and
93+ $null -ne $object.FailedCount -and
9194 (
92- $object.PassedCount +
93- $object.InconclusiveCount +
94- $object.SkippedCount +
95- $object.FailedCount
95+ [ int ] $object.PassedCount +
96+ [ int ] $object.InconclusiveCount +
97+ [ int ] $object.SkippedCount +
98+ [ int ] $object.FailedCount
9699 ) -gt 0
97100 ) {
101+ Write-Debug " Adding breakdown chart for $ ( $object.Name ) "
98102 $data = @ ()
99103 $data += New-SpectreChartItem - Label " Passed" - Value ($object.PassedCount ) - Color " Green"
100104 $data += New-SpectreChartItem - Label " Failed" - Value ($object.FailedCount ) - Color " Red"
101105 $data += New-SpectreChartItem - Label " Inconclusive" - Value ($object.InconclusiveCount ) - Color " Grey"
102106 $data += New-SpectreChartItem - Label " Skipped" - Value ($object.SkippedCount ) - Color " Yellow"
103- $result += Format-SpectreBreakdownChart - Data $data
107+ $results += Format-SpectreBreakdownChart - Data $data
104108 }
105109 # endregion Breakdown
106110
107111 # For Tests Let's print some more details
108112 if ($object.GetType ().Name -eq " Test" ) {
113+ Write-Debug " Selected item is a Test: $ ( $object.Name ) "
109114 $formatSpectrePanelSplat = @ {
110115 Header = " Test Result"
111116 Border = " Rounded"
112117 Color = " White"
113118 }
114- $result += $object.Result |
119+ $results += $object.Result |
115120 Format-SpectrePanel @formatSpectrePanelSplat
116121 # Show the code tested
117122 $formatSpectrePanelSplat = @ {
118123 Header = " Test Code"
119124 Border = " Rounded"
120125 Color = " White"
121126 }
122- $result += $object.ScriptBlock |
127+ $results += $object.ScriptBlock |
123128 Get-SpectreEscapedText |
124129 Format-SpectrePanel @formatSpectrePanelSplat
125130 } else {
131+ Write-Debug " Selected item is a Pester object: $ ( $object.Name ) "
126132 $data = Format-PesterTreeHash - Object $object
127133 Write-Debug $ ($data | ConvertTo-Json - Depth 10 )
128134 $formatSpectrePanelSplat = @ {
129135 Header = " Results"
130136 Border = " Rounded"
131137 Color = " White"
132138 }
133- $result += Format-SpectreTree - Data $data |
139+ $results += Format-SpectreTree - Data $data |
134140 Format-SpectrePanel @formatSpectrePanelSplat
135141 }
136142
137143 if ($null -ne $object.StandardOutput ){
144+ Write-Debug " Adding standard output for $ ( $object.Name ) "
138145 $formatSpectrePanelSplat = @ {
139146 Header = " Standard Output"
140147 Border = " Ascii"
141148 Color = " White"
142149 }
143- $result += $object.StandardOutput |
150+ $results += $object.StandardOutput |
144151 Get-SpectreEscapedText |
145152 Format-SpectrePanel @formatSpectrePanelSplat
146153
147154 }
148155
149156 # Print errors if they exist.
150157 if ($object.ErrorRecord.Count -gt 0 ) {
158+ Write-Debug " Adding error records for $ ( $object.Name ) "
151159 $errorRecords = @ ()
152160 $object.ErrorRecord | ForEach-Object {
153161 $errorRecords += $_ |
154162 Format-SpectreException - ExceptionFormat ShortenEverything
155163 }
156- $rows += $errorRecords | Format-SpectreRows | Format-SpectrePanel - Header " Errors" - Border " Rounded" - Color " Red"
164+ $results += $errorRecords | Format-SpectreRows | Format-SpectrePanel - Header " Errors" - Border " Rounded" - Color " Red"
157165 }
158166
159167 $formatSpectrePanelSplat = @ {
160168 Header = " [white]Preview[/]"
161- Width = $PreviewWidth
162- Height = $PreviewHeight
163169 Color = $paneColor
164- }
165-
166- $formatScrollableSpectrePanelSplat = @ {
167170 Height = $PreviewHeight
168171 Width = $PreviewWidth
169- ScrollPosition = $ScrollPosition
170- PanelSplat = $formatSpectrePanelSplat
171- Data = $ ($rows | Format-SpectreRows )
172+ Expand = $true
173+ }
174+
175+ if ($scrollPosition -ge $results.Count ) {
176+ # If the scroll position is greater than the number of items,
177+ # reset it to the last item
178+ Write-Debug " Resetting ScrollPosition to last item."
179+ $scrollPosition = $results.Count - 1
180+ }
181+ # If the scroll position is out of bounds, reset it
182+ if ($scrollPosition -lt 0 ) {
183+ Write-Debug " Resetting ScrollPosition to 0."
184+ $scrollPosition = 0
185+ }
186+
187+ if ($results.Count -eq 0 ) {
188+ # If there are no results, return an empty panel
189+ return " [grey]No results to display.[/]" |
190+ Format-SpectreAligned - HorizontalAlignment Center - VerticalAlignment Middle |
191+ Format-SpectrePanel @formatSpectrePanelSplat
192+ } else {
193+ Write-Debug " Reducing Preview List: $ ( $results.Count ) , ScrollPosition: $scrollPosition "
194+
195+ # Determine the height of each item in the results
196+ $totalHeight = 0
197+ $reducedList = @ ()
198+ if ($ScrollPosition -ne 0 ) {
199+ # If the scroll position is not zero, add a "back" item
200+ $reducedList += " [grey]...[/]"
201+ }
202+ for ($i = $scrollPosition ; $i -le $array.Count ; $i ++ ) {
203+ $itemHeight = Get-SpectreRenderableSize $results [$i ]
204+ $totalHeight += $itemHeight.Height
205+ if ($totalHeight -gt $PreviewHeight ) {
206+ # If the total height exceeds the preview height, stop adding items
207+ Write-Debug " Total height exceeded preview height. Stopping at item $i ."
208+ $reducedList += " [blue]...more. Switch to Panel and scroll with keys.[/]"
209+ break
210+ }
211+ $reducedList += $results [$i ]
212+ }
172213 }
173214
174- return $ (Format-ScrollableSpectrePanel @formatScrollableSpectrePanelSplat )
215+ return $reducedList | Format-SpectreRows |
216+ Format-SpectrePanel @formatSpectrePanelSplat
217+ # Format-ScrollableSpectrePanel @formatScrollableSpectrePanelSplat
175218}
0 commit comments