diff options
| -rw-r--r-- | crates/atuin/src/shell/atuin.ps1 | 38 |
1 files changed, 30 insertions, 8 deletions
diff --git a/crates/atuin/src/shell/atuin.ps1 b/crates/atuin/src/shell/atuin.ps1 index 33c2ba5f..7f6ad8cf 100644 --- a/crates/atuin/src/shell/atuin.ps1 +++ b/crates/atuin/src/shell/atuin.ps1 @@ -135,16 +135,40 @@ New-Module -Name Atuin -ScriptBlock { $previousOutputEncoding = [System.Console]::OutputEncoding $resultFile = New-TemporaryFile + $suggestion = "" + $errorOutput = "" try { [System.Console]::OutputEncoding = [System.Text.Encoding]::UTF8 - # Atuin is started through Start-Process to avoid interfering with the current shell. - $env:ATUIN_SHELL = "powershell" - $env:ATUIN_QUERY = Get-CommandLine - $argString = "search -i --result-file ""$resultFile"" $ExtraArgs" - Start-Process -PassThru -NoNewWindow -FilePath atuin -ArgumentList $argString | Wait-Process - $suggestion = (Get-Content -Raw $resultFile -Encoding UTF8 | Out-String).Trim() + # Start-Process does some crazy stuff, just use the Process class directly to have more control. + $process = New-Object System.Diagnostics.Process + $process.StartInfo.FileName = "atuin" + $process.StartInfo.Arguments = "search -i --result-file ""$resultFile"" $ExtraArgs" + $process.StartInfo.UseShellExecute = $false + $process.StartInfo.RedirectStandardError = $true + $process.StartInfo.StandardErrorEncoding = [System.Text.Encoding]::UTF8 + $process.StartInfo.EnvironmentVariables["ATUIN_SHELL"] = "powershell" + $process.StartInfo.EnvironmentVariables["ATUIN_QUERY"] = Get-CommandLine + + try { + $process.Start() | Out-Null + + # A single stream is redirected, so we can read it synchronously, but we have to start reading it + # before waiting for the process to exit, otherwise the buffer could fill up and cause a deadlock. + $errorOutput = $process.StandardError.ReadToEnd().Trim() + $process.WaitForExit() + + $suggestion = (Get-Content -Raw $resultFile -Encoding UTF8 | Out-String).Trim() + } + catch { + $errorOutput = $_ + } + + if ($errorOutput) { + Write-Host -ForegroundColor Red "Atuin error:" + Write-Host -ForegroundColor DarkRed $errorOutput + } # If no shell prompt offset is set, initialize it from the current prompt line count. if ($null -eq $env:ATUIN_POWERSHELL_PROMPT_OFFSET) { @@ -179,8 +203,6 @@ New-Module -Name Atuin -ScriptBlock { } finally { [System.Console]::OutputEncoding = $previousOutputEncoding - $env:ATUIN_SHELL = $null - $env:ATUIN_QUERY = $null Remove-Item $resultFile } } |
