1- using module " ..\..\private\completions\Completers.psm1"
2- using module " ..\..\private\completions\Transformers.psm1"
3-
4- function Write-SpectreRule {
5- <#
6- . SYNOPSIS
7- Writes a Spectre horizontal-rule to the console.
8-
9- . DESCRIPTION
10- The Write-SpectreRule function writes a Spectre horizontal-rule to the console with the specified title, alignment, and color.
11-
12- . PARAMETER Title
13- The title of the rule.
14-
15- . PARAMETER Alignment
16- The alignment of the text in the rule. The default value is Left.
17-
18- . PARAMETER Color
19- The color of the rule. The default value is the accent color of the script.
20-
21- . PARAMETER PassThru
22- Returns the Spectre Rule object instead of writing it to the console.
23-
24- . EXAMPLE
25- # **Example 1**
26- # This example demonstrates how to write a rule to the console.
27- Write-SpectreRule -Title "My Rule" -Alignment Center -Color Yellow
28- #>
29- [CmdletBinding (HelpUri = ' https://pwshspectreconsole.com/reference/writing/write-spectrerule/' )]
30- [Reflection.AssemblyMetadata (" title" , " Write-SpectreRule" )]
31- param (
32- [string ] $Title ,
33- [ValidateSet ([SpectreConsoleJustify ], ErrorMessage = " Value '{0}' is invalid. Try one of: {1}" )]
34- [string ] $Alignment = " Left" ,
35- [ColorTransformationAttribute ()]
36- [ArgumentCompletionsSpectreColors ()]
37- [Spectre.Console.Color ] $Color = $script :AccentColor ,
38- [ColorTransformationAttribute ()]
39- [ArgumentCompletionsSpectreColors ()]
40- [Spectre.Console.Color ] $LineColor = $script :DefaultValueColor ,
41- [switch ] $PassThru
42- )
43- $rule = [Spectre.Console.Rule ]::new()
44- if ($Title ) {
45- $rule.Title = " [$ ( $Color.ToMarkup ()) ]$Title [/]"
46- }
47- $rule.Style = [Spectre.Console.Style ]::new($LineColor )
48- $rule.Justification = [Spectre.Console.Justify ]::$Alignment
49-
50- if ($PassThru ) {
51- return $rule
52- }
53-
54- Write-AnsiConsole $rule
1+ using module " ..\..\private\completions\Completers.psm1"
2+ using module " ..\..\private\completions\Transformers.psm1"
3+
4+ function Write-SpectreRule {
5+ <#
6+ . SYNOPSIS
7+ Writes a Spectre horizontal-rule to the console.
8+
9+ . DESCRIPTION
10+ The Write-SpectreRule function writes a Spectre horizontal-rule to the console with the specified title, alignment, and color.
11+ You can control the width of the rule by specifying either the Width or WidthPercent parameter.
12+
13+ . PARAMETER Title
14+ The title of the rule.
15+
16+ . PARAMETER Alignment
17+ The alignment of the text in the rule. The default value is Left.
18+
19+ . PARAMETER Color
20+ The color of the rule. The default value is the accent color of the script.
21+
22+ . PARAMETER LineColor
23+ The color of the rule's line. The default value is the default value color of the script.
24+
25+ . PARAMETER Width
26+ The width of the rule in characters. If not specified, the rule will span the full width of the console.
27+ Cannot be used together with WidthPercent.
28+
29+ . PARAMETER WidthPercent
30+ The width of the rule as a percentage of the console width. Value must be between 1 and 100.
31+ Cannot be used together with Width.
32+
33+ . PARAMETER PassThru
34+ Returns the Spectre Rule object instead of writing it to the console.
35+
36+ . EXAMPLE
37+ # **Example 1**
38+ # This example demonstrates how to write a rule to the console.
39+ Write-SpectreRule -Title "My Rule" -Alignment Center -Color Yellow
40+
41+ . EXAMPLE
42+ # **Example 2**
43+ # This example demonstrates how to write a rule with a specific width.
44+ Write-SpectreRule -Title "Fixed Width Rule" -Width 40
45+
46+ . EXAMPLE
47+ # **Example 3**
48+ # This example demonstrates how to write a rule with a width that's a percentage of the console width.
49+ Write-SpectreRule -Title "Half Width Rule" -WidthPercent 50 -Alignment Center
50+ #>
51+ [CmdletBinding (HelpUri = ' https://pwshspectreconsole.com/reference/writing/write-spectrerule/' , DefaultParameterSetName = ' Default' )]
52+ [Reflection.AssemblyMetadata (" title" , " Write-SpectreRule" )]
53+ param (
54+ [string ] $Title ,
55+ [ValidateSet ([SpectreConsoleJustify ], ErrorMessage = " Value '{0}' is invalid. Try one of: {1}" )]
56+ [string ] $Alignment = " Left" ,
57+ [ColorTransformationAttribute ()]
58+ [ArgumentCompletionsSpectreColors ()]
59+ [Spectre.Console.Color ] $Color = $script :AccentColor ,
60+ [ColorTransformationAttribute ()]
61+ [ArgumentCompletionsSpectreColors ()]
62+ [Spectre.Console.Color ] $LineColor = $script :DefaultValueColor ,
63+ [Parameter (ParameterSetName = ' FixedWidth' )]
64+ [ValidateRange (1 , [int ]::MaxValue)]
65+ [int ] $Width ,
66+ [Parameter (ParameterSetName = ' PercentWidth' )]
67+ [ValidateRange (1 , 100 )]
68+ [int ] $WidthPercent ,
69+ [switch ] $PassThru
70+ )
71+ $rule = [Spectre.Console.Rule ]::new()
72+ if ($Title ) {
73+ $rule.Title = " [$ ( $Color.ToMarkup ()) ]$Title [/]"
74+ }
75+ $rule.Style = [Spectre.Console.Style ]::new($LineColor )
76+ $rule.Justification = [Spectre.Console.Justify ]::$Alignment
77+
78+ if ($PassThru ) {
79+ return $rule
80+ }
81+
82+ # Handle width customization
83+ if ($PSCmdlet.ParameterSetName -eq ' FixedWidth' ) {
84+ Write-AnsiConsoleWithWidth - RenderableObject $rule - MaxWidth $Width | Out-Host
85+ }
86+ elseif ($PSCmdlet.ParameterSetName -eq ' PercentWidth' ) {
87+ $consoleWidth = [Console ]::WindowWidth
88+ $calculatedWidth = [Math ]::Floor($consoleWidth * ($WidthPercent / 100 ))
89+ # Ensure minimum width of 1
90+ $calculatedWidth = [Math ]::Max(1 , $calculatedWidth )
91+ Write-AnsiConsoleWithWidth - RenderableObject $rule - MaxWidth $calculatedWidth | Out-Host
92+ }
93+ else {
94+ Write-AnsiConsole - RenderableObject $rule
95+ }
5596}
0 commit comments