Skip to content

Commit

Permalink
Merge pull request #5 from EDToaster/main
Browse files Browse the repository at this point in the history
feat: support custom date and time strftime strings
  • Loading branch information
h1romas4 authored May 7, 2024
2 parents 47cc142 + 4eb7e9f commit fca7248
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 12 deletions.
3 changes: 3 additions & 0 deletions plugin.kdl
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ layout {
timezone5 "NPT/+5.75"
// Testing that it will be in the specified timezone instead of timezone1
default_timezone "JST"
// Specify custom format strings
date_format "%Y/%m/%d %A"
time_format "%I:%M %p"
// Testing the Color Parser
background_color "#0080a0"
foreground_color "#ffffff"
Expand Down
18 changes: 18 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ static DEFAULT_TEXT_ALIGN: &str = "right";
pub struct Config {
timezone: LinkedHashMap<String, f64>,
default_timezone: String,
date_format: String,
time_format: String,
background_color: (u8, u8, u8),
foreground_color: (u8, u8, u8),
pane_color: (u8, u8, u8),
Expand All @@ -33,6 +35,8 @@ impl Default for Config {
Config {
timezone,
default_timezone: default_timezone.to_string(),
date_format: "%Y-%m-%d %a".to_string(),
time_format: "%H:%M".to_string(),
background_color: parse_color(DEFAULT_BACKGROUND_COLOR).unwrap(),
foreground_color: parse_color(DEFAULT_FOREGROUND_COLOR).unwrap(),
pane_color: parse_color(DEFAULT_PANE_COLOR).unwrap(),
Expand Down Expand Up @@ -91,6 +95,14 @@ impl Config {
}
}

pub fn get_date_format(&self) -> &String {
&self.date_format
}

pub fn get_time_format(&self) -> &String {
&self.time_format
}

pub fn get_background_color(&self) -> (u8, u8, u8) {
self.background_color
}
Expand Down Expand Up @@ -143,6 +155,12 @@ impl Config {
"default_timezone" => {
default_timezone = Some(value.trim().to_string());
}
"date_format" => {
self.date_format = value.trim().to_string();
}
"time_format" => {
self.time_format = value.trim().to_string();
}
"background_color" => {
if let Ok(color) = parse_color(value) {
self.background_color = (color.0, color.1, color.2);
Expand Down
14 changes: 2 additions & 12 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,18 +127,8 @@ impl ZellijPlugin for State {

fn render(&mut self, _rows: usize, cols: usize) {
if let Some(now) = self.now() {
let date = format!(
"{year}-{month:02}-{day:02} {weekday}",
year = now.year(),
month = now.month(),
day = now.day(),
weekday = now.weekday(),
);
let time = format!(
"{hour:02}:{minute:02}",
hour = now.hour(),
minute = now.minute(),
);
let date = now.format(self.config.get_date_format()).to_string();
let time = now.format(self.config.get_time_format()).to_string();
print!("{}", self.line.create(cols, &self.timezone, &date, &time));
}
}
Expand Down

0 comments on commit fca7248

Please sign in to comment.