diff options
Diffstat (limited to 'src/context/tab_context.rs')
-rw-r--r-- | src/context/tab_context.rs | 37 |
1 files changed, 35 insertions, 2 deletions
diff --git a/src/context/tab_context.rs b/src/context/tab_context.rs index 1393b06..ae05f55 100644 --- a/src/context/tab_context.rs +++ b/src/context/tab_context.rs @@ -3,18 +3,23 @@ use std::collections::HashMap; use uuid::Uuid; +use crate::config::option::{TabBarDisplayMode, TabBarDisplayOption}; use crate::tab::JoshutoTab; #[derive(Default)] pub struct TabContext { pub index: usize, pub tab_order: Vec<Uuid>, + pub display: TabBarDisplayOption, tabs: HashMap<Uuid, JoshutoTab>, } impl TabContext { - pub fn new() -> Self { - Self::default() + pub fn new(display: TabBarDisplayOption) -> Self { + Self { + display, + ..Default::default() + } } pub fn len(&self) -> usize { self.tab_order.len() @@ -56,4 +61,32 @@ impl TabContext { pub fn iter_mut(&mut self) -> IterMut<Uuid, JoshutoTab> { self.tabs.iter_mut() } + + pub fn tab_title_width(&self) -> usize { + self.tabs + .values() + .map(|tab| { + let title_len = tab.tab_title().len(); + (title_len > self.display.max_len) + .then(|| self.display.max_len) + .unwrap_or(title_len) + }) + .sum() + } + + pub fn tab_area_width(&self) -> usize { + let width_without_divider = match self.display.mode { + TabBarDisplayMode::Number => (1..=self.len()).map(|n| n.to_string().len() + 2).sum(), // each number has a horizontal padding(1 char width) + TabBarDisplayMode::Directory => self.tab_title_width(), + TabBarDisplayMode::All => { + // [number][: ](width = 2)[title] + self.tab_title_width() + + (1..=self.len()) + .map(|n| n.to_string().len() + 2) + .sum::<usize>() + } + }; + + width_without_divider + 3 * (self.len() - 1) + } } |