mirror of
https://github.com/ratatui-org/ratatui
synced 2025-02-16 22:18:51 +00:00
Add divider
to Tabs
to change appearance of tab dividers.
This commit is contained in:
parent
144bfb71cf
commit
72c2eb7182
1 changed files with 13 additions and 2 deletions
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue