Make box-drawing code more reusable
- Move it to 'pretty' module. - Have function work on colored strings titles and contents
This commit is contained in:
@@ -26,6 +26,7 @@ regex = "1.6.0"
|
||||
reqwest = "0.11.13"
|
||||
serde = { version = "1.0.144", features = ["derive"] }
|
||||
serde_json = { version = "1.0.85", features = ["preserve_order"] }
|
||||
strip-ansi-escapes = "0.1.1"
|
||||
thiserror = "1.0.37"
|
||||
tokio = { version = "1.23.0", features = ["full"] }
|
||||
toml = "0.5.9"
|
||||
|
||||
@@ -286,11 +286,11 @@ impl Diagnostic for Error {
|
||||
None => None,
|
||||
Some(hint) => {
|
||||
let budget = ExBudget { mem: i64::MAX, cpu: i64::MAX, };
|
||||
let left = pretty::boxed("left", match hint.left.eval(budget) {
|
||||
let left = pretty::boxed("left", &match hint.left.eval(budget) {
|
||||
(Ok(term), _, _) => format!("{term}"),
|
||||
(Err(err), _, _) => format!("{err}"),
|
||||
});
|
||||
let right = pretty::boxed("right", match hint.right.eval(budget) {
|
||||
let right = pretty::boxed("right", &match hint.right.eval(budget) {
|
||||
(Ok(term), _, _) => format!("{term}"),
|
||||
(Err(err), _, _) => format!("{err}"),
|
||||
});
|
||||
|
||||
@@ -1,26 +1,100 @@
|
||||
pub fn boxed(title: &str, content: String) -> String {
|
||||
let n = content.lines().fold(0, |max, l| {
|
||||
let n = l.len();
|
||||
pub fn ansi_len(s: &str) -> usize {
|
||||
String::from_utf8(strip_ansi_escapes::strip(s).unwrap())
|
||||
.unwrap()
|
||||
.chars()
|
||||
.count()
|
||||
}
|
||||
|
||||
pub fn len_longest_line(s: &str) -> usize {
|
||||
s.lines().fold(0, |max, l| {
|
||||
let n = ansi_len(l);
|
||||
if n > max {
|
||||
n
|
||||
} else {
|
||||
max
|
||||
}
|
||||
});
|
||||
})
|
||||
}
|
||||
|
||||
pub fn boxed(title: &str, content: &str) -> String {
|
||||
boxed_with(title, content, |s| s.to_string())
|
||||
}
|
||||
|
||||
pub fn boxed_with(title: &str, content: &str, border_style: fn(&str) -> String) -> String {
|
||||
let n = len_longest_line(content);
|
||||
|
||||
let content = content
|
||||
.lines()
|
||||
.map(|line| format!("│ {} │", pad_right(line.to_string(), n, " ")))
|
||||
.map(|line| {
|
||||
format!(
|
||||
"{} {} {}",
|
||||
border_style("│"),
|
||||
pad_right(line.to_string(), n, " "),
|
||||
border_style("│"),
|
||||
)
|
||||
})
|
||||
.collect::<Vec<String>>()
|
||||
.join("\n");
|
||||
|
||||
let top = format!("┍━ {}┑", pad_right(format!("{title} "), n, "━"));
|
||||
let bottom = format!("┕{}┙", pad_right(String::new(), n + 2, "━"));
|
||||
let top = format!(
|
||||
"{} {}{}",
|
||||
border_style("┍━"),
|
||||
pad_right(format!("{title} "), n, &border_style("━")),
|
||||
border_style("┑"),
|
||||
);
|
||||
|
||||
let bottom = format!(
|
||||
"{}{}{}",
|
||||
border_style("┕"),
|
||||
pad_right(String::new(), n + 2, &border_style("━")),
|
||||
border_style("┙")
|
||||
);
|
||||
|
||||
format!("{top}\n{content}\n{bottom}")
|
||||
}
|
||||
|
||||
pub fn open_box(
|
||||
title: &str,
|
||||
content: &str,
|
||||
footer: &str,
|
||||
border_style: fn(&str) -> String,
|
||||
) -> String {
|
||||
let i = ansi_len(content.lines().collect::<Vec<_>>().first().unwrap());
|
||||
let j = len_longest_line(content);
|
||||
let k = ansi_len(footer);
|
||||
|
||||
let content = content
|
||||
.lines()
|
||||
.map(|line| format!("{} {line}", border_style("│"),))
|
||||
.collect::<Vec<String>>()
|
||||
.join("\n");
|
||||
|
||||
let top = format!(
|
||||
"{} {}",
|
||||
border_style("┍━"),
|
||||
pad_right(format!("{title} "), i - 1, &border_style("━")),
|
||||
);
|
||||
|
||||
let bottom = format!(
|
||||
"{} {}",
|
||||
pad_right(border_style("┕"), j - k + 1, &border_style("━")),
|
||||
footer
|
||||
);
|
||||
|
||||
format!("{top}\n{content}\n{bottom}")
|
||||
}
|
||||
|
||||
pub fn indent(lines: &str, n: usize) -> String {
|
||||
let tab = pad_left(String::new(), n, " ");
|
||||
lines
|
||||
.lines()
|
||||
.map(|line| format!("{tab}{line}"))
|
||||
.collect::<Vec<_>>()
|
||||
.join("\n")
|
||||
}
|
||||
|
||||
pub fn pad_left(mut text: String, n: usize, delimiter: &str) -> String {
|
||||
let diff = n as i32 - text.len() as i32;
|
||||
let diff = n as i32 - ansi_len(&text) as i32;
|
||||
if diff.is_positive() {
|
||||
for _ in 0..diff {
|
||||
text.insert_str(0, delimiter);
|
||||
@@ -30,7 +104,7 @@ pub fn pad_left(mut text: String, n: usize, delimiter: &str) -> String {
|
||||
}
|
||||
|
||||
pub fn pad_right(mut text: String, n: usize, delimiter: &str) -> String {
|
||||
let diff = n as i32 - text.len() as i32;
|
||||
let diff = n as i32 - ansi_len(&text) as i32;
|
||||
if diff.is_positive() {
|
||||
for _ in 0..diff {
|
||||
text.push_str(delimiter);
|
||||
|
||||
Reference in New Issue
Block a user