1use rail_lang::{
2 rail_machine::{self, RailState},
3 RunConventions,
4};
5use rustyline::{error::ReadlineError, Editor};
6
7use crate::{run, LogLevel, Module};
8
9pub struct StapPrompt {
10 editor: Editor<()>,
11 conventions: RunConventions<'static>,
12 log_level: LogLevel,
13}
14
15impl StapPrompt {
16 pub fn new(conventions: RunConventions<'static>, log_level: LogLevel) -> Self {
17 let editor = Editor::<()>::new().expect("Unable to boot editor");
18 StapPrompt {
19 editor,
20 conventions,
21 log_level,
22 }
23 }
24
25 pub fn run(&mut self, state: RailState) -> RailState {
26 let mut state = state;
27
28 let mut input = String::new();
29
30 loop {
31 input = match self.editor.readline("> ") {
32 Err(e) => {
33 if let ReadlineError::Eof = e {
35 rail_machine::log_fatal(&self.conventions, "End of input");
36 return state;
37 } else if let ReadlineError::Interrupted = e {
38 rail_machine::log_fatal(&self.conventions, "Process interrupt");
39 return state;
40 }
41
42 eprintln!("Something bad happened. {:?}", e);
43 std::process::exit(1);
44 }
45 Ok(line) => input + &line,
46 };
47
48 let (module, remaining) = Module::parse_line(&input);
49 input = remaining;
50
51 state = run(state, module, self.log_level);
52 }
53 }
54}