fix: pass search query in via env (#1865)

* fix: pass search query in via env

* fix
This commit is contained in:
Ellie Huxtable 2024-03-12 17:04:02 +00:00 committed by GitHub
parent 02d79feea5
commit 6aa90c0eed
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 26 additions and 17 deletions

View File

@ -2,7 +2,7 @@ use std::io::{stderr, IsTerminal as _};
use atuin_common::utils::{self, Escapable as _};
use clap::Parser;
use eyre::{eyre, Result};
use eyre::Result;
use atuin_client::{
database::Database,
@ -121,22 +121,30 @@ pub struct Cmd {
}
impl Cmd {
// clippy: please write this instead
// clippy: now it has too many lines
// me: I'll do it later OKAY
#[allow(clippy::too_many_lines)]
pub async fn run(
self,
db: impl Database,
settings: &mut Settings,
store: SqliteStore,
) -> Result<()> {
let query: Vec<String> = if let Some(query) = self.query {
query
} else if let Ok(query) = std::env::var("ATUIN_QUERY") {
query
.split(' ')
.map(std::string::ToString::to_string)
.collect()
} else {
vec![]
};
let query = self.query.map_or_else(
|| {
std::env::var("ATUIN_QUERY").map_or_else(
|_| vec![],
|query| {
query
.split(' ')
.map(std::string::ToString::to_string)
.collect()
},
)
},
|query| query,
);
if (self.delete_it_all || self.delete) && self.limit.is_some() {
// Because of how deletion is implemented, it will always delete all matches

View File

@ -227,7 +227,7 @@ __atuin_history() {
local READLINE_LINE="" READLINE_POINT=0
local __atuin_output
__atuin_output=$(ATUIN_SHELL_BASH=t ATUIN_LOG=error atuin search "$@" -i -- "$READLINE_LINE" 3>&1 1>&2 2>&3)
__atuin_output=$(ATUIN_SHELL_BASH=t ATUIN_LOG=error ATUIN_QUERY="$READLINE_LINE" atuin search "$@" -i 3>&1 1>&2 2>&3)
# We do nothing when the search is canceled.
[[ $__atuin_output ]] || return 0
@ -265,7 +265,7 @@ if [[ ${BLE_VERSION-} ]] && ((_ble_version >= 400)); then
#
function ble/complete/auto-complete/source:atuin-history {
local suggestion
suggestion=$(atuin search --cmd-only --limit 1 --search-mode prefix -- "$_ble_edit_str")
suggestion=$(ATUIN_QUERY="$_ble_edit_str" atuin search --cmd-only --limit 1 --search-mode prefix)
[[ $suggestion == "$_ble_edit_str"?* ]] || return 1
ble/complete/auto-complete/enter h 0 "${suggestion:${#_ble_edit_str}}" '' "$suggestion"
}

View File

@ -35,7 +35,7 @@ function _atuin_search
# In fish 3.4 and above we can use `"$(some command)"` to keep multiple lines separate;
# but to support fish 3.3 we need to use `(some command | string collect)`.
# https://fishshell.com/docs/current/relnotes.html#id24 (fish 3.4 "Notable improvements and fixes")
set -l ATUIN_H (ATUIN_SHELL_FISH=t ATUIN_LOG=error atuin search --keymap-mode=$keymap_mode $argv -i -- (commandline -b) 3>&1 1>&2 2>&3 | string collect)
set -l ATUIN_H (ATUIN_SHELL_FISH=t ATUIN_LOG=error ATUIN_QUERY=(commandline -b) atuin search --keymap-mode=$keymap_mode $argv -i 3>&1 1>&2 2>&3 | string collect)
if test -n "$ATUIN_H"
if string match --quiet '__atuin_accept__:*' "$ATUIN_H"

View File

@ -32,10 +32,11 @@ def _atuin_postcommand(cmd: str, rtn: int, out, ts):
def _search(event, extra_args: list[str]):
buffer = event.current_buffer
cmd = ["atuin", "search", "--interactive", *extra_args, "--", buffer.text]
cmd = ["atuin", "search", "--interactive", *extra_args]
# We need to explicitly pass in xonsh env, in case user has set XDG_HOME or something else that matters
env = ${...}.detype()
env["ATUIN_SHELL_XONSH"] = "t"
env["ATUIN_QUERY"] = buffer.text
p = subprocess.run(cmd, stderr=subprocess.PIPE, encoding="utf-8", env=env)
result = p.stderr.rstrip("\n")

View File

@ -15,7 +15,7 @@ zmodload zsh/datetime 2>/dev/null
# you'd like to override this, then add your config after the $(atuin init zsh)
# in your .zshrc
_zsh_autosuggest_strategy_atuin() {
suggestion=$(atuin search --cmd-only --limit 1 --search-mode prefix -- "$1")
suggestion=$(ATUIN_QUERY="$1" atuin search --cmd-only --limit 1 --search-mode prefix)
}
if [ -n "${ZSH_AUTOSUGGEST_STRATEGY:-}" ]; then
@ -56,7 +56,7 @@ _atuin_search() {
# TODO: not this
local output
# shellcheck disable=SC2048
output=$(ATUIN_SHELL_ZSH=t ATUIN_LOG=error atuin search $* -i -- $BUFFER 3>&1 1>&2 2>&3)
output=$(ATUIN_SHELL_ZSH=t ATUIN_LOG=error ATUIN_QUERY=$BUFFER atuin search $* -i 3>&1 1>&2 2>&3)
zle reset-prompt