aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorDaniel Jankowski <daniel.jankowski@rub.de>2023-09-06 13:36:03 +0200
committerDaniel Jankowski <daniel.jankowski@rub.de>2023-09-06 13:36:03 +0200
commita203df979b584d41dcf3dceb551d6cc884f4c5e3 (patch)
tree52cca48deba36dd97e26ca4048d843db1978f54e /src
parentchore: update cargo dependencies (diff)
downloadsl0pstatus-a203df979b584d41dcf3dceb551d6cc884f4c5e3.tar.gz
sl0pstatus-a203df979b584d41dcf3dceb551d6cc884f4c5e3.tar.xz
sl0pstatus-a203df979b584d41dcf3dceb551d6cc884f4c5e3.zip
feat: implement clickable tabs
Diffstat (limited to 'src')
-rw-r--r--src/config.rs148
-rw-r--r--src/main.rs10
-rw-r--r--src/widgets/datetime.rs2
-rw-r--r--src/widgets/mode.rs30
-rw-r--r--src/widgets/session.rs2
-rw-r--r--src/widgets/tabs.rs27
-rw-r--r--src/widgets/widget.rs1
7 files changed, 196 insertions, 24 deletions
diff --git a/src/config.rs b/src/config.rs
index 84608f4..ce8b4b2 100644
--- a/src/config.rs
+++ b/src/config.rs
@@ -1,10 +1,15 @@
use std::{collections::BTreeMap, sync::Arc};
+use regex::Regex;
+use zellij_tile::prelude::Mouse;
+
use crate::{render::FormattedPart, widgets::widget::Widget, ZellijState};
#[derive(Default)]
pub struct ModuleConfig {
+ pub left_parts_config: String,
pub left_parts: Vec<FormattedPart>,
+ pub right_parts_config: String,
pub right_parts: Vec<FormattedPart>,
pub format_space: FormattedPart,
pub hide_frame_for_single_pane: bool,
@@ -22,20 +27,138 @@ impl ModuleConfig {
hide_frame_for_single_pane = toggle == "true";
}
+ let mut left_parts_config = "";
+ if let Some(conf) = config.get("format_left") {
+ left_parts_config = conf;
+ }
+
+ let mut right_parts_config = "";
+ if let Some(conf) = config.get("format_right") {
+ right_parts_config = conf;
+ }
+
Self {
- left_parts: parts_from_config(config.get("format_left")),
- right_parts: parts_from_config(config.get("format_right")),
+ left_parts_config: left_parts_config.to_string(),
+ left_parts: parts_from_config(Some(&left_parts_config.to_string())),
+ right_parts_config: right_parts_config.to_string(),
+ right_parts: parts_from_config(Some(&right_parts_config.to_string())),
format_space: FormattedPart::from_format_string(format_space_config.to_string()),
hide_frame_for_single_pane,
}
}
- pub fn render_bar(
+ pub fn handle_mouse_action(
&self,
state: ZellijState,
+ mouse: Mouse,
widget_map: BTreeMap<String, Arc<dyn Widget>>,
- cols: usize,
) {
+ let click_pos = match mouse {
+ Mouse::ScrollUp(_) => return,
+ Mouse::ScrollDown(_) => return,
+ Mouse::LeftClick(_, y) => y,
+ Mouse::RightClick(_, y) => y,
+ Mouse::Hold(_, y) => y,
+ Mouse::Release(_, y) => y,
+ };
+
+ let widget_string_left = self
+ .left_parts
+ .iter()
+ .map(|p| p.content.clone())
+ .fold(String::new(), |a, b| a + b.as_str());
+
+ let left_len = self.process_widget_click(
+ click_pos,
+ widget_string_left,
+ widget_map.clone(),
+ state.clone(),
+ 0,
+ );
+
+ if click_pos <= left_len {
+ return;
+ }
+
+ let mut output_right = "".to_string();
+ for part in self.right_parts.iter() {
+ output_right = format!(
+ "{}{}",
+ output_right,
+ part.format_string_with_widgets(widget_map.clone(), state.clone())
+ );
+ }
+
+ let widget_spacer = strip_ansi_escapes::strip_str(self.get_spacer(
+ " ".repeat(left_len),
+ output_right,
+ state.cols,
+ ));
+
+ let widget_string_right = self
+ .right_parts
+ .iter()
+ .map(|p| p.content.clone())
+ .fold(String::new(), |a, b| a + b.as_str());
+
+ self.process_widget_click(
+ click_pos,
+ widget_string_right,
+ widget_map,
+ state,
+ left_len + widget_spacer.len(),
+ );
+ }
+
+ fn process_widget_click(
+ &self,
+ click_pos: usize,
+ widget_string: String,
+ widget_map: BTreeMap<String, Arc<dyn Widget>>,
+ state: ZellijState,
+ offset: usize,
+ ) -> usize {
+ let mut rendered_output = widget_string.clone();
+
+ let tokens: Vec<String> = widget_map.keys().map(|k| format!("{{{k}}}")).collect();
+
+ let widgets_regex = Regex::new("(\\{[a-z]+\\})").unwrap();
+ for widget in widgets_regex.captures_iter(widget_string.as_str()) {
+ let match_name = widget.get(0).unwrap().as_str();
+ if !tokens.contains(&match_name.to_string()) {
+ continue;
+ }
+
+ let mut wid_name = match_name.replace('{', "");
+ wid_name = wid_name.replace('}', "");
+
+ let wid = widget_map.get(&wid_name);
+ if wid.is_none() {
+ continue;
+ }
+ let wid = wid.unwrap();
+
+ let pos = rendered_output.find(match_name);
+ if pos.is_none() {
+ continue;
+ }
+ let pos = pos.unwrap();
+
+ let wid_res = strip_ansi_escapes::strip_str(wid.process(state.clone()));
+ rendered_output = rendered_output.replace(match_name, &wid_res);
+
+ if click_pos < pos + offset || click_pos > pos + offset + wid_res.len() {
+ continue;
+ }
+
+ wid.process_click(state.clone(), click_pos - pos + offset);
+
+ }
+
+ rendered_output.len()
+ }
+
+ pub fn render_bar(&self, state: ZellijState, widget_map: BTreeMap<String, Arc<dyn Widget>>) {
let mut output_left = "".to_string();
for part in self.left_parts.iter() {
output_left = format!(
@@ -54,8 +177,17 @@ impl ModuleConfig {
);
}
- let text_count = strip_ansi_escapes::strip(output_left.clone()).len()
- + strip_ansi_escapes::strip(output_right.clone()).len();
+ print!(
+ "{}{}{}",
+ output_left,
+ self.get_spacer(output_left.clone(), output_right.clone(), state.cols),
+ output_right
+ );
+ }
+
+ fn get_spacer(&self, output_left: String, output_right: String, cols: usize) -> String {
+ let text_count = strip_ansi_escapes::strip(output_left).len()
+ + strip_ansi_escapes::strip(output_right).len();
let mut space_count = cols;
// verify we are able to count the difference, since zellij sometimes drops a col
@@ -64,9 +196,7 @@ impl ModuleConfig {
space_count -= text_count;
}
- let spaces = self.format_space.format_string(" ".repeat(space_count));
-
- print!("{}{}{}", output_left, spaces, output_right);
+ self.format_space.format_string(" ".repeat(space_count))
}
}
diff --git a/src/main.rs b/src/main.rs
index 41ffb09..ef189ce 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -22,6 +22,7 @@ struct State {
#[derive(Default, Debug, Clone)]
pub struct ZellijState {
+ pub cols: usize,
pub mode: ModeInfo,
pub tabs: Vec<TabInfo>,
pub sessions: Vec<SessionInfo>,
@@ -41,6 +42,7 @@ impl ZellijPlugin for State {
PermissionType::RunCommands,
]);
subscribe(&[
+ EventType::Mouse,
EventType::ModeUpdate,
EventType::PaneUpdate,
EventType::PermissionRequestResult,
@@ -53,6 +55,7 @@ impl ZellijPlugin for State {
self.widget_map = register_widgets(configuration);
self.state = ZellijState {
+ cols: 0,
mode: ModeInfo::default(),
tabs: Vec::new(),
sessions: Vec::new(),
@@ -62,6 +65,10 @@ impl ZellijPlugin for State {
fn update(&mut self, event: Event) -> bool {
let mut should_render = false;
match event {
+ Event::Mouse(mouse_info) => {
+ self.module_config
+ .handle_mouse_action(self.state.clone(), mouse_info, self.widget_map.clone());
+ }
Event::ModeUpdate(mode_info) => {
self.state.mode = mode_info;
should_render = true;
@@ -107,8 +114,9 @@ impl ZellijPlugin for State {
}
fn render(&mut self, _rows: usize, cols: usize) {
+ self.state.cols = cols;
self.module_config
- .render_bar(self.state.clone(), self.widget_map.clone(), cols);
+ .render_bar(self.state.clone(), self.widget_map.clone());
}
}
diff --git a/src/widgets/datetime.rs b/src/widgets/datetime.rs
index bc6a57c..cc4c3ff 100644
--- a/src/widgets/datetime.rs
+++ b/src/widgets/datetime.rs
@@ -62,4 +62,6 @@ impl Widget for DateTimeWidget {
self.color_format.format_string(output)
}
+
+ fn process_click(&self, _state: crate::ZellijState, _pos: usize) {}
}
diff --git a/src/widgets/mode.rs b/src/widgets/mode.rs
index b9dde99..9a235da 100644
--- a/src/widgets/mode.rs
+++ b/src/widgets/mode.rs
@@ -56,25 +56,28 @@ impl ModeWidget {
None => normal_format_string.to_string(),
});
- let enter_search_format = FormattedPart::from_format_string(match config.get("mode_enter_search") {
- 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") {
+ Some(form) => form.to_string(),
+ None => normal_format_string.to_string(),
+ });
let search_format = FormattedPart::from_format_string(match config.get("mode_search") {
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") {
- 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") {
+ 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") {
- 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") {
+ Some(form) => form.to_string(),
+ None => normal_format_string.to_string(),
+ });
let session_format = FormattedPart::from_format_string(match config.get("mode_session") {
Some(form) => form.to_string(),
@@ -96,7 +99,6 @@ impl ModeWidget {
None => normal_format_string.to_string(),
});
-
Self {
normal_format: FormattedPart::from_format_string(normal_format_string.to_string()),
locked_format,
@@ -127,6 +129,8 @@ impl Widget for ModeWidget {
format.format_string(content)
}
+
+ fn process_click(&self, _state: ZellijState, _pos: usize) {}
}
impl ModeWidget {
diff --git a/src/widgets/session.rs b/src/widgets/session.rs
index 361363b..90d7b0c 100644
--- a/src/widgets/session.rs
+++ b/src/widgets/session.rs
@@ -17,4 +17,6 @@ impl Widget for SessionWidget {
None => "".to_string(),
}
}
+
+ fn process_click(&self, _state: crate::ZellijState, _pos: usize) {}
}
diff --git a/src/widgets/tabs.rs b/src/widgets/tabs.rs
index a127f79..e7e0b8d 100644
--- a/src/widgets/tabs.rs
+++ b/src/widgets/tabs.rs
@@ -1,6 +1,6 @@
use std::collections::BTreeMap;
-use zellij_tile::prelude::TabInfo;
+use zellij_tile::{prelude::TabInfo, shim::switch_tab_to};
use crate::render::FormattedPart;
@@ -49,6 +49,31 @@ impl Widget for TabsWidget {
output
}
+
+ fn process_click(&self, state: crate::ZellijState, pos: usize) {
+ let mut output = "".to_string();
+
+ let mut offset = 0;
+ let mut index = 1;
+ for tab in state.tabs {
+ let formatter = self.select_format(tab.clone());
+
+ let mut content = formatter.content.clone();
+ if content.contains("{name}") {
+ content = content.replace("{name}", tab.name.as_str());
+ }
+ if pos > offset && pos < offset + content.len() {
+ switch_tab_to(index);
+
+ break;
+ }
+
+ index += 1;
+ offset += content.len();
+
+ output = format!("{}{}", output, content);
+ }
+ }
}
impl TabsWidget {
diff --git a/src/widgets/widget.rs b/src/widgets/widget.rs
index 6a53d2c..303284c 100644
--- a/src/widgets/widget.rs
+++ b/src/widgets/widget.rs
@@ -2,4 +2,5 @@ use crate::ZellijState;
pub trait Widget {
fn process(&self, state: ZellijState) -> String;
+ fn process_click(&self, state: ZellijState, pos: usize);
}