Skip to content

Commit

Permalink
feat: support previewing files containing non-UTF-8 characters (#958)
Browse files Browse the repository at this point in the history
  • Loading branch information
sxyazi authored Apr 26, 2024
1 parent bf91f35 commit 42a0fcd
Showing 1 changed file with 14 additions and 8 deletions.
22 changes: 14 additions & 8 deletions yazi-plugin/src/external/highlighter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ impl Highlighter {
}

pub async fn highlight(&self, skip: usize, limit: usize) -> Result<Text<'static>, PeekError> {
let mut reader = BufReader::new(File::open(&self.path).await?).lines();
let mut reader = BufReader::new(File::open(&self.path).await?);

let syntax = Self::find_syntax(&self.path).await;
let mut plain = syntax.is_err();
Expand All @@ -66,24 +66,30 @@ impl Highlighter {
let mut after = Vec::with_capacity(limit);

let mut i = 0;
while let Some(mut line) = reader.next_line().await? {
let mut buf = vec![];
while reader.read_until(b'\n', &mut buf).await.is_ok() {
i += 1;
if i > skip + limit {
if buf.is_empty() || i > skip + limit {
break;
}

if !plain && line.len() > 6000 {
if !plain && buf.len() > 6000 {
plain = true;
drop(mem::take(&mut before));
}

if buf.ends_with(b"\r\n") {
buf.pop();
buf.pop();
buf.push(b'\n');
}

if i > skip {
line.push('\n');
after.push(line);
after.push(String::from_utf8_lossy(&buf).into_owned());
} else if !plain {
line.push('\n');
before.push(line);
before.push(String::from_utf8_lossy(&buf).into_owned());
}
buf.clear();
}

if skip > 0 && i < skip + limit {
Expand Down

0 comments on commit 42a0fcd

Please sign in to comment.