use colored::*; use std::fmt; use std::fmt::Write; 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 { let mut buf = String::new(); if let Some(ref label) = self.label { write!(buf, "({})", label).unwrap(); } write!(buf, "=> {}", self.text).unwrap(); write!(f, "{}", match self.color { Some(c) => buf.color(c), None => buf.normal() }) } } 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 }