Giter Club home page Giter Club logo

tui-clap-rs's Introduction

tui-clap

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.

Getting Started

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:

  • fetching events must be included in the main loop
  • output and input widgets must be rendered
  • arg matches from clap must be handled

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()])
}

Example

See the example folder or run cargo run --example command

tui-clap-rs's People

Contributors

ducklin5 avatar kegesch avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.