Move pretty-printing utilities to project::pretty
This commit is contained in:
parent
87546e0abd
commit
921e7abbb6
|
@ -1,11 +1,7 @@
|
||||||
use std::collections::BTreeMap;
|
use std::collections::BTreeMap;
|
||||||
use std::{env, path::PathBuf, process};
|
use std::{env, path::PathBuf, process};
|
||||||
|
|
||||||
use aiken_project::{
|
use aiken_project::{config::Config, pretty, script::EvalInfo, telemetry, Project};
|
||||||
config::Config,
|
|
||||||
telemetry::{self, EvalInfo},
|
|
||||||
Project,
|
|
||||||
};
|
|
||||||
use miette::IntoDiagnostic;
|
use miette::IntoDiagnostic;
|
||||||
use owo_colors::OwoColorize;
|
use owo_colors::OwoColorize;
|
||||||
use uplc::machine::cost_model::ExBudget;
|
use uplc::machine::cost_model::ExBudget;
|
||||||
|
@ -107,7 +103,8 @@ impl telemetry::EventListener for Terminal {
|
||||||
"{} {} {}",
|
"{} {} {}",
|
||||||
" ┌──".bright_black(),
|
" ┌──".bright_black(),
|
||||||
module.bold().blue(),
|
module.bold().blue(),
|
||||||
pad_left("".to_string(), first - module.len() - 3, "─").bright_black()
|
pretty::pad_left("".to_string(), first - module.len() - 3, "─")
|
||||||
|
.bright_black()
|
||||||
);
|
);
|
||||||
for eval_info in infos {
|
for eval_info in infos {
|
||||||
println!(
|
println!(
|
||||||
|
@ -120,7 +117,8 @@ impl telemetry::EventListener for Terminal {
|
||||||
let summary = fmt_test_summary(infos, false).len();
|
let summary = fmt_test_summary(infos, false).len();
|
||||||
println!(
|
println!(
|
||||||
"{} {}\n",
|
"{} {}\n",
|
||||||
pad_right(" └".to_string(), last - summary + 5, "─").bright_black(),
|
pretty::pad_right(" └".to_string(), last - summary + 5, "─")
|
||||||
|
.bright_black(),
|
||||||
fmt_test_summary(infos, true),
|
fmt_test_summary(infos, true),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -138,19 +136,19 @@ fn fmt_test(eval_info: &EvalInfo, max_mem: usize, max_cpu: usize, styled: bool)
|
||||||
} = eval_info;
|
} = eval_info;
|
||||||
|
|
||||||
let ExBudget { mem, cpu } = spent_budget;
|
let ExBudget { mem, cpu } = spent_budget;
|
||||||
let mem_pad = pad_left(mem.to_string(), max_mem, " ");
|
let mem_pad = pretty::pad_left(mem.to_string(), max_mem, " ");
|
||||||
let cpu_pad = pad_left(cpu.to_string(), max_cpu, " ");
|
let cpu_pad = pretty::pad_left(cpu.to_string(), max_cpu, " ");
|
||||||
|
|
||||||
format!(
|
format!(
|
||||||
"{} [mem: {}, cpu: {}] {}",
|
"{} [mem: {}, cpu: {}] {}",
|
||||||
if *success {
|
if *success {
|
||||||
style_if(styled, "PASS".to_string(), |s| s.bold().green().to_string())
|
pretty::style_if(styled, "PASS".to_string(), |s| s.bold().green().to_string())
|
||||||
} else {
|
} else {
|
||||||
style_if(styled, "FAIL".to_string(), |s| s.bold().red().to_string())
|
pretty::style_if(styled, "FAIL".to_string(), |s| s.bold().red().to_string())
|
||||||
},
|
},
|
||||||
style_if(styled, mem_pad, |s| s.bright_white().to_string()),
|
pretty::style_if(styled, mem_pad, |s| s.bright_white().to_string()),
|
||||||
style_if(styled, cpu_pad, |s| s.bright_white().to_string()),
|
pretty::style_if(styled, cpu_pad, |s| s.bright_white().to_string()),
|
||||||
style_if(styled, script.name.clone(), |s| s.bright_blue().to_string()),
|
pretty::style_if(styled, script.name.clone(), |s| s.bright_blue().to_string()),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -168,14 +166,14 @@ fn fmt_test_summary(tests: &Vec<&EvalInfo>, styled: bool) -> String {
|
||||||
"{}",
|
"{}",
|
||||||
format!(
|
format!(
|
||||||
"{} | {} | {}",
|
"{} | {} | {}",
|
||||||
style_if(styled, format!("{} tests", tests.len()), |s| s
|
pretty::style_if(styled, format!("{} tests", tests.len()), |s| s
|
||||||
.bold()
|
.bold()
|
||||||
.to_string()),
|
.to_string()),
|
||||||
style_if(styled, format!("{} passed", n_passed), |s| s
|
pretty::style_if(styled, format!("{} passed", n_passed), |s| s
|
||||||
.bright_green()
|
.bright_green()
|
||||||
.bold()
|
.bold()
|
||||||
.to_string()),
|
.to_string()),
|
||||||
style_if(styled, format!("{} failed", n_failed), |s| s
|
pretty::style_if(styled, format!("{} failed", n_failed), |s| s
|
||||||
.bright_red()
|
.bright_red()
|
||||||
.bold()
|
.bold()
|
||||||
.to_string()),
|
.to_string()),
|
||||||
|
@ -183,14 +181,6 @@ fn fmt_test_summary(tests: &Vec<&EvalInfo>, styled: bool) -> String {
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn style_if(styled: bool, s: String, apply_style: fn(String) -> String) -> String {
|
|
||||||
if styled {
|
|
||||||
apply_style(s)
|
|
||||||
} else {
|
|
||||||
s
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn fmt_eval(eval_info: &EvalInfo, max_mem: usize, max_cpu: usize) -> String {
|
fn fmt_eval(eval_info: &EvalInfo, max_mem: usize, max_cpu: usize) -> String {
|
||||||
let EvalInfo {
|
let EvalInfo {
|
||||||
output,
|
output,
|
||||||
|
@ -205,8 +195,8 @@ fn fmt_eval(eval_info: &EvalInfo, max_mem: usize, max_cpu: usize) -> String {
|
||||||
" {}::{} [mem: {}, cpu: {}]\n │\n ╰─▶ {}",
|
" {}::{} [mem: {}, cpu: {}]\n │\n ╰─▶ {}",
|
||||||
script.module.blue(),
|
script.module.blue(),
|
||||||
script.name.bright_blue(),
|
script.name.bright_blue(),
|
||||||
pad_left(mem.to_string(), max_mem, " "),
|
pretty::pad_left(mem.to_string(), max_mem, " "),
|
||||||
pad_left(cpu.to_string(), max_cpu, " "),
|
pretty::pad_left(cpu.to_string(), max_cpu, " "),
|
||||||
output
|
output
|
||||||
.as_ref()
|
.as_ref()
|
||||||
.map(|x| format!("{}", x))
|
.map(|x| format!("{}", x))
|
||||||
|
@ -243,23 +233,3 @@ fn find_max_execution_units(xs: &[EvalInfo]) -> (usize, usize) {
|
||||||
|
|
||||||
(max_mem.to_string().len(), max_cpu.to_string().len())
|
(max_mem.to_string().len(), max_cpu.to_string().len())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn pad_left(mut text: String, n: usize, delimiter: &str) -> String {
|
|
||||||
let diff = n as i32 - text.len() as i32;
|
|
||||||
if diff.is_positive() {
|
|
||||||
for _ in 0..diff {
|
|
||||||
text.insert_str(0, delimiter);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
text
|
|
||||||
}
|
|
||||||
|
|
||||||
fn pad_right(mut text: String, n: usize, delimiter: &str) -> String {
|
|
||||||
let diff = n as i32 - text.len() as i32;
|
|
||||||
if diff.is_positive() {
|
|
||||||
for _ in 0..diff {
|
|
||||||
text.push_str(delimiter);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
text
|
|
||||||
}
|
|
||||||
|
|
|
@ -1,14 +1,9 @@
|
||||||
use std::{
|
|
||||||
collections::HashMap,
|
|
||||||
fs,
|
|
||||||
path::{Path, PathBuf},
|
|
||||||
};
|
|
||||||
|
|
||||||
pub mod config;
|
pub mod config;
|
||||||
pub mod error;
|
pub mod error;
|
||||||
pub mod format;
|
pub mod format;
|
||||||
pub mod module;
|
pub mod module;
|
||||||
pub mod options;
|
pub mod options;
|
||||||
|
pub mod pretty;
|
||||||
pub mod script;
|
pub mod script;
|
||||||
pub mod telemetry;
|
pub mod telemetry;
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,48 @@
|
||||||
|
pub fn boxed(title: &str, content: String) -> String {
|
||||||
|
let n = content.lines().fold(0, |max, l| {
|
||||||
|
let n = l.len();
|
||||||
|
if n > max {
|
||||||
|
n
|
||||||
|
} else {
|
||||||
|
max
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
let content = content
|
||||||
|
.lines()
|
||||||
|
.map(|line| format!("│ {} │", pad_right(line.to_string(), n, " ")))
|
||||||
|
.collect::<Vec<String>>()
|
||||||
|
.join("\n");
|
||||||
|
|
||||||
|
let top = format!("┍━ {}┑", pad_right(format!("{title} "), n, "━"));
|
||||||
|
let bottom = format!("┕{}┙", pad_right(String::new(), n + 2, "━"));
|
||||||
|
format!("{top}\n{content}\n{bottom}")
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn pad_left(mut text: String, n: usize, delimiter: &str) -> String {
|
||||||
|
let diff = n as i32 - text.len() as i32;
|
||||||
|
if diff.is_positive() {
|
||||||
|
for _ in 0..diff {
|
||||||
|
text.insert_str(0, delimiter);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
text
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn pad_right(mut text: String, n: usize, delimiter: &str) -> String {
|
||||||
|
let diff = n as i32 - text.len() as i32;
|
||||||
|
if diff.is_positive() {
|
||||||
|
for _ in 0..diff {
|
||||||
|
text.push_str(delimiter);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
text
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn style_if(styled: bool, s: String, apply_style: fn(String) -> String) -> String {
|
||||||
|
if styled {
|
||||||
|
apply_style(s)
|
||||||
|
} else {
|
||||||
|
s
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue