schala/schala-repl/src/response.rs

81 lines
2.0 KiB
Rust
Raw Normal View History

2019-07-08 21:02:07 -07:00
use colored::*;
use std::fmt;
2019-07-09 01:32:38 -07:00
use std::fmt::Write;
2019-07-08 21:02:07 -07:00
2021-10-07 01:19:35 -07:00
use crate::language::{ComputationResponse, DebugAsk};
2021-10-14 01:33:46 -07:00
use crate::ReplOptions;
2019-07-08 21:02:07 -07:00
pub struct ReplResponse {
2021-10-07 01:19:35 -07:00
label: Option<String>,
text: String,
color: Option<Color>,
2019-07-08 21:02:07 -07:00
}
impl fmt::Display for ReplResponse {
2021-10-07 01:19:35 -07:00
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(),
}
)
2019-07-08 21:02:07 -07:00
}
}
2021-10-07 01:19:35 -07:00
pub fn handle_computation_response(
response: ComputationResponse,
options: &ReplOptions,
) -> Vec<ReplResponse> {
let mut responses = vec![];
2019-07-08 21:02:07 -07:00
2021-10-07 01:19:35 -07:00
if options.show_total_time {
responses.push(ReplResponse {
label: Some("Total time".to_string()),
text: format!("{:?}", response.global_output_stats.total_duration),
color: None,
});
}
2019-07-08 21:02:07 -07:00
2021-10-07 01:19:35 -07:00
if options.show_stage_times {
responses.push(ReplResponse {
label: Some("Stage times".to_string()),
text: format!("{:?}", response.global_output_stats.stage_durations),
color: None,
});
}
2019-07-08 21:02:07 -07:00
2021-10-07 01:19:35 -07:00
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),
});
}
2019-07-08 21:02:07 -07:00
2021-10-07 01:19:35 -07:00
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),
},
2019-07-08 21:02:07 -07:00
});
2021-10-07 01:19:35 -07:00
responses
2019-07-08 21:02:07 -07:00
}