use std::io::{self, BufRead, Write};
use prettytable::{format, Cell, Row, Table};
use rpick::ui;
pub struct Cli {
verbose: bool,
}
impl Cli {
pub fn new(verbose: bool) -> Self {
Cli { verbose }
}
fn convert_row(row: &[ui::Cell], highlight: bool) -> Row {
let mut r = Row::empty();
for c in row {
let mut c = if let ui::Cell::Float(value) = c {
Cell::new(&format!("{:>6.2}%", value))
} else {
Cell::new(&String::from(c))
};
if highlight {
c = c.style_spec("bFy");
}
r.add_cell(c);
}
r
}
}
impl ui::Ui for Cli {
fn call_display_table(&self) -> bool {
self.verbose
}
fn display_table(&self, table: &ui::Table) {
let mut t = Table::new();
t.set_format(*format::consts::FORMAT_NO_BORDER_LINE_SEPARATOR);
t.set_titles(Cli::convert_row(&table.header, false));
for row in &table.rows {
t.add_row(Cli::convert_row(&row.cells, row.chosen));
}
t.add_row(Cli::convert_row(&table.footer, false));
println!();
t.printstd();
println!();
}
fn info(&self, message: &str) {
println!("{}", message);
}
fn prompt_choice(&self, choice: &str) -> bool {
print!("Choice is {}. Accept? (Y/n) ", choice);
io::stdout().flush().unwrap();
let line = io::stdin().lock().lines().next().unwrap().unwrap();
if ["", "y", "Y"].contains(&line.as_str()) {
return true;
}
false
}
}