From 921e7abbb66516fcbd595ad4989d47721b4be691 Mon Sep 17 00:00:00 2001 From: KtorZ Date: Wed, 14 Dec 2022 21:59:09 +0100 Subject: [PATCH] Move pretty-printing utilities to project::pretty --- crates/cli/src/lib.rs | 64 ++++++++++-------------------------- crates/project/src/lib.rs | 7 +--- crates/project/src/pretty.rs | 48 +++++++++++++++++++++++++++ 3 files changed, 66 insertions(+), 53 deletions(-) create mode 100644 crates/project/src/pretty.rs diff --git a/crates/cli/src/lib.rs b/crates/cli/src/lib.rs index 46885746..7064e8fc 100644 --- a/crates/cli/src/lib.rs +++ b/crates/cli/src/lib.rs @@ -1,11 +1,7 @@ use std::collections::BTreeMap; use std::{env, path::PathBuf, process}; -use aiken_project::{ - config::Config, - telemetry::{self, EvalInfo}, - Project, -}; +use aiken_project::{config::Config, pretty, script::EvalInfo, telemetry, Project}; use miette::IntoDiagnostic; use owo_colors::OwoColorize; use uplc::machine::cost_model::ExBudget; @@ -107,7 +103,8 @@ impl telemetry::EventListener for Terminal { "{} {} {}", " ┌──".bright_black(), 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 { println!( @@ -120,7 +117,8 @@ impl telemetry::EventListener for Terminal { let summary = fmt_test_summary(infos, false).len(); println!( "{} {}\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), ); } @@ -138,19 +136,19 @@ fn fmt_test(eval_info: &EvalInfo, max_mem: usize, max_cpu: usize, styled: bool) } = eval_info; let ExBudget { mem, cpu } = spent_budget; - let mem_pad = pad_left(mem.to_string(), max_mem, " "); - let cpu_pad = pad_left(cpu.to_string(), max_cpu, " "); + let mem_pad = pretty::pad_left(mem.to_string(), max_mem, " "); + let cpu_pad = pretty::pad_left(cpu.to_string(), max_cpu, " "); format!( "{} [mem: {}, cpu: {}] {}", 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 { - 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()), - 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, mem_pad, |s| s.bright_white().to_string()), + pretty::style_if(styled, cpu_pad, |s| s.bright_white().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!( "{} | {} | {}", - style_if(styled, format!("{} tests", tests.len()), |s| s + pretty::style_if(styled, format!("{} tests", tests.len()), |s| s .bold() .to_string()), - style_if(styled, format!("{} passed", n_passed), |s| s + pretty::style_if(styled, format!("{} passed", n_passed), |s| s .bright_green() .bold() .to_string()), - style_if(styled, format!("{} failed", n_failed), |s| s + pretty::style_if(styled, format!("{} failed", n_failed), |s| s .bright_red() .bold() .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 { let EvalInfo { output, @@ -205,8 +195,8 @@ fn fmt_eval(eval_info: &EvalInfo, max_mem: usize, max_cpu: usize) -> String { " {}::{} [mem: {}, cpu: {}]\n │\n ╰─▶ {}", script.module.blue(), script.name.bright_blue(), - pad_left(mem.to_string(), max_mem, " "), - pad_left(cpu.to_string(), max_cpu, " "), + pretty::pad_left(mem.to_string(), max_mem, " "), + pretty::pad_left(cpu.to_string(), max_cpu, " "), output .as_ref() .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()) } - -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 -} diff --git a/crates/project/src/lib.rs b/crates/project/src/lib.rs index bca79397..286582b2 100644 --- a/crates/project/src/lib.rs +++ b/crates/project/src/lib.rs @@ -1,14 +1,9 @@ -use std::{ - collections::HashMap, - fs, - path::{Path, PathBuf}, -}; - pub mod config; pub mod error; pub mod format; pub mod module; pub mod options; +pub mod pretty; pub mod script; pub mod telemetry; diff --git a/crates/project/src/pretty.rs b/crates/project/src/pretty.rs new file mode 100644 index 00000000..7761062a --- /dev/null +++ b/crates/project/src/pretty.rs @@ -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::>() + .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 + } +}