|
| 1 | +// Create a reedline object with in-line hint support. |
| 2 | +// cargo run --example cwd_aware_hinter |
| 3 | +// |
| 4 | +// Fish-style cwd history based hinting |
| 5 | +// assuming history ["abc", "ade"] |
| 6 | +// pressing "a" hints to abc. |
| 7 | +// Up/Down or Ctrl p/n, to select next/previous match |
| 8 | + |
| 9 | +use std::io; |
| 10 | + |
| 11 | +fn create_item(cwd: &str, cmd: &str, exit_status: i64) -> reedline::HistoryItem { |
| 12 | + use std::time::Duration; |
| 13 | + |
| 14 | + use reedline::HistoryItem; |
| 15 | + HistoryItem { |
| 16 | + id: None, |
| 17 | + start_timestamp: None, |
| 18 | + command_line: cmd.to_string(), |
| 19 | + session_id: None, |
| 20 | + hostname: Some("foohost".to_string()), |
| 21 | + cwd: Some(cwd.to_string()), |
| 22 | + duration: Some(Duration::from_millis(1000)), |
| 23 | + exit_status: Some(exit_status), |
| 24 | + more_info: None, |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +fn create_filled_example_history(home_dir: &str, orig_dir: &str) -> Box<dyn reedline::History> { |
| 29 | + use reedline::History; |
| 30 | + #[cfg(not(any(feature = "sqlite", feature = "sqlite-dynlib")))] |
| 31 | + let mut history = Box::new(reedline::FileBackedHistory::new(100)); |
| 32 | + #[cfg(any(feature = "sqlite", feature = "sqlite-dynlib"))] |
| 33 | + let mut history = Box::new(reedline::SqliteBackedHistory::in_memory().unwrap()); |
| 34 | + |
| 35 | + history.save(create_item(orig_dir, "dummy", 0)).unwrap(); // add dummy item so ids start with 1 |
| 36 | + history.save(create_item(orig_dir, "ls /usr", 0)).unwrap(); |
| 37 | + history.save(create_item(orig_dir, "pwd", 0)).unwrap(); |
| 38 | + |
| 39 | + history.save(create_item(home_dir, "cat foo", 0)).unwrap(); |
| 40 | + history.save(create_item(home_dir, "ls bar", 0)).unwrap(); |
| 41 | + history.save(create_item(home_dir, "rm baz", 0)).unwrap(); |
| 42 | + |
| 43 | + history |
| 44 | +} |
| 45 | + |
| 46 | +fn main() -> io::Result<()> { |
| 47 | + use nu_ansi_term::{Color, Style}; |
| 48 | + use reedline::{CwdAwareHinter, DefaultPrompt, Reedline, Signal}; |
| 49 | + |
| 50 | + let orig_dir = std::env::current_dir().unwrap(); |
| 51 | + #[allow(deprecated)] |
| 52 | + let home_dir = std::env::home_dir().unwrap(); |
| 53 | + |
| 54 | + let history = create_filled_example_history( |
| 55 | + &home_dir.to_string_lossy().to_string(), |
| 56 | + &orig_dir.to_string_lossy().to_string(), |
| 57 | + ); |
| 58 | + |
| 59 | + let mut line_editor = Reedline::create() |
| 60 | + .with_hinter(Box::new( |
| 61 | + CwdAwareHinter::default().with_style(Style::new().italic().fg(Color::Yellow)), |
| 62 | + )) |
| 63 | + .with_history(history); |
| 64 | + |
| 65 | + let prompt = DefaultPrompt::default(); |
| 66 | + |
| 67 | + let mut iterations = 0; |
| 68 | + loop { |
| 69 | + if iterations % 2 == 0 { |
| 70 | + std::env::set_current_dir(&orig_dir).unwrap(); |
| 71 | + } else { |
| 72 | + std::env::set_current_dir(&home_dir).unwrap(); |
| 73 | + } |
| 74 | + let sig = line_editor.read_line(&prompt)?; |
| 75 | + match sig { |
| 76 | + Signal::Success(buffer) => { |
| 77 | + println!("We processed: {buffer}"); |
| 78 | + } |
| 79 | + Signal::CtrlD | Signal::CtrlC => { |
| 80 | + println!("\nAborted!"); |
| 81 | + break Ok(()); |
| 82 | + } |
| 83 | + } |
| 84 | + iterations += 1; |
| 85 | + } |
| 86 | +} |
0 commit comments