Use cross-platform terminal handling (#34)
This commit is contained in:
17
src/linux.rs
17
src/linux.rs
@@ -52,7 +52,7 @@ impl Distribution {
|
||||
|
||||
pub fn upgrade_arch_linux(
|
||||
sudo: &Option<PathBuf>,
|
||||
terminal: &Terminal,
|
||||
terminal: &mut Terminal,
|
||||
) -> Result<(), failure::Error> {
|
||||
if let Some(yay) = which("yay") {
|
||||
if let Some(python) = which("python") {
|
||||
@@ -82,7 +82,10 @@ It's dangerous to run yay since Python based AUR packages will be installed in t
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn upgrade_redhat(sudo: &Option<PathBuf>, terminal: &Terminal) -> Result<(), failure::Error> {
|
||||
pub fn upgrade_redhat(
|
||||
sudo: &Option<PathBuf>,
|
||||
terminal: &mut Terminal,
|
||||
) -> Result<(), failure::Error> {
|
||||
if let Some(sudo) = &sudo {
|
||||
Command::new(&sudo)
|
||||
.args(&["yum", "upgrade"])
|
||||
@@ -96,7 +99,10 @@ pub fn upgrade_redhat(sudo: &Option<PathBuf>, terminal: &Terminal) -> Result<(),
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn upgrade_fedora(sudo: &Option<PathBuf>, terminal: &Terminal) -> Result<(), failure::Error> {
|
||||
pub fn upgrade_fedora(
|
||||
sudo: &Option<PathBuf>,
|
||||
terminal: &mut Terminal,
|
||||
) -> Result<(), failure::Error> {
|
||||
if let Some(sudo) = &sudo {
|
||||
Command::new(&sudo)
|
||||
.args(&["dnf", "upgrade"])
|
||||
@@ -110,7 +116,10 @@ pub fn upgrade_fedora(sudo: &Option<PathBuf>, terminal: &Terminal) -> Result<(),
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn upgrade_debian(sudo: &Option<PathBuf>, terminal: &Terminal) -> Result<(), failure::Error> {
|
||||
pub fn upgrade_debian(
|
||||
sudo: &Option<PathBuf>,
|
||||
terminal: &mut Terminal,
|
||||
) -> Result<(), failure::Error> {
|
||||
if let Some(sudo) = &sudo {
|
||||
Command::new(&sudo)
|
||||
.args(&["apt", "update"])
|
||||
|
||||
13
src/main.rs
13
src/main.rs
@@ -3,7 +3,6 @@ extern crate failure;
|
||||
extern crate which;
|
||||
#[macro_use]
|
||||
extern crate failure_derive;
|
||||
extern crate termion;
|
||||
extern crate toml;
|
||||
#[macro_use]
|
||||
extern crate serde_derive;
|
||||
@@ -14,6 +13,8 @@ extern crate shellexpand;
|
||||
#[macro_use]
|
||||
extern crate log;
|
||||
extern crate env_logger;
|
||||
extern crate term_size;
|
||||
extern crate termcolor;
|
||||
|
||||
mod config;
|
||||
mod git;
|
||||
@@ -94,7 +95,7 @@ fn run() -> Result<(), Error> {
|
||||
env_logger::init();
|
||||
let git = Git::new();
|
||||
let mut git_repos = Repositories::new(&git);
|
||||
let terminal = Terminal::new();
|
||||
let mut terminal = Terminal::new();
|
||||
let config = Config::read()?;
|
||||
let mut reports = Report::new();
|
||||
|
||||
@@ -116,11 +117,11 @@ fn run() -> Result<(), Error> {
|
||||
match linux::Distribution::detect() {
|
||||
Ok(distribution) => {
|
||||
match distribution {
|
||||
linux::Distribution::Arch => linux::upgrade_arch_linux(&sudo, &terminal),
|
||||
linux::Distribution::CentOS => linux::upgrade_redhat(&sudo, &terminal),
|
||||
linux::Distribution::Fedora => linux::upgrade_fedora(&sudo, &terminal),
|
||||
linux::Distribution::Arch => linux::upgrade_arch_linux(&sudo, &mut terminal),
|
||||
linux::Distribution::CentOS => linux::upgrade_redhat(&sudo, &mut terminal),
|
||||
linux::Distribution::Fedora => linux::upgrade_fedora(&sudo, &mut terminal),
|
||||
linux::Distribution::Ubuntu | linux::Distribution::Debian => {
|
||||
linux::upgrade_debian(&sudo, &terminal)
|
||||
linux::upgrade_debian(&sudo, &mut terminal)
|
||||
}
|
||||
}.report("System upgrade", &mut reports);
|
||||
}
|
||||
|
||||
@@ -1,79 +1,70 @@
|
||||
use std::cmp::{max, min};
|
||||
use termion;
|
||||
use termion::color;
|
||||
use std::io::Write;
|
||||
use term_size;
|
||||
use termcolor::{Color, ColorChoice, ColorSpec, StandardStream, WriteColor};
|
||||
|
||||
pub struct Terminal {
|
||||
width: Option<u16>,
|
||||
width: Option<usize>,
|
||||
stdout: StandardStream,
|
||||
}
|
||||
|
||||
impl Terminal {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
width: termion::terminal_size().map(|(w, _)| w).ok(),
|
||||
width: term_size::dimensions().map(|(w, _)| w),
|
||||
stdout: StandardStream::stdout(ColorChoice::Auto),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn print_separator<P: AsRef<str>>(&self, message: P) {
|
||||
pub fn print_separator<P: AsRef<str>>(&mut self, message: P) {
|
||||
let message = message.as_ref();
|
||||
match self.width {
|
||||
Some(width) => {
|
||||
println!(
|
||||
"\n{}―― {} {:―^border$}{}",
|
||||
color::Fg(color::LightWhite),
|
||||
let _ = self.stdout
|
||||
.set_color(ColorSpec::new().set_fg(Some(Color::White)).set_bold(true));
|
||||
let _ = write!(
|
||||
&mut self.stdout,
|
||||
"\n―― {} {:―^border$}\n",
|
||||
message,
|
||||
"",
|
||||
color::Fg(color::Reset),
|
||||
border = max(2, min(80, width as usize) - 3 - message.len())
|
||||
);
|
||||
let _ = self.stdout.reset();
|
||||
let _ = self.stdout.flush();
|
||||
}
|
||||
None => {
|
||||
println!("―― {} ――", message);
|
||||
let _ = write!(&mut self.stdout, "―― {} ――\n", message);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn print_warning<P: AsRef<str>>(&self, message: P) {
|
||||
pub fn print_warning<P: AsRef<str>>(&mut 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);
|
||||
}
|
||||
}
|
||||
let _ = self.stdout
|
||||
.set_color(ColorSpec::new().set_fg(Some(Color::Yellow)).set_bold(true));
|
||||
let _ = write!(&mut self.stdout, "{}", message);
|
||||
let _ = self.stdout.reset();
|
||||
let _ = self.stdout.flush();
|
||||
}
|
||||
|
||||
pub fn print_result<P: AsRef<str>>(&self, key: P, succeeded: bool) {
|
||||
pub fn print_result<P: AsRef<str>>(&mut self, key: P, succeeded: bool) {
|
||||
let key = key.as_ref();
|
||||
let _ = write!(&mut self.stdout, "{}: ", key);
|
||||
|
||||
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" });
|
||||
}
|
||||
}
|
||||
let _ = self.stdout.set_color(
|
||||
ColorSpec::new()
|
||||
.set_fg(Some(if succeeded { Color::Green } else { Color::Red }))
|
||||
.set_bold(true),
|
||||
);
|
||||
|
||||
let _ = write!(
|
||||
&mut self.stdout,
|
||||
"{}",
|
||||
if succeeded { "OK" } else { "FAILED" }
|
||||
);
|
||||
|
||||
let _ = self.stdout.reset();
|
||||
let _ = self.stdout.flush();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user