mirror of
https://github.com/sxyazi/yazi.git
synced 2026-07-25 08:41:05 +00:00
fix: jq previews empty when the user sets tab_size=8 (#320)
This commit is contained in:
parent
5c3c100d5d
commit
fc0057617e
2 changed files with 21 additions and 13 deletions
25
yazi-core/src/external/jq.rs
vendored
25
yazi-core/src/external/jq.rs
vendored
|
|
@ -1,16 +1,16 @@
|
||||||
use std::{path::Path, process::Stdio};
|
use std::{path::Path, process::Stdio};
|
||||||
|
|
||||||
use anyhow::Result;
|
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_config::PREVIEW;
|
||||||
use yazi_shared::PeekError;
|
use yazi_shared::PeekError;
|
||||||
|
|
||||||
pub async fn jq(path: &Path, skip: usize, limit: usize) -> Result<String, PeekError> {
|
pub async fn jq(path: &Path, skip: usize, limit: usize) -> Result<String, PeekError> {
|
||||||
let mut child = Command::new("jq")
|
let mut child = Command::new("jq")
|
||||||
.args(["-C", "--indent", &PREVIEW.tab_size.to_string(), "."])
|
.args(["-C", "--tab", "."])
|
||||||
.arg(path)
|
.arg(path)
|
||||||
.stdout(Stdio::piped())
|
.stdout(Stdio::piped())
|
||||||
.stderr(Stdio::null())
|
.stderr(Stdio::piped())
|
||||||
.kill_on_drop(true)
|
.kill_on_drop(true)
|
||||||
.spawn()?;
|
.spawn()?;
|
||||||
|
|
||||||
|
|
@ -21,18 +21,25 @@ pub async fn jq(path: &Path, skip: usize, limit: usize) -> Result<String, PeekEr
|
||||||
i += 1;
|
i += 1;
|
||||||
if i > skip + limit {
|
if i > skip + limit {
|
||||||
break;
|
break;
|
||||||
} else if i <= skip {
|
|
||||||
continue;
|
|
||||||
}
|
}
|
||||||
|
if i > skip {
|
||||||
lines.push_str(&line);
|
lines.push_str(&line);
|
||||||
lines.push('\n');
|
lines.push('\n');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
child.start_kill().ok();
|
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 {
|
if skip > 0 && i < skip + limit {
|
||||||
Err(PeekError::Exceed(i.saturating_sub(limit)))
|
Err(PeekError::Exceed(i.saturating_sub(limit)))
|
||||||
} else {
|
} else {
|
||||||
Ok(lines)
|
Ok(lines.replace('\t', &" ".repeat(PREVIEW.tab_size as usize)))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,6 @@
|
||||||
use std::{io::BufRead, path::Path, sync::atomic::{AtomicUsize, Ordering}};
|
use std::{io::BufRead, path::Path, sync::atomic::{AtomicUsize, Ordering}};
|
||||||
|
|
||||||
use anyhow::anyhow;
|
use anyhow::anyhow;
|
||||||
use futures::TryFutureExt;
|
|
||||||
use syntect::{easy::HighlightFile, util::as_24_bit_terminal_escaped};
|
use syntect::{easy::HighlightFile, util::as_24_bit_terminal_escaped};
|
||||||
use tokio::fs;
|
use tokio::fs;
|
||||||
use yazi_adaptor::ADAPTOR;
|
use yazi_adaptor::ADAPTOR;
|
||||||
|
|
@ -70,9 +69,11 @@ impl Provider {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(super) async fn json(path: &Path, skip: usize) -> Result<String, PeekError> {
|
pub(super) async fn json(path: &Path, skip: usize) -> Result<String, PeekError> {
|
||||||
external::jq(path, skip, MANAGER.layout.preview_height())
|
let result = external::jq(path, skip, MANAGER.layout.preview_height()).await;
|
||||||
.or_else(|_| Provider::highlight(path, skip))
|
if let Err(PeekError::Unexpected(_)) = result {
|
||||||
.await
|
return Self::highlight(path, skip).await;
|
||||||
|
}
|
||||||
|
result
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(super) async fn archive(path: &Path, skip: usize) -> Result<String, PeekError> {
|
pub(super) async fn archive(path: &Path, skip: usize) -> Result<String, PeekError> {
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue