mirror of
https://github.com/sxyazi/yazi.git
synced 2024-12-18 06:11:31 +03:00
feat: support previewing files containing non-UTF-8 characters (#958)
This commit is contained in:
parent
bf91f35d3e
commit
42a0fcd5cf
26
yazi-plugin/src/external/highlighter.rs
vendored
26
yazi-plugin/src/external/highlighter.rs
vendored
@ -57,7 +57,7 @@ impl Highlighter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub async fn highlight(&self, skip: usize, limit: usize) -> Result<Text<'static>, PeekError> {
|
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 syntax = Self::find_syntax(&self.path).await;
|
||||||
let mut plain = syntax.is_err();
|
let mut plain = syntax.is_err();
|
||||||
@ -66,24 +66,30 @@ impl Highlighter {
|
|||||||
let mut after = Vec::with_capacity(limit);
|
let mut after = Vec::with_capacity(limit);
|
||||||
|
|
||||||
let mut i = 0;
|
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;
|
i += 1;
|
||||||
if i > skip + limit {
|
if buf.is_empty() || i > skip + limit {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
if !plain && line.len() > 6000 {
|
if !plain && buf.len() > 6000 {
|
||||||
plain = true;
|
plain = true;
|
||||||
drop(mem::take(&mut before));
|
drop(mem::take(&mut before));
|
||||||
}
|
}
|
||||||
|
|
||||||
if i > skip {
|
if buf.ends_with(b"\r\n") {
|
||||||
line.push('\n');
|
buf.pop();
|
||||||
after.push(line);
|
buf.pop();
|
||||||
} else if !plain {
|
buf.push(b'\n');
|
||||||
line.push('\n');
|
|
||||||
before.push(line);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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 {
|
if skip > 0 && i < skip + limit {
|
||||||
|
Loading…
Reference in New Issue
Block a user