Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add enter_accept to immediately execute an accepted command #1311

Merged
merged 6 commits into from
Oct 20, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions atuin-client/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -123,3 +123,7 @@
## 4. Slack webhooks
## 5. Stripe live/test keys
# secrets_filter = true

## Defaults to true. If enabled, upon hitting enter Atuin will immediately execute the command. Press tab to return to the shell and edit.
# This applies for new installs. Old installs will keep the old behaviour unless configured otherwise.
enter_accept = true
8 changes: 8 additions & 0 deletions atuin-client/src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ pub struct Settings {

pub network_connect_timeout: u64,
pub network_timeout: u64,
pub enter_accept: bool,

// This is automatically loaded when settings is created. Do not set in
// config! Keep secrets and settings apart.
Expand Down Expand Up @@ -378,6 +379,12 @@ impl Settings {
.set_default("secrets_filter", true)?
.set_default("network_connect_timeout", 5)?
.set_default("network_timeout", 30)?
// enter_accept defaults to false here, but true in the default config file. The dissonance is
// intentional!
// Existing users will get the default "False", so we don't mess with any potential
// muscle memory.
// New users will get the new default, that is more similar to what they are used to.
.set_default("enter_accept", false)?
.add_source(
Environment::with_prefix("atuin")
.prefix_separator("_")
Expand All @@ -391,6 +398,7 @@ impl Settings {

create_dir_all(&config_dir)
.wrap_err_with(|| format!("could not create dir {config_dir:?}"))?;

create_dir_all(&data_dir).wrap_err_with(|| format!("could not create dir {data_dir:?}"))?;

let mut config_file = if let Ok(p) = std::env::var("ATUIN_CONFIG_DIR") {
Expand Down
19 changes: 17 additions & 2 deletions atuin/src/command/client/search/interactive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ struct State {
switched_search_mode: bool,
search_mode: SearchMode,
results_len: usize,
accept: bool,

search: SearchState,
engine: Box<dyn SearchEngine>,
Expand Down Expand Up @@ -130,7 +131,14 @@ impl State {
ExitMode::ReturnQuery => RETURN_QUERY,
})
}
KeyCode::Tab => {
return Some(self.results_state.selected());
}
KeyCode::Enter => {
if settings.enter_accept {
self.accept = true;
}

return Some(self.results_state.selected());
}
KeyCode::Char('y') if ctrl => {
Expand Down Expand Up @@ -588,7 +596,7 @@ impl Write for Stdout {
// this is a big blob of horrible! clean it up!
// for now, it works. But it'd be great if it were more easily readable, and
// modular. I'd like to add some more stats and stuff at some point
#[allow(clippy::cast_possible_truncation)]
#[allow(clippy::cast_possible_truncation, clippy::too_many_lines)]
pub async fn history(
query: &[String],
settings: &Settings,
Expand Down Expand Up @@ -646,10 +654,12 @@ pub async fn history(
},
engine: engines::engine(search_mode),
results_len: 0,
accept: false,
};

let mut results = app.query_results(&mut db).await?;

let accept;
let index = 'render: loop {
terminal.draw(|f| app.draw(f, &results, settings))?;

Expand All @@ -664,6 +674,7 @@ pub async fn history(
if event_ready?? {
loop {
if let Some(i) = app.handle_input(settings, &event::read()?, &mut std::io::stdout())? {
accept = app.accept;
break 'render i;
}
if !event::poll(Duration::ZERO)? {
Expand All @@ -690,8 +701,12 @@ pub async fn history(
}

if index < results.len() {
let mut command = results.swap_remove(index).command;
if accept {
command = String::from("__atuin_accept__:") + &command;
ellie marked this conversation as resolved.
Show resolved Hide resolved
}
// index is in bounds so we return that entry
Ok(results.swap_remove(index).command)
Ok(command)
} else if index == RETURN_ORIGINAL {
Ok(String::new())
} else if index == COPY_QUERY {
Expand Down
4 changes: 4 additions & 0 deletions atuin/src/command/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ pub struct Cmd {
disable_ctrl_r: bool,

/// Disable the binding of the Up Arrow key to atuin
/// Now the default, but left here to avoid breaking shells
///
/// Originally we bound the up arrow by default, but due to pretty constant questions I'm
/// changing the default
#[clap(long)]
disable_up_arrow: bool,
}
Expand Down
8 changes: 7 additions & 1 deletion atuin/src/shell/atuin.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,18 @@ _atuin_search() {
# shellcheck disable=SC2048
output=$(ATUIN_LOG=error atuin search $* -i -- $BUFFER 3>&1 1>&2 2>&3)

zle reset-prompt

if [[ -n $output ]]; then
RBUFFER=""
LBUFFER=$output
fi

zle reset-prompt
if [[ $LBUFFER == __atuin_accept__:* ]]
then
LBUFFER=${LBUFFER#__atuin_accept__:}
zle accept-line
fi
}

_atuin_up_search() {
Expand Down
13 changes: 13 additions & 0 deletions docs/docs/config/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -305,3 +305,16 @@ Default: 5

The max time (in seconds) we wait for a connection to become established with a
remote sync server. Any longer than this and the request will fail.

## enter_accept
Default: false

Only supported on Bash.
ellie marked this conversation as resolved.
Show resolved Hide resolved

When set to true, Atuin will default to immediately executing a command rather
than the user having to press enter twice. Pressing tab will return to the
shell and give the user a chance to edit.

This technically defaults to true for new users, but false for existing. We
have set `enter_accept = true` in the default config file. This is likely to
change to be the default for everyone in a later release.