|
| 1 | +use std::io; |
| 2 | +use std::time::{Duration, Instant}; |
| 3 | + |
| 4 | +use ratatui::{DefaultTerminal, text::Text}; |
| 5 | + |
| 6 | +use crate::markdown::{ |
| 7 | + commands::Command, |
| 8 | + events::{self, Action, CommandAction}, |
| 9 | + navigation::Navigation, |
| 10 | + search::Search, |
| 11 | + ui::{self, Mode}, |
| 12 | +}; |
| 13 | + |
| 14 | +/// Main application state and logic |
| 15 | +pub struct App { |
| 16 | + text: Text<'static>, |
| 17 | + original_text: Text<'static>, |
| 18 | + navigation: Navigation, |
| 19 | + search: Search, |
| 20 | + mode: Mode, |
| 21 | + command_input: String, |
| 22 | + status_message: Option<String>, |
| 23 | + highlight_time: Option<Instant>, |
| 24 | + exit: bool, |
| 25 | +} |
| 26 | + |
| 27 | +impl App { |
| 28 | + pub fn new(text: Text<'static>) -> Self { |
| 29 | + Self { |
| 30 | + original_text: text.clone(), |
| 31 | + text, |
| 32 | + navigation: Navigation::new(), |
| 33 | + search: Search::new(), |
| 34 | + mode: Mode::Normal, |
| 35 | + command_input: String::new(), |
| 36 | + status_message: None, |
| 37 | + highlight_time: None, |
| 38 | + exit: false, |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + pub fn run(&mut self, terminal: &mut DefaultTerminal) -> io::Result<()> { |
| 43 | + while !self.exit { |
| 44 | + terminal.draw(|frame| { |
| 45 | + ui::draw( |
| 46 | + frame, |
| 47 | + &self.text, |
| 48 | + self.navigation.scroll, |
| 49 | + &self.mode, |
| 50 | + &self.command_input, |
| 51 | + &self.status_message, |
| 52 | + ) |
| 53 | + })?; |
| 54 | + |
| 55 | + // Check if highlight time has expired |
| 56 | + if let Some(highlight_time) = self.highlight_time |
| 57 | + && highlight_time.elapsed() >= Duration::from_secs(2) |
| 58 | + { |
| 59 | + self.clear_highlights(); |
| 60 | + self.highlight_time = None; |
| 61 | + } |
| 62 | + |
| 63 | + self.handle_events()?; |
| 64 | + } |
| 65 | + Ok(()) |
| 66 | + } |
| 67 | + |
| 68 | + fn handle_events(&mut self) -> io::Result<()> { |
| 69 | + // Use a timeout to check for highlight expiration |
| 70 | + if let Some(key_event) = events::poll_events(Duration::from_millis(100))? { |
| 71 | + match self.mode { |
| 72 | + Mode::Normal => self.handle_normal_mode(key_event), |
| 73 | + Mode::Command => self.handle_command_mode(key_event), |
| 74 | + } |
| 75 | + } |
| 76 | + Ok(()) |
| 77 | + } |
| 78 | + |
| 79 | + fn handle_normal_mode(&mut self, key_event: crossterm::event::KeyEvent) { |
| 80 | + match events::handle_normal_mode(key_event) { |
| 81 | + Action::Quit => self.exit = true, |
| 82 | + Action::EnterCommandMode => { |
| 83 | + self.mode = Mode::Command; |
| 84 | + self.command_input.clear(); |
| 85 | + self.status_message = None; |
| 86 | + } |
| 87 | + Action::ScrollDown(amount) => self.navigation.scroll_down(amount, &self.text), |
| 88 | + Action::ScrollUp(amount) => self.navigation.scroll_up(amount), |
| 89 | + Action::NextMatch => self.next_search_result(), |
| 90 | + Action::PrevMatch => self.prev_search_result(), |
| 91 | + Action::None => {} |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + fn handle_command_mode(&mut self, key_event: crossterm::event::KeyEvent) { |
| 96 | + match events::handle_command_mode(key_event) { |
| 97 | + CommandAction::Exit => { |
| 98 | + self.mode = Mode::Normal; |
| 99 | + self.command_input.clear(); |
| 100 | + } |
| 101 | + CommandAction::Execute => { |
| 102 | + self.execute_command(); |
| 103 | + self.mode = Mode::Normal; |
| 104 | + } |
| 105 | + CommandAction::AppendChar(c) => { |
| 106 | + self.command_input.push(c); |
| 107 | + } |
| 108 | + CommandAction::Backspace => { |
| 109 | + self.command_input.pop(); |
| 110 | + } |
| 111 | + CommandAction::None => {} |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + fn execute_command(&mut self) { |
| 116 | + let command = Command::parse(&self.command_input); |
| 117 | + |
| 118 | + match command { |
| 119 | + Command::Quit => { |
| 120 | + self.exit = true; |
| 121 | + } |
| 122 | + Command::Search(query) => { |
| 123 | + self.perform_search(&query); |
| 124 | + } |
| 125 | + Command::Jump(line_num) => match self.navigation.jump_to_line(line_num, &self.text) { |
| 126 | + Ok(()) => { |
| 127 | + self.status_message = Some(format!("Jumped to line {}", line_num)); |
| 128 | + } |
| 129 | + Err(msg) => { |
| 130 | + self.status_message = Some(msg); |
| 131 | + } |
| 132 | + }, |
| 133 | + Command::Help => { |
| 134 | + self.status_message = Some(Command::help_text()); |
| 135 | + } |
| 136 | + Command::Unknown(msg) => { |
| 137 | + self.status_message = Some(msg); |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + self.command_input.clear(); |
| 142 | + } |
| 143 | + |
| 144 | + fn perform_search(&mut self, query: &str) { |
| 145 | + if query.is_empty() { |
| 146 | + self.status_message = Some("Empty search query".to_string()); |
| 147 | + return; |
| 148 | + } |
| 149 | + |
| 150 | + let count = self.search.perform_search(query, &self.original_text); |
| 151 | + |
| 152 | + if count > 0 { |
| 153 | + // Apply highlighting and start timer |
| 154 | + self.text = self.search.highlight_matches(&self.original_text); |
| 155 | + self.highlight_time = Some(Instant::now()); |
| 156 | + |
| 157 | + // Jump to first result |
| 158 | + if let Some(line) = self.search.current_match() { |
| 159 | + self.navigation.scroll_to_line(line as u16); |
| 160 | + } |
| 161 | + |
| 162 | + self.status_message = Some(format!("Found {} matches for '{}'", count, query)); |
| 163 | + } else { |
| 164 | + self.status_message = Some(format!("No matches found for '{}'", query)); |
| 165 | + } |
| 166 | + } |
| 167 | + |
| 168 | + fn next_search_result(&mut self) { |
| 169 | + if !self.search.has_results() { |
| 170 | + self.status_message = Some("No search results. Use :s <query> to search".to_string()); |
| 171 | + return; |
| 172 | + } |
| 173 | + |
| 174 | + if let Some(line) = self.search.next_match() { |
| 175 | + // Re-highlight to update the current match indicator and reset timer |
| 176 | + self.text = self.search.highlight_matches(&self.original_text); |
| 177 | + self.highlight_time = Some(Instant::now()); |
| 178 | + |
| 179 | + self.navigation.scroll_to_line(line as u16); |
| 180 | + |
| 181 | + self.status_message = Some(format!( |
| 182 | + "Match {}/{}", |
| 183 | + self.search.current_index + 1, |
| 184 | + self.search.results.len() |
| 185 | + )); |
| 186 | + } |
| 187 | + } |
| 188 | + |
| 189 | + fn prev_search_result(&mut self) { |
| 190 | + if !self.search.has_results() { |
| 191 | + self.status_message = Some("No search results. Use :s <query> to search".to_string()); |
| 192 | + return; |
| 193 | + } |
| 194 | + |
| 195 | + if let Some(line) = self.search.prev_match() { |
| 196 | + // Re-highlight to update the current match indicator and reset timer |
| 197 | + self.text = self.search.highlight_matches(&self.original_text); |
| 198 | + self.highlight_time = Some(Instant::now()); |
| 199 | + |
| 200 | + self.navigation.scroll_to_line(line as u16); |
| 201 | + |
| 202 | + self.status_message = Some(format!( |
| 203 | + "Match {}/{}", |
| 204 | + self.search.current_index + 1, |
| 205 | + self.search.results.len() |
| 206 | + )); |
| 207 | + } |
| 208 | + } |
| 209 | + |
| 210 | + fn clear_highlights(&mut self) { |
| 211 | + // Reset to original text without highlights |
| 212 | + self.text = self.original_text.clone(); |
| 213 | + self.search.clear(); |
| 214 | + } |
| 215 | +} |
0 commit comments