[go: up one dir, main page]

pub fn colorize_stderr() -> ColorChoice
Expand description

Whether to color logged output

Examples found in repository?
src/util.rs (line 19)
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
pub fn shell_print(status: &str, message: &str, color: Color, justified: bool) -> CargoResult<()> {
    let color_choice = colorize_stderr();
    let mut output = StandardStream::stderr(color_choice);

    output.set_color(ColorSpec::new().set_fg(Some(color)).set_bold(true))?;
    if justified {
        write!(output, "{status:>12}")?;
    } else {
        write!(output, "{}", status)?;
        output.set_color(ColorSpec::new().set_bold(true))?;
        write!(output, ":")?;
    }
    output.reset()?;

    writeln!(output, " {message}").with_context(|| "Failed to write message")?;

    Ok(())
}

/// Print a styled action message.
pub fn shell_status(action: &str, message: &str) -> CargoResult<()> {
    shell_print(action, message, Color::Green, true)
}

/// Print a styled warning message.
pub fn shell_warn(message: &str) -> CargoResult<()> {
    shell_print("warning", message, Color::Yellow, false)
}

/// Print a styled warning message.
pub fn shell_note(message: &str) -> CargoResult<()> {
    shell_print("note", message, Color::Cyan, false)
}

/// Print a part of a line with formatting
pub fn shell_write_stderr(fragment: impl std::fmt::Display, spec: &ColorSpec) -> CargoResult<()> {
    let color_choice = colorize_stderr();
    let mut output = StandardStream::stderr(color_choice);

    output.set_color(spec)?;
    write!(output, "{}", fragment)?;
    output.reset()?;
    Ok(())
}