diff --git a/src/core/ev_handler.rs b/src/core/ev_handler.rs index 66b510c..a16993b 100644 --- a/src/core/ev_handler.rs +++ b/src/core/ev_handler.rs @@ -33,7 +33,11 @@ pub fn handle_event( match ev { Command::SetData(text) => { p.screen.orig_text = text; - command_queue.push_back(Command::FormatRedrawDisplay); + p.format_lines(); + p.screen.line_count = p.screen.orig_text.lines().count(); + if !p.running.lock().is_uninitialized() { + display::draw_full(&mut out, p)?; + } } Command::UserInput(InputEvent::Exit) => { p.exit(); @@ -253,7 +257,10 @@ pub fn handle_event( } else { p.message = Some(text.to_string()); } - command_queue.push_back(Command::FormatRedrawPrompt); + p.format_prompt(); + if !p.running.lock().is_uninitialized() { + display::write_prompt(out, &p.displayed_prompt, p.rows.try_into().unwrap())?; + } } Command::SetLineNumbers(ln) => { p.line_numbers = ln; diff --git a/src/screen.rs b/src/screen.rs index fd3aaf9..5cdbae3 100644 --- a/src/screen.rs +++ b/src/screen.rs @@ -4,6 +4,7 @@ pub struct Screen { pub(crate) orig_text: String, pub(crate) formatted_lines: Vec, + pub(crate) line_count: usize, } impl Screen { @@ -16,8 +17,8 @@ impl Screen { /// /// NOTE: This operation might be expensive if the text data is too large. #[must_use] - pub fn line_count(&self) -> usize { - self.orig_text.lines().count() + pub fn get_line_count(&self) -> usize { + self.line_count } /// Returns all the text within the bounds pub(crate) fn get_formatted_lines_with_bounds(&self, start: usize, end: usize) -> &[String] { @@ -36,6 +37,7 @@ impl Default for Screen { Self { orig_text: String::with_capacity(100 * 1024), formatted_lines: Vec::with_capacity(500 * 1024), + line_count: 0, } } } diff --git a/src/state.rs b/src/state.rs index 13dd3a8..0727d78 100644 --- a/src/state.rs +++ b/src/state.rs @@ -259,6 +259,7 @@ impl PagerState { #[cfg(feature = "search")] &self.search_state.search_term, ); + #[cfg(feature = "search")] { self.search_state.search_idx = format_result.append_search_idx; @@ -384,12 +385,19 @@ impl PagerState { // * Push text // * Add number of lines we added to the original line count // * Count the digits again - let old_lc = self.screen.orig_text.lines().count(); + let old_lc = self.screen.get_line_count(); let old_lc_dgts = minus_core::digits(old_lc); self.screen.orig_text.push_str(text); - let new_lc = old_lc + text.lines().count(); + let new_lc = old_lc + + text + .lines() + .count() + // NOTE: Reduce one if we are attaching to the last line, otherwise the attaching + // line will be counted twice. + .saturating_sub(if append { 0 } else { 1 }); + self.screen.line_count = new_lc; let new_lc_dgts = minus_core::digits(new_lc); let append_opts = text::FormatOpts {