-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathposh-readline.ps1
408 lines (365 loc) · 17.1 KB
/
posh-readline.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
<#
.SYNOPSIS
PSReadLine & PSFzf Configuration File.
.DESCRIPTION
This script is automatically sourced by PowerShell and setup PSReadLine & PSFzf Settings
into current process.
.NOTES
!! This script is already included in my PowerShell profile (.ps1), not executed directly.
#>
#requires -Module PSReadline
#requires -Module PSFzf
#requires -Module CompletionPredictor
# fzf required
if (!(Get-Command fzf -ErrorAction SilentlyContinue)) { return }
# Catppuccin (syntax highlighting)
# -----------------------------------------------------------------
# Install Catppuccin PowerShell module if not already installed
if (-not (Get-Module -ListAvailable -Name Catppuccin -ErrorAction SilentlyContinue)) {
Write-Host "Installing PowerShell Module Catppuccin..." -ForegroundColor "Green"
git clone "https://github.com/catppuccin/powershell.git" "$env:USERPROFILE\Documents\PowerShell\Modules\Catppuccin"
}
# Import required Modules
Import-Module Catppuccin
$Flavor = $Catppuccin['Mocha']
#PSStyle
$PSStyle.Formatting.TableHeader = $Flavor.Mauve.Foreground()
$PSStyle.Formatting.CustomTableHeaderLabel = $Flavor.Mauve.Background() + $Flavor.Base.Foreground()
$PSStyle.Formatting.Debug = $Flavor.Peach.Foreground()
$PSStyle.Formatting.Verbose = $Flavor.Lavender.Foreground()
$PSStyle.Formatting.FeedbackText = $Flavor.Sky.Foreground()
$PSStyle.FileInfo.SymbolicLink = $Flavor.Blue.Foreground()
$PSStyle.Progress.Style = $Flavor.Blue.Background() + $Flavor.Base.Foreground()
# PSReadline
# ----------------------------------------------------------------
$PSReadLineOptions = @{
BellStyle = "None"
Colors = @{
Command = $Flavor.Blue.Foreground()
Comment = $Flavor.Overlay0.Foreground()
ContinuationPrompt = $Flavor.Teal.Foreground()
Default = $Flavor.Text.Foreground()
Emphasis = $Flavor.Lavender.Foreground()
Error = $Flavor.Red.Foreground()
InlinePrediction = $Flavor.Overlay0.Foreground()
Keyword = $Flavor.Mauve.Foreground()
ListPrediction = $Flavor.Overlay0.Foreground()
ListPredictionSelected = $Flavor.Surface0.Background() + $Flavor.Mauve.Foreground()
Member = $Flavor.Rosewater.Foreground()
Number = $Flavor.Peach.Foreground()
Operator = $Flavor.Yellow.Foreground()
Parameter = $Flavor.Pink.Foreground()
Selection = $Flavor.Surface0.Background()
String = $Flavor.Green.Foreground()
Type = $Flavor.Sky.Foreground()
Variable = $Flavor.Mauve.Foreground()
}
ExtraPromptLineCount = $True
HistoryNoDuplicates = $True
HistorySearchCursorMovesToEnd = $True
MaximumHistoryCount = 4096
PredictionSource = "HistoryAndPlugin"
PredictionViewStyle = "ListView"
PromptText = ''
ShowToolTips = $True
}
Set-PSReadLineOption @PSReadLineOptions
Set-PSReadLineKeyHandler -Key Tab -ScriptBlock { Invoke-FzfTabCompletion }
Set-PSReadLineKeyHandler -Key UpArrow -Function HistorySearchBackward
Set-PSReadLineKeyHandler -Key DownArrow -Function HistorySearchForward
# F1 for help on the command line
$parameters = @{
Key = "F1"
BriefDescription = "Command Help"
LongDescription = "Invoke the help window for the current command or prompt."
ScriptBlock = {
param($key, $arg)
$ast = $null
$tokens = $null
$errors = $null
$cursor = $null
[Microsoft.PowerShell.PSConsoleReadLine]::GetBufferState([ref]$ast, [ref]$tokens, [ref]$errors, [ref]$cursor)
$commandAst = $ast.FindAll( {
$node = $args[0]
$node -is [CommandAst] -and
$node.Extent.StartOffset -le $cursor -and
$node.Extent.EndOffset -ge $cursor
}, $true) | Select-Object -Last 1
if ($commandAst -ne $null) {
$commandName = $commandAst.GetCommandName()
if ($commandName -ne $null) {
$command = $ExecutionContext.InvokeCommand.GetCommand($commandName, 'All')
if ($command -is [AliasInfo]) {
$commandName = $command.ResolvedCommandName
}
if ($commandName -ne $null) {
Get-Help $commandName -ShowWindow
}
}
}
}
}
Set-PSReadLineKeyHandler @parameters
# Reference: copy from
# - https://ianmorozoff.com/2023/01/10/predictive-intellisense-on-by-default-in-powershell-7-3/#keybinding
$parameters1 = @{
Key = "F4"
BriefDescription = "Toggle PSReadLineOption PredictionSource"
LongDescription = "Toggle PSReadLineOption PredictionSource option between 'None' and 'HistoryAndPlugin'"
ScriptBlock = {
# Get the current state of PredictionSource
$state = (Get-PSReadLineOption).PredictionSource
# Toggle between None and HistoryAndPlugin
switch ($state) {
"None" { Set-PSReadLineOption -PredictionSource HistoryAndPlugin }
"History" { Set-PSReadLineOption -PredictionSource None }
"Plugin" { Set-PSReadLineOption -PredictionSource None }
"HistoryAndPlugin" { Set-PSReadLineOption -PredictionSource None }
Default { Write-Host "Current PSReadlineOption PredictionSource is Unknown" -ForegroundColor "Cyan" }
}
# Trigger autocomplete to appear without changing the line
# InvokePrompt() does not cause ListView style suggestions to disappear when toggling off
# [Microsoft.PowerShell.PSConsole.ReadLine]::InvokePrompt()
# Trigger autocomplete to appear or disappear while preserving the current input
[Microsoft.PowerShell.PSConsoleReadLine]::Insert(' ')
[Microsoft.PowerShell.PSConsoleReadLine]::BackwardDeleteChar()
}
}
Set-PSReadLineKeyHandler @parameters1
# This key handler shows the entire or filtered history using Out-GridView. The
# typed text is used as the substring pattern for filtering. A selected command
# is inserted to the command line without invoking. Multiple command selection
# is supported, e.g. selected by Ctrl + Click.
$parameters2 = @{
Key = "F7"
BriefDescription = "Toggle History"
LongDescription = "Show Command History"
ScriptBlock = {
$pattern = $null
[Microsoft.PowerShell.PSConsoleReadLine]::GetBufferState([ref]$pattern, [ref]$null)
if ($pattern) { $pattern = [regex]::Escape($pattern) }
$history = [System.Collections.ArrayList]@(
$last = ''
$lines = ''
foreach ($line in [System.IO.File]::ReadLines((Get-PSReadLineOption).HistorySavePath)) {
if ($line.EndsWith('`')) {
$line = $line.Substring(0, $line.Length - 1)
$lines = if ($lines) { "$lines`n$line" } else { $line }
continue
}
if ($lines) { $line = "$lines`n$line"; $lines = '' }
if (($line -cne $last) -and (!$pattern -or ($line -match $pattern))) { $last = $line; $line }
}
)
$history.Reverse()
$command = $history | Out-GridView -Title History -PassThru
if ($command) {
[Microsoft.PowerShell.PSConsoleReadLine]::RevertLine()
[Microsoft.PowerShell.PSConsoleReadLine]::Insert(($command -join "`n"))
}
}
}
Set-PSReadLineKeyHandler @parameters2
# Sometimes you want to get a property of invoke a member on what you've entered so far
# but you need parens to do that. This binding will help by putting parens around the current selection,
# or if nothing is selected, the whole line.
$parameters3 = @{
Key = 'Alt+('
BriefDescription = "Parenthesize Selection"
LongDescription = "Put parenthesis around the selection or entire line and move the cursor to after the closing parenthesis"
ScriptBlock = {
param($key, $arg)
$selectionStart = $null
$selectionLength = $null
[Microsoft.PowerShell.PSConsoleReadLine]::GetSelectionState([ref]$selectionStart, [ref]$selectionLength)
$line = $null
$cursor = $null
[Microsoft.PowerShell.PSConsoleReadLine]::GetBufferState([ref]$line, [ref]$cursor)
if ($selectionStart -ne 1) {
[Microsoft.PowerShell.PSConsoleReadLine]::Replace($selectionStart, $selectionLength, '(' + $line.SubString($selectionStart, $selectionLength) + ')')
[Microsoft.PowerShell.PSConsoleReadLine]::SetCursorPosition($selectionStart + $selectionLength + 2)
} else {
[Microsoft.PowerShell.PSConsoleReadLine]::Replace(0, $line.Length, '(' + $line + ')')
[Microsoft.PowerShell.PSConsoleReadLine]::EndOfLine()
}
}
}
Set-PSReadLineKeyHandler @parameters3
# Each time you press Alt+', this key handler will change the token
# under or before the cursor. It will cycle through single quotes, double quotes, or
# no quotes each time it is invoked.
$parameters4 = @{
Key = "Alt+'"
BriefDescription = "Toggle Quote Argument"
LongDescription = "Toggle quotes on the argument under the cursor"
ScriptBlock = {
param($key, $arg)
$ast = $null
$tokens = $null
$errors = $null
$cursor = $null
[Microsoft.PowerShell.PSConsoleReadLine]::GetBufferState([ref]$ast, [ref]$tokens, [ref]$errors, [ref]$cursor)
$tokenToChange = $null
foreach ($token in $tokens) {
$extent = $token.Extent
if ($extent.StartOffset -le $cursor -and $extent.EndOffset -ge $cursor) {
$tokenToChange = $token
# If the cursor is at the end (it's really 1 past the end) of the previous token,
# we only want to change the previous token if there is no token under the cursor
if ($extent.EndOffset -eq $cursor -and $foreach.MoveNext()) {
$nextToken = $foreach.Current
if ($nextToken.Extent.StartOffset -eq $cursor) {
$tokenToChange = $nextToken
}
}
break
}
}
if ($tokenToChange -ne $null) {
$extent = $tokenToChange.Extent
$tokenText = $extent.Text
if ($tokenText[0] -eq '"' -and $tokenText[-1] -eq '"') {
# Switch to no quotes
$replacement = $tokenText.Substring(1, $tokenText.Length - 2)
} elseif ($tokenText[0] -eq "'" -and $tokenText[-1] -eq "'") {
# Switch to double quotes
$replacement = '"' + $tokenText.Substring(1, $tokenText.Length - 2) + '"'
} else {
# Add single quotes
$replacement = "'" + $tokenText + "'"
}
[Microsoft.PowerShell.PSConsoleReadLine]::Replace(
$extent.StartOffset,
$tokenText.Length,
$replacement)
}
}
}
Set-PSReadLineKeyHandler @parameters4
# This example will replace any aliases on the command line with the resolved commands.
$parameters5 = @{
Key = "Alt+%"
BriefDescription = "Expand Aliases"
LongDescription = "Replace any aliases on the command line with the resolved commands"
ScriptBlock = {
param($key, $arg)
$ast = $null
$tokens = $null
$errors = $null
$cursor = $null
[Microsoft.PowerShell.PSConsoleReadLine]::GetBufferState([ref]$ast, [ref]$tokens, [ref]$errors, [ref]$cursor)
$startAdjustment = 0
foreach ($token in $tokens) {
if ($token.TokenFlags -band [TokenFlags]::CommandName) {
$alias = $ExecutionContext.InvokeCommand.GetCommand($token.Extent.Text, 'Alias')
if ($alias -ne $null) {
$resolvedCommand = $alias.ResolvedCommandName
if ($resolvedCommand -ne $null) {
$extent = $token.Extent
$length = $extent.EndOffset - $extent.StartOffset
[Microsoft.PowerShell.PSConsoleReadLine]::Replace(
$extent.StartOffset + $startAdjustment,
$length,
$resolvedCommand)
# Our copy of the tokens won't have been updated, so we need to
# adjust by the difference in length
$startAdjustment += ($resolvedCommand.Length - $length)
}
}
}
}
}
}
Set-PSReadLineKeyHandler @parameters5
# PSFzf
# ----------------------------------------------------------------
$ENV:FZF_DEFAULT_OPTS = @"
--color=bg+:$($Flavor.Surface0),bg:$($Flavor.Base),spinner:$($Flavor.Rosewater)
--color=hl:$($Flavor.Red),fg:$($Flavor.Text),header:$($Flavor.Red)
--color=info:$($Flavor.Mauve),pointer:$($Flavor.Rosewater),marker:$($Flavor.Rosewater)
--color=fg+:$($Flavor.Text),prompt:$($Flavor.Mauve),hl+:$($Flavor.Red)
--color=border:$($Flavor.Surface2)
--border="rounded" --border-label="" --preview-window="border-rounded" --padding="2"
--margin="3" --prompt="➽ " --marker="❯ " --pointer="✦ "
--separator="─" --scrollbar="❚" --layout="reverse" --info="right"
"@
$commandOverride = [ScriptBlock] { param($Location) Write-Host $Location }
$PSFzfOptions = @{
AltCCommand = $commandOverride
PSReadlineChordProvider = "Ctrl+t"
PSReadlineChordReverseHistory = "Ctrl+r"
GitKeyBindings = $True
TabExpansion = $True
EnableAliasFuzzyKillProcess = $True
}
Set-PsFzfOption @PSFzfOptions
# Instead of using default aliases of PSFzf Helper Functions, I preferred my own aliases
Set-Alias -Name 'fze' -Value 'Invoke-FuzzyEdit'
Set-Alias -Name 'fzg' -Value 'Invoke-FuzzyGitStatus'
Set-Alias -Name 'fzh' -Value 'Invoke-FuzzyHistory'
Set-Alias -Name 'fzd' -Value 'Invoke-FuzzySetLocation'
Set-Alias -Name 'fzs' -Value 'Invoke-FuzzyScoop'
# Helper functions for fzf
function _fzf_open_path {
param (
[Parameter(Mandatory = $True)]
[string]$InputPath
)
if ($InputPath -match "^.*:\d+:.*$") {
$InputPath = ($InputPath -split ":")[0]
}
if (-not (Test-Path $InputPath)) { Return }
$Cmds = @{
'bat' = { bat $InputPath }
'cat' = { Get-Content $InputPath }
'cd' = {
if (Test-Path $InputPath -PathType Leaf) { $InputPath = Split-Path $InputPath -Parent }
Set-Location $InputPath
}
'vim' = { vim $InputPath }
'code' = { code $InputPath }
'remove' = { Remove-Item -Recurse -Force $InputPath }
'echo' = { Write-Output $InputPath }
}
$Cmd = $Cmds.Keys | fzf --prompt 'Select Command> '
& $Cmds[$Cmd]
}
function _fzf_get_path_using_fd {
$InputPath = fd --type file --follow --hidden --exclude .git |
fzf --prompt 'Files> ' `
--header-first `
--header 'CTRL-T: Switch between Files/Directories' `
--bind 'ctrl-t:transform:if not "%FZF_PROMPT%"=="Files> " (echo ^change-prompt^(Files^> ^)^+^reload^(fd --type file^)) else (echo ^change-prompt^(Directory^> ^)^+^reload^(fd --type directory^))' `
--preview 'if "%FZF_PROMPT%"=="Files> " (bat --color=always {} --style=plain) else (eza -T --colour=always --icons=always {})'
return $InputPath
}
function _fzf_get_path_using_rg {
$INITIAL_QUERY = "${*:-}"
$RG_PREFIX = "rg --column --line-number --no-heading --color=always --smart-case"
$InputPath = "" |
fzf --ansi --disabled --query "$INITIAL_QUERY" `
--bind "start:reload:$RG_PREFIX {q}" `
--bind "change:reload:sleep 0.1 & $RG_PREFIX {q} || rem" `
--bind 'ctrl-t:transform:if not "%FZF_PROMPT%" == "1. ripgrep> " (echo ^rebind^(change^)^+^change-prompt^(1. ripgrep^> ^)^+^disable-search^+^transform-query:echo ^{q^} ^> %TEMP%\rg-fzf-f ^& type %TEMP%\rg-fzf-r) else (echo ^unbind^(change^)^+^change-prompt^(2. fzf^> ^)^+^enable-search^+^transform-query:echo ^{q^} ^> %TEMP%\rg-fzf-r ^& type %TEMP%\rg-fzf-f)' `
--color 'hl:-1:underline,hl+:-1:underline:reverse' `
--delimiter ':' `
--prompt '1. ripgrep> ' `
--preview-label 'Preview' `
--header 'CTRL-T: Switch between ripgrep/fzf' `
--header-first `
--preview 'bat --color=always {1} --highlight-line {2} --style=plain' `
--preview-window 'up,60%,border-bottom,+{2}+3/3'
return $InputPath
}
function fdz { _fzf_open_path $(_fzf_get_path_using_fd) }
function rgz { _fzf_open_path $(_fzf_get_path_using_rg) }
Set-PSReadLineKeyHandler -Key "Ctrl+f" -ScriptBlock {
[Microsoft.PowerShell.PSConsoleReadLine]::RevertLine()
[Microsoft.PowerShell.PSConsoleReadLine]::Insert("fdz")
[Microsoft.PowerShell.PSConsoleReadLine]::AcceptLine()
}
Set-PSReadLineKeyHandler -Key "Ctrl+g" -ScriptBlock {
[Microsoft.PowerShell.PSConsoleReadLine]::RevertLine()
[Microsoft.PowerShell.PSConsoleReadLine]::Insert("rgz")
[Microsoft.PowerShell.PSConsoleReadLine]::AcceptLine()
}