aboutsummaryrefslogtreecommitdiffhomepage
path: root/crates/mozart/src/console.rs
blob: 07eaf67f8720f665a19d231dd79c9a09efb47343 (plain)
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
use colored::{ColoredString, Colorize};
use dialoguer::{Confirm, Input};

/// `<info>` — green foreground
pub fn info(message: &str) -> ColoredString {
    message.green()
}

/// `<comment>` — yellow foreground
pub fn comment(message: &str) -> ColoredString {
    message.yellow()
}

/// `<error>` — white on red
pub fn error(message: &str) -> ColoredString {
    message.white().on_red()
}

/// `<question>` — black on cyan
pub fn question(message: &str) -> ColoredString {
    message.black().on_cyan()
}

/// `<highlight>` — red foreground (Composer extension)
pub fn highlight(message: &str) -> ColoredString {
    message.red()
}

/// `<warning>` — black on yellow (Composer extension)
pub fn warning(message: &str) -> ColoredString {
    message.black().on_yellow()
}

pub struct Console {
    pub interactive: bool,
    pub quiet: bool,
}

impl Console {
    pub fn new(no_interaction: bool, quiet: bool) -> Self {
        Self {
            interactive: !no_interaction,
            quiet,
        }
    }

    pub fn info(&self, msg: &str) {
        if !self.quiet {
            eprintln!("{msg}");
        }
    }

    pub fn error(&self, msg: &str) {
        eprintln!("{}", console::error(msg));
    }

    pub fn ask(&self, prompt: &str, default: &str) -> String {
        if !self.interactive {
            return default.to_string();
        }

        Input::new()
            .with_prompt(prompt)
            .default(default.to_string())
            .allow_empty(true)
            .interact_text()
            .unwrap_or_else(|_| default.to_string())
    }

    pub fn ask_validated<F>(
        &self,
        prompt: &str,
        default: &str,
        validator: F,
    ) -> Result<String, String>
    where
        F: Fn(&str) -> Result<(), String>,
    {
        if !self.interactive {
            validator(default)?;
            return Ok(default.to_string());
        }

        loop {
            let input: String = Input::new()
                .with_prompt(prompt)
                .default(default.to_string())
                .allow_empty(true)
                .interact_text()
                .unwrap_or_else(|_| default.to_string());

            match validator(&input) {
                Ok(()) => return Ok(input),
                Err(e) => {
                    self.error(&e);
                }
            }
        }
    }

    pub fn confirm(&self, prompt: &str) -> bool {
        if !self.interactive {
            return true;
        }

        Confirm::new()
            .with_prompt(prompt)
            .default(true)
            .interact()
            .unwrap_or(true)
    }
}