fix: jq previews empty when the user sets tab_size=8

This commit is contained in:
sxyazi 2023-10-25 17:39:01 +08:00
parent 24fd587431
commit 1bd984d641
No known key found for this signature in database
2 changed files with 22 additions and 13 deletions

View file

@ -1,16 +1,16 @@
use std::{path::Path, process::Stdio};
use anyhow::Result;
use tokio::{io::{AsyncBufReadExt, BufReader}, process::Command};
use tokio::{io::{AsyncBufReadExt, AsyncReadExt, BufReader}, process::Command, select};
use yazi_config::PREVIEW;
use yazi_shared::PeekError;
pub async fn jq(path: &Path, skip: usize, limit: usize) -> Result<String, PeekError> {
let mut child = Command::new("jq")
.args(["-C", "--indent", &PREVIEW.tab_size.to_string(), "."])
.args(["-C", "--indent", "-1", "."])
.arg(path)
.stdout(Stdio::piped())
.stderr(Stdio::null())
.stderr(Stdio::piped())
.kill_on_drop(true)
.spawn()?;
@ -21,18 +21,26 @@ pub async fn jq(path: &Path, skip: usize, limit: usize) -> Result<String, PeekEr
i += 1;
if i > skip + limit {
break;
} else if i <= skip {
continue;
}
if i > skip {
lines.push_str(&line);
lines.push('\n');
}
}
child.start_kill().ok();
if lines.is_empty() {
let mut stderr = child.stderr.take().unwrap();
select! {
Ok(_) = stderr.read_u8() => {
return Err("parse error".into());
}
}
}
if skip > 0 && i < skip + limit {
Err(PeekError::Exceed(i.saturating_sub(limit)))
} else {
Ok(lines)
Ok(lines.replace('\t', &" ".repeat(PREVIEW.tab_size as usize)))
}
}

View file

@ -1,7 +1,6 @@
use std::{io::BufRead, path::Path, sync::atomic::{AtomicUsize, Ordering}};
use anyhow::anyhow;
use futures::TryFutureExt;
use syntect::{easy::HighlightFile, util::as_24_bit_terminal_escaped};
use tokio::fs;
use yazi_adaptor::ADAPTOR;
@ -70,9 +69,11 @@ impl Provider {
}
pub(super) async fn json(path: &Path, skip: usize) -> Result<String, PeekError> {
external::jq(path, skip, MANAGER.layout.preview_height())
.or_else(|_| Provider::highlight(path, skip))
.await
let result = external::jq(path, skip, MANAGER.layout.preview_height()).await;
if let Err(PeekError::Unexpected(_)) = result {
return Self::highlight(path, skip).await;
}
result
}
pub(super) async fn archive(path: &Path, skip: usize) -> Result<String, PeekError> {