use std::cmp::{max, min}; use termion; use termion::color; pub struct Terminal { width: Option, } impl Terminal { pub fn new() -> Self { Self { width: termion::terminal_size().map(|(w, _)| w).ok(), } } pub fn print_separator>(&self, message: P) { let message = message.as_ref(); match self.width { Some(width) => { println!( "\n{}―― {} {:―^border$}{}", color::Fg(color::LightWhite), message, "", color::Fg(color::Reset), border = max(2, min(80, width as usize) - 3 - message.len()) ); } None => { println!("―― {} ――", message); } } } pub fn print_warning>(&self, message: P) { let message = message.as_ref(); match self.width { Some(_) => { println!( "{}{}{}", color::Fg(color::LightYellow), message, color::Fg(color::Reset) ); } None => { println!("{}", message); } } } pub fn print_result>(&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" }); } } } }