schala/schala-repl/src/response.rs

75 lines
1.9 KiB
Rust
Raw Normal View History

2021-11-24 23:51:30 -08:00
use std::{fmt, fmt::Write};
2019-07-08 21:02:07 -07:00
use colored::*;
2021-11-24 23:51:30 -08:00
use crate::{
language::{ComputationResponse, DebugAsk},
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 {
2021-11-24 23:51:30 -08:00
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
}