-
Notifications
You must be signed in to change notification settings - Fork 5
/
PSReadLine_FuzzyCommand.ps1
103 lines (93 loc) · 3.49 KB
/
PSReadLine_FuzzyCommand.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
91
92
93
94
95
96
97
98
99
100
101
102
103
function Find-CurrentCommand {
param([string]$line,[int]$cursor,[ref]$leftCursor,[ref]$rightCursor)
if ($line.Length -eq 0) {
$leftCursor.Value = $rightCursor.Value = 0
return $null
}
if ($cursor -ge $line.Length) {
$leftCursorTmp = $cursor - 1
} else {
$leftCursorTmp = $cursor
}
:leftSearch for (;$leftCursorTmp -ge 0;$leftCursorTmp--) {
if ([string]::IsNullOrWhiteSpace($line[$leftCursorTmp])) {
if (($leftCursorTmp -lt $cursor) -and ($leftCursorTmp -lt $line.Length-1)) {
$leftCursorTmpQuote = $leftCursorTmp - 1
$leftCursorTmp = $leftCursorTmp + 1
} else {
$leftCursorTmpQuote = $leftCursorTmp
}
for (;$leftCursorTmpQuote -ge 0;$leftCursorTmpQuote--) {
if (($line[$leftCursorTmpQuote] -eq '"') -and (($leftCursorTmpQuote -le 0) -or ($line[$leftCursorTmpQuote-1] -ne '"'))) {
$leftCursorTmp = $leftCursorTmpQuote
break leftSearch
}
elseif (($line[$leftCursorTmpQuote] -eq "'") -and (($leftCursorTmpQuote -le 0) -or ($line[$leftCursorTmpQuote-1] -ne "'"))) {
$leftCursorTmp = $leftCursorTmpQuote
break leftSearch
}
}
break leftSearch
}
}
:rightSearch for ($rightCursorTmp = $cursor;$rightCursorTmp -lt $line.Length;$rightCursorTmp++) {
if ([string]::IsNullOrWhiteSpace($line[$rightCursorTmp])) {
if ($rightCursorTmp -gt $cursor) {
$rightCursorTmp = $rightCursorTmp - 1
}
for ($rightCursorTmpQuote = $rightCursorTmp+1;$rightCursorTmpQuote -lt $line.Length;$rightCursorTmpQuote++) {
if (($line[$rightCursorTmpQuote] -eq '"') -and (($rightCursorTmpQuote -gt $line.Length) -or ($line[$rightCursorTmpQuote+1] -ne '"'))) {
$rightCursorTmp = $rightCursorTmpQuote
break rightSearch
}
elseif (($line[$rightCursorTmpQuote] -eq "'") -and (($rightCursorTmpQuote -gt $line.Length) -or ($line[$rightCursorTmpQuote+1] -ne "'"))) {
$rightCursorTmp = $rightCursorTmpQuote
break rightSearch
}
}
break rightSearch
}
}
if ($leftCursorTmp -lt 0 -or $leftCursorTmp -gt $line.Length-1) { $leftCursorTmp = 0}
if ($rightCursorTmp -ge $line.Length) { $rightCursorTmp = $line.Length-1 }
$leftCursor.Value = $leftCursorTmp
$rightCursor.Value = $rightCursorTmp
$str = -join ($line[$leftCursorTmp..$rightCursorTmp])
return $str.Trim("'").Trim('"')
}
function Invoke-FuzzyCommandPsReadlineHandler {
$leftCursor = $null
$rightCursor = $null
$line = $null
$cursor = $null
[Microsoft.PowerShell.PSConsoleReadline]::GetBufferState([ref]$line, [ref]$cursor)
$currentCommand = Find-CurrentCommand $line $cursor ([ref]$leftCursor) ([ref]$rightCursor)
$commands = Select-FuzzyCommand $currentCommand
if ($commands -ne $null) {
$command = $commands | Out-GridView -Title Commands -PassThru
if ($command) {
[Microsoft.PowerShell.PSConsoleReadLine]::RevertLine()
[Microsoft.PowerShell.PSConsoleReadLine]::Insert($command)
}
}
}
# install PSReadline shortcut:
if (Get-Module -ListAvailable -Name PSReadline) {
if ($args.Length -ge 1) {
$script:PSReadlineHandlerChord = $args[0]
}
else {
$script:PSReadlineHandlerChord = 'Ctrl+P'
}
if (Get-PSReadlineKeyHandler -Bound | Where Key -eq $script:PSReadlineHandlerChord) {
Write-Warning ("PSReadline chord {0} already in use - keyboard handler not installed" -f $script:PSReadlineHandlerChord)
}
else {
Set-PSReadlineKeyHandler -Key Ctrl+P -BriefDescription "SelectFuzzyCommand" -ScriptBlock {
Invoke-FuzzyCommandPsReadlineHandler
}
}
}
else {
Write-Warning "PSReadline module not found - keyboard handler not installed"
}