From a7b1ad31f19ca113ab63c8b5666d98c9e0f83fec Mon Sep 17 00:00:00 2001 From: Roey Darwish Dror Date: Wed, 31 Jul 2019 11:53:05 +0300 Subject: [PATCH] Set the terminal title according to the current step --- Cargo.lock | 1 + Cargo.toml | 3 +++ src/terminal.rs | 24 ++++++++++++++++++++++++ 3 files changed, 28 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index c004750d..74445ad5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1906,6 +1906,7 @@ dependencies = [ "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)", "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]] diff --git a/Cargo.toml b/Cargo.toml index a347aa56..99c8b91d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -35,6 +35,9 @@ openssl-probe = { version = "0.1.2", optional = true } [target.'cfg(unix)'.dependencies] nix = "0.14.0" +[target.'cfg(windows)'.dependencies] +winapi = { version = "0.3", features = ["wincon"] } + [profile.release] lto = true diff --git a/src/terminal.rs b/src/terminal.rs index 7fe4a502..261e8615 100644 --- a/src/terminal.rs +++ b/src/terminal.rs @@ -3,9 +3,18 @@ use console::{style, Term}; use lazy_static::lazy_static; use std::cmp::{max, min}; use std::env; +#[cfg(windows)] +use std::ffi::OsStr; +use std::fmt::Display; 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::sync::Mutex; +#[cfg(windows)] +use winapi::um::wincon::SetConsoleTitleW; lazy_static! { static ref TERMINAL: Mutex = Mutex::new(Terminal::new()); @@ -44,6 +53,7 @@ impl Terminal { } fn print_separator>(&mut self, message: P) { + set_title(format!("{}Topgrade - {}", self.prefix, message.as_ref())); let now = Local::now(); let message = format!( "{}{:02}:{:02}:{:02} - {}", @@ -117,6 +127,7 @@ impl Terminal { return Ok(false); } + set_title("Topgrade - Awaiting user"); self.term .write_fmt(format_args!( "\n{}", @@ -194,3 +205,16 @@ pub fn is_dumb() -> bool { pub fn get_char() -> char { TERMINAL.lock().unwrap().get_char().unwrap() } + +#[cfg(unix)] +pub fn set_title(title: T) { + print!("\x1b]0;{}\x07", title); +} + +#[cfg(windows)] +pub fn set_title(title: T) { + let buffer: Vec = OsStr::new(&format!("{}", title)).encode_wide().chain(once(0)).collect(); + unsafe { + SetConsoleTitleW(buffer.as_ptr()); + } +}