This repository has been archived by the owner on Dec 25, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 60
/
Copy pathTabExpansion.ps1
90 lines (76 loc) · 3.46 KB
/
TabExpansion.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# Check if function TabExpansion already exists and backup existing version to
# prevent breaking other TabExpansion implementations.
# Taken from posh-git https://github.com/dahlbyk/posh-git/blob/master/GitTabExpansion.ps1#L297
$tabExpansionBackup = 'PoshGVM_DefaultTabExpansion'
if (Test-Path Function:\TabExpansion) {
Rename-Item Function:\TabExpansion $tabExpansionBackup -ErrorAction SilentlyContinue
}
function TabExpansion($line, $lastWord) {
$lastBlock = [regex]::Split($line, '[|;]')[-1].TrimStart()
switch -regex ($lastBlock) {
# Execute gvm tab expansion for gvm command
'^gvm (.*)' { gvmTabExpansion($lastBlock) }
# Fall back on existing tab expansion
default { if (Test-Path Function:\$tabExpansionBackup) { & $tabExpansionBackup $line $lastWord } }
}
}
$Script:PGVM_TAB_COMMANDS = @('install','uninstall','rm','list','use','default','current','version','broadcast','help','offline','selfupdate','flush')
function gvmTabExpansion($lastBlock) {
if ( !($lastBlock -match '^gvm\s+(?<cmd>\S+)?(?<args> .*)?$') ) {
return
}
$command = $Matches['cmd']
$arguments = $Matches['args']
if ( !($arguments) ) {
# Try to complete the command
return $Script:PGVM_TAB_COMMANDS | Where-Object { $_.StartsWith($command) }
}
$arguments = $arguments.TrimStart()
# Help add correct parameters
switch -regex ($command) {
'^i(nstall)?' { gvmTabExpandion-Need-Candidate $command $arguments }
'^(uninstall|rm)'{ gvmTabExpandion-Need-Candidate $command $arguments }
'^(ls|list)' { gvmTabExpandion-Need-Candidate $command $arguments }
'^u(se)?' { gvmTabExpandion-Need-Candidate $command $arguments }
'^d(efault)?' { gvmTabExpandion-Need-Candidate $command $arguments }
'^c(urrent)?' { gvmTabExpandion-Need-Candidate $command $arguments }
'^offline' { gvmTabExpansion-Offline $arguments }
'^flush' { gvmTabExpansion-Flush $arguments }
default {}
}
}
function gvmTabExpandion-Need-Candidate($Command, $LastBlock) {
if ( !($LastBlock -match "^(?<candidate>\S+)?(?<args> .*)?$") ) {
return
}
$candidate = $Matches['candidate']
$arguments = $Matches['args']
Init-Candidate-Cache
if ( !($arguments) ) {
# Try to complete the command
return $Script:GVM_CANDIDATES | Where-Object { $_.StartsWith($candidate) }
}
if ( !($Script:GVM_CANDIDATES -contains $candidate) ) {
return
}
$arguments = $arguments.TrimStart()
# Help add correct parameters
switch -regex ($command) {
#'^i(nstall)?' { gvmTabExpandion-Need-Version $candidate $arguments }
'^(uninstall|rm)'{ gvmTabExpandion-Need-Version $candidate $arguments }
'^u(se)?' { gvmTabExpandion-Need-Version $candidate $arguments }
'^d(efault)?' { gvmTabExpandion-Need-Version $candidate $arguments }
default {}
}
}
function gvmTabExpandion-Need-Version($Candidate, $LastBlock) {
Get-Installed-Candidate-Version-List $Candidate | Where-Object { $_.StartsWith($LastBlock) }
}
function gvmTabExpansion-Offline($Arguments) {
@('enable','disable') | Where-Object { ([string]$_).StartsWith($Arguments) }
}
function gvmTabExpansion-Flush($Arguments) {
@('candidates','broadcast','archives','temp') | Where-Object { ([string]$_).StartsWith($Arguments) }
}
Export-ModuleMember TabExpansion
Export-ModuleMember gvmTabExpansion