From 3256935946a22f09255d08a603413a1b76177ac1 Mon Sep 17 00:00:00 2001 From: greg Date: Sat, 2 Feb 2019 00:24:59 -0800 Subject: [PATCH] Slight refactor of REPL code In preparation for multi-line REPL input --- schala-repl/src/repl/mod.rs | 30 +++++++++++++++++++----------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/schala-repl/src/repl/mod.rs b/schala-repl/src/repl/mod.rs index 56e85bb..bb09531 100644 --- a/schala-repl/src/repl/mod.rs +++ b/schala-repl/src/repl/mod.rs @@ -33,7 +33,7 @@ impl Repl { languages, current_language_index, interpreter_directive_sigil: ':', - line_reader + line_reader, } } @@ -90,16 +90,7 @@ impl Repl { }, Ok(ReadResult::Eof) => break, Ok(ReadResult::Signal(_)) => break, - Ok(ReadResult::Input(ref input)) => { - self.line_reader.add_history_unique(input.to_string()); - let output = match input.chars().nth(0) { - Some(ch) if ch == self.interpreter_directive_sigil => self.handle_interpreter_directive(input), - _ => Some(self.input_handler(input)), - }; - if let Some(o) = output { - println!("=> {}", o); - } - } + Ok(ReadResult::Input(input)) => self.input_loop(input), } } self.line_reader.save_history(HISTORY_SAVE_FILE).unwrap_or(()); @@ -107,6 +98,23 @@ impl Repl { println!("Exiting..."); } + fn input_loop(&mut self, input: String) { + if input == "" { + return; + } + + if input.chars().nth(0).unwrap() == self.interpreter_directive_sigil { + if let Some(output) = self.handle_interpreter_directive(&input) { + println!("{}", output); + } + return; + } + + let output = self.input_handler(&input); + self.line_reader.add_history_unique(input.clone()); + println!("=> {}", output); + } + fn input_handler(&mut self, input: &str) -> String { let ref mut language = self.languages[self.current_language_index]; let interpreter_output = language.execute_pipeline(input, &self.options);