From 4eb609c9b53232875010e6b5849c34731df76f0a Mon Sep 17 00:00:00 2001 From: Paul-Christian Volkmer Date: Thu, 19 Oct 2023 10:47:53 +0200 Subject: [PATCH] Provide custom time format config option --- README.adoc | 6 ++++++ src/config.rs | 7 +++++++ src/lib.rs | 6 +++--- src/main.rs | 2 +- 4 files changed, 17 insertions(+), 4 deletions(-) diff --git a/README.adoc b/README.adoc index 038f0eb..9720337 100644 --- a/README.adoc +++ b/README.adoc @@ -12,6 +12,9 @@ You should create a configuration file `.checkbar.toml` in your home directory, # Update interval in seconds. Default value if not set is 60 sec. interval = 60 +# Time format (optional), defaults to "%R" (= "%H:%M)" +time_format = "%H:%M:%S" # = "%T" + [[checks]] name = "Host 1" url = "https://host1.example.com" @@ -49,6 +52,9 @@ interval = 60 interval = "2m 30s" ---- +The value for `time_format` is used to set the time format to be shown. It defaults to `%R`. +For available options, see: https://docs.rs/chrono/latest/chrono/format/strftime/index.html + Each host or application to be checked consists of `name` and `url`. You can optionally specify `check_type`: diff --git a/src/config.rs b/src/config.rs index 742a267..e62805f 100644 --- a/src/config.rs +++ b/src/config.rs @@ -13,6 +13,8 @@ pub struct Config { deserialize_with = "deserialize_duration" )] pub interval: Duration, + #[serde(default = "Config::default_time_format")] + pub time_format: String, #[serde(default)] pub colors: ColorConfig, #[serde(default)] @@ -20,6 +22,10 @@ pub struct Config { } impl Config { + fn default_time_format() -> String { + return "%R".to_string(); + } + fn get_config_file() -> String { match env::args().nth(1) { Some(config_file) => config_file, @@ -46,6 +52,7 @@ impl Default for Config { fn default() -> Self { Self { interval: Duration::from_secs(60), + time_format: Self::default_time_format(), colors: ColorConfig::default(), checks: vec![], } diff --git a/src/lib.rs b/src/lib.rs index 8843a2c..f539abd 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -24,15 +24,15 @@ pub struct ClickEvent { pub button: MouseButton, } -pub async fn print_states(check_configs: &[CheckConfig]) { +pub async fn print_states(config: &Config) { print!("["); let mut entries = vec![]; - for check_config in check_configs { + for check_config in &config.checks { entries.push(format!("{}", check_host(check_config).await)); } entries.push( json!({ - "full_text": chrono::Local::now().format("%H:%M").to_string() + "full_text": chrono::Local::now().format(config.time_format.as_str()).to_string() }) .to_string(), ); diff --git a/src/main.rs b/src/main.rs index 2626ae9..b1796b3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -31,7 +31,7 @@ async fn main() { let checks = task::spawn(async { loop { let config = Config::read(); - print_states(&config.checks).await; + print_states(&config).await; let _ = sleep(config.interval).await; } });