Skip to main content

cli_ui/prompt/
log.rs

1//! Framed log messages — ports `@clack/prompts` `log.{info,warn,error,success,step,message}`.
2//!
3//! Each call prints to stderr, prefixed with `│` (a connector bar) so consecutive
4//! `log.*` calls render as a single connected column matching clack's frame style.
5
6use crate::styles::{paint, DIM, WHITE};
7
8const BAR: &str = "│";
9const S_INFO: &str = "●";
10const S_SUCCESS: &str = "◆";
11const S_STEP: &str = "◇";
12const S_WARN: &str = "▲";
13const S_ERROR: &str = "■";
14
15/// Print `message` with `symbol` as the first column for the first line,
16/// and `│` for any additional lines. Adds one blank `│` connector line above.
17pub fn message(msg: impl AsRef<str>, symbol: &str) {
18    let s = msg.as_ref();
19    eprintln!("{}", paint(DIM, BAR));
20    let mut lines = s.lines();
21    if let Some(first) = lines.next() {
22        eprintln!("{}  {}", symbol, paint(WHITE, first));
23    } else {
24        eprintln!("{}", symbol);
25    }
26    for line in lines {
27        if line.is_empty() {
28            eprintln!("{}", paint(DIM, BAR));
29        } else {
30            eprintln!("{}  {}", paint(DIM, BAR), line);
31        }
32    }
33}
34
35/// Accent `●` — neutral info.
36pub fn info(msg: impl AsRef<str>) {
37    message(msg, &paint(super::settings::colors().accent, S_INFO));
38}
39
40/// Success `◆` — successful completion.
41pub fn success(msg: impl AsRef<str>) {
42    message(msg, &paint(super::settings::colors().success, S_SUCCESS));
43}
44
45/// Success `◇` — single step completed (between prompts).
46pub fn step(msg: impl AsRef<str>) {
47    message(msg, &paint(super::settings::colors().success, S_STEP));
48}
49
50/// Error palette `▲` — warning.
51pub fn warn(msg: impl AsRef<str>) {
52    message(msg, &paint(super::settings::colors().error, S_WARN));
53}
54
55/// Alias for [`warn`].
56pub fn warning(msg: impl AsRef<str>) {
57    warn(msg);
58}
59
60/// Cancel palette `■` — error.
61pub fn error(msg: impl AsRef<str>) {
62    message(msg, &paint(super::settings::colors().cancel, S_ERROR));
63}