use super::*;
impl Globals {
#[cfg(feature = "line")]
pub(super) fn new_line_editor() -> rustyline::Editor<()> {
let mut editor = rustyline::Editor::<()>::with_config({
rustyline::Config::builder().auto_add_history(true).build()
});
if let Some(history_path) = crate::mtots_line_history_path() {
editor.load_history(&history_path).unwrap_or(());
}
editor
}
#[cfg(feature = "line")]
pub fn readline(&mut self, prompt: &str) -> Result<Option<String>> {
match self.line.readline(prompt) {
Ok(line) => Ok(Some(line)),
Err(rustyline::error::ReadlineError::Eof) => Ok(None),
Err(error) => Err(error.into()),
}
}
#[cfg(not(feature = "line"))]
pub fn readline(&mut self, prompt: &str) -> Result<Option<String>> {
use std::io::Write;
print!("{}", prompt);
std::io::stdout().flush().unwrap();
let mut buf = String::new();
let len = std::io::stdin().read_line(&mut buf)?;
Ok(if len == 0 { None } else { Some(buf) })
}
#[cfg(feature = "line")]
pub fn save_line_history(&mut self) -> Result<()> {
if let Some(path) = crate::mtots_line_history_path() {
self.line.save_history(&path)?;
}
Ok(())
}
#[cfg(not(feature = "line"))]
pub fn save_line_history(&mut self) -> Result<()> {
Ok(())
}
}