summaryrefslogtreecommitdiffstats
path: root/src/tabview.rs
blob: 0b3ca93c551a561797e2f2937fc2d56ed651b689 (plain)
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
use termion::event::Key;

use crate::widget::{Widget, WidgetCore};
use crate::fail::{HResult, HError, ErrorLog};
use crate::coordinates::Coordinates;

pub trait Tabbable {
    fn new_tab(&mut self) -> HResult<()>;
    fn close_tab(&mut self) -> HResult<()>;
    fn next_tab(&mut self) -> HResult<()>;
    fn prev_tab(&mut self) -> HResult<()>;
    fn goto_tab(&mut self, index: usize) -> HResult<()>;
    fn on_tab_switch(&mut self) -> HResult<()> {
        Ok(())
    }
    fn get_tab_names(&self) -> Vec<Option<String>>;
    fn active_tab(&self) -> &dyn Widget;
    fn active_tab_mut(&mut self) -> &mut dyn Widget;
    fn on_key_sub(&mut self, key: Key) -> HResult<()>;
    fn on_key(&mut self, key: Key) -> HResult<()> {
        self.on_key_sub(key)
    }
    fn on_refresh(&mut self) -> HResult<()> { Ok(()) }
    fn on_config_loaded(&mut self) -> HResult<()> { Ok(()) }
    fn on_new(&mut self) -> HResult<()> { Ok(()) }

}


#[derive(PartialEq)]
pub struct TabView<T> where T: Widget, TabView<T>: Tabbable {
    pub widgets: Vec<T>,
    pub active: usize,
    pub core: WidgetCore
}

impl<T> TabView<T> where T: Widget, TabView<T>: Tabbable {
    pub fn new(core: &WidgetCore) -> TabView<T> {
        let mut tabview = TabView {
            widgets: vec![],
            active: 0,
            core: core.clone()
        };

        Tabbable::on_new(&mut tabview).log();

        tabview
    }

    pub fn push_widget(&mut self, widget: T) -> HResult<()> {
        self.widgets.push(widget);
        Ok(())
    }

    pub fn pop_widget(&mut self) -> HResult<T> {
        let widget = self.widgets.pop()?;
        if self.widgets.len() <= self.active {
            self.active -= 1;
        }
        Ok(widget)
    }

    pub fn remove_widget(&mut self, index: usize) -> HResult<()> {
        let len = self.widgets.len();
        if len > 1 {
            self.widgets.remove(index);
            if index+1 == len {
                self.active -= 1;
            }
        }
        Ok(())
    }

    pub fn goto_tab_(&mut self, index: usize) -> HResult<()> {
        if index < self.widgets.len() {
            self.active = index;
            self.on_tab_switch().log();
        }
        Ok(())
    }

    pub fn active_tab_(&self) -> &T {
        &self.widgets[self.active]
    }

    pub fn active_tab_mut_(&mut self) -> &mut T {
        &mut self.widgets[self.active]
    }

    pub fn close_tab_(&mut self) -> HResult<()> {
        self.remove_widget(self.active).log();
        Ok(())
    }

    pub fn next_tab_(&mut self) {
        if self.active + 1 == self.widgets.len() {
            self.active = 0;
        } else {
            self.active += 1
        }
        self.on_tab_switch().log();
    }

    pub fn prev_tab_(&mut self) {
        if self.active == 0 {
            self.active = self.widgets.len() - 1;
        } else {
            self.active -= 1;
        }
        self.on_tab_switch().log();
    }
}

impl<T> Widget for TabView<T> where T: Widget, TabView<T>: Tabbable {
    fn get_core(&self) -> HResult<&WidgetCore> {
        Ok(&self.core)
    }
    fn get_core_mut(&mut self) -> HResult<&mut WidgetCore> {
        Ok(&mut self.core)
    }

    fn config_loaded(&mut self) -> HResult<()> {
        self.on_config_loaded()
    }

    fn set_coordinates(&mut self, coordinates: &Coordinates) -> HResult<()> {
        self.core.coordinates = coordinates.clone();
        for widget in &mut self.widgets {
            widget.set_coordinates(coordinates).log();
        }
        Ok(())
    }

    fn render_header(&self) -> HResult<String> {
        let xsize = self.get_coordinates()?.xsize();
        let header = self.active_tab_().render_header()?;
        let tab_names = self.get_tab_names();
        let mut nums_length = 0;
        let tabnums = (0..self.widgets.len()).map(|num| {
            nums_length += format!("{}:{} ",
                                   num,
                                   tab_names[num].as_ref().unwrap()).len();
            if num == self.active {
                format!(" {}{}:{}{}{}",
                        crate::term::invert(),
                        num,
                        tab_names[num].as_ref().unwrap(),
                        crate::term::reset(),
                        crate::term::header_color())
            } else {
                format!(" {}:{}", num, tab_names[num].as_ref().unwrap())
            }
        }).collect::<String>();


        let nums_pos = xsize.saturating_sub(nums_length as u16);

        Ok(format!("{}{}{}{}",
                header,
                crate::term::header_color(),
                crate::term::goto_xy(nums_pos, 1),
                tabnums))
    }

    fn render_footer(&self) -> HResult<String>
    {
        self.active_tab_().render_footer()
    }

    fn refresh(&mut self) -> HResult<()> {
        Tabbable::on_refresh(self).log();
        self.active_tab_mut().refresh()
    }

    fn get_drawlist(&self) -> HResult<String> {
        self.active_tab_().get_drawlist()
    }

    fn on_key(&mut self, key: Key) -> HResult<()> {
        match self.do_key(key) {
            Err(HError::WidgetUndefinedKeyError{..}) => Tabbable::on_key(self, key)?,
            e @ _ => e?
        }

        Ok(())
    }
}

use crate::keybind::*;

impl<T: Widget> Acting for TabView<T> where TabView<T>: Tabbable {
    type Action = TabAction;

    fn search_in(&self) -> Bindings<Self::Action> {
        self.core.config().keybinds.tab
    }

    fn do_action(&mut self, action: &Self::Action) -> HResult<()> {
        use TabAction::*;

        match action {
            GotoTab(n) => self.goto_tab(*n)?,
            NewTab => self.new_tab()?,
            CloseTab => self.close_tab()?,
            NextTab => self.next_tab()?,
            PrevTab => self.prev_tab()?,
        }

        Ok(())
    }
}