stynx_code_tui/widgets/
info_dialog.rs1use ratatui::{
2 buffer::Buffer,
3 layout::Rect,
4 style::{Modifier, Style},
5 text::{Line, Span},
6 widgets::{Block, Borders, Clear, Paragraph, Widget},
7};
8
9use crate::theme;
10
11pub struct InfoDialog<'a> {
12 pub title: &'a str,
13 pub rows: &'a [(String, String)],
14}
15
16impl<'a> InfoDialog<'a> {
17 pub fn new(title: &'a str, rows: &'a [(String, String)]) -> Self {
18 Self { title, rows }
19 }
20}
21
22impl<'a> Widget for InfoDialog<'a> {
23 fn render(self, area: Rect, buf: &mut Buffer) {
24 let max_key = self.rows.iter().map(|(k, _)| k.len()).max().unwrap_or(0);
25 let visible_rows = self.rows.len() as u16 + 4;
26 let h = visible_rows.min(area.height.saturating_sub(4));
27 let w = 60.min(area.width.saturating_sub(6));
28 let x = area.x + (area.width.saturating_sub(w)) / 2;
29 let y = area.y + (area.height.saturating_sub(h)) / 2;
30 let rect = Rect { x, y, width: w, height: h };
31 Clear.render(rect, buf);
32
33 let block = Block::default()
34 .borders(Borders::ALL)
35 .border_style(Style::default().fg(theme::BORDER_ACTIVE()))
36 .style(Style::default().bg(theme::BACKGROUND_PANEL()))
37 .title(Span::styled(
38 format!(" {} ", self.title),
39 Style::default().fg(theme::TEXT()).add_modifier(Modifier::BOLD),
40 ));
41 let inner = block.inner(rect);
42 block.render(rect, buf);
43
44 let mut lines: Vec<Line<'static>> = Vec::new();
45 for (k, v) in self.rows {
46 let key_padded = format!("{:width$}", k, width = max_key);
47 lines.push(Line::from(vec![
48 Span::styled(
49 format!(" {key_padded} "),
50 Style::default().fg(theme::TEXT_MUTED()).bg(theme::BACKGROUND_PANEL()),
51 ),
52 Span::styled(
53 v.clone(),
54 Style::default()
55 .fg(theme::TEXT())
56 .bg(theme::BACKGROUND_PANEL())
57 .add_modifier(Modifier::BOLD),
58 ),
59 ]));
60 }
61 lines.push(Line::from(""));
62 lines.push(Line::from(Span::styled(
63 " esc close ".to_string(),
64 Style::default().fg(theme::TEXT_MUTED()).bg(theme::BACKGROUND_PANEL()),
65 )));
66
67 Paragraph::new(lines)
68 .style(Style::default().bg(theme::BACKGROUND_PANEL()))
69 .render(inner, buf);
70 }
71}