summaryrefslogtreecommitdiffstats
path: root/src/main.rs
blob: 9e7d5458b1d587d64542ed9545b227f4f49ad8e6 (plain)
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
use imgui::*;
use rlua::{Lua, MultiValue};

mod support;
mod testbench;

#[derive(Default)]
struct State {
    lua: Lua,
    console: Vec<ImString>,
    repl_input: ImString,
}

impl State {
    fn new() -> State {
        State {
            lua: Lua::new(),
            console: Vec::new(),
            repl_input: ImString::with_capacity(256),
        }
    }
}

fn draw_console(_run: &mut bool, ui: &mut Ui, state: &mut State) {
    let win = Window::new(im_str!("Lua Console"))
        .size([400., 500.], Condition::Appearing)
        .position([440., 20.], Condition::Appearing);

    win.build(&ui, || {
        ChildWindow::new("console")
            .size([0., 400.])
            .scrollable(true)
            .build(&ui, || {
                for line in &state.console {
                    ui.text(line);
                }
            });

        ui.separator();
        ui.input_text(im_str!("Lua"), &mut state.repl_input).build();
        ui.same_line(0.);
        if ui.button(im_str!("Eval"), [0., 0.]) {
            let input = state.repl_input.to_str().clone();
            let mut new_text = String::new();

            state.lua.context(|ctx| {
                match ctx.load(input).eval::<MultiValue>() {
                    Ok(values) => {
                        new_text.push_str(
                            &values
                                .iter()
                                .map(|value| format!("{:?}", value))
                                .collect::<Vec<_>>()
                                .join("\t"),
                        );
                    }
                    Err(e) => {
                        new_text.push_str(&e.to_string());
                    }
                };
            });

            state.console.push(ImString::new(format!("> {}", input)));
            state.console.push(ImString::new(new_text));
            state.repl_input.clear();
        }
    });
}

fn main() {
    let system = support::init(file!());
    let mut state = State::new();
    let mut bench = testbench::Bench::new();

    system.main_loop(move |run, ui| {
        // ui.show_demo_window(run);
        bench.draw(run, ui);
        draw_console(run, ui, &mut state);
    });
}