From 13104a029ccae54e49c5dbb188c4e87fa4b98fb2 Mon Sep 17 00:00:00 2001 From: Roey Darwish Dror Date: Tue, 29 Mar 2022 03:42:47 +0300 Subject: [PATCH] Add option to control the time display (fix #860) --- config.example.toml | 3 +++ src/config.rs | 19 ++++++++----------- src/main.rs | 11 +++++++++++ src/terminal.rs | 31 +++++++++++++++++++++++-------- 4 files changed, 45 insertions(+), 19 deletions(-) diff --git a/config.example.toml b/config.example.toml index 512a2267..ac745708 100644 --- a/config.example.toml +++ b/config.example.toml @@ -31,6 +31,9 @@ # Do not set the terminal title #set_title = false +# Display the time in step titles +# display_time = true + # Cleanup temporary or old files #cleanup = true diff --git a/src/config.rs b/src/config.rs index 5da092c1..6e4e0a27 100644 --- a/src/config.rs +++ b/src/config.rs @@ -6,8 +6,7 @@ use std::{env, fs}; use anyhow::Result; use directories::BaseDirs; -use log::{debug, LevelFilter}; -use pretty_env_logger::formatted_timed_builder; +use log::debug; use regex::Regex; use serde::Deserialize; use structopt::StructOpt; @@ -247,6 +246,7 @@ pub struct ConfigFile { git_arguments: Option, tmux_arguments: Option, set_title: Option, + display_time: Option, assume_yes: Option, yay_arguments: Option, no_retry: Option, @@ -343,6 +343,7 @@ impl ConfigFile { fn edit(base_dirs: &BaseDirs) -> Result<()> { let config_path = Self::ensure(base_dirs)?; let editor = editor(); + debug!("Editor: {:?}", editor); let command = which(&editor[0])?; let args: Vec<&String> = editor.iter().skip(1).collect(); @@ -394,7 +395,7 @@ pub struct CommandLineArgs { /// Output logs #[structopt(short = "v", long = "verbose")] - verbose: bool, + pub verbose: bool, /// Prompt for a key before exiting #[structopt(short = "k", long = "keep")] @@ -447,14 +448,6 @@ impl Config { /// /// The function parses the command line arguments and reading the configuration file. pub fn load(base_dirs: &BaseDirs, opt: CommandLineArgs) -> Result { - let mut builder = formatted_timed_builder(); - - if opt.verbose { - builder.filter(Some("topgrade"), LevelFilter::Trace); - } - - builder.init(); - let config_directory = config_directory(base_dirs); let config_file = if config_directory.is_dir() { ConfigFile::read(base_dirs, opt.config.clone()).unwrap_or_else(|e| { @@ -872,4 +865,8 @@ impl Config { .and_then(|w| w.enable_winget) .unwrap_or(false); } + + pub fn display_time(&self) -> bool { + self.config_file.display_time.unwrap_or(true) + } } diff --git a/src/main.rs b/src/main.rs index 837aba26..c29779ed 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,6 +7,8 @@ use std::process::exit; use anyhow::{anyhow, Result}; use console::Key; use log::debug; +use log::LevelFilter; +use pretty_env_logger::formatted_timed_builder; use structopt::clap::crate_version; use structopt::StructOpt; @@ -38,6 +40,14 @@ fn run() -> Result<()> { let base_dirs = directories::BaseDirs::new().ok_or_else(|| anyhow!("No base directories"))?; let opt = CommandLineArgs::from_args(); + let mut builder = formatted_timed_builder(); + + if opt.verbose { + builder.filter(Some("topgrade"), LevelFilter::Trace); + } + + builder.init(); + if opt.edit_config() { Config::edit(&base_dirs)?; return Ok(()); @@ -50,6 +60,7 @@ fn run() -> Result<()> { let config = Config::load(&base_dirs, opt)?; terminal::set_title(config.set_title()); + terminal::display_time(config.display_time()); terminal::set_desktop_notifications(config.notify_each_step()); debug!("Version: {}", crate_version!()); diff --git a/src/terminal.rs b/src/terminal.rs index c41eacd0..ae51198c 100644 --- a/src/terminal.rs +++ b/src/terminal.rs @@ -48,6 +48,7 @@ struct Terminal { prefix: String, term: Term, set_title: bool, + display_time: bool, desktop_notification: bool, #[cfg(target_os = "linux")] notify_send: Option, @@ -63,6 +64,7 @@ impl Terminal { .map(|prefix| format!("({}) ", prefix)) .unwrap_or_else(|_| String::new()), set_title: true, + display_time: true, desktop_notification: false, #[cfg(target_os = "linux")] notify_send: which("notify-send"), @@ -77,6 +79,10 @@ impl Terminal { self.set_title = set_title } + fn display_time(&mut self, display_time: bool) { + self.display_time = display_time + } + #[allow(unused_variables)] fn notify_desktop>(&self, message: P, timeout: Option) { debug!("Desktop notification: {}", message.as_ref()); @@ -117,14 +123,19 @@ impl Terminal { } let now = Local::now(); - let message = format!( - "{}{:02}:{:02}:{:02} - {}", - self.prefix, - now.hour(), - now.minute(), - now.second(), - message.as_ref() - ); + let message = if self.display_time { + format!( + "{}{:02}:{:02}:{:02} - {}", + self.prefix, + now.hour(), + now.minute(), + now.second(), + message.as_ref() + ) + } else { + String::from(message.as_ref()) + }; + match self.width { Some(width) => { self.term @@ -311,3 +322,7 @@ pub fn prompt_yesno(question: &str) -> Result { pub fn notify_desktop>(message: P, timeout: Option) { TERMINAL.lock().unwrap().notify_desktop(message, timeout) } + +pub fn display_time(display_time: bool) { + TERMINAL.lock().unwrap().display_time(display_time); +}