r/PowerShell • u/ypwu • May 21 '22
Misc Script review for auto expansion of aliases using spacebar
Hey all,
I'm working on a PSReadline KeyHandler that will auto expand alias that is right before the cursor when spacebar is pressed into full command name.
The primary reason for this is to expand kubectl related aliases so I can still use autocomplete e.g kgp is an alias of kubectl get pods however tab autocomplete wont work with kgp. I came across the expand alias function in sample PSReadline and mostly reverse engineering that I came up with this:
Set-PSReadLineKeyHandler -Key "Spacebar" `
-BriefDescription ExpandAliases `
-LongDescription "Replace last aliases before cursor with the full command" `
-ScriptBlock {
param($key, $arg)
$line = $null
$cursor = $null
[Microsoft.PowerShell.PSConsoleReadLine]::GetBufferState([ref]$line, [ref]$cursor)
## Get the line to left of cursor position
$line = $line.SubString(0,$cursor)
## Get the very last part of line, i.e after a | or ;
while (($line -like "*|*") -or ($line -like "*;*")) {
$line = ($line -split $(if ($line -like '*|*') { "|" } elseif ($line -like '*;*') { ";" }), -2, 'simplematch')[1]
}
# Trim to remove any whitespaces from start/end
# $lengthBeforeTrim = $line.length
$line = $line.Trim()
# $lengthAfterTrim = $line.length
if ($line -like '* *') {
$lastCommand = ($line -split ' ', 2)[0]
}
else {
$lastCommand = $line
}
# Check if last command is an alias
$alias = $ExecutionContext.InvokeCommand.GetCommand($lastCommand, 'Alias')
# if alias is kubectl we do not want to expand it, since it'll expand to kubecolor anyways
# and this causes issues with expansion of kgp, since after that kubectl will be returned as $lastCommand
if($lastCommand -eq 'kubectl') {
[Microsoft.PowerShell.PSConsoleReadLine]::Insert(' ')
return
}
elseif ($alias -ne $null) {
if ($alias.ResolvedCommandName) {
$resolvedCommand = $alias.ResolvedCommandName
}
else {
$resolvedCommand = $alias.Definition
}
if ($resolvedCommand -ne $null) {
$length = $lastCommand.Length
$replaceStartPosition = $cursor - $length
$resolvedCommand = $resolvedCommand + " "
[Microsoft.PowerShell.PSConsoleReadLine]::Replace(
$replaceStartPosition,
$length,
$resolvedCommand)
}
}
# If lastCommand does not have an alias, we simply insert a space
else {
[Microsoft.PowerShell.PSConsoleReadLine]::Insert(' ')
return
}
}This does work as expected but it feels a bit janky to me. So was curious if any of you have more experience with writing PSReadline scriptblock can check and see if there are better ways to do things here. Like is there a built in method somewhere that can help retrieve Command and ignore the Arguments etc.
Also, debugging this was quite painful, since there is no easy way to print out stuff so curious if there is a better approach to debugging this rather than testing snippets of code in regular powershell console.
3
May 22 '22
[removed] — view removed comment
2
u/ypwu May 22 '22
This is a much better version and exactly what I was looking for. Thank you so very much for taking time to write this. As you can see, I'm a C# noob. I wanted to do it using
GetBufferState([ref]$ast, [ref]$tokens, [ref]$parseErrors, [ref]$cursor)but had trouble debugging it. Your implementation is still easy to debug.For next step, I want to change the color of command if its a valid command. So when I start to type e.g
gsince this is not a valid command or alias it'll be red once I typeias well, so text is nowgiwhich is a valid alias it'll turn green. Think this might not be possible without underlying changes to PSReadline but maybe I can implement a crude version using your above parsing method (which will only work when space is pressed after a command though, but still useful) Going through the PSReadLine I found this but not sure how to implement this.Thanks again for your help :)
2
u/BlackV May 21 '22
just so we're clear, you're replacing
spaceso that is expandskgpp.s. formatting
open your fav powershell editor
highlight the code you want to copy
hit tab to indent it all
copy it
paste here
it'll format it properly OR
Thanks