Files
topgrade/src/terminal.rs

78 lines
2.1 KiB
Rust
Raw Normal View History

2018-05-31 16:00:01 +03:00
use std::cmp::{max, min};
use termion;
use termion::color;
pub struct Terminal {
width: Option<u16>,
}
impl Terminal {
pub fn new() -> Self {
Self {
width: termion::terminal_size().map(|(w, _)| w).ok(),
}
}
pub fn print_separator<P: AsRef<str>>(&self, message: P) {
let message = message.as_ref();
2018-06-04 23:23:25 +03:00
match self.width {
Some(width) => {
print!("\n{}―― {} ", color::Fg(color::LightWhite), message);
let border = max(2, min(80, width as usize) - 3 - message.len());
for _ in 0..border {
print!("");
}
println!("{}", color::Fg(color::Reset));
}
None => {
println!("―― {} ――", message);
2018-05-31 16:00:01 +03:00
}
}
}
2018-06-03 16:43:53 +03:00
pub fn print_warning<P: AsRef<str>>(&self, message: P) {
2018-06-04 23:23:25 +03:00
let message = message.as_ref();
match self.width {
Some(_) => {
println!(
"{}{}{}",
color::Fg(color::LightYellow),
message,
color::Fg(color::Reset)
);
}
None => {
println!("{}", message);
}
2018-06-03 16:43:53 +03:00
}
}
2018-06-03 18:04:58 +03:00
pub fn print_result<P: AsRef<str>>(&self, key: P, succeeded: bool) {
let key = key.as_ref();
match self.width {
Some(_) => {
if succeeded {
println!(
"{}: {}OK{}",
key,
color::Fg(color::LightGreen),
color::Fg(color::Reset)
);
} else {
println!(
"{}: {}FAILED{}",
key,
color::Fg(color::LightRed),
color::Fg(color::Reset)
);
}
}
None => {
println!("{}: {}", key, if succeeded { "OK" } else { "FAILED" });
}
}
}
2018-05-31 16:00:01 +03:00
}