Skip to content

Commit c88a09d

Browse files
committed
fix(powershell): work around issue with PowerShell 7.5.0
PowerShell/PowerShell#24986
1 parent 738ae58 commit c88a09d

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;
@@ -127,6 +128,10 @@ pub struct Cmd {
127128
/// Include duplicate commands in the output (non-interactive only)
128129
#[arg(long)]
129130
include_duplicates: bool,
131+
132+
/// File name to write the result to (hidden from help as this is meant to be used from a script)
133+
#[arg(long = "result-file", hide = true)]
134+
result_file: Option<String>,
130135
}
131136

132137
impl Cmd {
@@ -209,7 +214,11 @@ impl Cmd {
209214

210215
if self.interactive {
211216
let item = interactive::history(&query, settings, db, &history_store, theme).await?;
212-
if stderr().is_terminal() {
217+
218+
if let Some(result_file) = self.result_file {
219+
let mut file = File::create(result_file)?;
220+
write!(file, "{item}")?;
221+
} else if stderr().is_terminal() {
213222
eprintln!("{}", item.escape_control());
214223
} else {
215224
eprintln!("{item}");

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)]

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)