From a1956b852acd89c3ad74a604363345af6d6898f9 Mon Sep 17 00:00:00 2001 From: Stephen Seo Date: Thu, 11 Aug 2022 11:47:49 +0900 Subject: [PATCH] Impl way to set the time format --- README.md | 7 ++++++- src/args.rs | 8 ++++++++ src/main.rs | 13 +++++++++++-- src/swaybar_object.rs | 30 ++---------------------------- 4 files changed, 27 insertions(+), 31 deletions(-) diff --git a/README.md b/README.md index 9e26f47..cb3aea2 100644 --- a/README.md +++ b/README.md @@ -20,7 +20,8 @@ tiling Wayland compositor](https://swaywm.org). cargo install swaybar_info # The `swaybar_info` binary should be placed in $HOME/.cargo/bin/ -Put the following in your `~/.config/sway/config`: +Put the following in your `~/.config/sway/config` (assuming the binary is at +`$HOME/.config/sway/swaybar_info`): bar { position top @@ -41,6 +42,10 @@ Put the following in your `~/.config/sway/config`: # This example gets battery info into the bar. # Multiple args should be separated with "[SPLIT]". # Note that the portion is optional. + + + # The following uses 24 hour time + #status_command $HOME/.config/sway/swaybar_info --time-format="%Y-%m-%d %R:%S" } ## Dependencies diff --git a/src/args.rs b/src/args.rs index 6ce46b1..352e684 100644 --- a/src/args.rs +++ b/src/args.rs @@ -27,6 +27,9 @@ pub fn get_args() -> ArgsResult { } else if arg.starts_with("--regex-cmd=") { let (_, back) = arg.split_at(12); regex_cmds.push(back.to_owned()); + } else if arg.starts_with("--time-format=") { + let (_, back) = arg.split_at(14); + map.insert("time-format".into(), back.to_owned()); } else if arg == "--help" || arg == "-h" { map.insert("help".into(), "".into()); } else { @@ -64,4 +67,9 @@ pub fn print_usage() { b" --regex-cmd=[SPLIT][SPLIT]\tUse an output of a command as a metric\n", ) .ok(); + stderr_handle + .write_all( + b" --date-format=\t\t\tSet the format string for the date\n", + ) + .ok(); } diff --git a/src/main.rs b/src/main.rs index 53ff188..b0324c1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -8,6 +8,8 @@ use std::io::{self, Write}; use std::time::Duration; use swaybar_object::*; +const DEFAULT_FMT_STRING: &str = "%F %r"; + fn main() { let args_result = args::get_args(); if args_result.map.contains_key("help") { @@ -77,6 +79,11 @@ fn main() { let batt_info_enabled: bool = args_result.map.contains_key("acpi-builtin"); let mut batt_info_error: bool = false; + let mut time_fmt_str = DEFAULT_FMT_STRING; + if let Some(s) = args_result.map.get("time-format") { + time_fmt_str = s; + } + println!( "{}", serde_json::to_string(&swaybar_object::SwaybarHeader::new()) @@ -258,9 +265,11 @@ fn main() { // time if is_empty { - array.push_object(SwaybarObject::default()); + let mut time_obj = SwaybarObject::new("current_time".to_owned()); + time_obj.update_as_date(time_fmt_str); + array.push_object(time_obj); } else if let Some(time_obj) = array.get_by_name_mut("current_time") { - time_obj.update_as_date(); + time_obj.update_as_date(time_fmt_str); } println!("{}", array); diff --git a/src/swaybar_object.rs b/src/swaybar_object.rs index 2fc9d48..4f5e7d6 100644 --- a/src/swaybar_object.rs +++ b/src/swaybar_object.rs @@ -149,9 +149,9 @@ impl SwaybarObject { self.color = Some("#88ff88ff".to_owned()); } - pub fn update_as_date(&mut self) { + pub fn update_as_date(&mut self, format_str: &str) { let current_time: DateTime = Local::now(); - let current_time = current_time.format("%F %r"); + let current_time = current_time.format(format_str); self.full_text = current_time.to_string(); self.color = None; } @@ -175,32 +175,6 @@ impl SwaybarObject { } } -impl Default for SwaybarObject { - fn default() -> Self { - let current_time: DateTime = Local::now(); - let current_time = current_time.format("%F %r"); - Self { - full_text: current_time.to_string(), - short_text: None, - color: None, - background: None, - border: Some("#ffffffff".into()), - border_top: None, - border_bottom: None, - border_left: None, - border_right: None, - min_width: None, - align: None, - name: Some("current_time".to_owned()), - instance: None, - urgent: None, - separator: None, - separator_block_width: None, - markup: None, - } - } -} - impl SwaybarArray { pub fn new() -> Self { Self {