safe_migrate/report/
interactive.rs1use crate::analysis::state::Confidence;
2use crate::report::violations::Violation;
3use anyhow::Result;
4use crossterm::{
5 cursor,
6 event::{self, Event, KeyCode, KeyEventKind},
7 execute, queue,
8 style::{Color, Print, ResetColor, SetForegroundColor},
9 terminal::{
10 Clear, ClearType, EnterAlternateScreen, LeaveAlternateScreen, disable_raw_mode,
11 enable_raw_mode,
12 },
13};
14use std::io::{Write, stdout};
15use terminal_size::{Height, Width, terminal_size};
16
17struct TerminalGuard;
18
19impl Drop for TerminalGuard {
20 fn drop(&mut self) {
21 let _ = disable_raw_mode();
22 let mut stdout = stdout();
23 let _ = execute!(
24 stdout,
25 cursor::Show,
26 Clear(ClearType::All),
27 LeaveAlternateScreen,
28 cursor::MoveTo(0, 0)
29 );
30 }
31}
32
33pub fn run_interactive(violations: &[Violation], confidence: &Confidence) -> Result<()> {
34 if violations.is_empty() {
35 println!("No violations found!");
36 return Ok(());
37 }
38
39 enable_raw_mode()?;
40 let _guard = TerminalGuard;
41
42 let mut stdout = stdout();
43 execute!(
44 stdout,
45 EnterAlternateScreen,
46 cursor::Hide,
47 Clear(ClearType::All),
48 cursor::MoveTo(0, 0)
49 )?;
50
51 let mut selected: usize = 0;
52
53 loop {
54 queue!(stdout, Clear(ClearType::All), cursor::MoveTo(0, 0))?;
55 queue!(
56 stdout,
57 SetForegroundColor(Color::Cyan),
58 Print(format!(
59 "Safe-Migrate Interactive Viewer ({} violations) [Confidence: {:?}]\r\n\r\n",
60 violations.len(),
61 confidence
62 )),
63 ResetColor,
64 )?;
65
66 let (_w, Height(h)) = terminal_size().unwrap_or((Width(80), Height(24)));
67 let window_size = (h.saturating_sub(18) as usize).max(3);
69
70 let start = selected.saturating_sub(window_size / 2);
71 let end = std::cmp::min(start + window_size, violations.len());
72
73 if start > 0 {
74 queue!(stdout, Print(" ...\r\n"))?;
75 }
76
77 for (i, v) in violations.iter().enumerate().take(end).skip(start) {
78 let prefix = if i == selected { " > " } else { " " };
79 let color = match v.tier {
80 crate::report::violations::ViolationTier::Tier1 => Color::Red,
81 crate::report::violations::ViolationTier::Tier2 => Color::Yellow,
82 crate::report::violations::ViolationTier::Tier3 => Color::Green,
83 };
84
85 queue!(
86 stdout,
87 Print(prefix),
88 SetForegroundColor(color),
89 Print(format!("[{:?}] ", v.tier)),
90 ResetColor,
91 Print(format!("{} (rule: {})\r\n", v.operation_kind, v.rule_id))
92 )?;
93 }
94
95 if end < violations.len() {
96 queue!(stdout, Print(" ...\r\n"))?;
97 }
98
99 queue!(
100 stdout,
101 Print("\r\n------------------------------------------------------------\r\n")
102 )?;
103
104 let active = &violations[selected];
105 queue!(
106 stdout,
107 SetForegroundColor(Color::White),
108 Print("Reason: "),
109 ResetColor,
110 Print(format!("{}\r\n", active.reason)),
111 SetForegroundColor(Color::White),
112 Print("Recipe: "),
113 ResetColor,
114 Print(format!("{}\r\n", active.recipe)),
115 )?;
116
117 if let Some(sql) = &active.sql {
118 let mut sql_lines: Vec<&str> = sql.lines().collect();
120 let mut truncated = false;
121 if sql_lines.len() > 5 {
122 sql_lines.truncate(5);
123 truncated = true;
124 }
125
126 queue!(
127 stdout,
128 SetForegroundColor(Color::White),
129 Print("\r\nSQL Context:\r\n"),
130 SetForegroundColor(Color::DarkGrey),
131 Print(format!("{}\r\n", sql_lines.join("\r\n"))),
133 )?;
134
135 if truncated {
136 queue!(stdout, Print("... (truncated)\r\n"))?;
137 }
138 queue!(stdout, ResetColor)?;
139 }
140
141 queue!(stdout, Print("\r\n[Up/Down] Navigate | [q/Esc] Quit\r\n"))?;
142
143 stdout.flush()?;
144
145 if let Event::Key(key) = event::read()?
146 && key.kind == KeyEventKind::Press
147 {
148 match key.code {
149 KeyCode::Char('q') | KeyCode::Esc => break,
150 KeyCode::Up if selected > 0 => selected -= 1,
151 KeyCode::Down if selected < violations.len() - 1 => selected += 1,
152 _ => {}
153 }
154 }
155 }
156
157 drop(_guard);
158 println!("Exited interactive mode.");
159
160 Ok(())
161}