fix: jq previews empty when the user sets tab_size=8 (#320)

This commit is contained in:
三咲雅 · Misaki Masa 2023-10-26 01:33:07 +08:00 committed by GitHub
parent 5c3c100d5d
commit fc0057617e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 21 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", "--tab", "."])
.arg(path)
.stdout(Stdio::piped())
.stderr(Stdio::null())
.stderr(Stdio::piped())
.kill_on_drop(true)
.spawn()?;
@ -21,18 +21,25 @@ 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;
}
lines.push_str(&line);
lines.push('\n');
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()),
else => {}
}
}
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> {