summaryrefslogtreecommitdiffstats
path: root/src/context/tab_context.rs
blob: 35ee9edea29fd033105a057420b196e281da445f (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
use std::collections::hash_map::{Iter, IterMut};
use std::collections::HashMap;

use uuid::Uuid;

use crate::tab::JoshutoTab;

#[derive(Default)]
pub struct TabContext {
    pub index: usize,
    pub tab_order: Vec<Uuid>,
    tabs: HashMap<Uuid, JoshutoTab>,
}

impl TabContext {
    pub fn new() -> Self {
        Self {
            ..Default::default()
        }
    }
    pub fn len(&self) -> usize {
        self.tab_order.len()
    }

    pub fn tab_ref(&self, id: &Uuid) -> Option<&JoshutoTab> {
        self.tabs.get(id)
    }

    pub fn tab_refs_in_order(&self) -> Vec<&JoshutoTab> {
        let mut tab_refs: Vec<&JoshutoTab> = vec![];
        for tab_id in self.tab_order.iter() {
            if let Some(tab_ref) = self.tab_ref(tab_id) {
                tab_refs.push(tab_ref);
            }
        }
        tab_refs
    }

    pub fn tab_mut(&mut self, id: &Uuid) -> Option<&mut JoshutoTab> {
        self.tabs.get_mut(id)
    }

    pub fn curr_tab_id(&self) -> Uuid {
        self.tab_order[self.index]
    }
    pub fn curr_tab_ref(&self) -> &JoshutoTab {
        let id = &self.tab_order[self.index];
        self.tabs.get(id).unwrap()
    }
    pub fn curr_tab_mut(&mut self) -> &mut JoshutoTab {
        let id = &self.tab_order[self.index];
        self.tabs.get_mut(id).unwrap()
    }
    pub fn insert_tab(&mut self, id: Uuid, tab: JoshutoTab) {
        self.tabs.insert(id, tab);
        self.tab_order.push(id);
    }
    pub fn remove_tab(&mut self, id: &Uuid) -> Option<JoshutoTab> {
        let tab = self.tabs.remove(id);
        for i in 0..self.tab_order.len() {
            if self.tab_order[i] == *id {
                self.tab_order.remove(i);
                break;
            }
        }
        tab
    }

    pub fn iter(&self) -> Iter<Uuid, JoshutoTab> {
        self.tabs.iter()
    }
    pub fn iter_mut(&mut self) -> IterMut<Uuid, JoshutoTab> {
        self.tabs.iter_mut()
    }
}