-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathComponents.ps1
68 lines (62 loc) · 1.85 KB
/
Components.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
$PromptComponentInfo = @{
PromptComponents = @{}
ComponentOrder = @()
}
function Get-PromptComponent {
param (
[string]$Name
)
if ($Name.Length -gt 0) {
return $PromptComponentInfo.PromptComponents[$Name]
}
return $PromptComponentInfo.PromptComponents
}
function Get-PromptComponentOrder {
return $PromptComponentInfo.ComponentOrder
}
function Set-PromptComponentOrder {
param (
[parameter(Mandatory=$true)]
[AllowEmptyCollection()]
[string[]]$Order
)
if (($Order | Where-Object { -not $PromptComponentInfo.PromptComponents.ContainsKey($_) }).Count -gt 0) {
throw "One or more specified components do not exist."
}
$PromptComponentInfo.ComponentOrder = $Order
}
function Register-PromptComponent {
param (
[parameter(Mandatory=$true)]
[string]$Name,
[parameter(Mandatory=$true)]
[scriptblock]$Component
)
if ($PromptComponentInfo.PromptComponents.ContainsKey($Name)) {
throw "Component '$Name' already exists."
}
if ($Component.InvokeReturnAsIs().GetType().FullName -ne 'PromptSegment') {
throw "This component seems not working."
}
$PromptComponentInfo.PromptComponents[$Name] = $Component
}
function Unregister-PromptComponent {
param (
[parameter(Mandatory=$true)]
[string]$Name
)
if (-not $PromptComponentInfo.PromptComponents.ContainsKey($Name)) {
throw "Component '$Name' does not exist."
}
if ($PromptComponentInfo.ComponentOrder.Contains($Name)) {
throw "Component '$Name' is currently in use."
}
$PromptComponentInfo.PromptComponents.Remove($Name)
}
Export-ModuleMember -Function @(
'Get-PromptComponent',
'Get-PromptComponentOrder',
'Set-PromptComponentOrder',
'Register-PromptComponent',
'Unregister-PromptComponent'
)