aboutsummaryrefslogtreecommitdiffstats
path: root/src/main.rs
blob: ef189cee565f788da4ce4fcff2bceb2b23b06d5f (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
use config::ModuleConfig;
use widgets::{
    datetime::DateTimeWidget, mode::ModeWidget, session::SessionWidget, tabs::TabsWidget,
    widget::Widget,
};
use zellij_tile::prelude::*;

use std::{collections::BTreeMap, sync::Arc, usize};

mod config;
mod frames;
mod render;
mod widgets;

#[derive(Default)]
struct State {
    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 cols: usize,
    pub mode: ModeInfo,
    pub tabs: Vec<TabInfo>,
    pub sessions: Vec<SessionInfo>,
}

#[cfg(not(test))]
register_plugin!(State);

impl ZellijPlugin for State {
    fn load(&mut self, configuration: BTreeMap<String, String>) {
        // we need the ReadApplicationState permission to receive the ModeUpdate and TabUpdate
        // events
        // we need the RunCommands permission to run "cargo test" in a floating window
        request_permission(&[
            PermissionType::ReadApplicationState,
            PermissionType::ChangeApplicationState,
            PermissionType::RunCommands,
        ]);
        subscribe(&[
            EventType::Mouse,
            EventType::ModeUpdate,
            EventType::PaneUpdate,
            EventType::PermissionRequestResult,
            EventType::TabUpdate,
            EventType::SessionUpdate,
        ]);

        self.userspace_configuration = configuration.clone();
        self.module_config = ModuleConfig::new(configuration.clone());
        self.widget_map = register_widgets(configuration);

        self.state = ZellijState {
            cols: 0,
            mode: ModeInfo::default(),
            tabs: Vec::new(),
            sessions: Vec::new(),
        };
    }

    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;
            }
            Event::PaneUpdate(pane_info) => {
                if self.module_config.hide_frame_for_single_pane {
                    frames::hide_frames_on_single_pane(
                        self.state.tabs.clone(),
                        pane_info,
                        get_plugin_ids(),
                    );

                    should_render = true;
                }
            }
            Event::PermissionRequestResult(_result) => {
                set_selectable(false);
            }
            Event::SessionUpdate(session_info) => {
                if self.module_config.hide_frame_for_single_pane {
                    let current_session = session_info.iter().find(|s| s.is_current_session);

                    if let Some(current_session) = current_session {
                        frames::hide_frames_on_single_pane(
                            current_session.clone().tabs,
                            current_session.clone().panes,
                            get_plugin_ids(),
                        );
                    }
                }

                self.state.sessions = session_info;

                should_render = true;
            }
            Event::TabUpdate(tab_info) => {
                self.state.tabs = tab_info;
                should_render = true;
            }
            _ => (),
        };
        should_render
    }

    fn render(&mut self, _rows: usize, cols: usize) {
        self.state.cols = cols;
        self.module_config
            .render_bar(self.state.clone(), self.widget_map.clone());
    }
}

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(
        "datetime".to_string(),
        Arc::new(DateTimeWidget::new(configuration.clone())),
    );
    widget_map.insert(
        "mode".to_string(),
        Arc::new(ModeWidget::new(configuration.clone())),
    );
    widget_map.insert(
        "session".to_string(),
        Arc::new(SessionWidget::new(configuration.clone())),
    );
    widget_map.insert("tabs".to_string(), Arc::new(TabsWidget::new(configuration)));

    widget_map
}