Skip to content

Commit

Permalink
Add enter_accept to immediately execute an accepted command (#1311)
Browse files Browse the repository at this point in the history
* make enter execute the command, tab copy it

* Add config for enter_accept

enter_accept will make Atuin immediately accept an execute a command
when selected. It defaults to false in our binary, but the default
config enables it.

This means that users who already use atuin will not default to the new
behaviour unless they opt in, but new users will have it by default.

Thanks to @davidhewitt for the patch and bulk of this implementation!
Currently we have it just for zsh, but I'll follow up with other shells
(unless anyone beats me to it :D)

* Add docs

* we need to tidy up the ui code anyway

* Check if using zsh

* Update docs/docs/config/config.md

Co-authored-by: Conrad Ludgate <conradludgate@gmail.com>

---------

Co-authored-by: David Hewitt <1939362+davidhewitt@users.noreply.github.com>
Co-authored-by: Conrad Ludgate <conradludgate@gmail.com>
  • Loading branch information
3 people committed Oct 20, 2023
1 parent ad48685 commit 88f3e2a
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 3 deletions.
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
5 changes: 5 additions & 0 deletions atuin-common/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,11 @@ pub fn get_current_dir() -> String {
}
}

pub fn is_zsh() -> bool {
// only set on zsh
env::var("ZSH_VERSION").is_ok()
}

#[cfg(test)]
mod tests {
use time::Month;
Expand Down
20 changes: 18 additions & 2 deletions atuin/src/command/client/search/interactive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::{
time::Duration,
};

use atuin_common::utils;
use crossterm::{
event::{
self, DisableMouseCapture, EnableMouseCapture, Event, KeyCode, KeyEvent, KeyModifiers,
Expand Down Expand Up @@ -47,6 +48,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 +132,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 +597,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 +655,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 +675,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 +702,12 @@ pub async fn history(
}

if index < results.len() {
let mut command = results.swap_remove(index).command;
if accept && utils::is_zsh() {
command = String::from("__atuin_accept__:") + &command;
}
// 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
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 Zsh.

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.

1 comment on commit 88f3e2a

@vercel
Copy link

@vercel vercel bot commented on 88f3e2a Oct 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

atuin-docs – ./

atuin-docs.vercel.app
atuin-docs-atuin.vercel.app
atuin-docs-git-main-atuin.vercel.app

Please sign in to comment.