diff options
Diffstat (limited to 'src/main.rs')
-rw-r--r-- | src/main.rs | 75 |
1 files changed, 75 insertions, 0 deletions
diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..df1a475 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,75 @@ +use imgui::*; +use rlua::{Lua, MultiValue}; + +mod support; + +#[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); + + 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(350.); + 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(); + + system.main_loop(move |run, ui| { + ui.show_demo_window(run); + draw_console(run, ui, &mut state); + }); +} |