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
// use std::io::{self, stdin, stdout, Write};

use colored::Colorize;

enum LogKind {
    Success,
    Error,
    Warn,
    Info,
}

/// Adds a "start line pattern" at the start of every line in
/// the given text.
pub fn with_start_line<T, L>(text: T, line_start: L) -> String
where
    T: AsRef<str>,
    L: AsRef<str>,
{
    format!(
        "{} {}",
        line_start.as_ref(),
        text.as_ref()
            .replace("\n", &["\n", line_start.as_ref(), " "].concat())
    )
}

fn format_message<M>(message: M, kind: LogKind) -> String
where
    M: AsRef<str>,
{
    if supports_color::on(supports_color::Stream::Stdout).is_some() && cfg!(target_family = "unix")
    {
        with_start_line(
            message,
            match kind {
                LogKind::Success => " ".on_bright_green(),
                LogKind::Error => " ".on_bright_red(),
                LogKind::Warn => " ".on_bright_yellow(),
                LogKind::Info => " ".on_white(),
            }
            .to_string(),
        )
    } else {
        message.as_ref().to_string()
    }
}

pub fn format_info<M>(message: M) -> String
where
    M: AsRef<str>,
{
    format_message(message, LogKind::Info)
}

pub fn println_info<M>(message: M)
where
    M: AsRef<str>,
{
    println!("{}", format_info(message));
}

pub fn format_success<M>(message: M) -> String
where
    M: AsRef<str>,
{
    format_message(message, LogKind::Success)
}

pub fn println_success<M>(message: M)
where
    M: AsRef<str>,
{
    println!("{}", format_success(message))
}

pub fn format_error<M>(message: M) -> String
where
    M: AsRef<str>,
{
    format_message(message, LogKind::Error)
}

pub fn println_error<M>(message: M)
where
    M: AsRef<str>,
{
    println!("{}", format_error(message))
}

pub fn format_warn<M>(message: M) -> String
where
    M: AsRef<str>,
{
    format_message(message, LogKind::Warn)
}

pub fn println_warn<M>(message: M)
where
    M: AsRef<str>,
{
    println!("{}", format_warn(message))
}

// pub fn prompt<M>(message: M) -> io::Result<String>
// where
//     M: AsRef<str>,
// {
//     print!("{}", format_info(message));
//     stdout().flush()?;
//     let mut out = String::new();
//     stdin().read_line(&mut out)?;
//     Ok(out)
// }