sql_cli/widgets/
tab_bar_widget.rs1use ratatui::{
2 layout::Rect,
3 style::{Color, Modifier, Style},
4 text::{Line, Span},
5 widgets::{Block, Borders, Tabs},
6 Frame,
7};
8
9pub struct TabBarWidget {
11 current_index: usize,
13 buffer_names: Vec<String>,
15 show_shortcuts: bool,
17}
18
19impl TabBarWidget {
20 pub fn new(current_index: usize, buffer_names: Vec<String>) -> Self {
21 Self {
22 current_index,
23 buffer_names,
24 show_shortcuts: true,
25 }
26 }
27
28 pub fn with_shortcuts(mut self, show: bool) -> Self {
30 self.show_shortcuts = show;
31 self
32 }
33
34 pub fn render(&self, f: &mut Frame, area: Rect) {
36 let titles: Vec<Line> = self
41 .buffer_names
42 .iter()
43 .enumerate()
44 .map(|(i, name)| {
45 let mut spans = vec![];
46
47 if self.show_shortcuts && i < 9 {
49 spans.push(Span::styled(
50 format!("{}:", i + 1),
51 Style::default()
52 .fg(Color::DarkGray)
53 .add_modifier(Modifier::DIM),
54 ));
55 }
56
57 let display_name = if name.len() > 20 {
59 format!("{}...", &name[..17])
60 } else {
61 name.clone()
62 };
63
64 spans.push(Span::raw(display_name));
65 Line::from(spans)
66 })
67 .collect();
68
69 let tabs = Tabs::new(titles)
70 .block(
71 Block::default()
72 .borders(Borders::BOTTOM)
73 .border_style(Style::default().fg(Color::DarkGray)),
74 )
75 .select(self.current_index)
76 .style(Style::default().fg(Color::Gray))
77 .highlight_style(
78 Style::default()
79 .fg(Color::White)
80 .add_modifier(Modifier::BOLD)
81 .bg(Color::DarkGray),
82 )
83 .divider(Span::styled(" │ ", Style::default().fg(Color::DarkGray)));
84
85 f.render_widget(tabs, area);
86 }
87
88 pub fn height(&self) -> u16 {
90 2 }
93}
94
95pub fn render_tab_bar(f: &mut Frame, area: Rect, current_index: usize, buffer_names: Vec<String>) {
97 let widget = TabBarWidget::new(current_index, buffer_names);
98 widget.render(f, area);
99}