aboutsummaryrefslogtreecommitdiff
path: root/dev/ci/src/res/print.rs
blob: 9d844a60024a5bc11126c8c797dfe606d651bc15 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
use colored::Colorize;
use mingling::config::ErrorOutput;
use mingling::hook::ProgramHook;
use mingling::{Program, macros::program_setup};
use mingling::{StringVec, this};

use crate::ThisProgram;

#[program_setup]
pub fn print_setup(p: &mut Program<ThisProgram>) {
    p.with_resource(CargoError::default());
    p.with_resource(CargoWarn::default());
    p.with_resource(CargoHelp::default());
    p.with_resource(CargoStatus::default());

    p.with_hook(ProgramHook::empty().on_begin::<_, ()>(move |_| {
        let p = this::<ThisProgram>();
        let silence_err = p.stdout_setting.error_output == ErrorOutput::Hide;

        p.modify_res(|r: &mut CargoError| r.silence = silence_err);
        p.modify_res(|r: &mut CargoWarn| r.silence = silence_err);
        p.modify_res(|r: &mut CargoHelp| r.silence = silence_err);
        p.modify_res(|r: &mut CargoStatus| r.silence = silence_err);
    }));
}

#[derive(Default, Clone)]
pub struct CargoError {
    silence: bool,
}

impl MessagePrinter for CargoError {
    fn format(&self, msg: impl Into<StringVec>) -> String {
        format!("{}: {}", "error".bold().bright_red(), msg.into().join(""))
    }

    fn std_mode(&self) -> StandardOutMode {
        if self.silence {
            StandardOutMode::Silence
        } else {
            StandardOutMode::Error
        }
    }
}

#[derive(Default, Clone)]
pub struct CargoWarn {
    silence: bool,
}

impl MessagePrinter for CargoWarn {
    fn format(&self, msg: impl Into<StringVec>) -> String {
        format!("{}: {}", "warning".bright_yellow(), msg.into().join(""))
    }

    fn std_mode(&self) -> StandardOutMode {
        if self.silence {
            StandardOutMode::Silence
        } else {
            StandardOutMode::Error
        }
    }
}

#[derive(Default, Clone)]
pub struct CargoHelp {
    silence: bool,
}

impl MessagePrinter for CargoHelp {
    fn format(&self, msg: impl Into<StringVec>) -> String {
        format!("{}: {}", "help".bright_white(), msg.into().join(""))
    }

    fn std_mode(&self) -> StandardOutMode {
        if self.silence {
            StandardOutMode::Silence
        } else {
            StandardOutMode::Error
        }
    }
}

#[derive(Default, Clone)]
pub struct CargoStatus {
    silence: bool,
}

impl MessagePrinter for CargoStatus {
    fn format(&self, msg: impl Into<StringVec>) -> String {
        let parts: Vec<String> = msg.into().to_vec();
        let first = if parts.is_empty() {
            String::new()
        } else {
            parts[0].trim().to_string()
        };

        let (prefix, content) = if first.is_empty() {
            // Empty: fall back to Info with full message
            ("Info".to_string(), parts.join(" "))
        } else if first.chars().count() == 1 {
            // Single character: prefix is Info, entire message is content
            ("Info".to_string(), parts.join(" "))
        } else if first.chars().count() <= 12 {
            // Single part that is a status prefix (no message after it)
            if parts.len() == 1 {
                ("Info".to_string(), first)
            } else {
                // First part is a status prefix, remaining parts are the message
                let content = parts[1..].join(" ").trim_start().to_string();
                (first, content)
            }
        } else {
            // First part too long: all is message, fall back to Info
            ("Info".to_string(), parts.join(" "))
        };

        let padding = " ".repeat(12usize.saturating_sub(prefix.chars().count()));

        format!(
            "{}{} {}",
            padding,
            prefix.bold().bright_green(),
            content.trim()
        )
    }

    fn std_mode(&self) -> StandardOutMode {
        if self.silence {
            StandardOutMode::Silence
        } else {
            StandardOutMode::Out
        }
    }
}

pub trait MessagePrinter {
    #[doc(hidden)]
    fn println(&self, msg: impl Into<StringVec>) {
        match self.std_mode() {
            StandardOutMode::Out => println!("{}", self.format(msg)),
            StandardOutMode::Error => eprintln!("{}", self.format(msg)),
            StandardOutMode::Silence => {}
        }
    }

    #[doc(hidden)]
    fn print(&self, msg: impl Into<StringVec>) {
        match self.std_mode() {
            StandardOutMode::Out => print!("{}", self.format(msg)),
            StandardOutMode::Error => eprint!("{}", self.format(msg)),
            StandardOutMode::Silence => {}
        }
    }

    /// Formats the message string before output.
    fn format(&self, msg: impl Into<StringVec>) -> String;

    /// Returns the standard output mode (stdout or stderr).
    fn std_mode(&self) -> StandardOutMode;
}

/// Specifies where standard output messages should be directed.
///
/// This enum determines whether messages are printed to stdout, stderr, or suppressed entirely.
#[repr(u8)]
pub enum StandardOutMode {
    /// Print messages to standard output (stdout).
    Out,
    /// Print messages to standard error (stderr).
    Error,
    /// Suppress all output.
    Silence,
}