feat: support previewing files containing non-UTF-8 characters (#958)

This commit is contained in:
三咲雅 · Misaki Masa 2024-04-26 20:42:39 +08:00 committed by GitHub
parent bf91f35d3e
commit 42a0fcd5cf
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -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();
@ -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 i > skip {
line.push('\n');
after.push(line);
} else if !plain {
line.push('\n');
before.push(line);
if buf.ends_with(b"\r\n") {
buf.pop();
buf.pop();
buf.push(b'\n');
}
if i > skip {
after.push(String::from_utf8_lossy(&buf).into_owned());
} else if !plain {
before.push(String::from_utf8_lossy(&buf).into_owned());
}
buf.clear();
}
if skip > 0 && i < skip + limit {