aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDaniel Jankowski <daniel.jankowski@rub.de>2023-08-31 19:47:42 +0200
committerDaniel Jankowski <daniel.jankowski@rub.de>2023-08-31 19:47:42 +0200
commit9756c19ab3272dc5e81db4f16ed6bc3e5ad46675 (patch)
treec541f61c91896c916d8e0c4025fb547bcd4d2921
parentchore: remove format suffix from config (diff)
downloadsl0pstatus-9756c19ab3272dc5e81db4f16ed6bc3e5ad46675.tar.gz
sl0pstatus-9756c19ab3272dc5e81db4f16ed6bc3e5ad46675.tar.xz
sl0pstatus-9756c19ab3272dc5e81db4f16ed6bc3e5ad46675.zip
feat: add left + right widgets + date widget
-rw-r--r--Cargo.lock33
-rw-r--r--Cargo.toml2
-rw-r--r--plugin-dev-workspace.kdl10
-rw-r--r--src/config.rs30
-rw-r--r--src/main.rs41
-rw-r--r--src/render.rs9
-rw-r--r--src/widgets/clock.rs47
-rw-r--r--src/widgets/mod.rs1
-rw-r--r--src/widgets/tabs.rs10
9 files changed, 142 insertions, 41 deletions
diff --git a/Cargo.lock b/Cargo.lock
index bc78a77..2204c9c 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -1725,9 +1725,21 @@ dependencies = [
[[package]]
name = "regex"
-version = "1.8.4"
+version = "1.9.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "d0ab3ca65655bb1e41f2a8c8cd662eb4fb035e67c3f78da1d61dffe89d07300f"
+checksum = "12de2eff854e5fa4b1295edd650e227e9d8fb0c9e90b12e7f36d6a6811791a29"
+dependencies = [
+ "aho-corasick",
+ "memchr",
+ "regex-automata",
+ "regex-syntax",
+]
+
+[[package]]
+name = "regex-automata"
+version = "0.3.7"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "49530408a136e16e5b486e883fbb6ba058e8e4e8ae6621a77b048b314336e629"
dependencies = [
"aho-corasick",
"memchr",
@@ -1736,9 +1748,9 @@ dependencies = [
[[package]]
name = "regex-syntax"
-version = "0.7.2"
+version = "0.7.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "436b050e76ed2903236f032a59761c1eb99e1b0aead2c257922771dab1fc8c78"
+checksum = "dbb5fb1acd8a1a18b3dd5be62d25485eb770e05afb408a9627d14d451bae12da"
[[package]]
name = "rmp"
@@ -2007,6 +2019,15 @@ dependencies = [
]
[[package]]
+name = "strip-ansi-escapes"
+version = "0.2.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "55ff8ef943b384c414f54aefa961dd2bd853add74ec75e7ac74cf91dba62bcfa"
+dependencies = [
+ "vte 0.11.1",
+]
+
+[[package]]
name = "strsim"
version = "0.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -2792,7 +2813,7 @@ dependencies = [
"serde_json",
"shellexpand",
"signal-hook 0.3.15",
- "strip-ansi-escapes",
+ "strip-ansi-escapes 0.1.1",
"strum",
"strum_macros",
"tempfile",
@@ -2810,5 +2831,7 @@ version = "0.1.0"
dependencies = [
"ansi_term",
"chrono",
+ "regex",
+ "strip-ansi-escapes 0.2.0",
"zellij-tile",
]
diff --git a/Cargo.toml b/Cargo.toml
index c8c9a2e..d67c1bc 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -8,3 +8,5 @@ edition = "2018"
ansi_term = "0.12.1"
zellij-tile = "0.38.0"
chrono = "0.4.0"
+regex = "1.9.4"
+strip-ansi-escapes = "0.2.0"
diff --git a/plugin-dev-workspace.kdl b/plugin-dev-workspace.kdl
index 1dce010..07a04c9 100644
--- a/plugin-dev-workspace.kdl
+++ b/plugin-dev-workspace.kdl
@@ -11,13 +11,17 @@ layout {
}
pane expanded=true {
plugin location="file:target/wasm32-wasi/debug/zjstatus.wasm" {
- format "#[bold,italic]{mode} #[fg=255] :: {tabs} {space} #[fg=255,bg=0] {date} "
+ format_left "#[bold,italic]{mode} #[fg=255] :: {tabs}"
+ format_right "#[fg=255,bg=0] {clock} "
mode_normal "#[fg=0,bg=3]>> {name} <<"
mode_tmux "#[fg=255,bg=0]>> tmux <<"
- tab_normal "#[fg=255,bg=0] {name}"
- tab_active "#[fg=255,bg=0,bold] {name}"
+ tab_normal "#[fg=255,bg=0] {name}"
+ tab_active "#[fg=255,bg=0,bold] {name}"
+
+ clock "#[fg=255] {time} "
+ clock_format "%H:%M"
}
}
}
diff --git a/src/config.rs b/src/config.rs
index 35f311b..7672f9b 100644
--- a/src/config.rs
+++ b/src/config.rs
@@ -4,8 +4,8 @@ use ansi_term::{Colour, Colour::Fixed};
#[derive(Default)]
pub struct ModuleConfig {
- pub enabled_modules: Vec<String>,
- pub formatted_parts: Vec<FormattedPart>,
+ pub left_parts: Vec<FormattedPart>,
+ pub right_parts: Vec<FormattedPart>,
}
#[derive(Clone, Debug)]
@@ -87,31 +87,31 @@ impl Default for FormattedPart {
}
pub fn parse_format(config: BTreeMap<String, String>) -> ModuleConfig {
- let format = config.get("format");
- let mut formatted_parts = Vec::new();
+ ModuleConfig {
+ left_parts: parts_from_config(config.get("format_left")),
+ right_parts: parts_from_config(config.get("format_right")),
+ }
+}
+fn parts_from_config(format: Option<&String>) -> Vec<FormattedPart> {
if format.is_none() {
- return ModuleConfig {
- enabled_modules: Vec::new(),
- formatted_parts,
- };
+ return Vec::new();
}
- let format = format.unwrap();
+ let mut output = Vec::new();
+
+ let format_left = format.unwrap();
let mut counter: u8 = 0;
- let color_parts = format.split("#[");
+ let color_parts = format_left.split("#[");
for color_part in color_parts {
let mut part = FormattedPart::from_format_string(color_part.to_string());
part.order = counter.clone();
- formatted_parts.push(part);
+ output.push(part);
counter += 1;
}
- ModuleConfig {
- enabled_modules: Vec::new(),
- formatted_parts,
- }
+ output
}
diff --git a/src/main.rs b/src/main.rs
index 6060ec6..5917dde 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,8 +1,4 @@
-use widgets::{
- mode::ModeWidget,
- tabs::TabsWidget,
- widget::Widget,
-};
+use widgets::{mode::ModeWidget, tabs::TabsWidget, widget::Widget, clock::ClockWidget};
use zellij_tile::prelude::*;
use std::{collections::BTreeMap, sync::Arc, u8, usize};
@@ -69,15 +65,30 @@ impl ZellijPlugin for State {
}
fn render(&mut self, rows: usize, cols: usize) {
- let mut output = "".to_string();
+ self.state.rows = rows.clone();
+ self.state.cols = cols.clone();
- self.state.rows = rows;
- self.state.cols = cols;
+ let mut output_left = "".to_string();
+ for part in self.module_config.left_parts.to_vec() {
+ output_left = format!(
+ "{}{}",
+ output_left,
+ format!(
+ "{}",
+ render::widgets_and_formatting(
+ part,
+ self.widget_map.clone(),
+ self.state.clone()
+ )
+ )
+ );
+ }
- for part in self.module_config.formatted_parts.to_vec() {
- output = format!(
+ let mut output_right = "".to_string();
+ for part in self.module_config.right_parts.to_vec() {
+ output_right = format!(
"{}{}",
- output,
+ output_right,
format!(
"{}",
render::widgets_and_formatting(
@@ -89,13 +100,19 @@ impl ZellijPlugin for State {
);
}
- println!("{}", output);
+ let left_count = strip_ansi_escapes::strip(output_left.clone()).len();
+ let right_count = strip_ansi_escapes::strip(output_right.clone()).len();
+
+ let spacer = " ".repeat(cols - left_count - right_count);
+
+ println!("{}{}{}", output_left, spacer, output_right);
}
}
fn register_widgets(configuration: BTreeMap<String, String>) -> BTreeMap<String, Arc<dyn Widget>> {
let mut widget_map = BTreeMap::<String, Arc<dyn Widget>>::new();
+ widget_map.insert("clock".to_string(), Arc::new(ClockWidget::new(configuration.clone())));
widget_map.insert(
"mode".to_string(),
Arc::new(ModeWidget::new(configuration.clone())),
diff --git a/src/render.rs b/src/render.rs
index 71f538a..ca6e2c1 100644
--- a/src/render.rs
+++ b/src/render.rs
@@ -26,6 +26,15 @@ pub fn widgets_and_formatting(
) -> String {
let mut output = part.content.clone();
+ if output.contains("{clock}") {
+ let result = match widgets.get("clock") {
+ Some(widget) => widget.process(state.clone()),
+ None => "Use of uninitialized widget".to_string(),
+ };
+
+ output = output.replace("{clock}", &result);
+ }
+
if output.contains("{mode}") {
let result = match widgets.get("mode") {
Some(widget) => widget.process(state.clone()),
diff --git a/src/widgets/clock.rs b/src/widgets/clock.rs
new file mode 100644
index 0000000..4351fc8
--- /dev/null
+++ b/src/widgets/clock.rs
@@ -0,0 +1,47 @@
+use std::collections::BTreeMap;
+
+use chrono::Local;
+
+use crate::{config::FormattedPart, render};
+
+use super::widget::Widget;
+
+pub struct ClockWidget {
+ format: String,
+ color_format: FormattedPart,
+}
+
+impl ClockWidget {
+ pub fn new(config: BTreeMap<String, String>) -> Self {
+ let mut format = "%H:%M";
+ if let Some(form) = config.get("clock_format") {
+ format = form;
+ }
+
+ let mut color_format = "";
+ if let Some(form) = config.get("clock") {
+ color_format = form;
+ }
+
+ Self {
+ format: format.to_string(),
+ color_format: FormattedPart::from_format_string(color_format.to_string()),
+ }
+ }
+}
+
+impl Widget for ClockWidget {
+ fn process(&self, _state: crate::ZellijState) -> String {
+ let mut output = self.color_format.content.clone();
+ if output.contains("{time}") {
+ let date = Local::now();
+
+ output = output.replace(
+ "{time}",
+ format!("{}", date.format(self.format.as_str())).as_str(),
+ );
+ }
+
+ render::formatting(self.color_format.clone(), output)
+ }
+}
diff --git a/src/widgets/mod.rs b/src/widgets/mod.rs
index a017530..124e372 100644
--- a/src/widgets/mod.rs
+++ b/src/widgets/mod.rs
@@ -1,3 +1,4 @@
+pub mod clock;
pub mod mode;
pub mod tabs;
pub mod widget;
diff --git a/src/widgets/tabs.rs b/src/widgets/tabs.rs
index 6353251..00b87f9 100644
--- a/src/widgets/tabs.rs
+++ b/src/widgets/tabs.rs
@@ -18,11 +18,10 @@ impl TabsWidget {
normal_tab_format_string = form;
}
- let active_tab_format =
- FormattedPart::from_format_string(match config.get("tab_active") {
- Some(form) => form.to_string(),
- None => normal_tab_format_string.to_string(),
- });
+ let active_tab_format = FormattedPart::from_format_string(match config.get("tab_active") {
+ Some(form) => form.to_string(),
+ None => normal_tab_format_string.to_string(),
+ });
Self {
normal_tab_format: FormattedPart::from_format_string(
@@ -39,7 +38,6 @@ impl Widget for TabsWidget {
for tab in state.tabs {
let formatter = self.select_format(tab.clone());
- println!("{:?}", tab.clone());
let mut content = formatter.content.clone();
if content.contains("{name}") {