git_same/output/
printer.rs1use console::style;
2
3#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
5pub enum Verbosity {
6 Quiet = 0,
8 Normal = 1,
10 Verbose = 2,
12 Debug = 3,
14}
15
16impl From<u8> for Verbosity {
17 fn from(v: u8) -> Self {
18 match v {
19 0 => Verbosity::Quiet,
20 1 => Verbosity::Normal,
21 2 => Verbosity::Verbose,
22 _ => Verbosity::Debug,
23 }
24 }
25}
26
27#[derive(Debug, Clone)]
29pub struct Output {
30 verbosity: Verbosity,
31 json: bool,
32}
33
34impl Output {
35 pub fn new(verbosity: Verbosity, json: bool) -> Self {
37 Self { verbosity, json }
38 }
39
40 pub fn quiet() -> Self {
42 Self::new(Verbosity::Quiet, false)
43 }
44
45 pub fn info(&self, msg: &str) {
47 if !self.json && self.verbosity >= Verbosity::Normal {
48 println!("{} {}", style("→").cyan(), msg);
49 }
50 }
51
52 pub fn success(&self, msg: &str) {
54 if !self.json && self.verbosity >= Verbosity::Normal {
55 println!("{} {}", style("✓").green(), msg);
56 }
57 }
58
59 pub fn warn(&self, msg: &str) {
61 if !self.json && self.verbosity >= Verbosity::Normal {
62 eprintln!("{} {}", style("⚠").yellow(), msg);
63 }
64 }
65
66 pub fn error(&self, msg: &str) {
68 if !self.json {
69 eprintln!("{} {}", style("✗").red(), msg);
70 }
71 }
72
73 pub fn plain(&self, msg: &str) {
77 if !self.json && self.verbosity >= Verbosity::Normal {
78 println!("{}", msg);
79 }
80 }
81
82 pub fn verbose(&self, msg: &str) {
84 if !self.json && self.verbosity >= Verbosity::Verbose {
85 println!("{} {}", style("·").dim(), msg);
86 }
87 }
88
89 pub fn debug(&self, msg: &str) {
91 if !self.json && self.verbosity >= Verbosity::Debug {
92 println!("{} {}", style("⋅").dim(), style(msg).dim());
93 }
94 }
95
96 pub fn is_json(&self) -> bool {
98 self.json
99 }
100
101 pub fn verbosity(&self) -> Verbosity {
103 self.verbosity
104 }
105}
106
107impl Default for Output {
108 fn default() -> Self {
109 Self::new(Verbosity::Normal, false)
110 }
111}
112
113pub fn format_count(count: usize, label: &str) -> String {
115 format!("{} {}", style(count).cyan().bold(), label)
116}
117
118pub fn format_success(msg: &str) -> String {
120 format!("{} {}", style("✓").green(), msg)
121}
122
123pub fn format_error(msg: &str) -> String {
125 format!("{} {}", style("✗").red(), msg)
126}
127
128pub fn format_warning(msg: &str) -> String {
130 format!("{} {}", style("⚠").yellow(), msg)
131}
132
133#[cfg(test)]
134#[path = "printer_tests.rs"]
135mod tests;