diff options
Diffstat (limited to 'src/macro_processor/main.rs')
-rw-r--r-- | src/macro_processor/main.rs | 44 |
1 files changed, 37 insertions, 7 deletions
diff --git a/src/macro_processor/main.rs b/src/macro_processor/main.rs index d8247d6..2435cf3 100644 --- a/src/macro_processor/main.rs +++ b/src/macro_processor/main.rs @@ -1,9 +1,31 @@ use skaldpress::macro_processor::MacroProcessor; use std::env; use std::fs; +#[cfg(not(feature = "readline"))] use std::io; +#[cfg(not(feature = "readline"))] use std::io::Write; +#[cfg(feature = "readline")] +use std::ffi::{CStr, CString}; +#[cfg(feature = "readline")] +use std::os::raw::c_char; + +#[cfg(feature = "readline")] +#[link(name = "readline")] +extern "C" { + fn readline(p: *const c_char) -> *const c_char; +} + +#[cfg(feature = "readline")] +fn readline_r(prompt: &str) -> String { + let c_prompt = CString::new(prompt).expect("Could not convert to c_string"); + unsafe { + let ret = readline(c_prompt.as_ptr()); + CStr::from_ptr(ret).to_string_lossy().into_owned() + } +} + fn repl() { println!("=Skaldpress Macro Processor (REPL)"); println!(" type \"quit\" to exit"); @@ -11,14 +33,22 @@ fn repl() { loop { let mut input = String::new(); loop { - print!("> "); - let _ = io::stdout().flush(); let mut _input = String::new(); - io::stdin() - .read_line(&mut _input) - .expect("error: unable to read user input"); - if _input == "\n" { - input.pop(); + + #[cfg(feature = "readline")] + { + _input = readline_r("> ") + } + #[cfg(not(feature = "readline"))] + { + print!("> "); + let _ = io::stdout().flush(); + io::stdin() + .read_line(&mut _input) + .expect("error: unable to read user input"); + } + + if _input == "" { break; } if _input == "quit\n" { |