]> git.seodisparate.com - swaybar_info/commitdiff
Impl way to set the time format
authorStephen Seo <seo.disparate@gmail.com>
Thu, 11 Aug 2022 02:47:49 +0000 (11:47 +0900)
committerStephen Seo <seo.disparate@gmail.com>
Thu, 11 Aug 2022 02:47:49 +0000 (11:47 +0900)
README.md
src/args.rs
src/main.rs
src/swaybar_object.rs

index 9e26f47742d0014bdd549551de12241f01717405..cb3aea21be84b08c301c7e8c63a4ac92b09463fa 100644 (file)
--- 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 <args...> portion is optional.
+
+
+        # The following uses 24 hour time
+        #status_command $HOME/.config/sway/swaybar_info --time-format="%Y-%m-%d %R:%S"
     }
 
 ## Dependencies
index 6ce46b13e43a62bcd8499eb4705eeca63087e4ef..352e68440e601371165e4f268bfaef1f8e8757ba 100644 (file)
@@ -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=<cmd>[SPLIT]<args...>[SPLIT]<regex>\tUse an output of a command as a metric\n",
         )
         .ok();
+    stderr_handle
+        .write_all(
+            b"  --date-format=<date format string>\t\t\tSet the format string for the date\n",
+        )
+        .ok();
 }
index 53ff188396556523a729f601e6f3748ee6d7b918..b0324c113cf527fda9faaddca84b5cce6d0ce0ca 100644 (file)
@@ -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);
index 2fc9d48c84e74edfd31cce3221188caf6c3892e2..4f5e7d6838b662b30bef4918af1b799175b7a4fd 100644 (file)
@@ -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> = 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> = 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 {