From 12869a8ea2a0be65424976a04134a05940ee5a45 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Thu, 29 Jan 2026 15:40:04 +0000 Subject: [PATCH] feat: Enhanced TUI with Filtering and Context View - Added severity filtering (All, High, Medium, Low) using keys 1-4. - Added context view popup (Enter key) showing +/- 2 lines of code. - Updated status bar to show current filter mode. - Refactored finding management to support filtering while maintaining the ability to repair/ignore findings from the filtered list. Co-authored-by: 09Catho <169603149+09Catho@users.noreply.github.com> --- src/main.rs | 81 +++++++++++++++++++++++++++++------- src/ui.rs | 115 +++++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 181 insertions(+), 15 deletions(-) diff --git a/src/main.rs b/src/main.rs index a8baf36..0c4e454 100644 --- a/src/main.rs +++ b/src/main.rs @@ -17,7 +17,8 @@ use ignore::WalkBuilder; use ratatui::{backend::CrosstermBackend, Terminal}; use scanner::{Finding, Severity}; use std::fs::File; -use std::io::{self, Write}; +use std::io; +use ui::FilterMode; // use std::path::Path; fn main() -> Result<()> { @@ -362,11 +363,51 @@ fn run_app( } match key.code { - KeyCode::Char('q') | KeyCode::Esc => return Ok(()), + KeyCode::Char('q') | KeyCode::Esc => { + if app.show_context { + app.show_context = false; + } else { + return Ok(()); + } + } + KeyCode::Enter => { + if app.show_context { + app.show_context = false; + } else if let Some(sel) = app.state.selected() { + if let Some(f) = app.findings.get(sel) { + match ui::get_file_context(&f.file_path, f.line_number) { + Ok(lines) => { + app.context_lines = Some(lines); + app.show_context = true; + } + Err(e) => { + app.clipboard_status = + Some(format!("Error reading context: {}", e)); + } + } + } + } + } KeyCode::Down => app.next(), KeyCode::Up => app.previous(), KeyCode::Char('s') => app.strict_mode = !app.strict_mode, KeyCode::Char('?') => app.show_help = !app.show_help, + KeyCode::Char('1') => { + app.filter_mode = FilterMode::All; + app.update_visible_findings(); + } + KeyCode::Char('2') => { + app.filter_mode = FilterMode::High; + app.update_visible_findings(); + } + KeyCode::Char('3') => { + app.filter_mode = FilterMode::Medium; + app.update_visible_findings(); + } + KeyCode::Char('4') => { + app.filter_mode = FilterMode::Low; + app.update_visible_findings(); + } KeyCode::Char('c') => { if let Some(sel) = app.state.selected() { if let Some(f) = app.findings.get(sel) { @@ -405,14 +446,21 @@ fn run_app( }; match fixer::fix_file(&f.file_path, vec![replacement]) { Ok(_) => { - app.findings.remove(sel); + let fingerprint = f.fingerprint.clone(); + // Remove from all_findings + if let Some(idx) = app + .all_findings + .iter() + .position(|x| x.fingerprint == fingerprint) + { + app.all_findings.remove(idx); + } + app.update_visible_findings(); app.clipboard_status = Some("Fixed!".to_string()); - if app.findings.is_empty() { + + if app.all_findings.is_empty() { return Ok(()); } - if sel >= app.findings.len() { - app.state.select(Some(app.findings.len() - 1)); - } } Err(e) => { app.clipboard_status = Some(format!("Error: {}", e)); @@ -433,14 +481,19 @@ fn run_app( ); let _ = baseline.save(); - // Remove from UI list - app.findings.remove(sel); - if app.findings.is_empty() { - return Ok(()); // All handled + let fingerprint = f.fingerprint.clone(); + // Remove from all_findings + if let Some(idx) = app + .all_findings + .iter() + .position(|x| x.fingerprint == fingerprint) + { + app.all_findings.remove(idx); } - // Adjust selection - if sel >= app.findings.len() { - app.state.select(Some(app.findings.len() - 1)); + app.update_visible_findings(); + + if app.all_findings.is_empty() { + return Ok(()); // All handled } } } diff --git a/src/ui.rs b/src/ui.rs index f8795bc..5e7052d 100644 --- a/src/ui.rs +++ b/src/ui.rs @@ -7,14 +7,28 @@ use ratatui::{ widgets::{Block, Borders, Clear, List, ListItem, ListState, Paragraph, Wrap}, Frame, }; +use std::fs::File; +use std::io::{self, BufRead, BufReader}; + +#[derive(Debug, PartialEq, Clone, Copy)] +pub enum FilterMode { + All, + High, + Medium, + Low, +} pub struct App { + pub all_findings: Vec, pub findings: Vec, pub state: ListState, pub strict_mode: bool, pub show_help: bool, pub _show_quit_confirm: bool, pub clipboard_status: Option, + pub filter_mode: FilterMode, + pub show_context: bool, + pub context_lines: Option>, } impl App { @@ -24,12 +38,16 @@ impl App { state.select(Some(0)); } App { + all_findings: findings.clone(), findings, state, strict_mode: strict, show_help: false, _show_quit_confirm: false, clipboard_status: None, + filter_mode: FilterMode::All, + show_context: false, + context_lines: None, } } @@ -66,6 +84,50 @@ impl App { }; self.state.select(Some(i)); } + + pub fn update_visible_findings(&mut self) { + self.findings = self + .all_findings + .iter() + .filter(|f| match self.filter_mode { + FilterMode::All => true, + FilterMode::High => f.severity == Severity::High, + FilterMode::Medium => f.severity == Severity::Medium, + FilterMode::Low => f.severity == Severity::Low, + }) + .cloned() + .collect(); + + // Reset selection if out of bounds + if self.findings.is_empty() { + self.state.select(None); + } else { + self.state.select(Some(0)); + } + } +} + +pub fn get_file_context(path: &str, line_num: usize) -> io::Result> { + let file = File::open(path)?; + let reader = BufReader::new(file); + + // Context window: +/- 2 lines + let start = line_num.saturating_sub(2); + let end = line_num + 2; + + let mut lines = Vec::new(); + for (i, line) in reader.lines().enumerate() { + let current_line = i + 1; + if current_line >= start && current_line <= end { + if let Ok(l) = line { + lines.push((current_line, l)); + } + } + if current_line > end { + break; + } + } + Ok(lines) } pub fn ui(f: &mut Frame, app: &mut App) { @@ -221,8 +283,14 @@ pub fn ui(f: &mut Frame, app: &mut App) { // --- BOTTOM BAR --- let mode_str = if app.strict_mode { "STRICT" } else { "NORMAL" }; + let filter_str = match app.filter_mode { + FilterMode::All => "ALL", + FilterMode::High => "HIGH", + FilterMode::Medium => "MED", + FilterMode::Low => "LOW", + }; let help_text = - "q:Quit | g:Baseline (Ignore) | c:Copy | r:Repair | s:Switch Mode | ?:Help | \u{2191}\u{2193}:Nav"; + "q:Quit | 1-4:Filter | g:Ignore | c:Copy | r:Repair | s:Mode | Enter:Ctx | ?:Help"; let status_bar = Paragraph::new(Line::from(vec![ Span::styled( @@ -230,6 +298,11 @@ pub fn ui(f: &mut Frame, app: &mut App) { Style::default().bg(Color::Blue).fg(Color::White), ), Span::raw(" "), + Span::styled( + format!(" FILTER: {} ", filter_str), + Style::default().bg(Color::Magenta).fg(Color::White), + ), + Span::raw(" "), Span::raw(help_text), ])) .alignment(Alignment::Center); @@ -281,6 +354,46 @@ pub fn ui(f: &mut Frame, app: &mut App) { .wrap(Wrap { trim: true }); f.render_widget(p, area); } + + if app.show_context { + if let Some(lines) = &app.context_lines { + let area = centered_rect(80, 60, f.size()); + f.render_widget(Clear, area); + + let context_block = Block::default() + .title(" Context View - Esc/Enter to Close ") + .borders(Borders::ALL) + .style(Style::default().bg(Color::Black)); + + let mut content = Vec::new(); + for (num, line) in lines { + let style = if let Some(idx) = app.state.selected() { + if let Some(finding) = app.findings.get(idx) { + if *num == finding.line_number { + Style::default().fg(Color::Red).add_modifier(Modifier::BOLD) + } else { + Style::default().fg(Color::Gray) + } + } else { + Style::default().fg(Color::Gray) + } + } else { + Style::default().fg(Color::Gray) + }; + + content.push(Line::from(vec![ + Span::styled(format!("{: >4} | ", num), style), + Span::styled(line.replace('\t', " "), style), + ])); + } + + let p = Paragraph::new(content) + .block(context_block) + .wrap(Wrap { trim: false }); // preserve indentation + + f.render_widget(p, area); + } + } } fn centered_rect(percent_x: u16, percent_y: u16, r: Rect) -> Rect {