promkit 0.9.0

A toolkit for building your own interactive command-line tools
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
# promkit

[![ci](https://github.com/ynqa/promkit/actions/workflows/ci.yml/badge.svg?branch=main)](https://github.com/ynqa/promkit/actions/workflows/ci.yml)
[![docs.rs](https://img.shields.io/docsrs/promkit)](https://docs.rs/promkit)

A toolkit for building your own interactive prompt in Rust.

## Getting Started

Put the package in your `Cargo.toml`.

```toml
[dependencies]
promkit = "0.9.0"
```

## Features

- Cross-platform support for both UNIX and Windows utilizing [crossterm]https://github.com/crossterm-rs/crossterm
- Modularized architecture
  - [promkit-core]https://github.com/ynqa/promkit/tree/v0.9.0/promkit-core/
    - Core functionality for basic terminal operations and pane management
  - [promkit-widgets]https://github.com/ynqa/promkit/tree/v0.9.0/promkit-widgets/
    - Various UI components (text, listbox, tree, etc.)
  - [promkit]https://github.com/ynqa/promkit/tree/v0.9.0/promkit
    - High-level presets and user interfaces
  - [promkit-derive]https://github.com/ynqa/promkit/tree/v0.9.0/promkit-derive/
    - A Derive macro that simplifies interactive form input
- Rich preset components
  - [Readline]https://github.com/ynqa/promkit/tree/v0.9.0#readline - Text input with auto-completion
  - [Confirm]https://github.com/ynqa/promkit/tree/v0.9.0#confirm - Yes/no confirmation prompt
  - [Password]https://github.com/ynqa/promkit/tree/v0.9.0#password - Password input with masking and validation
  - [Form]https://github.com/ynqa/promkit/tree/v0.9.0#form - Manage multiple text input fields
  - [Listbox]https://github.com/ynqa/promkit/tree/v0.9.0#listbox - Single selection interface from a list
  - [QuerySelector]https://github.com/ynqa/promkit/tree/v0.9.0#queryselector - Searchable selection interface
  - [Checkbox]https://github.com/ynqa/promkit/tree/v0.9.0#checkbox - Multiple selection checkbox interface
  - [Tree]https://github.com/ynqa/promkit/tree/v0.9.0#tree - Tree display for hierarchical data like file systems
  - [JSON]https://github.com/ynqa/promkit/tree/v0.9.0#json - Parse and interactively display JSON data
  - [Text]https://github.com/ynqa/promkit/tree/v0.9.0#text - Static text display

## Concept

See [here](https://github.com/ynqa/promkit/tree/v0.9.0/Concept.md).

## Projects using *promkit*

- [ynqa/empiriqa]https://github.com/ynqa/empiriqa
- [ynqa/jnv]https://github.com/ynqa/jnv
- [ynqa/logu]https://github.com/ynqa/logu
- [ynqa/sig]https://github.com/ynqa/sig

## Examples/Demos

*promkit* provides presets so that users can try prompts immediately without
having to build complex components for specific use cases.

Show you commands, code, and actual demo screens for examples
that can be executed immediately below.

### Readline

<details>
<summary>Command</summary>

```bash
cargo run --bin readline --manifest-path examples/readline/Cargo.toml
```

</details>

<details>
<summary>Code</summary>

```rust,ignore
use promkit::{preset::readline::Readline, suggest::Suggest, Result};

fn main() -> Result {
    let mut p = Readline::default()
        .title("Hi!")
        .enable_suggest(Suggest::from_iter([
            "apple",
            "applet",
            "application",
            "banana",
        ]))
        .validator(
            |text| text.len() > 10,
            |text| format!("Length must be over 10 but got {}", text.len()),
        )
        .prompt()?;
    println!("result: {:?}", p.run()?);
    Ok(())
}
```
</details>

<img src="https://github.com/ynqa/promkit/assets/6745370/d124268e-9496-4c4b-83be-c734e4d03591" width="50%" height="auto">

### Confirm

<details>
<summary>Command</summary>

```bash
cargo run --manifest-path examples/confirm/Cargo.toml
```

</details>

<details>
<summary>Code</summary>

```rust,ignore
use promkit::{preset::confirm::Confirm, Result};

fn main() -> Result {
    let mut p = Confirm::new("Do you have a pet?").prompt()?;
    println!("result: {:?}", p.run()?);
    Ok(())
}
```
</details>

<img src="https://github.com/ynqa/promkit/assets/6745370/ac9bac78-66cd-4653-a39f-6c9c0c24131f" width="50%" height="auto">

### Password

<details>
<summary>Command</summary>

```bash
cargo run --manifest-path examples/password/Cargo.toml
```

</details>

<details>
<summary>Code</summary>

```rust,ignore
use promkit::{preset::password::Password, Result};

fn main() -> Result {
    let mut p = Password::default()
        .title("Put your password")
        .validator(
            |text| 4 < text.len() && text.len() < 10,
            |text| format!("Length must be over 4 and within 10 but got {}", text.len()),
        )
        .prompt()?;
    println!("result: {:?}", p.run()?);
    Ok(())
}
```
</details>

<img src="https://github.com/ynqa/promkit/assets/6745370/396356ef-47de-44bc-a8d4-d03c7ac66a2f" width="50%" height="auto">

### Form

<details>
<summary>Command</summary>

```bash
cargo run --manifest-path examples/form/Cargo.toml
```

</details>

<details>
<summary>Code</summary>

```rust,ignore
use promkit::{crossterm::style::Color, preset::form::Form, style::StyleBuilder, text_editor};

fn main() -> anyhow::Result<()> {
    let mut p = Form::new([
        text_editor::State {
            texteditor: Default::default(),
            history: Default::default(),
            prefix: String::from("❯❯ "),
            mask: Default::default(),
            prefix_style: StyleBuilder::new().fgc(Color::DarkRed).build(),
            active_char_style: StyleBuilder::new().bgc(Color::DarkCyan).build(),
            inactive_char_style: StyleBuilder::new().build(),
            edit_mode: Default::default(),
            word_break_chars: Default::default(),
            lines: Default::default(),
        },
        text_editor::State {
            texteditor: Default::default(),
            history: Default::default(),
            prefix: String::from("❯❯ "),
            mask: Default::default(),
            prefix_style: StyleBuilder::new().fgc(Color::DarkGreen).build(),
            active_char_style: StyleBuilder::new().bgc(Color::DarkCyan).build(),
            inactive_char_style: StyleBuilder::new().build(),
            edit_mode: Default::default(),
            word_break_chars: Default::default(),
            lines: Default::default(),
        },
        text_editor::State {
            texteditor: Default::default(),
            history: Default::default(),
            prefix: String::from("❯❯ "),
            mask: Default::default(),
            prefix_style: StyleBuilder::new().fgc(Color::DarkBlue).build(),
            active_char_style: StyleBuilder::new().bgc(Color::DarkCyan).build(),
            inactive_char_style: StyleBuilder::new().build(),
            edit_mode: Default::default(),
            word_break_chars: Default::default(),
            lines: Default::default(),
        },
    ])
    .prompt()?;
    println!("result: {:?}", p.run()?);
    Ok(())
}
```

</details>

<img src="https://github.com/ynqa/promkit/assets/6745370/c3dc88a7-d0f0-42f4-90b8-bc4d2e23e36d" width="50%" height="auto">

### Listbox

<details>
<summary>Command</summary>

```bash
cargo run --manifest-path examples/listbox/Cargo.toml
```
</details>

<details>
<summary>Code</summary>

```rust,ignore
use promkit::{preset::listbox::Listbox, Result};

fn main() -> Result {
    let mut p = Listbox::new(0..100)
        .title("What number do you like?")
        .listbox_lines(5)
        .prompt()?;
    println!("result: {:?}", p.run()?);
    Ok(())
}
```
</details>

<img src="https://github.com/ynqa/promkit/assets/6745370/0da1b1d0-bb17-4951-8ea8-3b09cd2eb86a" width="50%" height="auto">

### QuerySelector

<details>
<summary>Command</summary>

```bash
cargo run --manifest-path examples/query_selector/Cargo.toml
```
</details>

<details>
<summary>Code</summary>

```rust,ignore
use promkit::{preset::query_selector::QuerySelector, Result};

fn main() -> Result {
    let mut p = QuerySelector::new(0..100, |text, items| -> Vec<String> {
        text.parse::<usize>()
            .map(|query| {
                items
                    .iter()
                    .filter(|num| query <= num.parse::<usize>().unwrap_or_default())
                    .map(|num| num.to_string())
                    .collect::<Vec<String>>()
            })
            .unwrap_or(items.clone())
    })
    .title("What number do you like?")
    .listbox_lines(5)
    .prompt()?;
    println!("result: {:?}", p.run()?);
    Ok(())
}
```
</details>

<img src="https://github.com/ynqa/promkit/assets/6745370/7ac2ed54-9f9e-4735-bffb-72f7cee06f6d" width="50%" height="auto">

### Checkbox

<details>
<summary>Command</summary>

```bash
cargo run --manifest-path examples/checkbox/Cargo.toml
```
</details>

<details>
<summary>Code</summary>

```rust,ignore
use promkit::{preset::checkbox::Checkbox, Result};

fn main() -> Result {
    let mut p = Checkbox::new(vec![
        "Apple",
        "Banana",
        "Orange",
        "Mango",
        "Strawberry",
        "Pineapple",
        "Grape",
        "Watermelon",
        "Kiwi",
        "Pear",
    ])
    .title("What are your favorite fruits?")
    .checkbox_lines(5)
    .prompt()?;
    println!("result: {:?}", p.run()?);
    Ok(())
}
```
</details>

<img src="https://github.com/ynqa/promkit/assets/6745370/350b16ce-6ef4-46f2-9466-d01b9dab4eaf" width="50%" height="auto">

### Tree

<details>
<summary>Command</summary>

```bash
cargo run --manifest-path examples/tree/Cargo.toml
```
</details>

<details>
<summary>Code</summary>

```rust,ignore
use promkit::{preset::tree::Tree, tree::Node, Result};

fn main() -> Result {
    let mut p = Tree::new(Node::try_from(&std::env::current_dir()?.join("src"))?)
        .title("Select a directory or file")
        .tree_lines(10)
        .prompt()?;
    println!("result: {:?}", p.run()?);
    Ok(())
}
```
</details>

<img src="https://github.com/ynqa/promkit/assets/6745370/61aefcd0-080a-443e-9dc6-ac627d306f55" width="50%" height="auto">

### JSON

<details>
<summary>Command</summary>

```bash
cargo run --manifest-path examples/json/Cargo.toml
```
</details>

<details>
<summary>Code</summary>

```rust,ignore
use promkit::{json::JsonStream, preset::json::Json, serde_json::Deserializer, Result};

fn main() -> Result {
    let stream = JsonStream::new(
        Deserializer::from_str(
            r#"{
              "number": 9,
              "map": {
                "entry1": "first",
                "entry2": "second"
              },
              "list": [
                "abc",
                "def"
              ]
            }"#,
        )
        .into_iter::<serde_json::Value>()
        .filter_map(serde_json::Result::ok),
        None,
    );

    let mut p = Json::new(stream)
        .title("JSON viewer")
        .json_lines(5)
        .prompt()?;
    println!("result: {:?}", p.run()?);
    Ok(())
}
```
</details>

<img src="https://github.com/ynqa/promkit/assets/6745370/751af3ae-5aff-45ca-8729-34cd004ee7d9" width="50%" height="auto">

## License

This project is licensed under the MIT License.
See the [LICENSE](https://github.com/ynqa/promkit/blob/main/LICENSE)
file for details.

## Stargazers over time
[![Stargazers over time](https://starchart.cc/ynqa/promkit.svg?variant=adaptive)](https://starchart.cc/ynqa/promkit)