diff options
| author | Daniel Jankowski <daniel.jankowski@rub.de> | 2023-08-31 18:53:55 +0200 |
|---|---|---|
| committer | Daniel Jankowski <daniel.jankowski@rub.de> | 2023-08-31 18:53:55 +0200 |
| commit | 671a341433711c861dd728651a380faa03d04a29 (patch) | |
| tree | 15fa3b016c864393666290a7e4a1167f43f062d5 /src | |
| parent | feat: implement mode widget (diff) | |
| download | sl0pstatus-671a341433711c861dd728651a380faa03d04a29.tar.gz sl0pstatus-671a341433711c861dd728651a380faa03d04a29.tar.xz sl0pstatus-671a341433711c861dd728651a380faa03d04a29.zip | |
feat: add tabs widget
Diffstat (limited to 'src')
| -rw-r--r-- | src/main.rs | 36 | ||||
| -rw-r--r-- | src/render.rs | 11 | ||||
| -rw-r--r-- | src/widgets/mod.rs | 1 | ||||
| -rw-r--r-- | src/widgets/tabs.rs | 63 |
4 files changed, 95 insertions, 16 deletions
diff --git a/src/main.rs b/src/main.rs index 538ad8b..6060ec6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,11 +1,11 @@ -use widgets::{mode::ModeWidget, widget::Widget}; +use widgets::{ + mode::ModeWidget, + tabs::TabsWidget, + widget::Widget, +}; use zellij_tile::prelude::*; -use std::{ - collections::BTreeMap, - sync::Arc, - u8, usize, -}; +use std::{collections::BTreeMap, sync::Arc, u8, usize}; mod config; mod render; @@ -22,7 +22,7 @@ struct State { #[derive(Default, Debug, Clone)] pub struct ZellijState { pub mode: ModeInfo, - pub tabs: Vec<String>, + pub tabs: Vec<TabInfo>, pub rows: usize, pub cols: usize, } @@ -42,13 +42,7 @@ impl ZellijPlugin for State { subscribe(&[EventType::ModeUpdate, EventType::TabUpdate, EventType::Key]); 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)); - - self.widget_map = widget_map; + self.widget_map = register_widgets(configuration); self.state = ZellijState { mode: ModeInfo::default(), @@ -66,7 +60,7 @@ impl ZellijPlugin for State { should_render = true; } Event::TabUpdate(tab_info) => { - self.state.tabs = tab_info.iter().map(|t| t.name.clone()).collect(); + self.state.tabs = tab_info; should_render = true; } _ => (), @@ -98,3 +92,15 @@ impl ZellijPlugin for State { println!("{}", output); } } + +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( + "mode".to_string(), + Arc::new(ModeWidget::new(configuration.clone())), + ); + widget_map.insert("tabs".to_string(), Arc::new(TabsWidget::new(configuration))); + + widget_map +} diff --git a/src/render.rs b/src/render.rs index 87b8a33..71f538a 100644 --- a/src/render.rs +++ b/src/render.rs @@ -28,12 +28,21 @@ pub fn widgets_and_formatting( if output.contains("{mode}") { let result = match widgets.get("mode") { - Some(widget) => widget.process(state), + Some(widget) => widget.process(state.clone()), None => "Use of uninitialized widget".to_string(), }; output = output.replace("{mode}", &result); } + if output.contains("{tabs}") { + let result = match widgets.get("tabs") { + Some(widget) => widget.process(state), + None => "Use of uninitialized widget".to_string(), + }; + + output = output.replace("{tabs}", &result); + } + formatting(part, output) } diff --git a/src/widgets/mod.rs b/src/widgets/mod.rs index 4abab8e..a017530 100644 --- a/src/widgets/mod.rs +++ b/src/widgets/mod.rs @@ -1,2 +1,3 @@ pub mod mode; +pub mod tabs; pub mod widget; diff --git a/src/widgets/tabs.rs b/src/widgets/tabs.rs new file mode 100644 index 0000000..396be4a --- /dev/null +++ b/src/widgets/tabs.rs @@ -0,0 +1,63 @@ +use std::collections::BTreeMap; + +use zellij_tile::prelude::TabInfo; + +use crate::{config::FormattedPart, render}; + +use super::widget::Widget; + +pub struct TabsWidget { + active_tab_format: FormattedPart, + normal_tab_format: FormattedPart, +} + +impl TabsWidget { + pub fn new(config: BTreeMap<String, String>) -> Self { + let mut normal_tab_format_string = ""; + if let Some(form) = config.get("tab_format") { + normal_tab_format_string = form; + } + + let active_tab_format = + FormattedPart::from_format_string(match config.get("tab_active_format") { + Some(form) => form.to_string(), + None => normal_tab_format_string.to_string(), + }); + + Self { + normal_tab_format: FormattedPart::from_format_string( + normal_tab_format_string.to_string(), + ), + active_tab_format, + } + } +} + +impl Widget for TabsWidget { + fn process(&self, state: crate::ZellijState) -> String { + let mut output = "".to_string(); + + 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}") { + content = content.replace("{name}", tab.name.as_str()); + } + + output = format!("{}{}", output, render::formatting(formatter, content),); + } + + output + } +} + +impl TabsWidget { + fn select_format(&self, info: TabInfo) -> FormattedPart { + match info.active { + false => self.normal_tab_format.clone(), + true => self.active_tab_format.clone(), + } + } +} |
