Refactor computation responses

This commit is contained in:
greg 2019-07-08 21:02:07 -07:00
parent 10e40669b5
commit 7c9154de53
2 changed files with 77 additions and 46 deletions

View File

@ -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<ReplResponse> {
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 {

View File

@ -0,0 +1,61 @@
use colored::*;
use std::fmt;
use super::ReplOptions;
use crate::language::{ DebugAsk, ComputationResponse};
pub struct ReplResponse {
label: Option<String>,
text: String,
color: Option<Color>
}
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<ReplResponse> {
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
}