From 4943a41bebd80765f91807f399fa1ee47f22fb7c Mon Sep 17 00:00:00 2001 From: Luis Alberto Santos Date: Tue, 23 May 2023 19:13:26 +0200 Subject: [PATCH] fixes #4 --- Cargo.lock | 2 +- Cargo.toml | 2 +- intelli-shell.fish | 18 ++++++------------ intelli-shell.ps1 | 6 +++--- intelli-shell.sh | 42 ++++++++++++------------------------------ src/main.rs | 7 +++++-- 6 files changed, 28 insertions(+), 49 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index d69b2f6..5384476 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -418,7 +418,7 @@ dependencies = [ [[package]] name = "intelli-shell" -version = "0.2.5" +version = "0.2.6" dependencies = [ "anyhow", "clap", diff --git a/Cargo.toml b/Cargo.toml index 6941515..3032acb 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "intelli-shell" description = "Like IntelliSense, but for shells" -version = "0.2.5" +version = "0.2.6" edition = "2021" license = "Apache-2.0" readme = "README.md" diff --git a/intelli-shell.fish b/intelli-shell.fish index a7d82ed..ff6c8de 100755 --- a/intelli-shell.fish +++ b/intelli-shell.fish @@ -3,18 +3,12 @@ function intelli-shell --description 'IntelliShell' end function _intelli_exec - # Temp file for output - set TMP_FILE (mktemp -t intelli-shell.XXXXXXXX) - set TMP_FILE_MSG (mktemp -t intelli-shell.XXXXXXXX) - # Exec command - intelli-shell --inline --inline-extra-line --file-output="$TMP_FILE" $argv 2> $TMP_FILE_MSG - # Capture output - set INTELLI_OUTPUT (cat "$TMP_FILE" | string collect) - set INTELLI_MESSAGE (cat "$TMP_FILE_MSG" | string collect) - rm -f $TMP_FILE - rm -f $TMP_FILE_MSG - if test -n "$INTELLI_MESSAGE" - echo $INTELLI_MESSAGE + set p_lines (fish_prompt | string split0 | wc -l) + # Swap stderr and stdout + if test (math $p_lines + 0) -gt "1" + set INTELLI_OUTPUT (intelli-shell --inline --inline-extra-line $argv 3>&1 1>&2 2>&3) + else + set INTELLI_OUTPUT (intelli-shell --inline $argv 3>&1 1>&2 2>&3) end # Replace line commandline -f repaint diff --git a/intelli-shell.ps1 b/intelli-shell.ps1 index 7405c22..072a0ba 100644 --- a/intelli-shell.ps1 +++ b/intelli-shell.ps1 @@ -10,7 +10,7 @@ Set-PSReadLineKeyHandler -Chord $IntelliSearchChord -BriefDescription "IntelliSh $TempFile = New-TemporaryFile $line = $line -replace '"','""""""""""""' $Command = 'intelli-shell.exe --file-output=""""' + $TempFile.FullName + '"""" search """"' + $line + '""""' - Start-Process powershell.exe -Wait -NoNewWindow -ArgumentList "-command", "$Command" -RedirectStandardError "NUL" + Start-Process powershell.exe -Wait -NoNewWindow -ArgumentList "-command", "$Command" $IntelliOutput = Get-Content -Raw $TempFile Remove-Item $TempFile @@ -33,7 +33,7 @@ Set-PSReadLineKeyHandler -Chord $IntelliBookmarkChord -BriefDescription "Intelli if ([string]::IsNullOrWhiteSpace($line)) { $Command = 'intelli-shell.exe --file-output=""""' + $TempFile.FullName + '"""" new' } - Start-Process powershell.exe -Wait -NoNewWindow -ArgumentList "-command", "$Command" -RedirectStandardError "NUL" + Start-Process powershell.exe -Wait -NoNewWindow -ArgumentList "-command", "$Command" $IntelliOutput = Get-Content -Raw $TempFile Remove-Item $TempFile @@ -53,7 +53,7 @@ Set-PSReadLineKeyHandler -Chord $IntelliLabelChord -BriefDescription "IntelliShe $TempFile = New-TemporaryFile $line = $line -replace '"','""""""""""""' $Command = 'intelli-shell.exe --file-output=""""' + $TempFile.FullName + '"""" label """"' + $line + '""""' - Start-Process powershell.exe -Wait -NoNewWindow -ArgumentList "-command", "$Command" -RedirectStandardError "NUL" + Start-Process powershell.exe -Wait -NoNewWindow -ArgumentList "-command", "$Command" $IntelliOutput = Get-Content -Raw $TempFile Remove-Item $TempFile diff --git a/intelli-shell.sh b/intelli-shell.sh index 7312aca..c3867e1 100755 --- a/intelli-shell.sh +++ b/intelli-shell.sh @@ -8,24 +8,18 @@ if [[ -n "$ZSH_VERSION" ]]; then intelli_label_key="${INTELLI_LABEL_HOTKEY:-^l}" function _intelli_exec { - ps1_lines=$(echo "$PS1" | wc -l) - # Temp file for output - tmp_file=$(mktemp -t intelli-shell.XXXXXXXX) - tmp_file_msg=$(mktemp -t intelli-shell.XXXXXXXX) - # Exec command - intelli-shell --inline --inline-extra-line --file-output="$tmp_file" "$@" 2> $tmp_file_msg - # Capture output - INTELLI_OUTPUT=$(<$tmp_file) - INTELLI_MESSAGE=$(<$tmp_file_msg) - rm -f $tmp_file - rm -f $tmp_file_msg - if [[ -n "$INTELLI_MESSAGE" ]]; then - echo "$INTELLI_MESSAGE" - [[ "$ps1_lines" -gt 1 ]] && echo "" + p_lines=$(echo "$PS1" | wc -l) + + # Swap stderr and stdout + if [ "$p_lines" -gt "1" ]; then + INTELLI_OUTPUT=$(intelli-shell --inline --inline-extra-line "$@" 3>&1 1>&2 2>&3) + else + INTELLI_OUTPUT=$(intelli-shell --inline "$@" 3>&1 1>&2 2>&3) fi + # Rewrite line zle reset-prompt - BUFFER="$INTELLI_OUTPUT" + BUFFER=$INTELLI_OUTPUT zle end-of-line } @@ -59,22 +53,10 @@ elif [[ -n "$BASH" ]]; then intelli_label_key="${INTELLI_LABEL_HOTKEY:-\C-l}" function _intelli_exec { - ps1_lines=$(echo "$PS1" | wc -l) - # Temp file for output - tmp_file=$(mktemp -t intelli-shell.XXXXXXXX) - tmp_file_msg=$(mktemp -t intelli-shell.XXXXXXXX) - # Exec command - intelli-shell --inline --file-output="$tmp_file" "$@" 2> $tmp_file_msg - # Capture output - INTELLI_OUTPUT=$(<$tmp_file) - INTELLI_MESSAGE=$(<$tmp_file_msg) - rm -f $tmp_file - rm -f $tmp_file_msg - if [[ -n "$INTELLI_MESSAGE" ]]; then - echo "$INTELLI_MESSAGE" - fi + # Swap stderr and stdout + INTELLI_OUTPUT=$(intelli-shell --inline "$@" 3>&1 1>&2 2>&3) # Rewrite line - READLINE_LINE="$INTELLI_OUTPUT" + READLINE_LINE=${INTELLI_OUTPUT} READLINE_POINT=${#READLINE_LINE} } diff --git a/src/main.rs b/src/main.rs index 84ee839..d8de0a5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -168,14 +168,17 @@ fn run(cli: Args) -> Result<()> { // Print any message received if let Some(msg) = res.message { - eprintln!("{msg}"); + println!("{msg}"); + if cli.inline_extra_line { + println!(); + } } // Write out the result match res.output { None => (), Some(output) => match cli.file_output { - None => execute!(io::stdout(), Print(format!("{output}\n")))?, + None => eprintln!("{output}"), Some(path) => fs::write(path, output)?, }, }