1414installed via the published tar balls in the GitHub repo.
1515
1616~~~ bash
17- pip install https://github.com/britive/python-cli/releases/download/v0.1.1 /pybritive-0.1.1 .tar.gz
17+ pip install https://github.com/britive/python-cli/releases/download/v0.1.2 /pybritive-0.1.2 .tar.gz
1818~~~
1919
2020The end user is free to install the CLI into a virtual environment or in the global scope, so it is available
@@ -82,13 +82,19 @@ the end user wants to persist the `.britive` directory. Note that `.britive` wil
8282that as part of the path.
8383
8484## Shell Completion
85+
86+ TODO: Provide more automated scripts here to automatically add the required configs to the profiles. For now the below works just fine though.
87+
8588Behind the scenes the ` pybritive ` CLI tool uses the python ` click ` package. ` click ` offers shell completion for
8689the following shells.
8790
8891* Bash
8992* Zsh
9093* Fish
91- * PowerShell (work in progress for the ` pybritive ` cli - NOT WORKING YET)
94+
95+ A shell completion script has been written for the following shells as well.
96+
97+ * PowerShell
9298
9399In order to set up shell completion, follow these steps. Once complete either ` source ` your environment again
94100or start a new shell in order for the changes to be loaded.
@@ -130,26 +136,40 @@ _PYBRITIVE_COMPLETE=fish_source pybritive > ~/.config/fish/completions/foo-bar.f
130136Append the below code to your PowerShell profile.
131137
132138~~~
133- if ((Test-Path Function:\TabExpansion) -and -not (Test-Path Function:\pybritiveTabExpansionBackup)) {
134- Rename-Item Function:\TabExpansion pybritiveTabExpansionBackup
135- }
136-
137- function TabExpansion($line, $lastWord) {
138- $lastBlock = [regex]::Split($line, '[|;]')[-1].TrimStart()
139- $aliases = @("pybritive") + @(Get-Alias | where { $_.Definition -eq "pybritive" } | select -Exp Name)
140- $aliasPattern = "($($aliases -join '|'))"
141- if($lastBlock -match "^$aliasPattern ") {
142- $Env:_PYBRITIVE_COMPLETE = "complete-powershell"
143- $Env:COMMANDLINE = "$lastBlock"
144- (pybritive) | ? {$_.trim() -ne "" }
145- Remove-Item Env:_PYBRITIVE_COMPLETE
146- Remove-Item Env:COMMANDLINE
147- }
148- elseif (Test-Path Function:\pybritiveTabExpansionBackup) {
149- # Fall back on existing tab expansion
150- pybritiveTabExpansionBackup $line $lastWord
139+ $pybritive_completion = {
140+ param($wordToComplete, $commandAst, $cursorPosition)
141+
142+ # in case of scripts, this object holds the current line after string conversion
143+ $line = "$commandAst"
144+
145+ # The behaviour of completion should depend on the trailing spaces in the current line:
146+ # * "command subcommand " --> TAB --> Completion items parameters/sub-subcommands of "subcommand"
147+ # * "command subcom" --> TAB --> Completion items to extend "subcom" into matching subcommands.
148+ # $line never contains the trailing spaces. However, $cursorPosition is the length of the original
149+ # line (with trailing spaces) in this case. This comparison allows the expected user experience.
150+ if ($cursorPosition -gt $line.Length) {
151+ $line = "$line "
151152 }
153+
154+ # set environment variables that pybritive completion will use
155+ New-Item -Path Env: -Name COMP_LINE -Value $line | Out-Null # Current line
156+ New-Item -Path Env: -Name _PYBRITIVE_COMPLETE -Value "powershell_complete" | Out-Null
157+
158+ # call pybritive and it will inspect env vars and provide completion results
159+ Invoke-Expression pybritive -ErrorAction SilentlyContinue | Tee-Object -Var completionResult | Out-Null
160+
161+ # cleanup environment variables
162+ Remove-Item Env:\COMP_LINE | Out-Null
163+ Remove-Item Env:\_PYBRITIVE_COMPLETE | Out-Null
164+
165+ # get list of completion items
166+ $items = $completionResult -split '\r?\n'
167+
168+ $items | ForEach-Object {"$_ "} # trailing space important as completion is "done"
152169}
170+
171+ # register tab completion
172+ Register-ArgumentCompleter -Native -CommandName pybritive -ScriptBlock $pybritive_completion
153173~~~
154174
155175
@@ -160,6 +180,8 @@ echo $profile
160180~~~
161181
162182And is generally something like ` C:\Users\{user}\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1 ` .
183+ Create the file (and any needed directories) if needed.
184+
163185
164186### Shell Completion - Profiles - Local Cache
165187
0 commit comments