diff --git a/schala-repl/src/repl/mod.rs b/schala-repl/src/repl/mod.rs index 3e03358..97946ab 100644 --- a/schala-repl/src/repl/mod.rs +++ b/schala-repl/src/repl/mod.rs @@ -1,10 +1,8 @@ use std::sync::Arc; use std::collections::HashSet; -use colored::*; use crate::language::{ProgrammingLanguageInterface, -ComputationRequest, ComputationResponse, -DebugAsk, LangMetaResponse, LangMetaRequest}; +ComputationRequest, LangMetaResponse, LangMetaRequest}; mod command_tree; use self::command_tree::CommandTree; @@ -14,6 +12,8 @@ mod directive_actions; mod directives; use directives::directives_from_pass_names; mod help; +mod response; +use response::ReplResponse; const HISTORY_SAVE_FILE: &'static str = ".schala_history"; const OPTIONS_SAVE_FILE: &'static str = ".schala_repl"; @@ -73,12 +73,16 @@ impl Repl { Ok(Eof) | Ok(Signal(_)) => break, Ok(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.handle_input(input)), - }; - if let Some(o) = output { - println!("=> {}", o); + match input.chars().nth(0) { + Some(ch) if ch == self.interpreter_directive_sigil => match self.handle_interpreter_directive(input) { + Some(directive_output) => println!("<> {}", directive_output), + None => (), + }, + _ => { + for repl_response in self.handle_input(input) { + println!("{}", repl_response); + } + } } } } @@ -118,50 +122,16 @@ impl Repl { &mut self.language_states[0] } - fn handle_input(&mut self, input: &str) -> String { + fn handle_input(&mut self, input: &str) -> Vec { let mut debug_requests = HashSet::new(); for ask in self.options.debug_asks.iter() { debug_requests.insert(ask.clone()); } - let request = ComputationRequest { - source: input, - debug_requests, - }; - + let request = ComputationRequest { source: input, debug_requests }; let ref mut language_state = self.get_cur_language_state(); let response = language_state.run_computation(request); - - self.handle_computation_response(response) - } - - fn handle_computation_response(&mut self, response: ComputationResponse) -> String { - let mut buf = String::new(); - - if self.options.show_total_time { - buf.push_str(&format!("Total duration: {:?}\n", response.global_output_stats.total_duration)); - } - - if self.options.show_stage_times { - buf.push_str(&format!("{:?}\n", response.global_output_stats.stage_durations)); - } - - - for debug_resp in response.debug_responses { - let stage_name = match debug_resp.ask { - DebugAsk::ByStage { stage_name, .. } => stage_name, - _ => continue, - }; - let s = format!("{} - {}\n", stage_name, debug_resp.value); - buf.push_str(&s); - } - - buf.push_str(&match response.main_output { - Ok(s) => s, - Err(e) => format!("{} {}", "Error".red(), e) - }); - - buf + response::handle_computation_response(response, &self.options) } fn get_directives(&mut self) -> CommandTree { diff --git a/schala-repl/src/repl/response.rs b/schala-repl/src/repl/response.rs new file mode 100644 index 0000000..d48fc02 --- /dev/null +++ b/schala-repl/src/repl/response.rs @@ -0,0 +1,61 @@ +use colored::*; +use std::fmt; + +use super::ReplOptions; +use crate::language::{ DebugAsk, ComputationResponse}; + +pub struct ReplResponse { + label: Option, + text: String, + color: Option +} + +impl fmt::Display for ReplResponse { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + if let Some(ref label) = self.label { + write!(f, "({})", label).unwrap(); + } + write!(f, "=> {}", self.text) + } +} + + +pub fn handle_computation_response(response: ComputationResponse, options: &ReplOptions) -> Vec { + let mut responses = vec![]; + + if options.show_total_time { + responses.push(ReplResponse { + label: Some("Total time".to_string()), + text: format!("{:?}", response.global_output_stats.total_duration), + color: None, + }); + } + + if options.show_stage_times { + responses.push(ReplResponse { + label: Some("Stage times".to_string()), + text: format!("{:?}", response.global_output_stats.stage_durations), + color: None, + }); + } + + for debug_resp in response.debug_responses { + let stage_name = match debug_resp.ask { + DebugAsk::ByStage { stage_name, .. } => stage_name, + _ => continue, + }; + responses.push(ReplResponse { + label: Some(stage_name.to_string()), + text: debug_resp.value, + color: Some(Color::Red), + }); + } + + responses.push(match response.main_output { + Ok(s) => ReplResponse { label: None, text: s, color: None }, + Err(e) => ReplResponse { label: Some("Error".to_string()), text: e, color: Some(Color::Red) }, + }); + + responses +} +