Debian Codegraph @

fuzz/uufuzz/src/pretty_print.rs

raw
1 // This file is part of the uutils coreutils package.
2 //
3 // For the full copyright and license information, please view the LICENSE
4 // file that was distributed with this source code.
5
6 use std::fmt;
7
8 use console::{Style, style};
9 use similar::TextDiff;
10
11 pub fn print_section<S: fmt::Display>(s: S) {
12 println!("{}", style(format!("=== {s}")).bold());
13 }
14
15 pub fn print_subsection<S: fmt::Display>(s: S) {
16 println!("{}", style(format!("--- {s}")).bright());
17 }
18
19 #[allow(dead_code)]
20 pub fn print_test_begin<S: fmt::Display>(msg: S) {
21 println!(
22 "{} {} {}",
23 style("===").bold(), // Kind of gray
24 style("TEST").black().on_yellow().bold(),
25 style(msg).bold()
26 );
27 }
28
29 pub fn print_end_with_status<S: fmt::Display>(msg: S, ok: bool) {
30 let ok = if ok {
31 style(" OK ").black().on_green().bold()
32 } else {
33 style(" KO ").black().on_red().bold()
34 };
35
36 println!(
37 "{} {ok} {}",
38 style("===").bold(), // Kind of gray
39 style(msg).bold()
40 );
41 }
42
43 pub fn print_or_empty(s: &str) {
44 let to_print = if s.is_empty() { "(empty)" } else { s };
45
46 println!("{}", style(to_print).dim());
47 }
48
49 pub fn print_with_style<S: fmt::Display>(msg: S, style: Style) {
50 println!("{}", style.apply_to(msg));
51 }
52
53 pub fn print_diff(got: &str, expected: &str) {
54 let diff = TextDiff::from_lines(got, expected);
55
56 print_subsection("START diff");
57
58 for change in diff.iter_all_changes() {
59 let (sign, style) = match change.tag() {
60 similar::ChangeTag::Equal => (" ", Style::new().dim()),
61 similar::ChangeTag::Delete => ("-", Style::new().red()),
62 similar::ChangeTag::Insert => ("+", Style::new().green()),
63 };
64 print!("{}{}", style.apply_to(sign).bold(), style.apply_to(change));
65 }
66
67 print_subsection("END diff");
68 println!();
69 }