From c9766b1ed2b5b40e1519348ed00c319bd6c23289 Mon Sep 17 00:00:00 2001 From: Chinmay Dalal Date: Fri, 20 Oct 2023 00:36:21 +0530 Subject: [PATCH] handle no sqlite feature in example --- examples/cwd_aware_hinter.rs | 77 +++++++++++++++++------------------- 1 file changed, 37 insertions(+), 40 deletions(-) diff --git a/examples/cwd_aware_hinter.rs b/examples/cwd_aware_hinter.rs index ece3e26b..f7674b38 100644 --- a/examples/cwd_aware_hinter.rs +++ b/examples/cwd_aware_hinter.rs @@ -1,4 +1,3 @@ -#![allow(dead_code)] // Create a reedline object with in-line hint support. // cargo run --example cwd_aware_hinter // @@ -8,6 +7,8 @@ // Up/Down or Ctrl p/n, to select next/previous match use std::io; + +use reedline::FileBackedHistory; fn create_item(cwd: &str, cmd: &str, exit_status: i64) -> reedline::HistoryItem { use std::time::Duration; @@ -25,9 +26,11 @@ fn create_item(cwd: &str, cmd: &str, exit_status: i64) -> reedline::HistoryItem } } -#[cfg(any(feature = "sqlite", feature = "sqlite-dynlib"))] fn create_filled_example_history(home_dir: &str, orig_dir: &str) -> Box { - use reedline::{History, SqliteBackedHistory}; + use reedline::History; + #[cfg(not(any(feature = "sqlite", feature = "sqlite-dynlib")))] + let mut history = FileBackedHistory::new(100); + #[cfg(any(feature = "sqlite", feature = "sqlite-dynlib"))] 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(); @@ -40,49 +43,43 @@ fn create_filled_example_history(home_dir: &str, orig_dir: &str) -> Box io::Result<()> { - #[cfg(any(feature = "sqlite", feature = "sqlite-dynlib"))] - return { - use nu_ansi_term::{Color, Style}; - use reedline::{CwdAwareHinter, DefaultPrompt, Reedline, Signal}; + 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 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 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 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 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 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}"); } - let sig = line_editor.read_line(&prompt)?; - match sig { - Signal::Success(buffer) => { - println!("We processed: {buffer}"); - } - Signal::CtrlD | Signal::CtrlC => { - println!("\nAborted!"); - break Ok(()); - } + 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"); + iterations += 1; + } }