Skip to content

Commit

Permalink
handle no sqlite feature in example
Browse files Browse the repository at this point in the history
  • Loading branch information
p00f committed Oct 19, 2023
1 parent 05b9d5e commit c9766b1
Showing 1 changed file with 37 additions and 40 deletions.
77 changes: 37 additions & 40 deletions examples/cwd_aware_hinter.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#![allow(dead_code)]
// Create a reedline object with in-line hint support.
// cargo run --example cwd_aware_hinter
//
Expand All @@ -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;

Expand All @@ -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<dyn reedline::History> {
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();
Expand All @@ -40,49 +43,43 @@ fn create_filled_example_history(home_dir: &str, orig_dir: &str) -> Box<dyn reed
}

fn main() -> 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;
}
}

0 comments on commit c9766b1

Please sign in to comment.