From c5c9b2e598bde19bda82c8f489fa0d6b2934274d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=89=E5=92=B2=E9=9B=85=20=C2=B7=20Misaki=20Masa?= Date: Sat, 5 Aug 2023 17:29:40 +0800 Subject: [PATCH] feat: improve the performance of highlighting big JSON file (#23) --- core/src/external/jq.rs | 31 ++++++++++++++++++++++--------- core/src/manager/preview.rs | 2 +- 2 files changed, 23 insertions(+), 10 deletions(-) diff --git a/core/src/external/jq.rs b/core/src/external/jq.rs index 1cdfc3d4..39af6398 100644 --- a/core/src/external/jq.rs +++ b/core/src/external/jq.rs @@ -1,19 +1,32 @@ -use std::path::Path; +use std::{path::Path, process::Stdio}; use anyhow::{bail, Result}; use config::PREVIEW; -use tokio::process::Command; +use tokio::{io::{AsyncBufReadExt, BufReader}, process::Command}; -pub async fn jq(path: &Path) -> Result { - let output = Command::new("jq") +pub async fn jq(path: &Path, mut lines: usize) -> Result { + let mut child = Command::new("jq") .args(["-C", "--indent", &PREVIEW.tab_size.to_string(), "."]) .arg(path) + .stdout(Stdio::piped()) + .stderr(Stdio::null()) .kill_on_drop(true) - .output() - .await?; + .spawn()?; - if !output.status.success() { - bail!("failed to get json: {}", String::from_utf8_lossy(&output.stderr)); + let mut it = BufReader::new(child.stdout.take().unwrap()).lines(); + let mut output = String::new(); + while let Ok(Some(line)) = it.next_line().await { + if lines < 1 { + break; + } + + output.push_str(&line); + output.push('\n'); + lines -= 1; } - Ok(String::from_utf8_lossy(&output.stdout).trim().to_string()) + + if output.is_empty() { + bail!("failed to get head of jq"); + } + Ok(output) } diff --git a/core/src/manager/preview.rs b/core/src/manager/preview.rs index f23919e9..1606470f 100644 --- a/core/src/manager/preview.rs +++ b/core/src/manager/preview.rs @@ -134,7 +134,7 @@ impl Preview { pub async fn json(path: &Path) -> Result { Ok( - external::jq(path) + external::jq(path, Self::rect().height as usize) .await? .lines() .take(Self::rect().height as usize)