-
Notifications
You must be signed in to change notification settings - Fork 162
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
add cwd aware hinter #647
Merged
Merged
add cwd aware hinter #647
Changes from 4 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
3ce7488
add cwd aware hinter
p00f 3e02a1e
handle the case where get_current_dir returns Err
p00f 51f05e6
WIP cwd aware hinter
p00f 00c59e4
document that CwdAwareHinter is only compatible with sqlite history
p00f 05b9d5e
handle non-sqlite history
p00f c9766b1
handle no sqlite feature in example
p00f 94fa7d6
fix warnings
p00f File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
// Create a reedline object with in-line hint support. | ||
// cargo run --example cwd_aware_hinter | ||
// | ||
// Fish-style cwd history based hinting | ||
// assuming history ["abc", "ade"] | ||
// pressing "a" hints to abc. | ||
// 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; | ||
|
||
use reedline::HistoryItem; | ||
HistoryItem { | ||
id: None, | ||
start_timestamp: None, | ||
command_line: cmd.to_string(), | ||
session_id: None, | ||
hostname: Some("foohost".to_string()), | ||
cwd: Some(cwd.to_string()), | ||
duration: Some(Duration::from_millis(1000)), | ||
exit_status: Some(exit_status), | ||
more_info: None, | ||
} | ||
} | ||
|
||
#[cfg(any(feature = "sqlite", feature = "sqlite-dynlib"))] | ||
fn create_filled_example_history(home_dir: &str, orig_dir: &str) -> Box<dyn reedline::History> { | ||
use reedline::{History, SqliteBackedHistory}; | ||
let mut history = 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(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(); | ||
Box::new(history) | ||
} | ||
|
||
fn main() -> io::Result<()> { | ||
#[cfg(any(feature = "sqlite", feature = "sqlite-dynlib"))] | ||
return { | ||
use nu_ansi_term::{Color, Style}; | ||
use reedline::{CwdAwareHinter, DefaultPrompt, Reedline, Signal}; | ||
|
||
let orig_dir = std::env::current_dir().unwrap(); | ||
#[allow(deprecated)] | ||
let home_dir = std::env::home_dir().unwrap(); | ||
|
||
let history = create_filled_example_history( | ||
&home_dir.to_string_lossy().to_string(), | ||
&orig_dir.to_string_lossy().to_string(), | ||
); | ||
|
||
let mut line_editor = Reedline::create() | ||
.with_hinter(Box::new( | ||
CwdAwareHinter::default().with_style(Style::new().italic().fg(Color::Yellow)), | ||
)) | ||
.with_history(history); | ||
|
||
let prompt = DefaultPrompt::default(); | ||
|
||
let mut iterations = 0; | ||
loop { | ||
if iterations % 2 == 0 { | ||
std::env::set_current_dir(&orig_dir).unwrap(); | ||
} else { | ||
std::env::set_current_dir(&home_dir).unwrap(); | ||
} | ||
let sig = line_editor.read_line(&prompt)?; | ||
match sig { | ||
Signal::Success(buffer) => { | ||
println!("We processed: {buffer}"); | ||
} | ||
Signal::CtrlD | Signal::CtrlC => { | ||
println!("\nAborted!"); | ||
break Ok(()); | ||
} | ||
} | ||
iterations += 1; | ||
} | ||
}; | ||
|
||
#[cfg(not(any(feature = "sqlite", feature = "sqlite-dynlib")))] | ||
panic!("cwd-aware hinter needs sqlite backed history"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
#[cfg(any(feature = "sqlite", feature = "sqlite-dynlib"))] | ||
use crate::{history::SearchQuery, Hinter, History}; | ||
#[cfg(any(feature = "sqlite", feature = "sqlite-dynlib"))] | ||
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"))] | ||
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, | ||
line: &str, | ||
#[allow(unused_variables)] pos: usize, | ||
history: &dyn History, | ||
use_ansi_coloring: bool, | ||
) -> String { | ||
self.current_hint = if line.chars().count() >= self.min_chars { | ||
history | ||
.search(SearchQuery::last_with_prefix_and_cwd( | ||
line.to_string(), | ||
history.session(), | ||
)) | ||
.expect("todo: error handling") | ||
.get(0) | ||
.map_or_else(String::new, |entry| { | ||
entry | ||
.command_line | ||
.get(line.len()..) | ||
.unwrap_or_default() | ||
.to_string() | ||
}) | ||
} else { | ||
String::new() | ||
}; | ||
|
||
if use_ansi_coloring && !self.current_hint.is_empty() { | ||
self.style.paint(&self.current_hint).to_string() | ||
} else { | ||
self.current_hint.clone() | ||
} | ||
} | ||
|
||
fn complete_hint(&self) -> String { | ||
self.current_hint.clone() | ||
} | ||
|
||
fn next_hint_token(&self) -> String { | ||
let mut reached_content = false; | ||
let result: String = self | ||
.current_hint | ||
.chars() | ||
.take_while(|c| match (c.is_whitespace(), reached_content) { | ||
(true, true) => false, | ||
(true, false) => true, | ||
(false, true) => true, | ||
(false, false) => { | ||
reached_content = true; | ||
true | ||
} | ||
}) | ||
.collect(); | ||
result | ||
} | ||
} | ||
|
||
#[cfg(any(feature = "sqlite", feature = "sqlite-dynlib"))] | ||
impl Default for CwdAwareHinter { | ||
fn default() -> Self { | ||
CwdAwareHinter { | ||
style: Style::new().fg(Color::LightGray), | ||
current_hint: String::new(), | ||
min_chars: 1, | ||
} | ||
} | ||
} | ||
|
||
#[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] | ||
pub fn with_style(mut self, style: Style) -> Self { | ||
self.style = style; | ||
self | ||
} | ||
|
||
/// A builder that sets the number of characters that have to be present to enable history hints | ||
#[must_use] | ||
pub fn with_min_chars(mut self, min_chars: usize) -> Self { | ||
self.min_chars = min_chars; | ||
self | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@sholderbach How does this look?