counter/
counter.rs

1use epkard::*;
2
3#[derive(Clone)]
4enum MyNode {
5    Small,
6    Large,
7}
8
9struct State {
10    count: usize,
11}
12
13impl Node for MyNode {
14    type State = State;
15    fn next<F>(self, rt: &mut Runtime<Self, F>) -> Control<Self>
16    where
17        F: Frontend,
18    {
19        rt.clear();
20        rt.println(format!("count: {}\n", rt.count));
21        match self {
22            MyNode::Small => {
23                // `Runtime::multiple_choice` prompts a user to choose
24                // from one of several choices
25                match rt
26                    .multiple_choice("How many do you want to add?", &["1", "2", "3", "more"])?
27                {
28                    n if n <= 2 => rt.count += n + 1,
29                    _ => return next(MyNode::Large),
30                }
31            }
32            MyNode::Large => {
33                // `Runtime::prompt` prompts a user to enter text
34                let input = rt.prompt("Enter a number to add")?;
35                match input.parse::<usize>() {
36                    Ok(i) => rt.count += i,
37                    Err(_) => rt.println("Invalid number"),
38                }
39            }
40        }
41        next(MyNode::Small)
42    }
43}
44
45fn main() {
46    run(
47        MyNode::Small,
48        &mut State { count: 0 },
49        &mut CliFrontend::new(),
50    );
51}