Stop passing the terminal object
This commit is contained in:
@@ -1,14 +1,20 @@
|
||||
use console::{style, Term};
|
||||
use lazy_static::lazy_static;
|
||||
use std::cmp::{max, min};
|
||||
use std::io::{self, Write};
|
||||
use std::sync::Mutex;
|
||||
|
||||
pub struct Terminal {
|
||||
lazy_static! {
|
||||
static ref TERMINAL: Mutex<Terminal> = Mutex::new(Terminal::new());
|
||||
}
|
||||
|
||||
struct Terminal {
|
||||
width: Option<u16>,
|
||||
term: Term,
|
||||
}
|
||||
|
||||
impl Terminal {
|
||||
pub fn new() -> Self {
|
||||
fn new() -> Self {
|
||||
let term = Term::stdout();
|
||||
Self {
|
||||
width: term.size_checked().map(|(_, w)| w),
|
||||
@@ -16,56 +22,59 @@ impl Terminal {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn print_separator<P: AsRef<str>>(&mut self, message: P) {
|
||||
fn print_separator<P: AsRef<str>>(&mut self, message: P) {
|
||||
let message = message.as_ref();
|
||||
match self.width {
|
||||
Some(width) => {
|
||||
println!(
|
||||
"{}",
|
||||
style(format!(
|
||||
"\n―― {} {:―^border$}",
|
||||
message,
|
||||
"",
|
||||
border = max(2, min(80, width as usize) - 3 - message.len())
|
||||
)).bold()
|
||||
.white()
|
||||
);
|
||||
self.term
|
||||
.write_fmt(format_args!(
|
||||
"{}\n",
|
||||
style(format_args!(
|
||||
"\n―― {} {:―^border$}",
|
||||
message,
|
||||
"",
|
||||
border = max(2, min(80, width as usize) - 3 - message.len())
|
||||
)).bold()
|
||||
.white()
|
||||
)).ok();
|
||||
}
|
||||
None => {
|
||||
println!("―― {} ――", message);
|
||||
self.term.write_fmt(format_args!("―― {} ――\n", message)).ok();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub fn print_warning<P: AsRef<str>>(&mut self, message: P) {
|
||||
fn print_warning<P: AsRef<str>>(&mut self, message: P) {
|
||||
let message = message.as_ref();
|
||||
println!("{}", style(message).yellow().bold());
|
||||
self.term
|
||||
.write_fmt(format_args!("{}\n", style(message).yellow().bold()))
|
||||
.ok();
|
||||
}
|
||||
|
||||
pub fn print_result<P: AsRef<str>>(&mut self, key: P, succeeded: bool) {
|
||||
fn print_result<P: AsRef<str>>(&mut self, key: P, succeeded: bool) {
|
||||
let key = key.as_ref();
|
||||
|
||||
println!(
|
||||
"{}: {}",
|
||||
key,
|
||||
if succeeded {
|
||||
style("OK").bold().green()
|
||||
} else {
|
||||
style("FAILED").bold().red()
|
||||
}
|
||||
);
|
||||
self.term
|
||||
.write_fmt(format_args!(
|
||||
"{}: {}\n",
|
||||
key,
|
||||
if succeeded {
|
||||
style("OK").bold().green()
|
||||
} else {
|
||||
style("FAILED").bold().red()
|
||||
}
|
||||
)).ok();
|
||||
}
|
||||
|
||||
pub fn should_retry(&mut self, running: bool) -> Result<bool, io::Error> {
|
||||
fn should_retry(&mut self, running: bool) -> Result<bool, io::Error> {
|
||||
if self.width.is_none() {
|
||||
return Ok(false);
|
||||
}
|
||||
|
||||
println!();
|
||||
self.term
|
||||
.write_fmt(format_args!(
|
||||
"{}",
|
||||
"\n{}",
|
||||
style(format!(
|
||||
"Retry? [y/N] {}",
|
||||
if !running {
|
||||
@@ -85,7 +94,8 @@ impl Terminal {
|
||||
}
|
||||
};
|
||||
|
||||
println!();
|
||||
self.term.write_str("\n").ok();
|
||||
|
||||
answer
|
||||
}
|
||||
}
|
||||
@@ -95,3 +105,19 @@ impl Default for Terminal {
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
|
||||
pub fn should_retry(running: bool) -> Result<bool, io::Error> {
|
||||
TERMINAL.lock().unwrap().should_retry(running)
|
||||
}
|
||||
|
||||
pub fn print_separator<P: AsRef<str>>(message: P) {
|
||||
TERMINAL.lock().unwrap().print_separator(message)
|
||||
}
|
||||
|
||||
pub fn print_warning<P: AsRef<str>>(message: P) {
|
||||
TERMINAL.lock().unwrap().print_warning(message)
|
||||
}
|
||||
|
||||
pub fn print_result<P: AsRef<str>>(key: P, succeeded: bool) {
|
||||
TERMINAL.lock().unwrap().print_result(key, succeeded)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user