aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorDaniel Jankowski <daniel.jankowski@rub.de>2023-08-31 18:23:47 +0200
committerDaniel Jankowski <daniel.jankowski@rub.de>2023-08-31 18:23:47 +0200
commitda4852f427cab335c08662007f7f49f4c559857a (patch)
treed600912dbe8293e3ba4e5c10fa008c2e07ff4a63 /src
parentchore: move render function to part (diff)
downloadsl0pstatus-da4852f427cab335c08662007f7f49f4c559857a.tar.gz
sl0pstatus-da4852f427cab335c08662007f7f49f4c559857a.tar.xz
sl0pstatus-da4852f427cab335c08662007f7f49f4c559857a.zip
feat: implement mode widget
Diffstat (limited to 'src')
-rw-r--r--src/config.rs24
-rw-r--r--src/main.rs54
-rw-r--r--src/render.rs39
-rw-r--r--src/widgets/mod.rs2
-rw-r--r--src/widgets/mode.rs151
-rw-r--r--src/widgets/widget.rs5
6 files changed, 246 insertions, 29 deletions
diff --git a/src/config.rs b/src/config.rs
index 1e54ff3..35f311b 100644
--- a/src/config.rs
+++ b/src/config.rs
@@ -1,6 +1,6 @@
use std::collections::BTreeMap;
-use ansi_term::{Colour, Colour::Fixed, Style};
+use ansi_term::{Colour, Colour::Fixed};
#[derive(Default)]
pub struct ModuleConfig {
@@ -20,6 +20,7 @@ pub struct FormattedPart {
fn parse_color(color: &str) -> Option<Colour> {
if color.starts_with("#") {
+ // TODO: render hex to color
return Some(Fixed(1));
}
@@ -32,6 +33,10 @@ fn parse_color(color: &str) -> Option<Colour> {
impl FormattedPart {
pub fn from_format_string(format: String) -> Self {
+ let mut format = format;
+ if format.starts_with("#[") {
+ format = format.strip_prefix("#[").unwrap().to_string();
+ }
let mut result = FormattedPart::default();
let format_content_split = format.split("]");
@@ -66,23 +71,6 @@ impl FormattedPart {
result
}
-
- pub fn render(&self) -> String {
- // TODO: render widgets
-
- let mut style = match self.fg {
- Some(color) => Style::new().fg(color),
- None => Style::new(),
- };
-
- style.background = self.bg;
- style.is_italic = self.italic;
- style.is_bold = self.bold;
-
- let style = style.paint(self.content.clone());
-
- format!("{}", style)
- }
}
impl Default for FormattedPart {
diff --git a/src/main.rs b/src/main.rs
index bee5cf9..538ad8b 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,18 +1,30 @@
+use widgets::{mode::ModeWidget, widget::Widget};
use zellij_tile::prelude::*;
use std::{
- collections::{BTreeMap, HashMap},
- u8,
+ collections::BTreeMap,
+ sync::Arc,
+ u8, usize,
};
mod config;
+mod render;
+mod widgets;
#[derive(Default)]
struct State {
- mode_log: HashMap<String, usize>,
- tabs: Vec<String>,
+ state: ZellijState,
userspace_configuration: BTreeMap<String, String>,
module_config: config::ModuleConfig,
+ widget_map: BTreeMap<String, Arc<dyn Widget>>,
+}
+
+#[derive(Default, Debug, Clone)]
+pub struct ZellijState {
+ pub mode: ModeInfo,
+ pub tabs: Vec<String>,
+ pub rows: usize,
+ pub cols: usize,
}
register_plugin!(State);
@@ -29,22 +41,32 @@ impl ZellijPlugin for State {
]);
subscribe(&[EventType::ModeUpdate, EventType::TabUpdate, EventType::Key]);
- self.module_config = config::parse_format(configuration);
+ self.module_config = config::parse_format(configuration.clone());
+
+ let mut widget_map = BTreeMap::<String, Arc<dyn Widget>>::new();
+
+ let mode_widget = ModeWidget::new(configuration);
+ widget_map.insert("mode".to_string(), Arc::new(mode_widget));
- // TODO: current mode
+ self.widget_map = widget_map;
+
+ self.state = ZellijState {
+ mode: ModeInfo::default(),
+ tabs: Vec::new(),
+ rows: 0,
+ cols: 0,
+ };
}
fn update(&mut self, event: Event) -> bool {
let mut should_render = false;
match event {
Event::ModeUpdate(mode_info) => {
- let mode = format!("{:?}", mode_info.mode);
- let count = self.mode_log.entry(mode).or_insert(0);
- *count += 1;
+ self.state.mode = mode_info;
should_render = true;
}
Event::TabUpdate(tab_info) => {
- self.tabs = tab_info.iter().map(|t| t.name.clone()).collect();
+ self.state.tabs = tab_info.iter().map(|t| t.name.clone()).collect();
should_render = true;
}
_ => (),
@@ -55,11 +77,21 @@ impl ZellijPlugin for State {
fn render(&mut self, rows: usize, cols: usize) {
let mut output = "".to_string();
+ self.state.rows = rows;
+ self.state.cols = cols;
+
for part in self.module_config.formatted_parts.to_vec() {
output = format!(
"{}{}",
output,
- format!("{}", part.render())
+ format!(
+ "{}",
+ render::widgets_and_formatting(
+ part,
+ self.widget_map.clone(),
+ self.state.clone()
+ )
+ )
);
}
diff --git a/src/render.rs b/src/render.rs
new file mode 100644
index 0000000..87b8a33
--- /dev/null
+++ b/src/render.rs
@@ -0,0 +1,39 @@
+use std::{collections::BTreeMap, sync::Arc};
+
+use ansi_term::Style;
+
+use crate::{config::FormattedPart, widgets::widget::Widget, ZellijState};
+
+pub fn formatting(part: FormattedPart, text: String) -> String {
+ let mut style = match part.fg {
+ Some(color) => Style::new().fg(color),
+ None => Style::new(),
+ };
+
+ style.background = part.bg;
+ style.is_italic = part.italic;
+ style.is_bold = part.bold;
+
+ let style = style.paint(text);
+
+ format!("{}", style)
+}
+
+pub fn widgets_and_formatting(
+ part: FormattedPart,
+ widgets: BTreeMap<String, Arc<dyn Widget>>,
+ state: ZellijState,
+) -> String {
+ let mut output = part.content.clone();
+
+ if output.contains("{mode}") {
+ let result = match widgets.get("mode") {
+ Some(widget) => widget.process(state),
+ None => "Use of uninitialized widget".to_string(),
+ };
+
+ output = output.replace("{mode}", &result);
+ }
+
+ formatting(part, output)
+}
diff --git a/src/widgets/mod.rs b/src/widgets/mod.rs
new file mode 100644
index 0000000..4abab8e
--- /dev/null
+++ b/src/widgets/mod.rs
@@ -0,0 +1,2 @@
+pub mod mode;
+pub mod widget;
diff --git a/src/widgets/mode.rs b/src/widgets/mode.rs
new file mode 100644
index 0000000..629cd0f
--- /dev/null
+++ b/src/widgets/mode.rs
@@ -0,0 +1,151 @@
+use std::collections::BTreeMap;
+
+use zellij_tile::prelude::InputMode;
+
+use crate::{config::FormattedPart, render, ZellijState};
+
+use super::widget::Widget;
+
+#[derive(Debug)]
+pub struct ModeWidget {
+ normal_format: FormattedPart,
+ locked_format: FormattedPart,
+ resize_format: FormattedPart,
+ pane_format: FormattedPart,
+ tab_format: FormattedPart,
+ scroll_format: FormattedPart,
+ enter_search_format: FormattedPart,
+ search_format: FormattedPart,
+ rename_tab_format: FormattedPart,
+ rename_pane_format: FormattedPart,
+ session_format: FormattedPart,
+ move_format: FormattedPart,
+ prompt_format: FormattedPart,
+ tmux_format: FormattedPart,
+}
+
+impl ModeWidget {
+ pub fn new(config: BTreeMap<String, String>) -> Self {
+ let mut normal_format_string = "";
+ if let Some(form) = config.get("mode_normal_format") {
+ normal_format_string = form;
+ }
+
+ let locked_format = FormattedPart::from_format_string(match config.get("mode_locked_format") {
+ Some(form) => form.to_string(),
+ None => normal_format_string.to_string(),
+ });
+
+ let resize_format = FormattedPart::from_format_string(match config.get("mode_resize_format") {
+ Some(form) => form.to_string(),
+ None => normal_format_string.to_string(),
+ });
+
+ let pane_format = FormattedPart::from_format_string(match config.get("mode_pane_format") {
+ Some(form) => form.to_string(),
+ None => normal_format_string.to_string(),
+ });
+
+ let tab_format = FormattedPart::from_format_string(match config.get("mode_tab_format") {
+ Some(form) => form.to_string(),
+ None => normal_format_string.to_string(),
+ });
+
+ let scroll_format = FormattedPart::from_format_string(match config.get("mode_scroll_format") {
+ Some(form) => form.to_string(),
+ None => normal_format_string.to_string(),
+ });
+
+ let enter_search_format = FormattedPart::from_format_string(match config.get("mode_enter_search_format") {
+ Some(form) => form.to_string(),
+ None => normal_format_string.to_string(),
+ });
+
+ let search_format = FormattedPart::from_format_string(match config.get("mode_search_format") {
+ Some(form) => form.to_string(),
+ None => normal_format_string.to_string(),
+ });
+
+ let rename_tab_format = FormattedPart::from_format_string(match config.get("mode_rename_tab_format") {
+ Some(form) => form.to_string(),
+ None => normal_format_string.to_string(),
+ });
+
+ let rename_pane_format = FormattedPart::from_format_string(match config.get("mode_rename_pane_format") {
+ Some(form) => form.to_string(),
+ None => normal_format_string.to_string(),
+ });
+
+ let session_format = FormattedPart::from_format_string(match config.get("mode_session_format") {
+ Some(form) => form.to_string(),
+ None => normal_format_string.to_string(),
+ });
+
+ let move_format = FormattedPart::from_format_string(match config.get("mode_move_format") {
+ Some(form) => form.to_string(),
+ None => normal_format_string.to_string(),
+ });
+
+ let prompt_format = FormattedPart::from_format_string(match config.get("mode_prompt_format") {
+ Some(form) => form.to_string(),
+ None => normal_format_string.to_string(),
+ });
+
+ let tmux_format = FormattedPart::from_format_string(match config.get("mode_tmux_format") {
+ Some(form) => form.to_string(),
+ None => normal_format_string.to_string(),
+ });
+
+
+ Self {
+ normal_format: FormattedPart::from_format_string(normal_format_string.to_string()),
+ locked_format,
+ resize_format,
+ pane_format,
+ tab_format,
+ scroll_format,
+ enter_search_format,
+ search_format,
+ rename_tab_format,
+ rename_pane_format,
+ session_format,
+ move_format,
+ prompt_format,
+ tmux_format,
+ }
+ }
+}
+
+impl Widget for ModeWidget {
+ fn process(&self, state: ZellijState) -> String {
+ let format = self.select_format(state.mode.mode);
+
+ let mut content = format.content.clone();
+ if content.contains("{name}") {
+ content = content.replace("{name}", format!("{:?}", state.mode.mode).as_str());
+ }
+
+ render::formatting(format.clone(), content)
+ }
+}
+
+impl ModeWidget {
+ fn select_format(&self, mode: InputMode) -> FormattedPart {
+ match mode {
+ InputMode::Normal => self.normal_format.clone(),
+ InputMode::Locked => self.locked_format.clone(),
+ InputMode::Resize => self.resize_format.clone(),
+ InputMode::Pane => self.pane_format.clone(),
+ InputMode::Tab => self.tab_format.clone(),
+ InputMode::Scroll => self.scroll_format.clone(),
+ InputMode::EnterSearch => self.enter_search_format.clone(),
+ InputMode::Search => self.search_format.clone(),
+ InputMode::RenameTab => self.rename_tab_format.clone(),
+ InputMode::RenamePane => self.rename_pane_format.clone(),
+ InputMode::Session => self.session_format.clone(),
+ InputMode::Move => self.move_format.clone(),
+ InputMode::Prompt => self.prompt_format.clone(),
+ InputMode::Tmux => self.tmux_format.clone(),
+ }
+ }
+}
diff --git a/src/widgets/widget.rs b/src/widgets/widget.rs
new file mode 100644
index 0000000..6a53d2c
--- /dev/null
+++ b/src/widgets/widget.rs
@@ -0,0 +1,5 @@
+use crate::ZellijState;
+
+pub trait Widget {
+ fn process(&self, state: ZellijState) -> String;
+}