@@ -49,8 +49,8 @@ const cli = createCLI({
4949 publish : { name : 'publish' , description : 'Submit a component to the community registry' } ,
5050 completion : {
5151 name : 'completion' ,
52- description : 'Print shell completion script (bash/zsh/fish)' ,
53- args : { shell : { description : 'Shell: bash | zsh | fish' , required : true } } ,
52+ description : 'Print shell completion script (bash/zsh/fish/powershell )' ,
53+ args : { shell : { description : 'Shell: bash | zsh | fish | powershell ' , required : true } } ,
5454 } ,
5555 } ,
5656} ) ;
@@ -134,11 +134,11 @@ async function main() {
134134
135135 case 'completion' : {
136136 const shell = cmdArgs [ 0 ] ;
137- if ( ! shell || ! [ 'bash' , 'zsh' , 'fish' ] . includes ( shell ) ) {
138- console . error ( `\x1b[31mError:\x1b[0m Usage: npx termui completion <bash|zsh|fish>` ) ;
137+ if ( ! shell || ! [ 'bash' , 'zsh' , 'fish' , 'powershell' ] . includes ( shell ) ) {
138+ console . error ( `\x1b[31mError:\x1b[0m Usage: npx termui completion <bash|zsh|fish|powershell >` ) ;
139139 process . exit ( 1 ) ;
140140 }
141- await runCompletion ( shell as 'bash' | 'zsh' | 'fish' ) ;
141+ await runCompletion ( shell as 'bash' | 'zsh' | 'fish' | 'powershell' ) ;
142142 break ;
143143 }
144144
@@ -252,7 +252,7 @@ async function interactiveMenu(): Promise<void> {
252252 }
253253}
254254
255- async function runCompletion ( shell : 'bash' | 'zsh' | 'fish' ) : Promise < void > {
255+ async function runCompletion ( shell : 'bash' | 'zsh' | 'fish' | 'powershell' ) : Promise < void > {
256256 const { getLocalRegistry } = await import ( './registry/client.js' ) ;
257257 const registry = getLocalRegistry ( ) ;
258258 const comps = Object . keys ( registry . components ) . join ( ' ' ) ;
@@ -261,7 +261,43 @@ async function runCompletion(shell: 'bash' | 'zsh' | 'fish'): Promise<void> {
261261 const CMDS =
262262 'init add list diff update theme preview dev docs create publish completion help --help --version' ;
263263
264- if ( shell === 'bash' ) {
264+ if ( shell === 'powershell' ) {
265+ const cmdList = CMDS . split ( ' ' ) . map ( ( c ) => `'${ c } '` ) . join ( ', ' ) ;
266+ const compList = comps . split ( ' ' ) . filter ( Boolean ) . map ( ( c ) => `'${ c } '` ) . join ( ', ' ) ;
267+ const themeList = THEMES . split ( ' ' ) . map ( ( t ) => `'${ t } '` ) . join ( ', ' ) ;
268+ process . stdout . write ( `# TermUI PowerShell completion
269+ # Add the following to your PowerShell profile ($PROFILE):
270+ # npx termui completion powershell | Out-String | Invoke-Expression
271+
272+ Register-ArgumentCompleter -Native -CommandName @('termui', 'npx') -ScriptBlock {
273+ param($wordToComplete, $commandAst, $cursorPosition)
274+ $cmds = @(${ cmdList } )
275+ $comps = @(${ compList } )
276+ $themes = @(${ themeList } )
277+ $words = $commandAst.CommandElements
278+ if ($words.Count -le 2) {
279+ $cmds | Where-Object { $_ -like "$wordToComplete*" } |
280+ ForEach-Object { [System.Management.Automation.CompletionResult]::new($_, $_, 'ParameterValue', $_) }
281+ return
282+ }
283+ $prev = $words[$words.Count - 2].ToString()
284+ switch ($prev) {
285+ { $_ -in 'add','update','diff' } {
286+ $comps | Where-Object { $_ -like "$wordToComplete*" } |
287+ ForEach-Object { [System.Management.Automation.CompletionResult]::new($_, $_, 'ParameterValue', $_) }
288+ }
289+ 'theme' {
290+ $themes | Where-Object { $_ -like "$wordToComplete*" } |
291+ ForEach-Object { [System.Management.Automation.CompletionResult]::new($_, $_, 'ParameterValue', $_) }
292+ }
293+ 'completion' {
294+ @('bash','zsh','fish','powershell') | Where-Object { $_ -like "$wordToComplete*" } |
295+ ForEach-Object { [System.Management.Automation.CompletionResult]::new($_, $_, 'ParameterValue', $_) }
296+ }
297+ }
298+ }
299+ ` ) ;
300+ } else if ( shell === 'bash' ) {
265301 process . stdout . write ( `# TermUI bash completion — source <(npx termui completion bash)
266302_termui_completions() {
267303 local cur=\${COMP_WORDS[COMP_CWORD]}
@@ -273,7 +309,7 @@ _termui_completions() {
273309 case "\${prev}" in
274310 add|update|diff) COMPREPLY=( $(compgen -W "${ comps } " -- "\${cur}") ); return ;;
275311 theme) COMPREPLY=( $(compgen -W "${ THEMES } " -- "\${cur}") ); return ;;
276- completion) COMPREPLY=( $(compgen -W "bash zsh fish" -- "\${cur}") ); return ;;
312+ completion) COMPREPLY=( $(compgen -W "bash zsh fish powershell " -- "\${cur}") ); return ;;
277313 esac
278314}
279315complete -F _termui_completions termui npx
@@ -290,7 +326,7 @@ _termui() {
290326 case $words[1] in
291327 add|update|diff) _arguments '*: :(${ comps } )' ;;
292328 theme) _arguments '*: :(${ THEMES } )' ;;
293- completion) _arguments '*: :(bash zsh fish)' ;;
329+ completion) _arguments '*: :(bash zsh fish powershell )' ;;
294330 esac
295331 ;;
296332 esac
0 commit comments