mirror of
https://github.com/sxyazi/yazi.git
synced 2024-12-25 09:46:37 +03:00
fix: jq
previews empty when the user sets tab_size=8
(#320)
This commit is contained in:
parent
5c3c100d5d
commit
fc0057617e
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 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)))
|
||||
}
|
||||
}
|
||||
|
@ -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> {
|
||||
|
Loading…
Reference in New Issue
Block a user