Skip to content

Commit

Permalink
Merge commit 'f47d32ba034f906f60143953582ccf91e4377677'
Browse files Browse the repository at this point in the history
  • Loading branch information
h1romas4 committed Jun 26, 2023
2 parents 805d67b + f47d32b commit f654cad
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 32 deletions.
2 changes: 2 additions & 0 deletions .zellij-datetime.kdl
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ timezone {
}

defalut_timezone "JST"

background_color "#202020"
3 changes: 2 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "zellij-datetime"
version = "0.15.0"
version = "0.16.0"
authors = ["h1romas4 <[email protected]>"]
edition = "2021"

Expand All @@ -9,8 +9,9 @@ zellij-tile = "0.37.2"
zellij-tile-utils = "0.37.2"
ansi_term = "^0.12"
chrono-wasi = "^0.4"
kdl = { version = "4.5.0" }
linked-hash-map = "0.5.6"
kdl = { version = "^4.5" }
linked-hash-map = "^0.5"
csscolorparser = "^0.6"

[profile.release]
lto = true
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Since it is a WebAssembly/WASI build, it will probably work in all environments,
- [x] Support for timezone definition files.
- [x] Binary size reduction.
- [ ] Improved parsing of configuration files.
- [ ] Support for background color specification.
- [x] Support for background color specification.
- [ ] When a Zellij session is detached and reattached, the plugin stops without getting drawing and timer events.
- [ ] Unnecessary borderlines appear when this plugin is placed at the bottom of the workspace with borderless=true.

Expand Down Expand Up @@ -102,6 +102,8 @@ timezone {
}
defalut_timezone "JST"
background_color "#202020"
```

https://github.com/h1romas4/zellij-datetime/assets/4337664/bc6af70a-9211-44bc-aea6-8a9e54389070
Expand Down
25 changes: 23 additions & 2 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
use csscolorparser::Color;
use kdl::KdlDocument;
use linked_hash_map::LinkedHashMap;

static DEFALUT_BG_COLOR: (u8, u8, u8) = (32, 32, 32);

pub struct Config {
timezone: LinkedHashMap<String, i32>,
default_timezone: String,
backgound_color: (u8, u8, u8),
}

impl Default for Config {
Expand All @@ -17,6 +21,7 @@ impl Default for Config {
Config {
timezone,
default_timezone: default_timezone.to_string(),
backgound_color: DEFALUT_BG_COLOR,
}
}
}
Expand Down Expand Up @@ -51,7 +56,7 @@ impl Config {
}
let timezone = match prev {
Some(prev) => prev,
None => self.timezone.keys().last().unwrap() , // last key
None => self.timezone.keys().last().unwrap(), // last key
};
timezone.to_string()
}
Expand All @@ -63,10 +68,14 @@ impl Config {
}
}

pub fn get_backgound_color(&self) -> (u8, u8, u8) {
self.backgound_color
}

pub fn load_config(&mut self, setting: &str) {
let mut config_timezone: LinkedHashMap<String, i32> = LinkedHashMap::new();
if let Ok(doc) = setting.parse::<KdlDocument>() {
// timezone tree (TODO: using KQL)
// timezone tree (TODO: using KQL or macro)
if let Some(timezone) = doc.get("timezone") {
if let Some(children) = timezone.children() {
for node in children.nodes() {
Expand Down Expand Up @@ -95,6 +104,18 @@ impl Config {
self.default_timezone = self.timezone.keys().next().unwrap().to_string();
}
}
// backgound color
if let Some(backgound_color) = doc.get_arg("background_color") {
if let Ok(color) = backgound_color
.to_string()
.replace('"', "")
.trim()
.parse::<Color>()
{
let color = color.to_rgba8();
self.backgound_color = (color[0], color[1], color[2]);
}
}
}
}
}
44 changes: 25 additions & 19 deletions src/line.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ pub struct Line {
fg_color: PaletteColor,
bg_color: PaletteColor,
datetime_bg_color: PaletteColor,
sp_1: String,
sp_2: String,
sp_3: String,
separator: (String, String, String),
}

impl Line {
Expand All @@ -22,23 +20,24 @@ impl Line {
self.bg_color = style.colors.bg;
self.datetime_bg_color = PaletteColor::Rgb(datetime_bg_color);
// create charctor
let bg1 = self.bg_color;
let bg2 = self.datetime_bg_color;
let arrow = &style!(bg2, bg2).bold().paint(ARROW_SPACE).to_string();
let sep_1 = &style!(bg2, bg1).bold().paint(ARROW_SEPARATOR_1).to_string();
let sep_2 = &style!(bg1, bg2).bold().paint(ARROW_SEPARATOR_2).to_string();
self.sp_1 = String::new();
self.sp_1.push_str(sep_1);
self.sp_1.push_str(arrow);
self.sp_2 = String::new();
self.sp_2.push_str(arrow);
self.sp_2.push_str(sep_2);
self.sp_2.push_str(arrow);
self.sp_3 = String::new();
self.sp_3.push_str(arrow);
let bg_1 = self.bg_color;
let bg_2 = self.datetime_bg_color;
let arrow = &style!(bg_2, bg_2).bold().paint(ARROW_SPACE).to_string();
let sep_1 = &style!(bg_2, bg_1).bold().paint(ARROW_SEPARATOR_1).to_string();
let sep_2 = &style!(bg_1, bg_2).bold().paint(ARROW_SEPARATOR_2).to_string();
let mut sp_0 = String::new();
sp_0.push_str(sep_1);
sp_0.push_str(arrow);
let mut sp_1 = String::new();
sp_1.push_str(arrow);
sp_1.push_str(sep_2);
sp_1.push_str(arrow);
let mut sp_2 = String::new();
sp_2.push_str(arrow);
self.separator = (sp_0, sp_1, sp_2);
}

pub fn render(&self, cols: usize, timezone: &str, date: &str, time: &str) -> String {
pub fn create(&self, cols: usize, timezone: &str, date: &str, time: &str) -> String {
// padding (support full width)
let timezone_len = timezone
.chars()
Expand All @@ -61,7 +60,14 @@ impl Line {

format!(
"{}{}{}{}{}{}{}{}",
padding, self.sp_1, timezone, self.sp_2, date, self.sp_2, time, self.sp_3
padding,
self.separator.0,
timezone,
self.separator.1,
date,
self.separator.1,
time,
self.separator.2
)
}
}
10 changes: 4 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@ use zellij_tile::prelude::*;
use crate::config::Config;
use crate::line::Line;

// FIXME: DateTime backgorund color
static DATETIME_BG_COLOR: (u8, u8, u8) = (32, 32, 32);

static INTERVAL_TIME: f64 = 1.0;

#[derive(Default)]
Expand Down Expand Up @@ -77,7 +74,8 @@ impl ZellijPlugin for State {
Event::ModeUpdate(mode_info) => {
if self.style != mode_info.style {
self.style = mode_info.style;
self.line.update_style(self.style, DATETIME_BG_COLOR);
self.line
.update_style(self.style, self.config.get_backgound_color());
}
}
Event::Mouse(mouse) => match mouse {
Expand All @@ -98,7 +96,7 @@ impl ZellijPlugin for State {
render = true;
}
_ => {}
}
},
_ => {}
}
render
Expand All @@ -118,7 +116,7 @@ impl ZellijPlugin for State {
hour = now.hour(),
minute = now.minute(),
);
print!("{}", self.line.render(cols, &self.timezone, &date, &time));
print!("{}", self.line.create(cols, &self.timezone, &date, &time));
}
}
}
Expand Down

0 comments on commit f654cad

Please sign in to comment.