Skip to content

Commit 21bb83a

Browse files
committed
fix(powershell): work around issue with PowerShell 7.5.0
PowerShell/PowerShell#24986
1 parent 59c6f95 commit 21bb83a

File tree

3 files changed

+24
-20
lines changed

3 files changed

+24
-20
lines changed

Diff for: crates/atuin/src/command/client/search.rs

+11-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
use std::io::{stderr, IsTerminal as _};
1+
use std::fs::File;
2+
use std::io::{stderr, IsTerminal as _, Write};
23

34
use atuin_common::utils::{self, Escapable as _};
45
use clap::Parser;
@@ -119,6 +120,10 @@ pub struct Cmd {
119120
/// Set the maximum number of lines Atuin's interface should take up.
120121
#[arg(long = "inline-height")]
121122
inline_height: Option<u16>,
123+
124+
/// File name to write the result to (hidden from help as this is meant to be used from a script)
125+
#[arg(long = "result-file", hide = true)]
126+
result_file: Option<String>,
122127
}
123128

124129
impl Cmd {
@@ -199,7 +204,11 @@ impl Cmd {
199204

200205
if self.interactive {
201206
let item = interactive::history(&query, settings, db, &history_store, theme).await?;
202-
if stderr().is_terminal() {
207+
208+
if let Some(result_file) = self.result_file {
209+
let mut file = File::create(result_file)?;
210+
write!(file, "{item}")?;
211+
} else if stderr().is_terminal() {
203212
eprintln!("{}", item.escape_control());
204213
} else {
205214
eprintln!("{item}");

Diff for: crates/atuin/src/command/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ mod gen_completions;
1616

1717
#[derive(Subcommand)]
1818
#[command(infer_subcommands = true)]
19+
#[allow(clippy::large_enum_variant)]
1920
pub enum AtuinCmd {
2021
#[cfg(feature = "client")]
2122
#[command(flatten)]

Diff for: crates/atuin/src/shell/atuin.ps1

+12-18
Original file line numberDiff line numberDiff line change
@@ -63,29 +63,21 @@ New-Module -Name Atuin -ScriptBlock {
6363
function RunSearch {
6464
param([string]$ExtraArgs = "")
6565

66-
$line = $null
67-
[Microsoft.PowerShell.PSConsoleReadLine]::GetBufferState([ref]$line, [ref]$null)
68-
69-
# Atuin is started through Start-Process to avoid interfering with the current shell,
70-
# and to capture its output which is provided in stderr (redirected to a temporary file).
71-
72-
$suggestion = ""
66+
$previousOutputEncoding = [System.Console]::OutputEncoding
7367
$resultFile = New-TemporaryFile
74-
try {
75-
$env:ATUIN_SHELL_POWERSHELL = "true"
76-
$argString = "search -i $ExtraArgs -- $line"
77-
Start-Process -Wait -NoNewWindow -RedirectStandardError $resultFile.FullName -FilePath atuin -ArgumentList $argString
78-
$suggestion = (Get-Content -Raw $resultFile -Encoding UTF8 | Out-String).Trim()
79-
}
80-
finally {
81-
$env:ATUIN_SHELL_POWERSHELL = $null
82-
Remove-Item $resultFile
83-
}
8468

85-
$previousOutputEncoding = [System.Console]::OutputEncoding
8669
try {
8770
[System.Console]::OutputEncoding = [System.Text.Encoding]::UTF8
8871

72+
$line = $null
73+
[Microsoft.PowerShell.PSConsoleReadLine]::GetBufferState([ref]$line, [ref]$null)
74+
75+
# Atuin is started through Start-Process to avoid interfering with the current shell.
76+
$env:ATUIN_SHELL_POWERSHELL = "true"
77+
$argString = "search -i --result-file ""$resultFile"" $ExtraArgs -- $line"
78+
Start-Process -Wait -NoNewWindow -FilePath atuin -ArgumentList $argString
79+
$suggestion = (Get-Content -Raw $resultFile -Encoding UTF8 | Out-String).Trim()
80+
8981
# PSReadLine maintains its own cursor position, which will no longer be valid if Atuin scrolls the display in inline mode.
9082
# Fortunately, InvokePrompt can receive a new Y position and reset the internal state.
9183
[Microsoft.PowerShell.PSConsoleReadLine]::InvokePrompt($null, $Host.UI.RawUI.CursorPosition.Y + [int]$env:ATUIN_POWERSHELL_PROMPT_OFFSET)
@@ -108,6 +100,8 @@ New-Module -Name Atuin -ScriptBlock {
108100
}
109101
finally {
110102
[System.Console]::OutputEncoding = $previousOutputEncoding
103+
$env:ATUIN_SHELL_POWERSHELL = $null
104+
Remove-Item $resultFile
111105
}
112106
}
113107

0 commit comments

Comments
 (0)