-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathHelpers.ps1
More file actions
70 lines (62 loc) · 2.51 KB
/
Helpers.ps1
File metadata and controls
70 lines (62 loc) · 2.51 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
function global:Get-RenderedText {
<#
.SYNOPSIS
Returns the rendered text from a panel object.
.DESCRIPTION
This function processes a panel object to extract and return the rendered
text. It filters out control codes and specific characters, joining the
remaining text segments into a single string.
.PARAMETER Panel
The panel object to be processed. It should have a Render method that
returns a collection of text segments.
.PARAMETER RenderOptions
Options to control the rendering of the panel. This is passed to the Render method of the panel.
.PARAMETER ContainerWidth
The width of the container in which the panel is rendered. This is also passed to the Render method of the panel.
.EXAMPLE
$panel = Get-PanelObject -Name "ExamplePanel"
$renderOptions = Get-RenderOptions -SomeOption "Value"
$containerWidth = 80
$renderedText = global:Get-RenderedText -panel $panel -renderOptions $renderOptions -containerWidth $containerWidth
This example retrieves a panel object, specifies rendering options and
container width, and then calls the function to get the rendered text.
.NOTES
This is a helper function we can use for our tests.
#>
param (
[Parameter(Mandatory = $true)]
#[Spectre.Console.Panel]
$Panel,
[Parameter()]
[int]
$ContainerHeight = 200,
[Parameter()]
[int]
$ContainerWidth = 100
)
$size = [Spectre.Console.Size]::new($ContainerWidth, $ContainerHeight)
$renderOptions = [Spectre.Console.Rendering.RenderOptions]::new(
[Spectre.Console.AnsiConsole]::Console.Profile.Capabilities,
$size
)
$render = $Panel.Render($RenderOptions, $ContainerWidth)
# These are rendered segments.
$onlyText = $render |
Where-Object {
#$_.IsLineBreak -ne $true -and
$_.IsControlCode -ne $true -and
#$_.IsWhiteSpace -ne $true -and
$_.Text -notin @('┌', '┐', '└', '┘', '─', '│') -and
$_.Text -notmatch '─{2,}'
}
# Join the text segments into a single string
$output = [System.Text.StringBuilder]::new()
foreach ($textSegment in $onlyText) {
if ($textSegment.IsLineBreak) {
[void]$output.AppendLine() # Append a newline for line breaks
} else {
[void]$output.Append($textSegment.Text)
}
}
return $output.ToString().Trim()
}