scripts: west_commands: Add powershell autocompletion (west -b)

Windows powershell can by default not autocomplete known boards when
tapping which can cause frustration when you can't quite remember the
exact name or you don't want to type it out.
west build .\path\to\application\ -p -b stm<tab> will search for
available board including 'stm' in their name.

Signed-off-by: Tais Hjortshøj <tbh@mjolner.dk>
This commit is contained in:
Tais Hjortshøj 2025-04-13 00:46:22 +02:00 committed by Benjamin Cabé
commit 0709014549
4 changed files with 82 additions and 2 deletions

View file

@ -53,6 +53,7 @@ West currently supports shell completion in the following shells:
* bash
* zsh
* fish
* powershell (board qualifiers only)
In order to enable shell completion, you will need to obtain the corresponding
completion script and have it sourced.
@ -102,5 +103,22 @@ Using the completion scripts:
west completion fish > $HOME/.config/fish/completions/west.fish
.. group-tab:: powershell
*One-time setup*:
.. code-block:: powershell
west completion powershell | Out-String | Invoke-Expression
*Permanent setup*:
.. code-block:: powershell
Set-ExecutionPolicy RemoteSigned -Scope CurrentUser
New-item -type file -force $PROFILE
west completion powershell > $HOME/west-completion.ps1
(Add-Content -Path $PROFILE -Value ". '{$HOME/west-completion.ps1}'")
.. _PyPI:
https://pypi.org/project/west/

View file

@ -40,6 +40,7 @@ It currently supports the following shells:
- bash
- zsh
- fish
- powershell (board qualifiers only)
Additional instructions are available in the command's help::

View file

@ -36,6 +36,14 @@ to stdout. Using the completion scripts:
# permanent
west completion fish > $HOME/.config/fish/completions/west.fish
powershell:
# one-time
west completion powershell | Out-String | Invoke-Expression
# permanent
Set-ExecutionPolicy RemoteSigned -Scope CurrentUser
New-item -type file -force $PROFILE
(Add-Content -Path $PROFILE -Value ". '{$HOME/west-completion.ps1}'")
positional arguments:
source_dir application source directory
cmake_opt extra options to pass to cmake; implies -c
@ -44,6 +52,12 @@ positional arguments:
class Completion(WestCommand):
_EXT_MAPPING = {
"bash": "bash",
"fish": "fish",
"powershell": "ps1",
"zsh": "zsh",
}
def __init__(self):
super().__init__(
@ -62,7 +76,7 @@ class Completion(WestCommand):
# Remember to update west-completion.bash if you add or remove
# flags
parser.add_argument('shell', nargs=1, choices=['bash', 'zsh', 'fish'],
parser.add_argument('shell', nargs=1, choices=self._EXT_MAPPING.keys(),
help='''Shell that which the completion
script is intended for.''')
return parser
@ -71,7 +85,7 @@ class Completion(WestCommand):
cf = os.path.join(os.path.dirname(os.path.realpath(__file__)),
*COMPLETION_REL_PATH.split('/'))
cf += '.' + args.shell[0]
cf += '.' + self._EXT_MAPPING[args.shell[0]]
try:
with open(cf, 'r') as f:

View file

@ -0,0 +1,47 @@
# Copyright © 2025, Tais Hjortshøj <tbh@mjolner.dk> / Mjølner Informatics A/S
# SPDX-License-Identifier: Apache-2.0
# region custom west board finder initialize
$s = {
param($wordToComplete, $commandAst, $cursorPosition)
function Get-MatchingBoards {
param($wordToComplete)
west boards | Out-String | ForEach-Object {
$_ -split '\r?\n' | Where-Object { $_ -CMatch "^$wordToComplete.*" } | Sort-Object
}
}
$commandDecider = (($commandAst -split ' ') | Select-Object -First 2 -ExpandProperty $_) -join ' '
if ($commandDecider -eq 'west build') {
$argDecider = (($commandAst -split ' ') | Select-Object -Last 2)
if ($argDecider -contains '-b' -or $argDecider -contains '--board') {
$boardsFound = Get-MatchingBoards -wordToComplete $wordToComplete
$output = $boardsFound
} else {
# Fallback to default behavior of suggesting files in the current directory
$output = (Get-NexusRepository).Name
}
} else {
# Fallback to default behavior of suggesting files in the current directory
$output = (Get-NexusRepository).Name
}
# Uncomment the following lines to log the output for debugging purposes
# @("wordToComplete: $wordToComplete",
# "commandAst: $commandAst",
# "cursorPosition: $cursorPosition",
# "commandDecider: $commandDecider",
# "argDecider: $argDecider",
# "",
# "boardsFound:",
# ($boardsFound | ForEach-Object { $_ -split ' '})
# ) | Set-Content log.txt
$output
}
Register-ArgumentCompleter -Native -CommandName west -ScriptBlock $s
echo "West completion tool loaded"
# endregion