Skip to content

Commit 84510a1

Browse files
committed
first cut at getting powershell working #156
1 parent 67bc455 commit 84510a1

File tree

3 files changed

+25
-9
lines changed

3 files changed

+25
-9
lines changed

django_typer/management/commands/shellcompletion.py

+1
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@ def source_vars(self) -> t.Dict[str, t.Any]:
180180
if self.command.fallback
181181
else "",
182182
"is_installed": not isinstance(self.command.manage_script, Path),
183+
"shell": self.name,
183184
}
184185

185186
def load_template(self) -> t.Union[BaseTemplate, DjangoTemplate]:

django_typer/management/commands/shells/powershell.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ def get_user_profile(self) -> Path:
4242

4343
def install(self) -> Path:
4444
assert self.prog_name
45+
self.uninstall()
4546
self.set_execution_policy()
4647
profile = self.get_user_profile()
4748
profile.parent.mkdir(parents=True, exist_ok=True)
@@ -55,6 +56,8 @@ def uninstall(self):
5556
assert self.prog_name
5657
self.set_execution_policy()
5758
profile = self.get_user_profile()
59+
if not profile.exists():
60+
return
5861
edited_lines = []
5962
mark = None
6063
with open(profile, "rt", encoding="utf-8") as pwr_sh:
@@ -65,7 +68,7 @@ def uninstall(self):
6568
elif (
6669
mark is not None
6770
and line.startswith("Register-ArgumentCompleter")
68-
and f" {self.prog_name} " in line
71+
and f" {self.command.manage_script_name} " in line
6972
):
7073
edited_lines = edited_lines[:mark]
7174
mark = None

django_typer/templates/shell_complete/powershell.ps1

+20-8
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,30 @@ Import-Module PSReadLine
22
Set-PSReadLineKeyHandler -Chord Tab -Function MenuComplete
33
$scriptblock = {
44
param($wordToComplete, $commandAst, $cursorPosition)
5-
$Env:%(autocomplete_var)s = "complete_powershell"
6-
$Env:_TYPER_COMPLETE_ARGS = $commandAst.ToString()
7-
$Env:_TYPER_COMPLETE_WORD_TO_COMPLETE = $wordToComplete
8-
{{ manage_script }} {{ django_command }} {{ color }} complete | ForEach-Object {
5+
6+
$commandText = $commandAst.Extent.Text
7+
8+
# trailing white space is lopped off, add it back if necessary
9+
if ($cursorPosition -gt $commandText.Length) {
10+
$commandText += " "
11+
}
12+
13+
$settingsOption = ""
14+
if ($commandText -match "--settings(?:\s+|=)([^\s]+)\s+") {
15+
$settingsOption = "--settings=$($matches[1])"
16+
}
17+
18+
$pythonPathOption = ""
19+
if ($commandText -match "--pythonpath(?:\s+|=)([^\s]+)\s+") {
20+
$pythonPathOption = "--pythonpath=$($matches[1])"
21+
}
22+
23+
{{ manage_script_name }} {{ django_command }} $settingsOption $pythonPathOption --shell {{ shell }} {{ color }} complete "$($commandText)" | ForEach-Object {
924
$commandArray = $_ -Split ":::"
1025
$command = $commandArray[0]
1126
$helpString = $commandArray[1]
1227
[System.Management.Automation.CompletionResult]::new(
1328
$command, $command, 'ParameterValue', $helpString)
1429
}
15-
$Env:%(autocomplete_var)s = ""
16-
$Env:_TYPER_COMPLETE_ARGS = ""
17-
$Env:_TYPER_COMPLETE_WORD_TO_COMPLETE = ""
1830
}
19-
Register-ArgumentCompleter -Native -CommandName %(prog_name)s -ScriptBlock $scriptblock
31+
Register-ArgumentCompleter -Native -CommandName {{ manage_script_name }} -ScriptBlock $scriptblock

0 commit comments

Comments
 (0)