aboutsummaryrefslogtreecommitdiffstats
path: root/src/widgets/swap_layout.rs
blob: d8f9ffb65c232023944ed0d3bcfd36e65e6b8206 (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
use std::collections::BTreeMap;
use zellij_tile::shim::next_swap_layout;

use crate::render::FormattedPart;
use crate::{config::ZellijState, widgets::widget::Widget};

pub struct SwapLayoutWidget {
    format: Vec<FormattedPart>,
    hide_if_empty: bool,
}

impl SwapLayoutWidget {
    pub fn new(config: &BTreeMap<String, String>) -> Self {
        let mut format: Vec<FormattedPart> = Vec::new();
        if let Some(form) = config.get("swap_layout_format") {
            format = FormattedPart::multiple_from_format_string(form);
        }

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

        Self {
            format,
            hide_if_empty,
        }
    }
}

impl Widget for SwapLayoutWidget {
    fn process(&self, _name: &str, state: &ZellijState) -> String {
        let active_tab = state.tabs.iter().find(|t| t.active);

        if active_tab.is_none() {
            return "".to_owned();
        }

        let active_tab = active_tab.unwrap();

        let name = match active_tab.active_swap_layout_name.clone() {
            Some(n) => n,
            None => "".to_owned(),
        };

        if name.is_empty() && self.hide_if_empty {
            return "".to_owned();
        }

        if self.format.is_empty() {
            return name;
        }

        let mut output = "".to_owned();

        for f in &self.format {
            let mut content = f.content.clone();

            if content.contains("{name}") {
                content = content.replace("{name}", &name);
            }

            output = format!("{}{}", output, f.format_string(&content));
        }

        output
    }

    fn process_click(&self, _name: &str, _state: &ZellijState, _pos: usize) {
        next_swap_layout()
    }
}