aboutsummaryrefslogtreecommitdiffstats
path: root/src/config.rs
blob: e314d3fc53ac400b6d4e12fa6829436f3d5683c8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
use std::{collections::BTreeMap, sync::Arc};

use regex::Regex;
use zellij_tile::prelude::*;

use crate::{
    border::{parse_border_config, BorderConfig, BorderPosition},
    render::FormattedPart,
    widgets::{command::CommandResult, widget::Widget},
};
use chrono::{DateTime, Local};

#[derive(Default, Debug, Clone)]
pub struct ZellijState {
    pub cols: usize,
    pub command_results: BTreeMap<String, CommandResult>,
    pub mode: ModeInfo,
    pub plugin_uuid: String,
    pub tabs: Vec<TabInfo>,
    pub sessions: Vec<SessionInfo>,
    pub start_time: DateTime<Local>,
}

#[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,
    pub border: BorderConfig,
}

impl ModuleConfig {
    pub fn new(config: BTreeMap<String, String>) -> Self {
        let format_space_config = match config.get("format_space") {
            Some(space_config) => space_config,
            None => "",
        };

        let hide_frame_for_single_pane = match config.get("hide_frame_for_single_pane") {
            Some(toggle) => toggle == "true",
            None => false,
        };

        let left_parts_config = match config.get("format_left") {
            Some(conf) => conf,
            None => "",
        };

        let right_parts_config = match config.get("format_right") {
            Some(conf) => conf,
            None => "",
        };

        let border_config = match parse_border_config(config.clone()) {
            Some(bc) => bc,
            None => BorderConfig::default(),
        };

        Self {
            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,
            border: border_config,
        }
    }

    pub fn handle_mouse_action(
        &self,
        state: ZellijState,
        mouse: Mouse,
        widget_map: BTreeMap<String, Arc<dyn Widget>>,
    ) {
        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 output_right = self.right_parts.iter().fold("".to_string(), |acc, part| {
            format!(
                "{}{}",
                acc,
                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 wid_name = match_name.trim_matches(|c| c == '{' || c == '}');

            let wid = match widget_map.get(wid_name) {
                Some(wid) => wid,
                None => continue,
            };

            let pos = match rendered_output.find(match_name) {
                Some(pos) => pos,
                None => continue,
            };

            let wid_res = strip_ansi_escapes::strip_str(wid.process(wid_name, 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 output_left = self.left_parts.iter().fold("".to_string(), |acc, part| {
            format!(
                "{acc}{}",
                part.format_string_with_widgets(widget_map.clone(), state.clone())
            )
        });

        let output_right = self.right_parts.iter().fold("".to_string(), |acc, part| {
            format!(
                "{acc}{}",
                part.format_string_with_widgets(widget_map.clone(), state.clone())
            )
        });

        let mut border_top = "".to_string();
        if self.border.enabled && self.border.position == BorderPosition::Top {
            border_top = format!("{}\n", self.border.draw(state.cols));
        }

        let mut border_bottom = "".to_string();
        if self.border.enabled && self.border.position == BorderPosition::Bottom {
            border_bottom = format!("\n{}", self.border.draw(state.cols));
        }

        print!(
            "{}{}{}{}{}",
            border_top,
            output_left,
            self.get_spacer(output_left.clone(), output_right.clone(), state.cols),
            output_right,
            border_bottom,
        );
    }

    fn get_spacer(&self, output_left: String, output_right: String, cols: usize) -> String {
        let text_count = strip_ansi_escapes::strip_str(output_left).chars().count()
            + strip_ansi_escapes::strip_str(output_right).chars().count();

        // verify we are able to count the difference, since zellij sometimes drops a col
        // count of 0 on tab creation
        let space_count = cols.saturating_sub(text_count);

        self.format_space.format_string(" ".repeat(space_count))
    }
}

fn parts_from_config(format: Option<&String>) -> Vec<FormattedPart> {
    match format {
        Some(format) => format
            .split("#[")
            .map(|f| FormattedPart::from_format_string(f.to_string()))
            .collect(),
        None => vec![],
    }
}

#[cfg(test)]
mod test {
    use super::*;
    use anstyle::RgbColor;

    #[test]
    fn test_formatted_part_from_string() {
        let input = "#[fg=#ff0000,bg=#00ff00,bold,italic]foo";

        let part = FormattedPart::from_format_string(input.to_string());

        assert_eq!(
            part,
            FormattedPart {
                fg: Some(RgbColor(255, 0, 0).into()),
                bg: Some(RgbColor(0, 255, 0).into()),
                bold: true,
                italic: true,
                content: "foo".to_string(),
            },
        )
    }
}