Skip to content

Commit

Permalink
Merge commit 'eee50a142af3c87cecf15d0fedeb3720cdbe8fb0'
Browse files Browse the repository at this point in the history
  • Loading branch information
h1romas4 committed Sep 11, 2023
2 parents 31e9acc + eee50a1 commit 639d37e
Show file tree
Hide file tree
Showing 6 changed files with 216 additions and 35 deletions.
33 changes: 32 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,11 @@ layout {
foreground_color "#ffffff"
pane_color "#1e1e1e"
enable_right_click false
arrow_separator1 ""
arrow_separator2 ""
arrow_separator3 ""
padding_adjust 0
text_align "right"
}
}
}
Expand All @@ -92,9 +97,14 @@ layout {
| `timezone[2-9]` | `"name/offset"` | - | |
| `default_timezone` | `"name"` | `"UTC"` | |
| `background_color` | `"#color"` | `"#0080a0"` | |
| `foreground_color` | `"#color"` | `"#ffffff"` | It may be adjusted automatically depending on the `background_color` |
| `foreground_color` | `"#color"` | `"#ffffff"` | It may be adjusted automatically depending on the `background_color`. |
| `pane_color` | `"#color"` | `"#1e1e1e"` | |
| `enable_right_click` | bool | `false` | Right-clicking on the clock outputs the string format to stdin; Allow `PermissionType::WriteToStdin` permission when starting the plugin. |
| `arrow_separator1` | `"string"` | `""` | Delimiter string on line. Only the first character. |
| `arrow_separator2` | `"string"` | `""` | 📅 Only the first character. |
| `arrow_separator3` | `"string"` | `""` | ⌚ Only the first character. |
| `padding_adjust` | `i32` | `0` | It can be used to adjust left-justified padding. For example, adjusting the separator width if it is off by full-width. |
| `text_align` | `"string"` | `"right"` | `right` or `left` or `center` |

## Build

Expand Down Expand Up @@ -127,6 +137,25 @@ cp -p target/wasm32-wasi/release/zellij-datetime-snip.wasm ~/.config/zellij/plug
zellij
```

Development Test with container

- It can be tested on the main branch of Zellij.
- There is no confusion with the current Zellij session.
- You can test the operation of the plugin authority from the initial state.

```bash
git clone https://github.com/zellij-org/zellij.git
git clone https://github.com/h1romas4/zellij-datetime
# build Zellij (A newer version of protobuf-compiler is required)
cd zellij
cargo xtask build --release
# build zellij-datetime
cd ../zellij-datetime
cargo build
# Run with container (podman or docker)
podman run --name zellij-datetime --env SHELL=/usr/bin/bash -v ../zellij/target/release/:/opt/zellij -v .:/opt/zellij-datetime -w /opt/zellij-datetime -it --rm ubuntu:22.04 /opt/zellij/zellij -l plugin.kb
```

## License

MIT License
Expand All @@ -140,6 +169,8 @@ MIT License
- [x] Support for background color specification.
- [ ] When a Zellij session is detached and reattached, the plugin stops without getting drawing and timer events. [#2575](https://github.com/zellij-org/zellij/issues/2575)
- [x] Unnecessary borderlines appear when this plugin is placed at the bottom of the workspace with borderless=true.
- [ ] Display is disturbed when the screen width is smaller than the display string.
- [ ] Separate control of the line from control of datetime string generation.

## Note

Expand Down
35 changes: 33 additions & 2 deletions plugin.kdl
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,16 @@ layout {
background_color "#0080a0"
foreground_color "#ffffff"
pane_color "#1e1e1e"
// Testing Arrow
arrow_separator1 ""
arrow_separator2 ""
arrow_separator3 ""
padding_adjust 0
// Testing Plug-in Permissions
// To be tested at the first interactive query and at the automatic configuration cache.
enable_right_click true
// Testing Text align
text_align "right"
// Debugging options (not yet used)
enable_debug true
}
Expand All @@ -26,14 +33,38 @@ layout {
pane size=1 borderless=true {
plugin location="file:./target/wasm32-wasi/debug/zellij-datetime.wasm" {
// Testing that the optional keys are sorted
timezone4 "JST/+9"
timezone4 "日本/+9"
timezone3 "CEST/+2"
// Testing full-width string
default_timezone "日本"
// Testing Arrow render
arrow_separator1 "🌍"
arrow_separator2 "📅"
arrow_separator3 "⌚"
padding_adjust 1
pane_color "#0080a0"
text_align "center"
}
}
pane size=1 borderless=true {
plugin location="file:./target/wasm32-wasi/debug/zellij-datetime.wasm" {
timezone1 "CEST/+2"
timezone2 "JST/+9"
background_color "#000"
foreground_color "#0080a0"
pane_color "#1e1e1e"
// Testing Arrow render
arrow_separator1 ""
arrow_separator2 ""
arrow_separator3 ""
padding_adjust -2
text_align "left"
}
}
pane size=1 borderless=true {
plugin location="zellij:tab-bar"
}
pane
pane focus=true
pane command="tail" close_on_exit=true {
args "-f" "zellij.log"
cwd "/tmp/zellij-0/zellij-log"
Expand Down
2 changes: 1 addition & 1 deletion rust-toolchain
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[toolchain]
channel = "1.71.1"
channel = "1.72.0"
targets = [ "wasm32-wasi" ]
48 changes: 48 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ static DEFAULT_TIMEZONE: &str = "UTC";
static DEFAULT_BACKGROUND_COLOR: &str = "#0080a0";
static DEFAULT_FOREGROUND_COLOR: &str = "#ffffff";
static DEFAULT_PANE_COLOR: &str = "#1e1e1e";
static DEFAULT_ARROW_SEPARATOR_1: &str = "";
static DEFAULT_ARROW_SEPARATOR_2: &str = "";
static DEFAULT_ARROW_SEPARATOR_3: &str = "";
static DEFAULT_TEXT_ALIGN: &str = "right";

pub struct Config {
timezone: LinkedHashMap<String, i32>,
Expand All @@ -15,6 +19,9 @@ pub struct Config {
foreground_color: (u8, u8, u8),
pane_color: (u8, u8, u8),
enable_right_click: bool,
separator: (String, String, String),
padding_adjust: i32,
text_align: String,
enable_debug: bool,
}

Expand All @@ -30,6 +37,13 @@ impl Default for Config {
foreground_color: parse_color(DEFAULT_FOREGROUND_COLOR).unwrap(),
pane_color: parse_color(DEFAULT_PANE_COLOR).unwrap(),
enable_right_click: false,
separator: (
DEFAULT_ARROW_SEPARATOR_1.to_string(),
DEFAULT_ARROW_SEPARATOR_2.to_string(),
DEFAULT_ARROW_SEPARATOR_3.to_string(),
),
padding_adjust: 0,
text_align: DEFAULT_TEXT_ALIGN.to_string(),
enable_debug: false,
}
}
Expand Down Expand Up @@ -93,6 +107,18 @@ impl Config {
self.enable_right_click
}

pub fn get_separator(&self) -> &(String, String, String) {
&self.separator
}

pub fn get_padding_adjust(&self) -> i32 {
self.padding_adjust
}

pub fn get_text_align(&self) -> &String {
&self.text_align
}

#[allow(unused)]
pub fn get_enable_debug(&self) -> bool {
self.enable_debug
Expand Down Expand Up @@ -135,6 +161,21 @@ impl Config {
"enable_right_click" => {
self.enable_right_click = value.trim().parse().unwrap_or(false);
}
"arrow_separator1" => {
self.separator.0 = get_first_char_or_blank(value.trim());
}
"arrow_separator2" => {
self.separator.1 = get_first_char_or_blank(value.trim());
}
"arrow_separator3" => {
self.separator.2 = get_first_char_or_blank(value.trim());
}
"padding_adjust" => {
self.padding_adjust = value.trim().parse().unwrap_or(0);
}
"text_align" => {
self.text_align = value.trim().to_ascii_lowercase().to_string();
}
"enable_debug" => {
self.enable_debug = value.trim().parse().unwrap_or(false);
}
Expand All @@ -161,3 +202,10 @@ fn parse_color(color: &str) -> Result<(u8, u8, u8), &str> {
}
Err("Color format parse error")
}

fn get_first_char_or_blank(string: &str) -> String {
if let Some(first) = string.chars().next() {
return first.to_string();
}
"".to_string()
}
130 changes: 99 additions & 31 deletions src/line.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,27 @@
use zellij_tile::prelude::*;
use zellij_tile_utils::style;

static ARROW_SEPARATOR_1: &str = "";
static ARROW_SEPARATOR_2: &str = "";
static ARROW_SPACE: &str = " ";

#[derive(Default)]
pub struct Line {
backgound_color: PaletteColor,
foreground_color: PaletteColor,
pane_color: PaletteColor,
separator: (String, String, String),
space: String,
padding: i32,
text_align: TextAlign,
}

enum TextAlign {
Right,
Left,
Center,
}

impl Default for TextAlign {
fn default() -> Self {
Self::Right
}
}

impl Line {
Expand All @@ -19,33 +30,55 @@ impl Line {
backgound_color: (u8, u8, u8),
foreground_color: (u8, u8, u8),
pane_color: (u8, u8, u8),
separator: &(String, String, String),
padding_adjust: i32,
text_align: &str,
) {
// set color
self.backgound_color = PaletteColor::Rgb(backgound_color);
self.foreground_color = PaletteColor::Rgb(foreground_color);
self.pane_color = PaletteColor::Rgb(pane_color);
// text align
self.text_align = match text_align {
"right" => TextAlign::Right,
"left" => TextAlign::Left,
"center" => TextAlign::Center,
_ => TextAlign::Right,
};
// create charctor
let bg_1 = self.pane_color;
let bg_2 = self.backgound_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 space = &style!(bg_2, bg_2).paint(" ").to_string();
let sep_1 = &style!(bg_2, bg_1).bold().paint(&separator.0).to_string();
let sep_2 = &style!(bg_1, bg_2).bold().paint(&separator.1).to_string();
let sep_3 = &style!(bg_1, bg_2).bold().paint(&separator.2).to_string();
let mut sp_0 = String::new();
sp_0.push_str(sep_1);
sp_0.push_str(arrow);
match self.text_align {
TextAlign::Right | TextAlign::Center => {
sp_0.push_str(sep_1);
sp_0.push_str(space);
}
TextAlign::Left => {
sp_0.push_str(space);
sp_0.push_str(sep_1);
}
}
let mut sp_1 = String::new();
sp_1.push_str(arrow);
sp_1.push_str(space);
sp_1.push_str(sep_2);
sp_1.push_str(arrow);
sp_1.push_str(space);
let mut sp_2 = String::new();
sp_2.push_str(arrow);
sp_2.push_str(space);
sp_2.push_str(sep_3);
sp_2.push_str(space);
self.separator = (sp_0, sp_1, sp_2);
// last space
self.space = space.to_string();
// padding (Assume all as half width)
let length = 9;
// Getting the exact width is too much processing for a plugin,
// so it can be adjusted by user specification.
self.padding = length + padding_adjust;
}

pub fn create(&self, cols: usize, timezone: &str, date: &str, time: &str) -> String {
Expand All @@ -54,10 +87,15 @@ impl Line {
.chars()
.map(|c| if c.is_ascii() { 1 } else { 2 })
.sum::<usize>();
let width = timezone_len + date.len() + time.len() + 9;
let width = ((timezone_len + date.len() + time.len()) as i32) + self.padding;
let width = width as usize;
// There are cases where cols may be declared momentarily low at render time.
let padding: String = if cols as isize - width as isize > 0 {
let space = ARROW_SPACE.repeat(cols - width);
let size = match self.text_align {
TextAlign::Right | TextAlign::Left => cols - width,
TextAlign::Center => (cols - width) / 2,
};
let space = " ".repeat(size);
style!(self.foreground_color, self.pane_color)
.paint(space)
.to_string()
Expand All @@ -69,16 +107,46 @@ impl Line {
let date = style!(self.foreground_color, self.backgound_color).paint(date);
let time = style!(self.foreground_color, self.backgound_color).paint(time);

format!(
"{}{}{}{}{}{}{}{}",
padding,
self.separator.0,
timezone,
self.separator.1,
date,
self.separator.1,
time,
self.separator.2
)
match self.text_align {
TextAlign::Right => {
format!(
"{}{}{}{}{}{}{}{}",
padding,
self.separator.0,
timezone,
self.separator.1,
date,
self.separator.2,
time,
self.space
)
}
TextAlign::Left => {
format!(
"{}{}{}{}{}{}{}{}",
self.space,
timezone,
self.separator.1,
date,
self.separator.2,
time,
self.separator.0,
padding,
)
}
TextAlign::Center => {
format!(
"{}{}{}{}{}{}{}{}",
padding,
self.separator.0,
timezone,
self.separator.1,
date,
self.separator.2,
time,
padding
)
}
}
}
}
Loading

0 comments on commit 639d37e

Please sign in to comment.