Skip to content

Instantly share code, notes, and snippets.

@nebelgrau77
Last active July 3, 2020 09:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nebelgrau77/277dabc8a3383b75a85fd32ab63188ab to your computer and use it in GitHub Desktop.
Save nebelgrau77/277dabc8a3383b75a85fd32ab63188ab to your computer and use it in GitHub Desktop.
simple text-based user interface - FIXED

A simple app to choose a random episode of a TV show :)

extern crate cursive;

use cursive::views::{SelectView, TextView, Dialog};
use cursive::align::*;
use cursive::Cursive;
use cursive::traits::*;

use rand::Rng;

fn main() {

    let mut app = cursive::default();

    let mut select = SelectView::new().h_align(HAlign::Center);

    select.add_item("Friends", ("Friends", 10,24));
    select.add_item("Seinfeld", ("Seinfeld", 9,23));

    select.set_on_submit(|s, (show, seasons, episodes)| {
        // s.pop_layer(); - this actually has to be removed, so the selection window can stay 'below' the next one
        let (season, episode) = next_episode(*seasons,*episodes);
        let text = format!("{}:\n\nwatch episode {} of season {}", show, season, episode);
        s.add_layer(Dialog::around(TextView::new(text))   
                            .dismiss_button("Again") //dismisses the window and goes back to the previous layer
                            .button("Quit", |s| s.quit()));
    });
    
    app.add_layer(Dialog::around(select).title("Choose your show:"));

    app.run();

}

fn next_episode(seasons: u8, episodes: u8) -> (u8,u8) {
    //choose random season and episode number
    let season = rand::thread_rng().gen_range(1,seasons + 1);
    let episode = rand::thread_rng().gen_range(1,episodes + 1);
    return (season, episode);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment