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

Improve history API #680

Closed
wants to merge 18 commits into from
Closed
Show file tree
Hide file tree
Changes from 16 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
80 changes: 74 additions & 6 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,13 @@ clipboard = { version = "0.5.0", optional = true }
crossbeam = { version = "0.8.2", optional = true }
crossterm = { version = "0.27.0", features = ["serde"] }
fd-lock = "3.0.3"
indexmap = "2.1.0"
itertools = "0.10.3"
nu-ansi-term = "0.49.0"
rand = { version = "0.8.5", default-features = false, features = [
"small_rng",
"getrandom",
] }
rusqlite = { version = "0.29.0", optional = true }
serde = { version = "1.0", features = ["derive"] }
serde_json = { version = "1.0.79", optional = true }
Expand Down
23 changes: 15 additions & 8 deletions examples/cwd_aware_hinter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,21 @@
// pressing "a" hints to abc.
// Up/Down or Ctrl p/n, to select next/previous match

use std::io;
use std::{
io,
sync::atomic::{AtomicI64, Ordering},
};

use reedline::HistoryItemId;

static COUNTER: AtomicI64 = AtomicI64::new(0);

fn create_item(cwd: &str, cmd: &str, exit_status: i64) -> reedline::HistoryItem {
use std::time::Duration;

use reedline::HistoryItem;
HistoryItem {
id: None,
id: HistoryItemId(COUNTER.fetch_add(1, Ordering::SeqCst)),
start_timestamp: None,
command_line: cmd.to_string(),
session_id: None,
Expand All @@ -32,13 +39,13 @@ fn create_filled_example_history(home_dir: &str, orig_dir: &str) -> Box<dyn reed
#[cfg(any(feature = "sqlite", feature = "sqlite-dynlib"))]
let mut history = Box::new(reedline::SqliteBackedHistory::in_memory().unwrap());

history.save(create_item(orig_dir, "dummy", 0)).unwrap(); // add dummy item so ids start with 1
history.save(create_item(orig_dir, "ls /usr", 0)).unwrap();
history.save(create_item(orig_dir, "pwd", 0)).unwrap();
history.save(&create_item(orig_dir, "dummy", 0)).unwrap(); // add dummy item so ids start with 1
history.save(&create_item(orig_dir, "ls /usr", 0)).unwrap();
history.save(&create_item(orig_dir, "pwd", 0)).unwrap();

history.save(create_item(home_dir, "cat foo", 0)).unwrap();
history.save(create_item(home_dir, "ls bar", 0)).unwrap();
history.save(create_item(home_dir, "rm baz", 0)).unwrap();
history.save(&create_item(home_dir, "cat foo", 0)).unwrap();
history.save(&create_item(home_dir, "ls bar", 0)).unwrap();
history.save(&create_item(home_dir, "rm baz", 0)).unwrap();

history
}
Expand Down
35 changes: 22 additions & 13 deletions src/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1730,32 +1730,41 @@
fn submit_buffer(&mut self, prompt: &dyn Prompt) -> io::Result<EventStatus> {
let buffer = self.editor.get_buffer().to_string();
self.hide_hints = true;

// Additional repaint to show the content without hints etc.
if let Some(transient_prompt) = self.transient_prompt.take() {
self.repaint(transient_prompt.as_ref())?;
self.transient_prompt = Some(transient_prompt);
} else {
self.repaint(prompt)?;
}
if !buffer.is_empty() {
let mut entry = HistoryItem::from_command_line(&buffer);
entry.session_id = self.get_history_session_id();

if self
if !buffer.is_empty() {
let filtered = self

Check warning on line 1743 in src/engine.rs

View check run for this annotation

Codecov / codecov/patch

src/engine.rs#L1742-L1743

Added lines #L1742 - L1743 were not covered by tests
.history_exclusion_prefix
.as_ref()
.map(|prefix| buffer.starts_with(prefix))
.unwrap_or(false)
{
entry.id = Some(Self::FILTERED_ITEM_ID);
self.history_last_run_id = entry.id;
self.history_excluded_item = Some(entry);
.map_or(false, |prefix| buffer.starts_with(prefix));

Check warning on line 1746 in src/engine.rs

View check run for this annotation

Codecov / codecov/patch

src/engine.rs#L1746

Added line #L1746 was not covered by tests

let entry_id = if filtered {
Self::FILTERED_ITEM_ID

Check warning on line 1749 in src/engine.rs

View check run for this annotation

Codecov / codecov/patch

src/engine.rs#L1748-L1749

Added lines #L1748 - L1749 were not covered by tests
} else {
self.history.generate_id()

Check warning on line 1751 in src/engine.rs

View check run for this annotation

Codecov / codecov/patch

src/engine.rs#L1751

Added line #L1751 was not covered by tests
};

let mut entry = HistoryItem::from_command_line(&buffer, entry_id);

entry.session_id = self.get_history_session_id();

if filtered {
self.history.replace(&entry).expect("todo: error handling");

Check warning on line 1759 in src/engine.rs

View check run for this annotation

Codecov / codecov/patch

src/engine.rs#L1754-L1759

Added lines #L1754 - L1759 were not covered by tests
ClementNerma marked this conversation as resolved.
Show resolved Hide resolved
} else {
entry = self.history.save(entry).expect("todo: error handling");
self.history_last_run_id = entry.id;
self.history_excluded_item = None;
self.history.save(&entry).expect("todo: error handling");

Check warning on line 1761 in src/engine.rs

View check run for this annotation

Codecov / codecov/patch

src/engine.rs#L1761

Added line #L1761 was not covered by tests
}

self.history_last_run_id = Some(entry_id);
self.history_excluded_item = if filtered { Some(entry) } else { None };

Check warning on line 1765 in src/engine.rs

View check run for this annotation

Codecov / codecov/patch

src/engine.rs#L1764-L1765

Added lines #L1764 - L1765 were not covered by tests
}

self.run_edit_commands(&[EditCommand::Clear]);
self.editor.reset_undo_stack();

Expand Down
Loading