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

View File

@@ -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<Terminal> = Mutex::new(Terminal::new());
@@ -44,6 +53,7 @@ impl Terminal {
}
fn print_separator<P: AsRef<str>>(&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<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());
}
}