|
| 1 | +use ratatui::{ |
| 2 | + crossterm::event::KeyCode, |
| 3 | + layout::{Alignment, Rect}, |
| 4 | + style::{Color, Modifier, Style}, |
| 5 | + text::Line, |
| 6 | + widgets::{Block, BorderType, Borders, Clear, Paragraph, Wrap}, |
| 7 | +}; |
| 8 | + |
| 9 | +use super::PopUp; |
| 10 | + |
| 11 | +#[derive(Debug)] |
| 12 | +pub struct InfoPopUp { |
| 13 | + title: String, |
| 14 | + info: String, |
| 15 | + offset: (u16, u16), |
| 16 | + max_offset: (u16, u16), |
| 17 | + dimensions: (u16, u16), |
| 18 | +} |
| 19 | + |
| 20 | +impl InfoPopUp { |
| 21 | + /// Generate a pop-up with a title and an arbitrary information. |
| 22 | + pub fn generate_info_popup(title: &str, info: &str) -> Box<dyn PopUp> { |
| 23 | + let mut lines = 0; |
| 24 | + let mut columns = 0; |
| 25 | + |
| 26 | + for line in info.lines() { |
| 27 | + lines += 1; |
| 28 | + let line_len = line.len() as u16; |
| 29 | + if line_len > columns { |
| 30 | + columns = line_len; |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + let dimensions = (30, 50); // TODO: Calculate percentage based on the lines and screen size |
| 35 | + |
| 36 | + Box::new(InfoPopUp { |
| 37 | + title: title.to_string(), |
| 38 | + info: info.to_string(), |
| 39 | + offset: (0, 0), |
| 40 | + max_offset: (lines, columns), |
| 41 | + dimensions, |
| 42 | + }) |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +impl PopUp for InfoPopUp { |
| 47 | + fn dimensions(&self) -> (u16, u16) { |
| 48 | + self.dimensions |
| 49 | + } |
| 50 | + |
| 51 | + /// Renders a centered overlaying pop-up with a title and an arbitrary info. |
| 52 | + fn render(&self, f: &mut ratatui::Frame, chunk: Rect) { |
| 53 | + let bold_blue = Style::default() |
| 54 | + .add_modifier(Modifier::BOLD) |
| 55 | + .fg(Color::Blue); |
| 56 | + let block = Block::default() |
| 57 | + .title(self.title.clone()) |
| 58 | + .title_alignment(Alignment::Center) |
| 59 | + .title_style(bold_blue) |
| 60 | + .title_bottom(Line::styled("(ESC / q) Close", bold_blue)) |
| 61 | + .borders(Borders::ALL) |
| 62 | + .border_type(BorderType::Double) |
| 63 | + .style(Style::default()); |
| 64 | + |
| 65 | + let pop_up = Paragraph::new(self.info.clone()) |
| 66 | + .block(block) |
| 67 | + .alignment(Alignment::Left) |
| 68 | + .wrap(Wrap { trim: true }) |
| 69 | + .scroll(self.offset); |
| 70 | + |
| 71 | + f.render_widget(Clear, chunk); |
| 72 | + f.render_widget(pop_up, chunk); |
| 73 | + } |
| 74 | + |
| 75 | + /// Handles simple one-char width navigation. |
| 76 | + fn handle(&mut self, key: ratatui::crossterm::event::KeyEvent) -> color_eyre::Result<()> { |
| 77 | + match key.code { |
| 78 | + KeyCode::Up | KeyCode::Char('k') => { |
| 79 | + if self.offset.0 > 0 { |
| 80 | + self.offset.0 -= 1; |
| 81 | + } |
| 82 | + } |
| 83 | + KeyCode::Down | KeyCode::Char('j') => { |
| 84 | + if self.offset.0 < self.max_offset.0 { |
| 85 | + self.offset.0 += 1; |
| 86 | + } |
| 87 | + } |
| 88 | + KeyCode::Left | KeyCode::Char('h') => { |
| 89 | + if self.offset.1 > 0 { |
| 90 | + self.offset.1 -= 1; |
| 91 | + } |
| 92 | + } |
| 93 | + KeyCode::Right | KeyCode::Char('l') => { |
| 94 | + if self.offset.1 < self.max_offset.1 { |
| 95 | + self.offset.1 += 1; |
| 96 | + } |
| 97 | + } |
| 98 | + _ => {} |
| 99 | + } |
| 100 | + |
| 101 | + Ok(()) |
| 102 | + } |
| 103 | +} |
0 commit comments