Add option to control the time display (fix #860)

This commit is contained in:
Roey Darwish Dror 2022-03-29 03:42:47 +03:00
parent 5166f651ea
commit 13104a029c
4 changed files with 45 additions and 19 deletions

View File

@ -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

View File

@ -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<String>,
tmux_arguments: Option<String>,
set_title: Option<bool>,
display_time: Option<bool>,
assume_yes: Option<bool>,
yay_arguments: Option<String>,
no_retry: Option<bool>,
@ -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<Self> {
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)
}
}

View File

@ -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!());

View File

@ -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<PathBuf>,
@ -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<P: AsRef<str>>(&self, message: P, timeout: Option<Duration>) {
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<bool, io::Error> {
pub fn notify_desktop<P: AsRef<str>>(message: P, timeout: Option<Duration>) {
TERMINAL.lock().unwrap().notify_desktop(message, timeout)
}
pub fn display_time(display_time: bool) {
TERMINAL.lock().unwrap().display_time(display_time);
}