Use the console crate
This commit is contained in:
13
Cargo.lock
generated
13
Cargo.lock
generated
@@ -463,16 +463,6 @@ dependencies = [
|
||||
"unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "term_size"
|
||||
version = "0.3.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"libc 0.2.43 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "termcolor"
|
||||
version = "1.0.4"
|
||||
@@ -539,8 +529,6 @@ dependencies = [
|
||||
"serde_derive 1.0.80 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"shellexpand 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"structopt 0.2.12 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"term_size 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"termcolor 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"toml 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"walkdir 2.2.6 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"which 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
@@ -711,7 +699,6 @@ dependencies = [
|
||||
"checksum structopt-derive 0.2.12 (registry+https://github.com/rust-lang/crates.io-index)" = "17ff01fe96de9d16e7372ae5f19dd7ece2c703b51043c3db9ea27f9e393ea311"
|
||||
"checksum syn 0.15.17 (registry+https://github.com/rust-lang/crates.io-index)" = "3391038ebc3e4ab24eb028cb0ef2f2dc4ba0cbf72ee895ed6a6fad730640b5bc"
|
||||
"checksum synstructure 0.10.1 (registry+https://github.com/rust-lang/crates.io-index)" = "73687139bf99285483c96ac0add482c3776528beac1d97d444f6e91f203a2015"
|
||||
"checksum term_size 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "9e5b9a66db815dcfd2da92db471106457082577c3c278d4138ab3e3b4e189327"
|
||||
"checksum termcolor 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)" = "4096add70612622289f2fdcdbd5086dc81c1e2675e6ae58d6c4f62a16c6d7f2f"
|
||||
"checksum termion 1.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "689a3bdfaab439fd92bc87df5c4c78417d3cbe537487274e9b0b2dce76e92096"
|
||||
"checksum termios 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "72b620c5ea021d75a735c943269bb07d30c9b77d6ac6b236bc8b5c496ef05625"
|
||||
|
||||
@@ -19,8 +19,6 @@ shellexpand = "1.0.0"
|
||||
structopt = "0.2.12"
|
||||
log = "0.4.6"
|
||||
env_logger = "0.5.13"
|
||||
term_size = "0.3.1"
|
||||
termcolor = "1.0.4"
|
||||
walkdir = "2.2.6"
|
||||
console = "0.6.2"
|
||||
|
||||
|
||||
@@ -19,8 +19,6 @@ extern crate nix;
|
||||
#[cfg(unix)]
|
||||
#[macro_use]
|
||||
extern crate lazy_static;
|
||||
extern crate term_size;
|
||||
extern crate termcolor;
|
||||
extern crate walkdir;
|
||||
|
||||
#[cfg(target_os = "linux")]
|
||||
|
||||
@@ -1,19 +1,18 @@
|
||||
use console::Term;
|
||||
use console::{style, Term};
|
||||
use std::cmp::{max, min};
|
||||
use std::io::{self, Write};
|
||||
use term_size;
|
||||
use termcolor::{Color, ColorChoice, ColorSpec, StandardStream, WriteColor};
|
||||
|
||||
pub struct Terminal {
|
||||
width: Option<usize>,
|
||||
stdout: StandardStream,
|
||||
width: Option<u16>,
|
||||
term: Term,
|
||||
}
|
||||
|
||||
impl Terminal {
|
||||
pub fn new() -> Self {
|
||||
let term = Term::stdout();
|
||||
Self {
|
||||
width: term_size::dimensions().map(|(w, _)| w),
|
||||
stdout: StandardStream::stdout(ColorChoice::Auto),
|
||||
width: term.size_checked().map(|(_, w)| w),
|
||||
term,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,21 +20,19 @@ impl Terminal {
|
||||
let message = message.as_ref();
|
||||
match self.width {
|
||||
Some(width) => {
|
||||
let _ = self
|
||||
.stdout
|
||||
.set_color(ColorSpec::new().set_fg(Some(Color::White)).set_bold(true));
|
||||
let _ = writeln!(
|
||||
&mut self.stdout,
|
||||
"\n―― {} {:―^border$}",
|
||||
message,
|
||||
"",
|
||||
border = max(2, min(80, width as usize) - 3 - message.len())
|
||||
println!(
|
||||
"{}",
|
||||
style(format!(
|
||||
"\n―― {} {:―^border$}",
|
||||
message,
|
||||
"",
|
||||
border = max(2, min(80, width as usize) - 3 - message.len())
|
||||
)).bold()
|
||||
.white()
|
||||
);
|
||||
let _ = self.stdout.reset();
|
||||
let _ = self.stdout.flush();
|
||||
}
|
||||
None => {
|
||||
let _ = writeln!(&mut self.stdout, "―― {} ――", message);
|
||||
println!("―― {} ――", message);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -43,29 +40,21 @@ impl Terminal {
|
||||
#[allow(dead_code)]
|
||||
pub fn print_warning<P: AsRef<str>>(&mut self, message: P) {
|
||||
let message = message.as_ref();
|
||||
|
||||
let _ = self
|
||||
.stdout
|
||||
.set_color(ColorSpec::new().set_fg(Some(Color::Yellow)).set_bold(true));
|
||||
let _ = writeln!(&mut self.stdout, "{}", message);
|
||||
let _ = self.stdout.reset();
|
||||
let _ = self.stdout.flush();
|
||||
println!("{}", style(message).yellow().bold());
|
||||
}
|
||||
|
||||
pub fn print_result<P: AsRef<str>>(&mut self, key: P, succeeded: bool) {
|
||||
let key = key.as_ref();
|
||||
let _ = write!(&mut self.stdout, "{}: ", key);
|
||||
|
||||
let _ = self.stdout.set_color(
|
||||
ColorSpec::new()
|
||||
.set_fg(Some(if succeeded { Color::Green } else { Color::Red }))
|
||||
.set_bold(true),
|
||||
println!(
|
||||
"{}: {}",
|
||||
key,
|
||||
if succeeded {
|
||||
style("OK").bold().green()
|
||||
} else {
|
||||
style("FAILED").bold().red()
|
||||
}
|
||||
);
|
||||
|
||||
let _ = writeln!(&mut self.stdout, "{}", if succeeded { "OK" } else { "FAILED" });
|
||||
|
||||
let _ = self.stdout.reset();
|
||||
let _ = self.stdout.flush();
|
||||
}
|
||||
|
||||
pub fn should_retry(&mut self, running: bool) -> Result<bool, io::Error> {
|
||||
@@ -75,17 +64,21 @@ impl Terminal {
|
||||
|
||||
println!();
|
||||
loop {
|
||||
let _ = self
|
||||
.stdout
|
||||
.set_color(ColorSpec::new().set_fg(Some(Color::Yellow)).set_bold(true));
|
||||
let _ = write!(&mut self.stdout, "Retry? [y/N] ");
|
||||
if !running {
|
||||
write!(&mut self.stdout, "(Press Ctrl+C again to stop Topgrade) ");
|
||||
}
|
||||
let _ = self.stdout.reset();
|
||||
let _ = self.stdout.flush();
|
||||
self.term
|
||||
.write_fmt(format_args!(
|
||||
"{}",
|
||||
style(format!(
|
||||
"Retry? [y/N] {}",
|
||||
if !running {
|
||||
"(Press Ctrl+C again to stop Topgrade) "
|
||||
} else {
|
||||
""
|
||||
}
|
||||
)).yellow()
|
||||
.bold()
|
||||
)).ok();
|
||||
|
||||
match Term::stdout().read_char()? {
|
||||
match self.term.read_char()? {
|
||||
'y' | 'Y' => return Ok(true),
|
||||
'n' | 'N' | '\r' | '\n' => return Ok(false),
|
||||
_ => (),
|
||||
|
||||
Reference in New Issue
Block a user