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

Added "accept-to-edit" for (Back)Space / Home / Left #2000

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions crates/atuin-client/src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,12 @@ pub struct Settings {
pub enter_accept: bool,
pub smart_sort: bool,

pub exit_with_backspace: bool,
pub exit_with_space: bool,
pub exit_with_home: bool,
pub exit_with_cursor_left: bool,
pub exit_positions_cursor: bool,

#[serde(default)]
pub stats: Stats,

Expand Down Expand Up @@ -674,6 +680,11 @@ impl Settings {
.set_default("keymap_mode_shell", "auto")?
.set_default("keymap_cursor", HashMap::<String, String>::new())?
.set_default("smart_sort", false)?
.set_default("exit_with_backspace", false)?
.set_default("exit_with_space", false)?
.set_default("exit_with_home", false)?
.set_default("exit_with_cursor_left", false)?
.set_default("exit_positions_cursor", false)?
.set_default("store_failed", true)?
.set_default(
"prefers_reduced_motion",
Expand Down
5 changes: 5 additions & 0 deletions crates/atuin/src/command/client/search/cursor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,11 @@ impl Cursor {
self.source
}

/// Checks if there's currently no input
pub fn is_empty(&self) -> bool {
self.source.is_empty()
}

/// Returns the string before the cursor
pub fn substring(&self) -> &str {
&self.source[..self.index]
Expand Down
97 changes: 90 additions & 7 deletions crates/atuin/src/command/client/search/interactive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,20 @@ use ratatui::{
const TAB_TITLES: [&str; 2] = ["Search", "Inspect"];

pub enum InputAction {
Accept(usize),
Accept(usize, InputAcceptKind),
Copy(usize),
Delete(usize),
ReturnOriginal,
ReturnQuery,
Continue,
Redraw,
}
pub enum InputAcceptKind {
Default,
Backspace,
Space,
Offset(i64),
}

#[allow(clippy::struct_field_names)]
pub struct State {
Expand Down Expand Up @@ -214,7 +220,10 @@ impl State {
KeyCode::Char('c' | 'g') if ctrl => Some(InputAction::ReturnOriginal),
KeyCode::Esc if esc_allow_exit => Some(Self::handle_key_exit(settings)),
KeyCode::Char('[') if ctrl && esc_allow_exit => Some(Self::handle_key_exit(settings)),
KeyCode::Tab => Some(InputAction::Accept(self.results_state.selected())),
KeyCode::Tab => Some(InputAction::Accept(
self.results_state.selected(),
InputAcceptKind::Default,
)),
KeyCode::Char('o') if ctrl => {
self.tab_index = (self.tab_index + 1) % TAB_TITLES.len();

Expand Down Expand Up @@ -273,7 +282,7 @@ impl State {
if settings.enter_accept {
self.accept = true;
}
InputAction::Accept(self.results_state.selected())
InputAction::Accept(self.results_state.selected(), InputAcceptKind::Default)
}

#[allow(clippy::too_many_lines)]
Expand Down Expand Up @@ -374,7 +383,10 @@ impl State {
}
KeyCode::Char(c @ '1'..='9') if modfr => {
return c.to_digit(10).map_or(InputAction::Continue, |c| {
InputAction::Accept(self.results_state.selected() + c as usize)
InputAction::Accept(
self.results_state.selected() + c as usize,
InputAcceptKind::Default,
)
})
}
KeyCode::Left if ctrl => self
Expand All @@ -386,6 +398,12 @@ impl State {
.input
.prev_word(&settings.word_chars, settings.word_jump_mode),
KeyCode::Left => {
if settings.exit_with_cursor_left && self.search.input.is_empty() {
return InputAction::Accept(
self.results_state.selected(),
InputAcceptKind::Offset(-1),
);
}
self.search.input.left();
}
KeyCode::Char('b') if ctrl => {
Expand All @@ -401,14 +419,28 @@ impl State {
.next_word(&settings.word_chars, settings.word_jump_mode),
KeyCode::Right => self.search.input.right(),
KeyCode::Char('f') if ctrl => self.search.input.right(),
KeyCode::Home => self.search.input.start(),
KeyCode::Home => {
if settings.exit_with_home && self.search.input.is_empty() {
return InputAction::Accept(
self.results_state.selected(),
InputAcceptKind::Offset(0),
);
}
self.search.input.start();
}
KeyCode::Char('e') if ctrl => self.search.input.end(),
KeyCode::End => self.search.input.end(),
KeyCode::Backspace if ctrl => self
.search
.input
.remove_prev_word(&settings.word_chars, settings.word_jump_mode),
KeyCode::Backspace => {
if settings.exit_with_backspace && self.search.input.is_empty() {
return InputAction::Accept(
self.results_state.selected(),
InputAcceptKind::Backspace,
);
}
self.search.input.back();
}
KeyCode::Char('h' | '?') if ctrl => {
Expand Down Expand Up @@ -494,6 +526,15 @@ impl State {
KeyCode::Char('l') if ctrl => {
return InputAction::Redraw;
}
KeyCode::Char(' ') => {
if settings.exit_with_space && self.search.input.is_empty() {
return InputAction::Accept(
self.results_state.selected(),
InputAcceptKind::Space,
);
}
self.search.input.insert(' ');
}
KeyCode::Char(c) => {
self.search.input.insert(c);
}
Expand Down Expand Up @@ -1134,12 +1175,54 @@ pub async fn history(
}

match result {
InputAction::Accept(index) if index < results.len() => {
InputAction::Accept(index, kind) if index < results.len() => {
let mut command = results.swap_remove(index).command;
if accept
&& (utils::is_zsh() || utils::is_fish() || utils::is_bash() || utils::is_xonsh())
{
command = String::from("__atuin_accept__:") + &command;
} else {
match kind {
InputAcceptKind::Default => {}
InputAcceptKind::Backspace => {
// trim the end of the selected command *and* remove the
// last character, as tab-completion might have added
// trailing whitespace (which can't be seen in the UI)
command = command.trim_end().to_string();
command.pop();
}
InputAcceptKind::Space => {
// trim the end and add one space character
command = command.trim_end().to_string() + " ";
}
InputAcceptKind::Offset(offset) => {
if settings.exit_positions_cursor {
// for negative offsets (move left), remove trailing whitespace
if offset < 0 {
command = command.trim_end().to_string();
}
#[allow(clippy::cast_possible_wrap)]
let length = command.len() as i64;
let position = if offset >= 0 {
// start editing at specified position,
// counting from the start
length.min(offset)
} else {
// start editing at specified position,
// counting from the (trimmed) end
0.max(length - offset.abs())
};
// One of bash's READLINE_POINT or zsh's LBUFFER/RBUFFER is required
// for positioning the cursor; we still allow to go back to the shell
// even when not using these shells (if users configure the respective
// options), but the actual positioning is only possible with bash/zsh
// (and is only implemented in the bash and zsh shell integrations)
if utils::is_bash() || utils::is_zsh() {
command = format!("__atuin_edit_at__:{position}:{command}");
}
}
}
}
}

// index is in bounds so we return that entry
Expand All @@ -1151,7 +1234,7 @@ pub async fn history(
set_clipboard(cmd);
Ok(String::new())
}
InputAction::ReturnQuery | InputAction::Accept(_) => {
InputAction::ReturnQuery | InputAction::Accept(_, _) => {
// Either:
// * index == RETURN_QUERY, in which case we should return the input
// * out of bounds -> usually implies no selected entry so we return the input
Expand Down
6 changes: 6 additions & 0 deletions crates/atuin/src/shell/atuin.bash
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,9 @@ __atuin_history() {
# We do nothing when the search is canceled.
[[ $__atuin_output ]] || return 0

local __atuin_edit_at_rx='^__atuin_edit_at__:([0-9]+):(.*)$'
if [[ $__atuin_output == __atuin_accept__:* ]]; then
# Execute selected command
__atuin_output=${__atuin_output#__atuin_accept__:}

if [[ ${BLE_ATTACHED-} ]]; then
Expand All @@ -263,6 +265,10 @@ __atuin_history() {

READLINE_LINE=""
READLINE_POINT=${#READLINE_LINE}
elif [[ $__atuin_output =~ $__atuin_edit_at_rx ]]; then
# Edit selected command at specified cursor position
READLINE_LINE="${BASH_REMATCH[2]}"
READLINE_POINT=${BASH_REMATCH[1]}
else
READLINE_LINE=$__atuin_output
READLINE_POINT=${#READLINE_LINE}
Expand Down
8 changes: 8 additions & 0 deletions crates/atuin/src/shell/atuin.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,14 @@ _atuin_search() {
then
LBUFFER=${LBUFFER#__atuin_accept__:}
zle accept-line
elif [[ $LBUFFER =~ ^__atuin_edit_at__:([0-9]+):(.*)$ ]]
then
# shellcheck disable=SC2154 # $match array contains the regexp groups
local POS=${match[1]}
# shellcheck disable=SC2154 # $match array contains the regexp groups
local LINE=${match[2]}
LBUFFER="${LINE[0,${POS}]}"
RBUFFER="${LINE[$((POS+1)),${#LINE}]}"
fi
fi
}
Expand Down
Loading