Skip to content

Commit

Permalink
feat: add limit-length option
Browse files Browse the repository at this point in the history
  • Loading branch information
edy555 committed Apr 30, 2024
1 parent dc0cdfb commit 14f6f6c
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 7 deletions.
24 changes: 17 additions & 7 deletions src/jnv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,18 @@ mod keymap;
/// # Returns
/// An `anyhow::Result` wrapping a vector of `serde_json::Value`. On success, it contains the parsed
/// JSON data. On failure, it contains an error detailing what went wrong during parsing.
fn deserialize_json(json_str: &str) -> anyhow::Result<Vec<serde_json::Value>> {
Deserializer::from_str(json_str)
.into_iter::<serde_json::Value>()
.map(|res| res.map_err(anyhow::Error::from))
.collect::<anyhow::Result<Vec<serde_json::Value>>>()
fn deserialize_json(json_str: &str, limit_length: Option<usize>) -> anyhow::Result<Vec<serde_json::Value>> {
match limit_length {
Some(l) => Deserializer::from_str(json_str)
.into_iter::<serde_json::Value>()
.take(l)
.map(|res| res.map_err(anyhow::Error::from))
.collect::<anyhow::Result<Vec<serde_json::Value>>>(),
None => Deserializer::from_str(json_str)
.into_iter::<serde_json::Value>()
.map(|res| res.map_err(anyhow::Error::from))
.collect::<anyhow::Result<Vec<serde_json::Value>>>(),
}
}

fn run_jq(query: &str, json_stream: &[serde_json::Value]) -> anyhow::Result<Vec<String>> {
Expand Down Expand Up @@ -96,6 +103,7 @@ pub struct Jnv {
suggest: Suggest,

json_expand_depth: Option<usize>,
json_limit_length: Option<usize>,
no_hint: bool,
}

Expand All @@ -107,9 +115,10 @@ impl Jnv {
suggestions: listbox::State,
json_theme: json::Theme,
json_expand_depth: Option<usize>,
json_limit_length: Option<usize>,
no_hint: bool,
) -> Result<Prompt<Self>> {
let input_stream = deserialize_json(&input)?;
let input_stream = deserialize_json(&input, json_limit_length)?;

let mut trie = FilterTrie::default();
trie.insert(".", input_stream.clone());
Expand Down Expand Up @@ -160,6 +169,7 @@ impl Jnv {
trie,
suggest,
json_expand_depth,
json_limit_length,
no_hint,
input_stream,
},
Expand Down Expand Up @@ -252,7 +262,7 @@ impl promkit::Renderer for Jnv {
JsonStream::new(searched.clone(), self.json_expand_depth);
}
} else {
match deserialize_json(&ret.join("\n")) {
match deserialize_json(&ret.join("\n"), self.json_limit_length) {
Ok(jsonl) => {
let stream =
JsonStream::new(jsonl.clone(), self.json_expand_depth);
Expand Down
13 changes: 13 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,18 @@ pub struct Args {
)]
pub json_expand_depth: Option<usize>,

#[arg(
short = 's',
long = "limit-length",
default_value = "50",
help = "Limit length of JSON array in the visualization.",
long_help = "
Specifies the limit length to load JSON array in the visualization.
Note: Increasing this length can significantly slow down the display for large datasets.
"
)]
pub json_limit_length: Option<usize>,

#[arg(
short = 'l',
long = "suggestion-list-length",
Expand Down Expand Up @@ -208,6 +220,7 @@ fn main() -> Result<()> {
suggestions,
json_theme,
args.json_expand_depth,
args.json_limit_length,
args.no_hint,
)?;
let _ = prompt.run()?;
Expand Down

0 comments on commit 14f6f6c

Please sign in to comment.