1
0
mirror of https://github.com/pcvolkmer/checkbar.git synced 2025-04-19 11:06:50 +00:00

Provide custom time format config option

This commit is contained in:
Paul-Christian Volkmer 2023-10-19 10:47:53 +02:00
parent 01bc8e7ade
commit 4eb609c9b5
4 changed files with 17 additions and 4 deletions

View File

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

View File

@ -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![],
}

View File

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

View File

@ -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;
}
});