Skip to content

fix: add char boundary check when write lines in horizontal scroll #144

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
32 changes: 29 additions & 3 deletions src/core/utils/display/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -346,14 +346,40 @@ pub fn write_lines_in_horizontal_scroll(

if start < line.len() {
if line_numbers {
// make sure "line_number_padding + line_number_ascii_seq_len" not overflow
let first_end = (line_number_padding + line_number_ascii_seq_len).min(line.len());
// make sure "shifted_start" not downflow
let second_start = shifted_start.min(line.len() - 1).max(0);
// make sure end not overflow
let second_end = end.min(line.len());

// check char boundary, for example "▲" takes up 3 bytes,
// we must promise that second_start points to the first
// byte or the fourth byte;
let mut i = second_start;
while i < second_end && line.is_char_boundary(i) == false {
i += 1;
}
let second_start = i;
writeln!(
out,
"\r{}{}",
&line[0..line_number_padding + line_number_ascii_seq_len],
&line[shifted_start..end]
&line[0..first_end],
&line[second_start..second_end]
)?;
} else {
writeln!(out, "\r{}", &line[shifted_start..end])?;
// make sure "shifted_start" not overflow
let resolved_start = shifted_start.min(line.len() - 1);
// make sure end not overflow
let resolved_end = end.min(line.len());

// check char boundary like above
let mut i = resolved_start;
while i < resolved_end && line.is_char_boundary(i) == false {
i += 1;
}
let resolved_start = i;
writeln!(out, "\r{}", &line[resolved_start..resolved_end])?;
}
} else {
writeln!(out, "\r")?;
Expand Down