Skip to content

Commit 4e47bdf

Browse files
committed
fix(powershell): work around issue with PowerShell 7.5.0
PowerShell/PowerShell#24986
1 parent 358fd79 commit 4e47bdf

File tree

3 files changed

+24
-20
lines changed

3 files changed

+24
-20
lines changed

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::{IsTerminal as _, stderr};
1+
use std::fs::File;
2+
use std::io::{IsTerminal as _, Write, stderr};
23

34
use atuin_common::utils::{self, Escapable as _};
45
use clap::Parser;
@@ -131,6 +132,10 @@ pub struct Cmd {
131132
/// Include duplicate commands in the output (non-interactive only)
132133
#[arg(long)]
133134
include_duplicates: bool,
135+
136+
/// File name to write the result to (hidden from help as this is meant to be used from a script)
137+
#[arg(long = "result-file", hide = true)]
138+
result_file: Option<String>,
134139
}
135140

136141
impl Cmd {
@@ -213,7 +218,11 @@ impl Cmd {
213218

214219
if self.interactive {
215220
let item = interactive::history(&query, settings, db, &history_store, theme).await?;
216-
if stderr().is_terminal() {
221+
222+
if let Some(result_file) = self.result_file {
223+
let mut file = File::create(result_file)?;
224+
write!(file, "{item}")?;
225+
} else if stderr().is_terminal() {
217226
eprintln!("{}", item.escape_control());
218227
} else {
219228
eprintln!("{item}");

crates/atuin/src/command/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ mod external;
1818

1919
#[derive(Subcommand)]
2020
#[command(infer_subcommands = true)]
21+
#[allow(clippy::large_enum_variant)]
2122
pub enum AtuinCmd {
2223
#[cfg(feature = "client")]
2324
#[command(flatten)]

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)