-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSegments.ps1
54 lines (45 loc) · 1.52 KB
/
Segments.ps1
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
class TextSegment {
[string]$Text
[ConsoleColor]$Color
TextSegment([string]$Text, [ConsoleColor]$Color) {
$this.Text = $Text
$this.Color = $Color
}
}
class PromptSegment {
[TextSegment[]]$TextSegments = @()
[ConsoleColor]$BackgroundColor
[ConsoleColor]$ForegroundColor
PromptSegment([ConsoleColor]$BackgroundColor, [ConsoleColor]$ForegroundColor) {
$this.BackgroundColor = $BackgroundColor
$this.ForegroundColor = $ForegroundColor
}
[void]Append([string]$Text) {
$this.TextSegments += [TextSegment]::new($Text, $this.ForegroundColor)
}
[void]Append([string]$Text, [ConsoleColor]$TextColor) {
$this.TextSegments += [TextSegment]::new($Text, $TextColor)
}
[bool]IsEmpty() {
return $this.TextSegments.Count -eq 0
}
[void]Render() {
if (-not $this.IsEmpty()) {
Write-Host ' ' -NoNewline -BackgroundColor $this.BackgroundColor
$this.TextSegments | ForEach-Object {
Write-Host $_.Text -NoNewline -BackgroundColor $this.BackgroundColor -ForegroundColor $_.Color
}
Write-Host ' ' -NoNewline -BackgroundColor $this.BackgroundColor
}
}
}
function New-PromptSegment {
param (
[parameter(Mandatory=$true)][ConsoleColor]$BackgroundColor,
[parameter(Mandatory=$true)][ConsoleColor]$ForegroundColor
)
return [PromptSegment]::new($BackgroundColor, $ForegroundColor)
}
Export-ModuleMember -Function @(
'New-PromptSegment'
)