pub mod check;
mod content;
use content::build_content;
use crossterm::{
event::{self, DisableMouseCapture, EnableMouseCapture, Event, KeyCode},
execute,
terminal::{EnterAlternateScreen, LeaveAlternateScreen, disable_raw_mode, enable_raw_mode},
};
use ratatui::{
Frame, Terminal,
backend::{Backend, CrosstermBackend},
layout::{Alignment, Constraint, Direction, Layout, Rect},
style::{Color, Style},
text::{Line, Span},
widgets::Paragraph,
};
use std::io;
use std::path::PathBuf;
use crate::parser::FileParseResult;
use crate::verifier::VerificationResult;
pub struct TuiApp {
pub file_path: PathBuf,
pub parsed: FileParseResult,
pub results: Vec<VerificationResult>,
pub scroll_v: usize,
pub scroll_h: usize,
pub max_v: usize,
pub max_w: usize,
pub show_help: bool,
pub show_detail: bool,
}
impl TuiApp {
pub fn new(
file_path: PathBuf,
parsed: FileParseResult,
results: Vec<VerificationResult>,
) -> Self {
TuiApp {
file_path,
parsed,
results,
scroll_v: 0,
scroll_h: 0,
max_v: 0,
max_w: 0,
show_help: false,
show_detail: false,
}
}
pub fn run(&mut self) -> io::Result<()> {
enable_raw_mode()?;
let mut stdout = io::stdout();
execute!(stdout, EnterAlternateScreen, EnableMouseCapture)?;
let backend = CrosstermBackend::new(stdout);
let mut terminal = Terminal::new(backend)?;
let res = self.run_app(&mut terminal);
disable_raw_mode()?;
execute!(
terminal.backend_mut(),
LeaveAlternateScreen,
DisableMouseCapture
)?;
terminal.show_cursor()?;
res
}
fn run_app<B: Backend>(&mut self, terminal: &mut Terminal<B>) -> io::Result<()> {
loop {
terminal.draw(|f| self.draw(f))?;
if let Event::Key(key) = event::read()? {
match key.code {
KeyCode::Char('q') => return Ok(()),
KeyCode::Char('h') => self.show_help = !self.show_help,
KeyCode::Down | KeyCode::Char('j') => {
self.scroll_v = self.scroll_v.saturating_add(1)
}
KeyCode::Up | KeyCode::Char('k') => {
self.scroll_v = self.scroll_v.saturating_sub(1)
}
KeyCode::Right | KeyCode::Char('l') => {
self.scroll_h = self.scroll_h.saturating_add(2)
}
KeyCode::Left => self.scroll_h = self.scroll_h.saturating_sub(2),
KeyCode::Enter => self.show_detail = !self.show_detail,
KeyCode::Esc => self.show_detail = false,
_ => {}
}
}
}
}
pub fn draw(&mut self, f: &mut Frame) {
let chunks = Layout::default()
.direction(Direction::Vertical)
.margin(1)
.constraints([
Constraint::Length(1),
Constraint::Min(4),
Constraint::Length(1),
])
.split(f.size());
self.draw_title(f, chunks[0]);
self.draw_body(f, chunks[1]);
self.draw_footer(f, chunks[2]);
}
fn draw_title(&self, f: &mut Frame, area: Rect) {
let name = self
.file_path
.file_name()
.unwrap_or_default()
.to_string_lossy();
let help = if self.show_help {
" j/↓↑:scroll ←→:hscroll Enter:detail Esc:close h:help q:quit "
} else {
" h:help "
};
let line = Line::from(vec![
Span::styled(format!(" {} ", name), Style::default().fg(Color::Cyan)),
Span::styled(help, Style::default().fg(Color::Gray)),
]);
f.render_widget(Paragraph::new(line), area);
}
fn draw_body(&mut self, f: &mut Frame, area: Rect) {
let lines = build_content(
&self.parsed.ori,
&self.parsed.steps,
&self.results,
self.show_detail,
self.parsed
.steps
.first()
.map_or(&[] as &[usize], |s| s.col_widths.as_slice()),
);
let line_count = lines.len();
let max_line_w = lines.iter().map(|l| l.width()).max().unwrap_or(0);
self.max_v = line_count.saturating_sub(area.height as usize);
self.max_w = max_line_w.saturating_sub(area.width as usize);
self.scroll_v = self.scroll_v.min(self.max_v);
self.scroll_h = self.scroll_h.min(self.max_w);
f.render_widget(
Paragraph::new(lines).scroll((self.scroll_v as u16, self.scroll_h as u16)),
area,
);
}
fn draw_footer(&self, f: &mut Frame, area: Rect) {
let mut icons = String::new();
if self.scroll_h > 0 {
icons.push_str("← ");
}
if self.scroll_v > 0 {
icons.push_str("↑ ");
}
if self.scroll_v < self.max_v {
icons.push_str("↓ ");
}
if self.scroll_h < self.max_w {
icons.push_str("→ ");
}
let text = if self.show_help {
" j/↓↑:scroll ←→:hscroll Enter:detail Esc:close h:help q:quit "
} else {
" h:help q:quit "
};
let line = Line::from(vec![
Span::raw(icons),
Span::styled(text, Style::default().fg(Color::Gray)),
]);
f.render_widget(Paragraph::new(line).alignment(Alignment::Right), area);
}
}