Created
July 30, 2023 23:20
-
-
Save JKamsker/f31428a9ad8a181fd961314254c834a5 to your computer and use it in GitHub Desktop.
AutoComplete Ideas
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$script:process = $null | |
$script:path = "C:\Users\W31rd0\source\repos\work\Apro\Apro.AutoUpdater\ApGet\bin\Debug\net6.0\ApGet.exe" | |
$script:logPath = "C:\Users\W31rd0\Downloads\tmp\gpt\log.log" | |
$script:useCompletionServer = $false | |
function Invoke-ApGet { | |
# call external debugger | |
return & $script:path $args | |
} | |
function Invoke-StartServer { | |
# if not useCompletionServer return | |
if (-not $script:useCompletionServer) { | |
return | |
} | |
$shouldStart = $script:process -eq $null -or $script:process.HasExited | |
if (-not $shouldStart) { | |
return | |
} | |
$startInfo = New-Object System.Diagnostics.ProcessStartInfo | |
$startInfo.FileName = $script:path | |
$startInfo.RedirectStandardInput = $true | |
$startInfo.RedirectStandardOutput = $true | |
$startInfo.UseShellExecute = $false | |
# $startInfo.ArgumentList = @("cli", "complete", "--server", "--format", "json") | |
$startArgs = @("cli", "complete", "--server", "--format", "json") -join ' ' | |
$startInfo.Arguments = $startArgs | |
$script:process = New-Object System.Diagnostics.Process | |
$script:process.StartInfo = $startInfo | |
try { | |
$script:process.Start() | Out-Null | |
} | |
catch { | |
Write-Error "Failed to start the process: $_" | |
return | |
} | |
} | |
function Invoke-Initialization { | |
Register-EngineEvent -SourceIdentifier PowerShell.Exiting -Action { | |
if ($null -ne $script:process -and -not $script:process.HasExited) { | |
$script:process.StandardInput.WriteLine("exit") # This might need to change depending on how to close your program | |
$script:process.WaitForExit() | |
"Process exiting" | Out-File -FilePath $script:logPath -Append | |
} | |
} | Out-Null | |
Register-CompleterFor -name "ApGet" | |
Register-CompleterFor -name "apget" | |
Register-CompleterFor -name "Invoke-ApGet" | |
} | |
function Invoke-Completer { | |
param ( | |
[Parameter(Mandatory = $true)] $wordToComplete, | |
[Parameter(Mandatory = $true)] $cursorPosition | |
) | |
if ($script:useCompletionServer) { | |
Invoke-StartServer | |
if ($script:process -eq $null -or $script:process.HasExited) { | |
Invoke-Initialization | |
} | |
if ($script:process -eq $null) { | |
Write-Error "Process is not running" | |
return | |
} | |
$stringWords = "$wordToComplete"; | |
$command = @{"command" = $stringWords; "cursorPosition" = $cursorPosition } | ConvertTo-Json -Compress | |
$script:process.StandardInput.WriteLine($command) | |
$completionsJsonRaw = $script:process.StandardOutput.ReadLine() | |
$completions = $completionsJsonRaw | ConvertFrom-Json | |
return $completions | |
} else { | |
$completions = Invoke-ApGet cli complete --position $cursorPosition "$wordToComplete" | |
return $completions | |
} | |
} | |
# Register-CompleterFor -CommandName ApGet | |
function Register-CompleterFor { | |
#appname parameter | |
param( | |
[Parameter(Mandatory = $true)] | |
[string]$name | |
) | |
Register-ArgumentCompleter -Native -CommandName $name -ScriptBlock { | |
param($commandName, $wordToComplete, $cursorPosition) | |
$completions = @() | |
# $completions = & "C:\Users\W31rd0\source\repos\work\Apro\Apro.AutoUpdater\ApGet\bin\Debug\net6.0\ApGet.exe" cli complete --position $cursorPosition "$wordToComplete" | |
# $completionsJsonRaw = Invoke-CompletionRequest --command "$wordToComplete" --position $cursorPosition # returns json string array ["command", "command", ...] | |
$completions = Invoke-Completer -wordToComplete $wordToComplete -cursorPosition $cursorPosition | |
if ($completions) { | |
foreach ($completion in $completions) { | |
if ($completion -is [string]) { | |
[System.Management.Automation.CompletionResult]::new($completion, $completion, 'ParameterValue', $completion) | |
} | |
else { | |
$description = $completion.description | |
if ($description -eq $null) { | |
$description = $completion.value | |
} | |
[System.Management.Automation.CompletionResult]::new($completion.value, $completion.value, 'ParameterValue', $description) | |
} | |
} | |
} | |
else { | |
$null | |
} | |
} | |
} | |
Invoke-Initialization |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Old: