Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 18 additions & 21 deletions src/core/ev_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ pub fn handle_event(
Command::SetData(text) => {
p.screen.orig_text = text;
p.screen.line_count = p.screen.orig_text.lines().count();
p.format_lines();
p.reformat_display();
command_queue.push_back(Command::Io(IoCommand::RedrawDisplay));
}
Command::UserInput(InputEvent::Exit) => {
Expand Down Expand Up @@ -149,13 +149,13 @@ pub fn handle_event(
Command::UserInput(InputEvent::UpdateTermArea(c, r)) => {
p.rows = r;
p.cols = c;
p.format_lines();
p.reformat_display();
// Readjust the text wrapping for the new number of columns
command_queue.push_back(Command::Io(IoCommand::RedrawDisplay));
}
Command::UserInput(InputEvent::UpdateLineNumber(l)) => {
p.line_numbers = l;
p.format_lines();
p.reformat_display();
command_queue.push_back(Command::Io(IoCommand::RedrawDisplay));
}
Command::UserInput(InputEvent::Number(n)) => {
Expand Down Expand Up @@ -278,7 +278,7 @@ pub fn handle_event(

Command::UserInput(InputEvent::HorizontalScroll(val)) => {
p.screen.line_wrapping = val;
p.format_lines();
p.reformat_display();
command_queue.push_back(Command::Io(IoCommand::RedrawDisplay));
}

Expand Down Expand Up @@ -316,7 +316,7 @@ pub fn handle_event(
}
Command::SetLineNumbers(ln) => {
p.line_numbers = ln;
p.format_lines();
p.reformat_display();
command_queue.push_back(Command::Io(IoCommand::RedrawDisplay));
}
Command::SetExitStrategy(es) => {
Expand All @@ -336,7 +336,7 @@ pub fn handle_event(
}
Command::LineWrapping(lw) => {
p.screen.line_wrapping = lw;
p.format_lines();
p.reformat_display();
}
#[cfg(feature = "static_output")]
Command::SetRunNoOverflow(val) => p.run_no_overflow = val,
Expand Down Expand Up @@ -420,17 +420,6 @@ pub fn handle_io_command(
drop(active);
cvar.notify_one();

command_queue.push_back(Command::Io(IoCommand::RedrawPrompt));
// If we have incremental search cache directly use it and return
if let Some(incremental_search_result) = search_result.incremental_search_result {
p.search_state.search_term = search_result.compiled_regex;
p.upper_mark = incremental_search_result.upper_mark;
p.search_state.search_mark = incremental_search_result.search_mark;
p.search_state.search_idx = incremental_search_result.search_idx;
p.screen.formatted_lines = incremental_search_result.formatted_lines;
return Ok(());
}

// If we only have compiled regex cached, use that otherwise compile the original
// string query if its not empty
p.search_state.search_term = if search_result.compiled_regex.is_some() {
Expand All @@ -445,11 +434,19 @@ pub fn handle_io_command(
}
compiled_regex
} else {
command_queue.push_back(Command::Io(IoCommand::RedrawDisplay));
return Ok(());
};

p.format_lines();
p.reformat_display();
p.upper_mark = *p
.search_state
.search_idx
.iter()
.nth(p.search_state.search_mark)
.unwrap();
command_queue.push_back(Command::Io(IoCommand::RedrawDisplay));
command_queue.push_back(Command::Io(IoCommand::RedrawPrompt));
}
}
Ok(())
Expand Down Expand Up @@ -579,7 +576,7 @@ mod tests {
let _ = writeln!(t, "line {idx}");
t
});
ps.format_lines();
ps.reformat_display();
ps.upper_mark = 3;
ps.selection_anchor = Some(Selection {
absolute_row: 3,
Expand Down Expand Up @@ -617,7 +614,7 @@ mod tests {
let _ = writeln!(t, "line {idx}");
t
});
ps.format_lines();
ps.reformat_display();
ps.upper_mark = 3;
ps.selection_anchor = Some(Selection {
absolute_row: 3,
Expand Down Expand Up @@ -655,7 +652,7 @@ mod tests {
let _ = writeln!(t, "line {idx}");
t
});
ps.format_lines();
ps.reformat_display();
ps.upper_mark = 2;
ps.selection_anchor = Some(Selection {
absolute_row: 2,
Expand Down
32 changes: 16 additions & 16 deletions src/core/utils/display/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crossterm::{
terminal::{Clear, ClearType},
};

use std::{cmp::Ordering, convert::TryInto, io::Write};
use std::{cmp::Ordering, convert::TryInto, fmt::Display, io::Write};

use super::term;
use crate::screen::Row;
Expand Down Expand Up @@ -219,9 +219,9 @@ pub fn draw_append_text(
/// the number of lines of text data. This rule is disobeyed in only one special case which is if number of lines of
/// text is less than available rows. In this situation, upper mark is always 0.
#[allow(clippy::too_many_arguments)]
pub fn write_text_checked(
pub fn write_text_checked<L: Display + AsRef<str>>(
out: &mut impl Write,
lines: &[String],
lines: &[L],
mut upper_mark: usize,
rows: usize,
cols: usize,
Expand All @@ -246,8 +246,7 @@ pub fn write_text_checked(
lower_mark = upper_mark.saturating_add(writable_rows.min(line_count));
}

// Add \r to ensure cursor is placed at the beginning of each row
let display_lines: &[String] = &lines[upper_mark..lower_mark];
let display_lines = &lines[upper_mark..lower_mark];

term::move_cursor(out, 0, 0, false)?;
term::clear_entire_screen(out, false)?;
Expand Down Expand Up @@ -280,9 +279,9 @@ pub fn write_from_pagerstate(out: &mut impl Write, ps: &mut PagerState) -> Resul
write_raw_lines(out, &display_lines, Some("\r"))
}

pub fn write_lines(
pub fn write_lines<L: Display + AsRef<str>>(
out: &mut impl Write,
lines: &[String],
lines: &[L],
cols: usize,
line_wrapping: bool,
left_mark: usize,
Expand All @@ -296,28 +295,29 @@ pub fn write_lines(
}
}

pub fn write_lines_in_horizontal_scroll(
pub fn write_lines_in_horizontal_scroll<L: Display + AsRef<str>>(
out: &mut impl Write,
lines: &[String],
lines: &[L],
cols: usize,
start: usize,
line_numbers: bool,
line_count: usize,
) -> crate::Result {
for line in lines {
let line_str = line.as_ref();
let (first_end, second_start, second_end) =
get_horizontal_scroll_bounds(line, cols, start, line_numbers, line_count);
get_horizontal_scroll_bounds(line_str, cols, start, line_numbers, line_count);

if start < line.len() {
if start < line.as_ref().len() {
if line_numbers {
writeln!(
out,
"\r{}{}",
&line[0..first_end],
&line[second_start..second_end]
&line_str[0..first_end],
&line_str[second_start..second_end]
)?;
} else {
writeln!(out, "\r{}", &line[second_start..second_end])?;
writeln!(out, "\r{}", &line_str[second_start..second_end])?;
}
} else {
writeln!(out, "\r")?;
Expand Down Expand Up @@ -393,9 +393,9 @@ pub fn get_horizontal_scroll_bounds(
/// `initial` tells any extra text to be inserted before each line. For functions that use this
/// function over terminals, this should be set to `\r` to avoid broken display.
/// The `\r` resets the cursor to the start of the line.
pub fn write_raw_lines(
pub fn write_raw_lines<L: Display>(
out: &mut impl Write,
lines: &[String],
lines: &[L],
initial: Option<&str>,
) -> Result<(), MinusError> {
for line in lines {
Expand Down
28 changes: 14 additions & 14 deletions src/core/utils/display/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ fn short_no_line_numbers() {
let mut pager = PagerState::new().unwrap();

pager.screen.orig_text = lines.to_string();
pager.format_lines();
pager.reformat_display();

let mut out = Vec::with_capacity(lines.len());

Expand Down Expand Up @@ -53,7 +53,7 @@ fn long_no_line_numbers() {
// One extra line for prompt
pager.rows = 4;
pager.screen.orig_text = lines.to_string();
pager.format_lines();
pager.reformat_display();

assert!(write_from_pagerstate(&mut out, &mut pager).is_ok());

Expand All @@ -67,7 +67,7 @@ fn long_no_line_numbers() {
let mut out = Vec::with_capacity(lines.len());
pager.screen.orig_text = "Another line\nThird line\nFourth line\nFifth line\n".to_string();
pager.upper_mark = 1;
pager.format_lines();
pager.reformat_display();

assert!(write_from_pagerstate(&mut out, &mut pager).is_ok());

Expand Down Expand Up @@ -99,7 +99,7 @@ fn short_with_line_numbers() {
let mut pager = PagerState::new().unwrap();
pager.screen.orig_text = lines.to_string();
pager.line_numbers = LineNumbers::Enabled;
pager.format_lines();
pager.reformat_display();

assert!(write_from_pagerstate(&mut out, &mut pager).is_ok());

Expand Down Expand Up @@ -134,7 +134,7 @@ fn long_with_line_numbers() {
pager.rows = 4;
pager.screen.orig_text = lines.to_string();
pager.line_numbers = LineNumbers::Enabled;
pager.format_lines();
pager.reformat_display();

assert!(write_from_pagerstate(&mut out, &mut pager).is_ok());

Expand Down Expand Up @@ -186,7 +186,7 @@ fn big_line_numbers_are_padded() {
pager.rows = 11;
pager.screen.orig_text = lines;
pager.line_numbers = LineNumbers::AlwaysOn;
pager.format_lines();
pager.reformat_display();

assert!(write_from_pagerstate(&mut out, &mut pager).is_ok());

Expand Down Expand Up @@ -229,7 +229,7 @@ fn draw_short_no_line_numbers() {
let mut pager = PagerState::new().unwrap();
pager.screen.orig_text = lines.to_string();
pager.line_numbers = LineNumbers::AlwaysOff;
pager.format_lines();
pager.reformat_display();

assert!(draw_full(&mut out, &mut pager).is_ok());

Expand Down Expand Up @@ -264,7 +264,7 @@ fn draw_long_no_line_numbers() {
let mut pager = PagerState::new().unwrap();
pager.rows = 3;
pager.screen.orig_text = lines.to_string();
pager.format_lines();
pager.reformat_display();

assert!(draw_full(&mut out, &mut pager).is_ok());

Expand Down Expand Up @@ -310,7 +310,7 @@ fn draw_short_with_line_numbers() {
let mut pager = PagerState::new().unwrap();
pager.screen.orig_text = lines.to_string();
pager.line_numbers = LineNumbers::Enabled;
pager.format_lines();
pager.reformat_display();

assert!(draw_full(&mut out, &mut pager).is_ok());
assert!(
Expand Down Expand Up @@ -345,7 +345,7 @@ fn draw_long_with_line_numbers() {
pager.rows = 3;
pager.screen.orig_text = lines.to_string();
pager.line_numbers = LineNumbers::Enabled;
pager.format_lines();
pager.reformat_display();

assert!(draw_full(&mut out, &mut pager).is_ok());

Expand Down Expand Up @@ -399,7 +399,7 @@ fn draw_big_line_numbers_are_padded() {
pager.upper_mark = 95;
pager.screen.orig_text = lines;
pager.line_numbers = LineNumbers::Enabled;
pager.format_lines();
pager.reformat_display();

assert!(draw_full(&mut out, &mut pager).is_ok());

Expand All @@ -426,7 +426,7 @@ fn draw_wrapping_line_numbers() {
pager.cols = 30;
pager.upper_mark = 2;
pager.line_numbers = LineNumbers::Enabled;
pager.format_lines();
pager.reformat_display();

assert!(draw_full(&mut out, &mut pager).is_ok());

Expand Down Expand Up @@ -457,7 +457,7 @@ fn test_draw_no_overflow() {
let mut out = Vec::with_capacity(TEXT.len());
let mut pager = PagerState::new().unwrap();
pager.screen.orig_text = TEXT.to_string();
pager.format_lines();
pager.reformat_display();
draw_full(&mut out, &mut pager).unwrap();
assert!(
String::from_utf8(out)
Expand Down Expand Up @@ -488,7 +488,7 @@ mod draw_for_change_tests {
let mut ps = PagerState::new().unwrap();
ps.upper_mark = 0;
ps.screen.orig_text = lines;
ps.format_lines();
ps.reformat_display();
ps.format_prompt();
ps
}
Expand Down
15 changes: 9 additions & 6 deletions src/core/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,15 @@ impl LinesRowMap {
self.0.get(ln)
}

#[allow(dead_code)]
pub fn row_to_line(&self, row: usize) -> Option<usize> {
match self.0.binary_search(&row) {
Ok(line) => Some(line),
Err(0) => None,
Err(next_line) => Some(next_line - 1),
pub(crate) fn row_to_line(&self, row: usize) -> Option<usize> {
if self.0.is_empty() {
return None;
}

Some(match self.0.binary_search(&row) {
Ok(idx) => idx,
Err(0) => 0,
Err(idx) => idx.saturating_sub(1),
})
}
}
Loading
Loading