use std::io;
use std::io::BufRead;
use std::string::String;
use install_framework_base::interface::SimpleInterpreter;
use crate::error::CliError;
pub struct CliInterpreter
{
step: String,
substep: String
}
impl CliInterpreter
{
pub fn new() -> CliInterpreter
{
return CliInterpreter
{
step: String::new(),
substep: String::new()
}
}
}
pub fn read_console() -> Result<String, CliError>
{
if let Some(res) = io::stdin().lock().lines().next()
{
match res
{
Ok(data) => return Ok(data),
Err(e) => return Err(CliError::Io(e))
};
}
return Err(CliError::Generic(String::from("Could not read line from console")));
}
impl SimpleInterpreter<CliError> for CliInterpreter
{
fn read_user_input_text(&mut self, msg: &str) -> Result<String, CliError>
{
println!("{}:", msg);
return read_console();
}
fn begin_step(&mut self, message: &str) -> Result<(), CliError>
{
println!("{}", message);
self.step = String::from(message);
return Ok(());
}
fn update_step(&mut self, value: f32) -> Result<(), CliError>
{
println!("{} ({:.2}%)", self.step, value * 100.0);
return Ok(());
}
fn end_step(&mut self) -> Result<(), CliError>
{
println!("{} (OK)", self.step);
return Ok(());
}
fn begin_substep(&mut self, message: &str) -> Result<(), CliError>
{
print!("{}\r", message);
self.substep = String::from(message);
return Ok(());
}
fn update_substep(&mut self, value: f32) -> Result<(), CliError>
{
print!("{} ({:.2}%)\r", self.substep, value * 100.0);
return Ok(());
}
fn end_substep(&mut self) -> Result<(), CliError>
{
println!("{} (OK) ", self.substep);
return Ok(());
}
}