Impl way to set the time format

This commit is contained in:
Stephen Seo 2022-08-11 11:47:49 +09:00
parent f2a0c3dcd9
commit a1956b852a
4 changed files with 27 additions and 31 deletions

View file

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

View 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();
}

View 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);

View 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 {