Skip to content

Commit

Permalink
perf(screen): Store line count as an attr in Screen
Browse files Browse the repository at this point in the history
  • Loading branch information
AMythicDev committed Feb 6, 2024
1 parent ed5cd83 commit 4a015a8
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 6 deletions.
11 changes: 9 additions & 2 deletions src/core/ev_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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;
Expand Down
6 changes: 4 additions & 2 deletions src/screen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
pub struct Screen {
pub(crate) orig_text: String,
pub(crate) formatted_lines: Vec<String>,
pub(crate) line_count: usize,
}

impl Screen {
Expand All @@ -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] {
Expand All @@ -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,
}
}
}
12 changes: 10 additions & 2 deletions src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 4a015a8

Please sign in to comment.