Add terminal separator

This commit is contained in:
Roey Darwish Dror
2018-05-31 16:00:01 +03:00
parent f2633ac4ee
commit 047a2980cb
4 changed files with 68 additions and 1 deletions

29
src/terminal.rs Normal file
View File

@@ -0,0 +1,29 @@
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();
if let Some(width) = self.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));
} else {
println!("―― {} ――", message);
}
}
}