Skip to content

Commit 80ffd5b

Browse files
authored
Merge pull request #46 from ynqa/v0.6.1/dev
v0.6.1
2 parents 98f17d0 + 6a36881 commit 80ffd5b

File tree

6 files changed

+60
-37
lines changed

6 files changed

+60
-37
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ Put the package in your `Cargo.toml`.
1111

1212
```toml
1313
[dependencies]
14-
promkit = "0.6.0"
14+
promkit = "0.6.1"
1515
```
1616

1717
## Features

promkit/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "promkit"
3-
version = "0.6.0"
3+
version = "0.6.1"
44
authors = ["ynqa <[email protected]>"]
55
edition = "2021"
66
description = "A toolkit for building your own interactive command-line tools"

promkit/src/core/listbox.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,14 @@ impl Listbox {
3333
))
3434
}
3535

36+
pub fn len(&self) -> usize {
37+
self.0.contents().len()
38+
}
39+
40+
pub fn push_string(&mut self, item: String) {
41+
self.0.contents_mut().push(StyledGraphemes::from(item));
42+
}
43+
3644
/// Creates a new `Listbox` from a vector of `StyledGraphemes`.
3745
pub fn from_styled_graphemes(items: Vec<StyledGraphemes>) -> Self {
3846
Self(Cursor::new(items, 0, false))
@@ -79,4 +87,8 @@ impl Listbox {
7987
pub fn move_to_tail(&mut self) {
8088
self.0.move_to_tail()
8189
}
90+
91+
pub fn is_tail(&self) -> bool {
92+
self.0.is_tail()
93+
}
8294
}

promkit/src/jsonz.rs

Lines changed: 35 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
use std::collections::HashSet;
2-
31
use rayon::prelude::*;
42

53
pub mod format;
@@ -392,36 +390,49 @@ pub fn create_rows<'a, T: IntoIterator<Item = &'a serde_json::Value>>(iter: T) -
392390
rows
393391
}
394392

395-
fn collect_paths(value: &serde_json::Value, current_path: &str, paths: &mut HashSet<String>) {
396-
paths.insert(current_path.to_string());
393+
#[derive(Debug)]
394+
pub struct PathIterator<'a> {
395+
stack: Vec<(String, &'a serde_json::Value)>,
396+
}
397397

398-
match value {
399-
serde_json::Value::Object(obj) => {
400-
for (key, val) in obj {
401-
let new_path = if current_path == "." {
402-
format!(".{}", key)
403-
} else {
404-
format!("{}.{}", current_path, key)
405-
};
406-
collect_paths(val, &new_path, paths);
407-
}
408-
}
409-
serde_json::Value::Array(arr) => {
410-
for (i, val) in arr.iter().enumerate() {
411-
let new_path = format!("{}[{}]", current_path, i);
412-
collect_paths(val, &new_path, paths);
398+
impl<'a> Iterator for PathIterator<'a> {
399+
type Item = String;
400+
401+
fn next(&mut self) -> Option<Self::Item> {
402+
if let Some((current_path, value)) = self.stack.pop() {
403+
match value {
404+
serde_json::Value::Object(obj) => {
405+
for (key, val) in obj.iter() {
406+
let new_path = if current_path == "." {
407+
format!(".{}", key)
408+
} else {
409+
format!("{}.{}", current_path, key)
410+
};
411+
self.stack.push((new_path, val));
412+
}
413+
}
414+
serde_json::Value::Array(arr) => {
415+
for (i, val) in arr.iter().enumerate() {
416+
let new_path = format!("{}[{}]", current_path, i);
417+
self.stack.push((new_path, val));
418+
}
419+
}
420+
_ => {}
413421
}
422+
423+
Some(current_path)
424+
} else {
425+
None
414426
}
415-
_ => {}
416427
}
417428
}
418429

419430
pub fn get_all_paths<'a, T: IntoIterator<Item = &'a serde_json::Value>>(
420431
iter: T,
421-
) -> HashSet<String> {
422-
let mut paths = HashSet::new();
432+
) -> impl Iterator<Item = String> + 'a {
433+
let mut stack = Vec::new();
423434
for value in iter {
424-
collect_paths(value, ".", &mut paths);
435+
stack.push((".".to_string(), value));
425436
}
426-
paths
437+
PathIterator { stack }
427438
}

promkit/src/lib.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,21 @@
1111
//!
1212
//! ```toml
1313
//! [dependencies]
14-
//! promkit = "0.6.0"
14+
//! promkit = "0.6.1"
1515
//! ```
1616
//!
1717
//! ## Features
1818
//!
1919
//! - Support cross-platform both UNIX and Windows owing to [crossterm](https://github.com/crossterm-rs/crossterm)
2020
//! - Various building methods
2121
//! - Preset; Support for quickly setting up a UI by providing simple parameters.
22-
//! - [Readline](https://github.com/ynqa/promkit/tree/v0.6.0#readline)
23-
//! - [Confirm](https://github.com/ynqa/promkit/tree/v0.6.0#confirm)
24-
//! - [Password](https://github.com/ynqa/promkit/tree/v0.6.0#password)
25-
//! - [Select](https://github.com/ynqa/promkit/tree/v0.6.0#select)
26-
//! - [QuerySelect](https://github.com/ynqa/promkit/tree/v0.6.0#queryselect)
27-
//! - [Checkbox](https://github.com/ynqa/promkit/tree/v0.6.0#checkbox)
28-
//! - [Tree](https://github.com/ynqa/promkit/tree/v0.6.0#tree)
22+
//! - [Readline](https://github.com/ynqa/promkit/tree/v0.6.1#readline)
23+
//! - [Confirm](https://github.com/ynqa/promkit/tree/v0.6.1#confirm)
24+
//! - [Password](https://github.com/ynqa/promkit/tree/v0.6.1#password)
25+
//! - [Select](https://github.com/ynqa/promkit/tree/v0.6.1#select)
26+
//! - [QuerySelect](https://github.com/ynqa/promkit/tree/v0.6.1#queryselect)
27+
//! - [Checkbox](https://github.com/ynqa/promkit/tree/v0.6.1#checkbox)
28+
//! - [Tree](https://github.com/ynqa/promkit/tree/v0.6.1#tree)
2929
//! - Combining various UI components.
3030
//! - They are provided with the same interface, allowing users to choose and
3131
//! assemble them according to their preferences.
@@ -39,7 +39,7 @@
3939
//!
4040
//! ## Examples/Demos
4141
//!
42-
//! See [here](https://github.com/ynqa/promkit/tree/v0.6.0#examplesdemos)
42+
//! See [here](https://github.com/ynqa/promkit/tree/v0.6.1#examplesdemos)
4343
//!
4444
//! ## Why *promkit*?
4545
//!

promkit/tests/jsonz_get_all_paths_test.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ mod get_all_paths {
5353
)
5454
.unwrap();
5555

56-
let actual = jsonz::get_all_paths([&v]);
56+
let actual = jsonz::get_all_paths([&v]).collect::<HashSet<_>>();
5757
let expected = HashSet::from_iter(
5858
[
5959
".",
@@ -114,7 +114,7 @@ mod get_all_paths {
114114
.filter_map(serde_json::Result::ok)
115115
.collect::<Vec<_>>();
116116

117-
let actual = jsonz::get_all_paths(binding.iter());
117+
let actual = jsonz::get_all_paths(binding.iter()).collect::<HashSet<_>>();
118118
let expected = HashSet::from_iter(
119119
[
120120
".",

0 commit comments

Comments
 (0)