Skip to content

Commit

Permalink
handle non-sqlite history
Browse files Browse the repository at this point in the history
  • Loading branch information
p00f committed Oct 19, 2023
1 parent 00c59e4 commit 26628b7
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 12 deletions.
1 change: 0 additions & 1 deletion examples/cwd_aware_hinter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
// Up/Down or Ctrl p/n, to select next/previous match
use std::io;

#[cfg(any(feature = "sqlite", feature = "sqlite-dynlib"))]
fn create_item(cwd: &str, cmd: &str, exit_status: i64) -> reedline::HistoryItem {
use std::time::Duration;

Expand Down
26 changes: 17 additions & 9 deletions src/hinter/cwd_aware.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
#[cfg(any(feature = "sqlite", feature = "sqlite-dynlib"))]
use crate::{history::SearchQuery, Hinter, History};
#[cfg(any(feature = "sqlite", feature = "sqlite-dynlib"))]
use crate::{
history::SearchQuery,
result::{ReedlineError, ReedlineErrorVariants::HistoryFeatureUnsupported},
Hinter, History,
};
use nu_ansi_term::{Color, Style};

/// A hinter that uses the completions or the history to show a hint to the user
/// NOTE: Only use this with `SqliteBackedHistory`!
/// Similar to `fish` autosuggestins
#[cfg(any(feature = "sqlite", feature = "sqlite-dynlib"))]
///
/// Similar to `fish` autosuggestions
pub struct CwdAwareHinter {
style: Style,
current_hint: String,
min_chars: usize,
}

#[cfg(any(feature = "sqlite", feature = "sqlite-dynlib"))]
impl Hinter for CwdAwareHinter {
fn handle(
&mut self,
Expand All @@ -28,6 +28,16 @@ impl Hinter for CwdAwareHinter {
line.to_string(),
history.session(),
))
.or_else(|err| {
if let ReedlineError(HistoryFeatureUnsupported { .. }) = err {
history.search(SearchQuery::last_with_prefix(
line.to_string(),
history.session(),
))

Check warning on line 36 in src/hinter/cwd_aware.rs

View check run for this annotation

Codecov / codecov/patch

src/hinter/cwd_aware.rs#L18-L36

Added lines #L18 - L36 were not covered by tests
} else {
Err(err)

Check warning on line 38 in src/hinter/cwd_aware.rs

View check run for this annotation

Codecov / codecov/patch

src/hinter/cwd_aware.rs#L38

Added line #L38 was not covered by tests
}
})
.expect("todo: error handling")
.get(0)
.map_or_else(String::new, |entry| {
Expand Down Expand Up @@ -71,7 +81,6 @@ impl Hinter for CwdAwareHinter {
}

Check warning on line 81 in src/hinter/cwd_aware.rs

View check run for this annotation

Codecov / codecov/patch

src/hinter/cwd_aware.rs#L78-L81

Added lines #L78 - L81 were not covered by tests
}

#[cfg(any(feature = "sqlite", feature = "sqlite-dynlib"))]
impl Default for CwdAwareHinter {
fn default() -> Self {
CwdAwareHinter {
Expand All @@ -82,7 +91,6 @@ impl Default for CwdAwareHinter {
}

Check warning on line 91 in src/hinter/cwd_aware.rs

View check run for this annotation

Codecov / codecov/patch

src/hinter/cwd_aware.rs#L85-L91

Added lines #L85 - L91 were not covered by tests
}

#[cfg(any(feature = "sqlite", feature = "sqlite-dynlib"))]
impl CwdAwareHinter {
/// A builder that sets the style applied to the hint as part of the buffer
#[must_use]
Expand Down
1 change: 0 additions & 1 deletion src/hinter/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
mod cwd_aware;
mod default;
#[cfg(any(feature = "sqlite", feature = "sqlite-dynlib"))]
pub use cwd_aware::CwdAwareHinter;
pub use default::DefaultHinter;

Expand Down
1 change: 0 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,6 @@ mod completion;
pub use completion::{Completer, DefaultCompleter, Span, Suggestion};

mod hinter;
#[cfg(any(feature = "sqlite", feature = "sqlite-dynlib"))]
pub use hinter::CwdAwareHinter;
pub use hinter::{DefaultHinter, Hinter};

Expand Down

0 comments on commit 26628b7

Please sign in to comment.