Set the terminal title according to the current step

This commit is contained in:
Roey Darwish Dror
2019-07-31 11:53:05 +03:00
parent 4a28090896
commit a7b1ad31f1
3 changed files with 28 additions and 0 deletions

1
Cargo.lock generated
View File

@@ -1906,6 +1906,7 @@ dependencies = [
"toml 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)", "toml 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)",
"walkdir 2.2.8 (registry+https://github.com/rust-lang/crates.io-index)", "walkdir 2.2.8 (registry+https://github.com/rust-lang/crates.io-index)",
"which 2.0.1 (registry+https://github.com/rust-lang/crates.io-index)", "which 2.0.1 (registry+https://github.com/rust-lang/crates.io-index)",
"winapi 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)",
] ]
[[package]] [[package]]

View File

@@ -35,6 +35,9 @@ openssl-probe = { version = "0.1.2", optional = true }
[target.'cfg(unix)'.dependencies] [target.'cfg(unix)'.dependencies]
nix = "0.14.0" nix = "0.14.0"
[target.'cfg(windows)'.dependencies]
winapi = { version = "0.3", features = ["wincon"] }
[profile.release] [profile.release]
lto = true lto = true

View File

@@ -3,9 +3,18 @@ use console::{style, Term};
use lazy_static::lazy_static; use lazy_static::lazy_static;
use std::cmp::{max, min}; use std::cmp::{max, min};
use std::env; use std::env;
#[cfg(windows)]
use std::ffi::OsStr;
use std::fmt::Display;
use std::io::{self, Write}; use std::io::{self, Write};
#[cfg(windows)]
use std::iter::once;
#[cfg(windows)]
use std::os::windows::ffi::OsStrExt;
use std::process::Command; use std::process::Command;
use std::sync::Mutex; use std::sync::Mutex;
#[cfg(windows)]
use winapi::um::wincon::SetConsoleTitleW;
lazy_static! { lazy_static! {
static ref TERMINAL: Mutex<Terminal> = Mutex::new(Terminal::new()); static ref TERMINAL: Mutex<Terminal> = Mutex::new(Terminal::new());
@@ -44,6 +53,7 @@ impl Terminal {
} }
fn print_separator<P: AsRef<str>>(&mut self, message: P) { fn print_separator<P: AsRef<str>>(&mut self, message: P) {
set_title(format!("{}Topgrade - {}", self.prefix, message.as_ref()));
let now = Local::now(); let now = Local::now();
let message = format!( let message = format!(
"{}{:02}:{:02}:{:02} - {}", "{}{:02}:{:02}:{:02} - {}",
@@ -117,6 +127,7 @@ impl Terminal {
return Ok(false); return Ok(false);
} }
set_title("Topgrade - Awaiting user");
self.term self.term
.write_fmt(format_args!( .write_fmt(format_args!(
"\n{}", "\n{}",
@@ -194,3 +205,16 @@ pub fn is_dumb() -> bool {
pub fn get_char() -> char { pub fn get_char() -> char {
TERMINAL.lock().unwrap().get_char().unwrap() TERMINAL.lock().unwrap().get_char().unwrap()
} }
#[cfg(unix)]
pub fn set_title<T: Display>(title: T) {
print!("\x1b]0;{}\x07", title);
}
#[cfg(windows)]
pub fn set_title<T: Display>(title: T) {
let buffer: Vec<u16> = OsStr::new(&format!("{}", title)).encode_wide().chain(once(0)).collect();
unsafe {
SetConsoleTitleW(buffer.as_ptr());
}
}