tui_kit/tabs.rs
1use ratatui::text::{Line, Span};
2
3use crate::Theme;
4
5/// Builds a horizontal tab-bar [`Line`] from a list of `(label, is_active)` pairs.
6///
7/// Active tab uses [`Theme::tab_active`], inactive tabs use [`Theme::tab_inactive`].
8/// Tabs are separated by two spaces. A leading and trailing space is added so the
9/// line reads cleanly inside a block title.
10///
11/// The returned `Line` is `'static` (all content is owned) so it can be used
12/// freely as a block title without lifetime concerns.
13///
14/// # Example
15///
16/// ```ignore
17/// let title = tab_line(&[
18/// ("Browse", matches!(tab, Tab::Browse)),
19/// ("Favorites", matches!(tab, Tab::Favorites)),
20/// ("History", matches!(tab, Tab::History)),
21/// ], &theme);
22/// ```
23pub fn tab_line(tabs: &[(&str, bool)], theme: &Theme) -> Line<'static> {
24 let mut spans: Vec<Span<'static>> = vec![Span::raw(" ")];
25
26 for (i, (label, active)) in tabs.iter().enumerate() {
27 if i > 0 {
28 spans.push(Span::styled(" - ", theme.hint));
29 }
30 let style = if *active { theme.tab_active } else { theme.tab_inactive };
31 spans.push(Span::styled(label.to_string(), style));
32 }
33
34 spans.push(Span::raw(" "));
35 Line::from(spans)
36}