Skip to content

Commit

Permalink
Recall recent history index only on empty line and down key
Browse files Browse the repository at this point in the history
  • Loading branch information
gwenn committed Jul 7, 2024
1 parent 4db3767 commit c1fef6b
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 10 deletions.
3 changes: 2 additions & 1 deletion src/edit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -636,6 +636,7 @@ impl<'out, 'prompt, H: Helper> State<'out, 'prompt, H> {
if history.is_empty() {
return Ok(());
}
self.ctx.recall(!prev && self.line.is_empty());
if self.ctx.history_index == history.len() {
if prev {
// Save the current edited line before overwriting it
Expand Down Expand Up @@ -762,7 +763,7 @@ pub fn init_state<'out, H: Helper>(
byte_buffer: [0; 4],
changes: Changeset::new(),
helper,
ctx: Context::new(history),
ctx: Context::new(history, None),
hint: Some(Box::new("hint".to_owned())),
highlight_char: false,
forced_refresh: false,
Expand Down
2 changes: 1 addition & 1 deletion src/hint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ mod test {
#[test]
pub fn empty_history() {
let history = DefaultHistory::new();
let ctx = Context::new(&history);
let ctx = Context::new(&history, None);
let hinter = HistoryHinter {};
let hint = hinter.hint("test", 4, &ctx);
assert_eq!(None, hint);
Expand Down
8 changes: 4 additions & 4 deletions src/history.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ pub trait History {
*/

/// recently used index
fn recent_index(&self) -> Option<usize>;
fn recent_index(&mut self) -> Option<usize>;
/// Update recently used index
fn set_recent_index(&mut self, entry: Option<(usize, &str)>);
}
Expand Down Expand Up @@ -427,8 +427,8 @@ impl History for MemHistory {
}
}

fn recent_index(&self) -> Option<usize> {
self.recent
fn recent_index(&mut self) -> Option<usize> {
self.recent.take()
}

fn set_recent_index(&mut self, entry: Option<(usize, &str)>) {
Expand Down Expand Up @@ -813,7 +813,7 @@ impl History for FileHistory {
self.mem.starts_with(term, start, dir)
}

fn recent_index(&self) -> Option<usize> {
fn recent_index(&mut self) -> Option<usize> {
self.mem.recent_index()
}

Expand Down
17 changes: 14 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -557,15 +557,17 @@ impl<'h, H: ?Sized + Helper> Helper for &'h H {}
pub struct Context<'h> {
history: &'h dyn History,
history_index: usize,
recent_index: Option<usize>,
}

impl<'h> Context<'h> {
/// Constructor. Visible for testing.
#[must_use]
pub fn new(history: &'h dyn History) -> Self {
pub fn new(history: &'h dyn History, recent_index: Option<usize>) -> Self {
Context {
history,
history_index: history.recent_index().unwrap_or(history.len()),
history_index: history.len(),
recent_index,
}
}

Expand All @@ -580,6 +582,14 @@ impl<'h> Context<'h> {
pub fn history_index(&self) -> usize {
self.history_index
}

pub(crate) fn recall(&mut self, next_and_empty: bool) {
if let Some(idx) = self.recent_index.take() {
if next_and_empty {
self.history_index = idx;
}
}
}
}

/// Line editor
Expand Down Expand Up @@ -694,7 +704,8 @@ impl<H: Helper, I: History> Editor<H, I> {
let mut stdout = self.term.create_writer();

self.kill_ring.reset(); // TODO recreate a new kill ring vs reset
let ctx = Context::new(&self.history);
let recent_index = self.history.recent_index();
let ctx = Context::new(&self.history, recent_index);
let mut s = State::new(&mut stdout, prompt, self.helper.as_ref(), ctx);

let mut input_state = InputState::new(&self.config, &self.custom_bindings);
Expand Down
2 changes: 1 addition & 1 deletion src/sqlite_history.rs
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,7 @@ PRAGMA incremental_vacuum;
self.search_match(term, start, dir, true)
}

fn recent_index(&self) -> Option<usize> {
fn recent_index(&mut self) -> Option<usize> {
None // TODO
}

Expand Down

0 comments on commit c1fef6b

Please sign in to comment.