zephyr/scripts/west_commands/completion/west-completion.ps1
Tais Hjortshøj 0709014549 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>
2025-05-02 01:16:00 +02:00

48 lines
1.7 KiB
PowerShell

# 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