Add divider to Tabs

to change appearance of tab dividers.
This commit is contained in:
Jens Krause 2019-01-09 19:08:25 +01:00 committed by Florian Dehau
parent 144bfb71cf
commit 72c2eb7182

View file

@ -13,12 +13,14 @@ use crate::widgets::{Block, Widget};
/// ```
/// # use tui::widgets::{Block, Borders, Tabs};
/// # use tui::style::{Style, Color};
/// # use tui::symbols::{DOT};
/// # fn main() {
/// Tabs::default()
/// .block(Block::default().title("Tabs").borders(Borders::ALL))
/// .titles(&["Tab1", "Tab2", "Tab3", "Tab4"])
/// .style(Style::default().fg(Color::White))
/// .highlight_style(Style::default().fg(Color::Yellow));
/// .divider(DOT);
/// # }
/// ```
pub struct Tabs<'a, T>
@ -35,6 +37,8 @@ where
style: Style,
/// The style used to display the selected item
highlight_style: Style,
/// Tab divider
divider: &'a str,
}
impl<'a, T> Default for Tabs<'a, T>
@ -48,6 +52,7 @@ where
selected: 0,
style: Default::default(),
highlight_style: Default::default(),
divider: line::VERTICAL,
}
}
}
@ -80,6 +85,11 @@ where
self.highlight_style = style;
self
}
pub fn divider(mut self, divider: &'a str) -> Tabs<'a, T> {
self.divider = divider;
self
}
}
impl<'a, T> Widget for Tabs<'a, T>
@ -102,6 +112,7 @@ where
self.background(&tabs_area, buf, self.style.bg);
let mut x = tabs_area.left();
let divider_width = self.divider.chars().count() as u16;
for (title, style) in self.titles.iter().enumerate().map(|(i, t)| {
if i == self.selected {
(t, self.highlight_style)
@ -119,10 +130,10 @@ where
break;
} else {
buf.get_mut(x, tabs_area.top())
.set_symbol(line::VERTICAL)
.set_symbol(self.divider)
.set_fg(self.style.fg)
.set_bg(self.style.bg);
x += 1;
x += divider_width;
}
}
}