From 047a2980cb3cdc23ee5adafba2faf61458124ea5 Mon Sep 17 00:00:00 2001 From: Roey Darwish Dror Date: Thu, 31 May 2018 16:00:01 +0300 Subject: [PATCH] Add terminal separator --- Cargo.lock | 27 +++++++++++++++++++++++++++ Cargo.toml | 1 + src/main.rs | 12 +++++++++++- src/terminal.rs | 29 +++++++++++++++++++++++++++++ 4 files changed, 68 insertions(+), 1 deletion(-) create mode 100644 src/terminal.rs diff --git a/Cargo.lock b/Cargo.lock index df05e9ca..314e2061 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -101,6 +101,19 @@ name = "quote" version = "0.3.15" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "redox_syscall" +version = "0.1.40" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "redox_termios" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "redox_syscall 0.1.40 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "regex" version = "0.2.11" @@ -153,6 +166,16 @@ dependencies = [ "syn 0.11.11 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "termion" +version = "1.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "libc 0.2.41 (registry+https://github.com/rust-lang/crates.io-index)", + "redox_syscall 0.1.40 (registry+https://github.com/rust-lang/crates.io-index)", + "redox_termios 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "thread_local" version = "0.3.5" @@ -168,6 +191,7 @@ version = "0.1.0" dependencies = [ "error-chain 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)", "os_type 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)", + "termion 1.5.1 (registry+https://github.com/rust-lang/crates.io-index)", "which 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -242,12 +266,15 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum os_type 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "081f0539f57611638feee0d72fe98ad0b685e99da117be6b254bd1dfd7421609" "checksum pkg-config 0.3.11 (registry+https://github.com/rust-lang/crates.io-index)" = "110d5ee3593dbb73f56294327fe5668bcc997897097cbc76b51e7aed3f52452f" "checksum quote 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)" = "7a6e920b65c65f10b2ae65c831a81a073a89edd28c7cce89475bff467ab4167a" +"checksum redox_syscall 0.1.40 (registry+https://github.com/rust-lang/crates.io-index)" = "c214e91d3ecf43e9a4e41e578973adeb14b474f2bee858742d127af75a0112b1" +"checksum redox_termios 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "7e891cfe48e9100a70a3b6eb652fef28920c117d366339687bd5576160db0f76" "checksum regex 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)" = "9329abc99e39129fcceabd24cf5d85b4671ef7c29c50e972bc5afe32438ec384" "checksum regex-syntax 0.5.6 (registry+https://github.com/rust-lang/crates.io-index)" = "7d707a4fa2637f2dca2ef9fd02225ec7661fe01a53623c1e6515b6916511f7a7" "checksum rustc-demangle 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)" = "76d7ba1feafada44f2d38eed812bd2489a03c0f5abb975799251518b68848649" "checksum syn 0.11.11 (registry+https://github.com/rust-lang/crates.io-index)" = "d3b891b9015c88c576343b9b3e41c2c11a51c219ef067b264bd9c8aa9b441dad" "checksum synom 0.11.3 (registry+https://github.com/rust-lang/crates.io-index)" = "a393066ed9010ebaed60b9eafa373d4b1baac186dd7e008555b0f702b51945b6" "checksum synstructure 0.6.1 (registry+https://github.com/rust-lang/crates.io-index)" = "3a761d12e6d8dcb4dcf952a7a89b475e3a9d69e4a69307e01a470977642914bd" +"checksum termion 1.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "689a3bdfaab439fd92bc87df5c4c78417d3cbe537487274e9b0b2dce76e92096" "checksum thread_local 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)" = "279ef31c19ededf577bfd12dfae728040a21f635b06a24cd670ff510edd38963" "checksum ucd-util 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "fd2be2d6639d0f8fe6cdda291ad456e23629558d466e2789d2c3e9892bda285d" "checksum unicode-xid 0.0.4 (registry+https://github.com/rust-lang/crates.io-index)" = "8c1f860d7d29cf02cb2f3f359fd35991af3d30bac52c57d265a3c461074cb4dc" diff --git a/Cargo.toml b/Cargo.toml index e4499c8c..d5ed2f9b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,3 +7,4 @@ authors = ["Roey Darwish Dror "] which = "2.0.0" error-chain = "0.11.0" os_type = "2.0.0" +termion = "1.5.1" diff --git a/src/main.rs b/src/main.rs index 3362988d..6b4f6bc8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,6 +2,7 @@ extern crate os_type; extern crate which; #[macro_use] extern crate error_chain; +extern crate termion; mod error { error_chain!{ @@ -13,6 +14,7 @@ mod error { } mod git; +mod terminal; use error::*; use git::Git; @@ -21,6 +23,7 @@ use std::collections::HashSet; use std::env::home_dir; use std::path::PathBuf; use std::process::{Command, ExitStatus}; +use terminal::Terminal; use which::which; trait Chain @@ -65,6 +68,7 @@ fn tpm() -> Option { fn run() -> Result<()> { let git = Git::new(); let mut git_repos: HashSet = HashSet::new(); + let terminal = Terminal::new(); { let mut collect_repo = |path| { @@ -83,12 +87,13 @@ fn run() -> Result<()> { } for repo in git_repos { - terminal.print_seperator(format!("Pulling {}", repo)); + terminal.print_separator(format!("Pulling {}", repo)); git.pull(repo)?; } if cfg!(unix) { if let Ok(zsh) = which("zsh") { + terminal.print_separator("zplug"); if home_path(".zplug").exists() { Command::new(&zsh) .arg("-c") @@ -99,6 +104,7 @@ fn run() -> Result<()> { } if let Some(tpm) = tpm() { + terminal.print_separator("tmux plugins"); Command::new(&tpm).arg("all").spawn()?.wait()?; } } @@ -108,6 +114,7 @@ fn run() -> Result<()> { match os_type::current_platform().os_type { OSType::Arch => { + terminal.print_separator("System update"); if let Ok(yay) = which("yay") { Command::new(yay).spawn()?.wait()?; } else { @@ -125,6 +132,7 @@ fn run() -> Result<()> { if let Ok(sudo) = &sudo { if let Ok(needrestart) = which("needrestart") { + terminal.print_separator("Check for needed restarts"); Command::new(&sudo).arg(&needrestart).spawn()?.wait()?; } } @@ -132,6 +140,7 @@ fn run() -> Result<()> { if cfg!(target_os = "macos") { if let Ok(brew) = which("brew") { + terminal.print_separator("Homebrew"); Command::new(&brew) .arg("update") .spawn()? @@ -146,6 +155,7 @@ fn run() -> Result<()> { })?; } + terminal.print_separator("System update"); Command::new("softwareupdate") .arg("--install") .arg("--all") diff --git a/src/terminal.rs b/src/terminal.rs new file mode 100644 index 00000000..1b883971 --- /dev/null +++ b/src/terminal.rs @@ -0,0 +1,29 @@ +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(); + 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); + } + } +}