|
| 1 | +use crate::utils; |
| 2 | +use crossterm::event::{self, Event, KeyCode, KeyEventKind}; |
| 3 | +use ratatui::{ |
| 4 | + layout::{Alignment, Constraint, Layout, Margin, Rect}, |
| 5 | + prelude::Buffer, |
| 6 | + style::{Color, Style}, |
| 7 | + symbols::scrollbar, |
| 8 | + widgets::{Block, BorderType, Paragraph, Scrollbar, ScrollbarState, StatefulWidget, Widget}, |
| 9 | +}; |
| 10 | + |
| 11 | +#[derive(Default, Debug)] |
| 12 | +pub struct ResultPageState { |
| 13 | + pub vertical_scroll: u16, |
| 14 | + pub vertical_scroll_state: ScrollbarState, |
| 15 | + pub horizontal_scroll: u16, |
| 16 | + pub horizontal_scroll_state: ScrollbarState, |
| 17 | + pub ack: bool, |
| 18 | +} |
| 19 | + |
| 20 | +#[derive(Default, Debug)] |
| 21 | +pub struct ResultPage { |
| 22 | + result_text: Paragraph<'static>, |
| 23 | +} |
| 24 | + |
| 25 | +impl ResultPage { |
| 26 | + pub fn new(result_text: String, state: &mut ResultPageState) -> Self { |
| 27 | + let max_width = result_text |
| 28 | + .lines() |
| 29 | + .map(|line| line.len()) |
| 30 | + .max() |
| 31 | + .unwrap_or(0); |
| 32 | + |
| 33 | + let num_lines = result_text.lines().count(); |
| 34 | + |
| 35 | + state.vertical_scroll_state = state |
| 36 | + .vertical_scroll_state |
| 37 | + .content_length(num_lines); |
| 38 | + |
| 39 | + state.horizontal_scroll_state = state.horizontal_scroll_state.content_length(max_width); |
| 40 | + |
| 41 | + Self { |
| 42 | + result_text: Paragraph::new(result_text), |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + fn render_left(&self, buf: &mut Buffer, left: Rect) { |
| 47 | + let left_block = Block::bordered() |
| 48 | + .border_type(BorderType::Plain) |
| 49 | + .border_style(Style::default().fg(Color::Yellow)); |
| 50 | + |
| 51 | + let left_text = Paragraph::new(utils::get_ascii_art()); |
| 52 | + |
| 53 | + left_text.block(left_block).render(left, buf); |
| 54 | + } |
| 55 | + |
| 56 | + fn render_right(&self, buf: &mut Buffer, right: Rect, state: &mut ResultPageState) { |
| 57 | + let right_block = Block::bordered() |
| 58 | + .border_type(BorderType::Plain) |
| 59 | + .border_style(Style::default().fg(Color::Yellow)) |
| 60 | + .title_bottom("Press q to quit...") |
| 61 | + .title_style(Style::default().fg(Color::Red)) |
| 62 | + .title_alignment(Alignment::Right); |
| 63 | + |
| 64 | + let result_text = self |
| 65 | + .result_text |
| 66 | + .clone() |
| 67 | + .block(right_block) |
| 68 | + .scroll((state.vertical_scroll as u16, state.horizontal_scroll as u16)); |
| 69 | + result_text.render(right, buf); |
| 70 | + } |
| 71 | + |
| 72 | + pub fn handle_key_event(&mut self, state: &mut ResultPageState) { |
| 73 | + if event::poll(std::time::Duration::from_millis(50)).unwrap() { |
| 74 | + if let Event::Key(key) = event::read().unwrap() { |
| 75 | + if key.kind != KeyEventKind::Press { |
| 76 | + return; |
| 77 | + } |
| 78 | + if key.code == KeyCode::Char('q') { |
| 79 | + state.ack = true; |
| 80 | + } |
| 81 | + |
| 82 | + match key.code { |
| 83 | + KeyCode::Char('j') | KeyCode::Down => { |
| 84 | + state.vertical_scroll = state.vertical_scroll.saturating_add(1); |
| 85 | + state.vertical_scroll_state = state |
| 86 | + .vertical_scroll_state |
| 87 | + .position(state.vertical_scroll as usize); |
| 88 | + } |
| 89 | + KeyCode::Char('k') | KeyCode::Up => { |
| 90 | + state.vertical_scroll = state.vertical_scroll.saturating_sub(1); |
| 91 | + state.vertical_scroll_state = state |
| 92 | + .vertical_scroll_state |
| 93 | + .position(state.vertical_scroll as usize); |
| 94 | + } |
| 95 | + KeyCode::Char('h') | KeyCode::Left => { |
| 96 | + state.horizontal_scroll = state.horizontal_scroll.saturating_sub(1); |
| 97 | + state.horizontal_scroll_state = state |
| 98 | + .horizontal_scroll_state |
| 99 | + .position(state.horizontal_scroll as usize); |
| 100 | + } |
| 101 | + KeyCode::Char('l') | KeyCode::Right => { |
| 102 | + state.horizontal_scroll = state.horizontal_scroll.saturating_add(1); |
| 103 | + state.horizontal_scroll_state = state |
| 104 | + .horizontal_scroll_state |
| 105 | + .position(state.horizontal_scroll as usize); |
| 106 | + } |
| 107 | + _ => {} |
| 108 | + } |
| 109 | + } |
| 110 | + } |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | +impl StatefulWidget for &ResultPage { |
| 115 | + type State = ResultPageState; |
| 116 | + |
| 117 | + fn render(self, area: Rect, buf: &mut Buffer, state: &mut ResultPageState) { |
| 118 | + let layout = Layout::horizontal([Constraint::Percentage(45), Constraint::Percentage(55)]); |
| 119 | + let [left, right] = layout.areas(area); |
| 120 | + |
| 121 | + self.render_left(buf, left); |
| 122 | + self.render_right(buf, right, state); |
| 123 | + |
| 124 | + let vertical_scrollbar = |
| 125 | + Scrollbar::new(ratatui::widgets::ScrollbarOrientation::VerticalLeft) |
| 126 | + .symbols(scrollbar::VERTICAL); |
| 127 | + |
| 128 | + let horizontal_scrollbar = |
| 129 | + Scrollbar::new(ratatui::widgets::ScrollbarOrientation::HorizontalBottom) |
| 130 | + .symbols(scrollbar::HORIZONTAL); |
| 131 | + |
| 132 | + vertical_scrollbar.render( |
| 133 | + right.inner(&Margin { |
| 134 | + vertical: 1, |
| 135 | + horizontal: 0, |
| 136 | + }), |
| 137 | + buf, |
| 138 | + &mut state.vertical_scroll_state, |
| 139 | + ); |
| 140 | + horizontal_scrollbar.render( |
| 141 | + right.inner(&Margin { |
| 142 | + vertical: 0, |
| 143 | + horizontal: 1, |
| 144 | + }), |
| 145 | + buf, |
| 146 | + &mut state.horizontal_scroll_state, |
| 147 | + ); |
| 148 | + } |
| 149 | +} |
0 commit comments