Crates.io | tui-clap |
lib.rs | tui-clap |
version | 0.2.0 |
source | src |
created_at | 2021-09-12 08:58:43.614093 |
updated_at | 2021-11-14 09:23:26.562587 |
description | Provides tui-rs widgets to output and text input which is parsed by clap |
homepage | https://github.com/kegesch/tui-clap-rs |
repository | https://github.com/kegesch/tui-clap-rs |
max_upload_size | |
id | 450028 |
size | 32,007 |
Input widgets are not supported by tui-rs out of the box. This crate provides an abstraction of input handling together with clap's command argument parsing.
tui-clap
is providing two widgets (input and output) and takes care of parsing the input against a clap
app.
To get it work three points must be implemented manually:
The following code demonstrates these three points.
fn main() -> Result<(), io::Error> {
let yaml = load_yaml!("cli.yaml");
let clapp = App::from(yaml);
let stdout = io::stdout();
let backend = CrosstermBackend::new(stdout);
let mut terminal = Terminal::new(backend)?;
// Create a TuiClap instance and pass over a function that handles the arg matches
let mut tui = TuiClap::from_app(clapp);
terminal.clear();
// handle events, Events struct is a helper struct to read from crossterm events
let events = Events::default();
loop {
// your drawing method
draw(&mut terminal, &mut tui)?;
// handle events manually with the provided events struct, but you can use your own
if let Ok(Some(Event::Key(key_event))) = events.next() {
match key_event.code {
KeyCode::Backspace => {
tui.state().del_char()
}
KeyCode::Enter => {
if let Ok(matches) = tui.parse() {
match handle_matches(matches) {
Ok(output) => {
for message in output {
tui.write_to_output(message)
}
}
Err(err) => tui.write_to_output(err)
}
}
}
KeyCode::Char(char) => {
tui.state().add_char(char)
},
_ => {}
}
}
}
}
// your drawing method
fn draw<B: Backend>(terminal: &mut Terminal<B>, tui: &mut TuiClap) -> io::Result<()>{
terminal.draw(|f| {
let size = f.size();
// render the input widget of tui-clap
tui.render_input(f, size);
// render the output widget of tui-clap
tui.render_output(f, size);
});
Ok(())
}
// function that handles arg matches and returns a vec of strings that is pushed to the output widget
// return Ok() with vec of message that should be added to the output
// return Err(message) to display an error in the output
fn handle_matches(matches: ArgMatches) -> Result<Vec<String>, String> {}
Ok(vec!["handled".to_string()])
}
See the example
folder or run cargo run --example command