use std::fmt;
use crate::block::Block;
use crate::output::{self, Val};
use crate::style::palette::{AMBER, BLUSH, LINE, MUTED, RED, ROSE, SAGE, SOFT, TEXT};
use crate::style::{Rgb, bold, paint};
use crate::term::content_width;
use crate::text;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Tone {
Success,
Info,
Warn,
Error,
Hint,
}
impl Tone {
pub fn line(self, msg: &str) -> String {
let (glyph, color, text_color): (&str, Rgb, Rgb) = match self {
Tone::Success => ("●", ROSE, TEXT),
Tone::Info => ("›", BLUSH, TEXT),
Tone::Warn => ("▲", AMBER, TEXT),
Tone::Error => ("✕", RED, TEXT),
Tone::Hint => ("→", MUTED, SOFT),
};
format!(" {} {}", paint(color, glyph), paint(text_color, msg))
}
pub fn print(self, msg: &str) {
if output::json() {
return output::emit("message", &[("level", Val::Str(self.name())), ("text", Val::Str(msg))]);
}
if output::quiet() && !matches!(self, Tone::Warn | Tone::Error) {
return;
}
match self {
Tone::Error => output::err_line(&self.line(msg)),
_ => output::line(&self.line(msg)),
}
}
}
impl Tone {
fn name(self) -> &'static str {
match self {
Tone::Success => "success",
Tone::Info => "info",
Tone::Warn => "warn",
Tone::Error => "error",
Tone::Hint => "hint",
}
}
}
pub fn success(msg: &str) {
Tone::Success.print(msg);
}
pub fn info(msg: &str) {
Tone::Info.print(msg);
}
pub fn warn(msg: &str) {
Tone::Warn.print(msg);
}
pub fn error(msg: &str) {
Tone::Error.print(msg);
}
pub fn hint(msg: &str) {
Tone::Hint.print(msg);
}
pub fn counter(n: usize, total: usize, msg: &str) {
if output::json() {
let fields = [
("n", Val::Num(n as f64)),
("total", Val::Num(total as f64)),
("text", Val::Str(msg)),
];
return output::emit("counter", &fields);
}
if output::quiet() {
return;
}
let w = total.to_string().len();
output::line(&format!(
" {} {}",
paint(MUTED, &format!("{n:>w$}/{total}")),
paint(TEXT, msg)
));
}
pub fn title(text: &str, subtitle: Option<&str>) {
if output::json() {
let sub = subtitle.unwrap_or_default();
return output::emit("title", &[("text", Val::Str(text)), ("subtitle", Val::Str(sub))]);
}
if output::quiet() {
return;
}
let sub = subtitle.map(|s| format!(" {}", paint(SOFT, s))).unwrap_or_default();
output::line(&format!("\n {}{sub}\n", bold(&paint(ROSE, text))));
}
pub fn rule(label: &str) -> String {
let w = content_width().saturating_sub(2);
if label.is_empty() {
return format!(" {}", paint(LINE, &"─".repeat(w)));
}
let rest = w.saturating_sub(text::width(label) + 4);
format!(
" {} {} {}",
paint(LINE, "──"),
paint(SOFT, label),
paint(LINE, &"─".repeat(rest))
)
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Callout {
Note,
Tip,
Warning,
Danger,
}
impl Callout {
fn color(self) -> Rgb {
match self {
Callout::Note => ROSE,
Callout::Tip => SAGE,
Callout::Warning => AMBER,
Callout::Danger => RED,
}
}
}
pub fn callout(kind: Callout, title: &str, body: &str) -> Block {
let bar = paint(kind.color(), "▎");
let mut out = Block::new();
out.push(format!(" {bar} {}", bold(&paint(kind.color(), title))));
for line in text::wrap(body, content_width().saturating_sub(6)) {
out.push(format!(" {bar} {}", paint(SOFT, &line)));
}
out
}
#[derive(Clone, Debug, Default)]
pub struct Report {
title: String,
causes: Vec<String>,
code: Option<crate::layout::Code>,
hint: Option<String>,
}
impl Report {
pub fn new(title: impl Into<String>) -> Self {
Report {
title: title.into(),
..Self::default()
}
}
pub fn from_error(title: impl Into<String>, err: &dyn std::error::Error) -> Self {
let mut report = Report::new(title).cause(err.to_string());
let mut source = err.source();
while let Some(e) = source {
report = report.cause(e.to_string());
source = e.source();
}
report
}
pub fn cause(mut self, cause: impl Into<String>) -> Self {
self.causes.push(cause.into());
self
}
pub fn code(mut self, code: crate::layout::Code) -> Self {
self.code = Some(code);
self
}
pub fn hint(mut self, hint: impl Into<String>) -> Self {
self.hint = Some(hint.into());
self
}
pub fn lines(&self) -> Block {
let mut out = Block::new();
out.push(format!(" {} {}", paint(RED, "✕"), bold(&paint(TEXT, &self.title))));
for (depth, cause) in self.causes.iter().enumerate() {
let indent = " ".repeat(5 + depth * 3);
out.push(format!("{indent}{} {}", paint(LINE, "╰─"), paint(SOFT, cause)));
}
if let Some(code) = &self.code {
for line in code.lines() {
out.push(format!(" {line}"));
}
}
if let Some(h) = &self.hint {
out.push(Tone::Hint.line(h));
}
out
}
pub fn eprint(&self) {
if output::json() {
let hint = self.hint.as_deref().unwrap_or_default();
let code: Vec<String> = self
.code
.iter()
.flat_map(|c| c.lines().plain().lines().map(String::from).collect::<Vec<_>>())
.collect();
let fields = [
("title", Val::Str(&self.title)),
("causes", Val::List(&self.causes)),
("code", Val::List(&code)),
("hint", Val::Str(hint)),
];
return output::emit("error", &fields);
}
self.lines().eprint();
}
}
impl fmt::Display for Report {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.lines().fmt(f)
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::output::{Output, capture, with_output};
#[test]
fn messages_print_as_lines_events_or_not_at_all() {
crate::set_color_mode(crate::ColorMode::None);
let out = capture(|| {
success("built");
info("cached");
warn("slow");
hint("try again");
error("broke");
});
for want in [" ● built\n", " › cached\n", " ▲ slow\n", " → try again\n"] {
assert!(out.contains(want), "{want:?} in {out:?}");
}
assert!(out.contains("broke\n"), "errors go to stderr, and are caught too");
let quiet = capture(|| {
with_output(Output::Quiet, || {
Tone::Success.print("q-msg");
Tone::Warn.print("q-msg");
})
});
assert!(quiet.contains("▲ q-msg") && !quiet.contains("● q-msg"), "{quiet:?}");
let json = capture(|| with_output(Output::Json, || error("j-msg")));
assert!(
json.contains(r#"{"event":"message","level":"error","text":"j-msg"}"#),
"{json}"
);
}
#[test]
fn counters_and_titles() {
crate::set_color_mode(crate::ColorMode::None);
let out = capture(|| {
counter(2, 10, "c-pretty");
title("t-pretty", Some("sub"));
title("t-bare", None);
});
assert!(out.contains(" 2/10 c-pretty\n"), "{out:?}");
assert!(
out.contains("\n t-pretty sub\n\n") && out.contains("\n t-bare\n\n"),
"{out:?}"
);
let quiet = capture(|| {
with_output(Output::Quiet, || {
counter(1, 2, "c-quiet");
title("t-quiet", None);
})
});
assert!(!quiet.contains("c-quiet") && !quiet.contains("t-quiet"));
let json = capture(|| {
with_output(Output::Json, || {
counter(1, 2, "c-json");
title("t-json", None);
})
});
assert!(
json.contains(r#"{"event":"counter","n":1,"total":2,"text":"c-json"}"#),
"{json}"
);
assert!(
json.contains(r#"{"event":"title","text":"t-json","subtitle":""}"#),
"{json}"
);
}
#[test]
fn a_rule_without_a_label_is_just_a_line() {
crate::set_color_mode(crate::ColorMode::None);
let line = rule("");
assert!(line.trim().chars().all(|c| c == '─') && line.len() > 10);
}
#[test]
fn every_callout_has_its_colour() {
let colours: Vec<Rgb> = [Callout::Note, Callout::Tip, Callout::Warning, Callout::Danger]
.iter()
.map(|c| c.color())
.collect();
assert_eq!(colours, [ROSE, SAGE, AMBER, RED]);
}
#[derive(Debug)]
struct Wrapped(std::io::Error);
impl fmt::Display for Wrapped {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("couldn't read the config")
}
}
impl std::error::Error for Wrapped {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
Some(&self.0)
}
}
#[test]
fn reports_follow_the_whole_chain_and_show_code() {
crate::set_color_mode(crate::ColorMode::None);
let report = Report::from_error("couldn't start", &Wrapped(std::io::Error::other("disk full")))
.code(crate::layout::Code::around("port = x\n", 1, 0).caret(8, 1, "here"));
let text = report.to_string();
assert!(
text.contains("╰─ couldn't read the config") && text.contains("╰─ disk full"),
"{text}"
);
assert!(text.contains("port = x") && text.contains("here"), "{text}");
report.eprint();
let json = capture(|| with_output(Output::Json, || report.clone().hint("fix it").eprint()));
assert!(json.contains(r#""event":"error","title":"couldn't start""#), "{json}");
assert!(
json.contains(r#""hint":"fix it""#) && json.contains("port = x"),
"{json}"
);
}
#[test]
fn a_rule_carries_its_label() {
crate::set_color_mode(crate::ColorMode::None);
assert!(rule("build").starts_with(" ── build ──"));
}
#[test]
fn every_level_is_named_in_events() {
let json = capture(|| {
with_output(Output::Json, || {
for print in [success, info, warn, hint] {
print("x");
}
})
});
for level in ["success", "info", "warn", "hint"] {
assert!(json.contains(&format!(r#""level":"{level}""#)), "{level} in {json}");
}
}
}