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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
use std::env;
use std::io::{self, Write};

use console::{Style, Term};
use indicatif::{ProgressBar, ProgressDrawTarget, ProgressStyle};
use lazy_static::lazy_static;

static PB_TICK_INTERVAL_MS: u64 = 50;
static PB_TEMPL_COUNT: &str =
    "{spinner:.green} {prefix} [{elapsed_precise}] [{wide_bar:.cyan/blue}] {pos}/{len} {per_sec} ETA {eta}";
static PB_TEMPL_BYTES: &str =
    "{spinner:.green} {prefix} [{elapsed_precise}] [{wide_bar:.cyan/blue}] \
     {bytes:>9}/{total_bytes:>9} {bytes_per_sec:>11} ETA {eta:>3}";
static PB_PROGRESS_CHARS: &str = "#>-";

#[derive(Debug)]
enum Inner {
    Term(Term),
    Buf(Vec<u8>),
    Sink(io::Sink),
}

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct ConsoleConfig {
    pub assume_yes: bool,
}

impl Default for ConsoleConfig {
    fn default() -> Self {
        Self { assume_yes: false }
    }
}

#[derive(Debug)]
pub struct Console {
    inner: Inner,
    conf: ConsoleConfig,
}

impl Console {
    pub fn term(conf: ConsoleConfig) -> Self {
        Self {
            inner: Inner::Term(Term::stderr()),
            conf,
        }
    }

    pub fn buf(conf: ConsoleConfig) -> Self {
        Self {
            inner: Inner::Buf(Vec::new()),
            conf,
        }
    }

    pub fn sink(conf: ConsoleConfig) -> Self {
        Self {
            inner: Inner::Sink(io::sink()),
            conf,
        }
    }

    pub fn take_buf(self) -> Option<Vec<u8>> {
        match self.inner {
            Inner::Buf(buf) => Some(buf),
            _ => None,
        }
    }

    #[inline(always)]
    fn as_mut_write(&mut self) -> &mut dyn Write {
        match self.inner {
            Inner::Term(ref mut w) => w,
            Inner::Buf(ref mut w) => w,
            Inner::Sink(ref mut w) => w,
        }
    }

    pub fn warn(&mut self, message: &str) -> io::Result<()> {
        writeln!(self, "WARN: {}", message)
    }

    pub fn confirm(&mut self, message: &str, default: bool) -> io::Result<bool> {
        if self.conf.assume_yes {
            return Ok(true);
        }

        let prompt = format!("{} ({}) ", message, if default { "Y/n" } else { "y/N" });
        let input = self.prompt_and_read(&prompt, false)?;
        match input.to_lowercase().as_str() {
            "y" | "yes" => Ok(true),
            "n" | "no" => Ok(false),
            _ => Ok(default),
        }
    }

    pub fn get_env_or_prompt_and_read(
        &mut self,
        env_name: &str,
        prompt: &str,
        is_password: bool,
    ) -> io::Result<String> {
        if let Ok(val) = env::var(env_name) {
            writeln!(
                self,
                "{}{:16} (read from env {})",
                prompt,
                if is_password { "********" } else { &val },
                env_name
            )?;
            return Ok(val);
        };
        self.prompt_and_read(prompt, is_password)
    }

    fn read_user(&mut self, is_password: bool) -> io::Result<String> {
        match &self.inner {
            Inner::Term(term) => {
                if is_password {
                    term.read_secure_line()
                } else {
                    term.read_line()
                }
            }
            _ => Ok(String::from("")),
        }
    }

    fn prompt(&mut self, prompt: &str) -> io::Result<()> {
        write!(self, "{}", prompt)?;
        self.flush()?;
        Ok(())
    }

    fn prompt_and_read(&mut self, prompt: &str, is_password: bool) -> io::Result<String> {
        self.prompt(prompt)?;
        self.read_user(is_password)
    }

    pub fn build_pb_count(&self, len: u64) -> ProgressBar {
        self.build_pb_with(len, PB_TEMPL_COUNT)
    }

    pub fn build_pb_bytes(&self, len: u64) -> ProgressBar {
        self.build_pb_with(len, PB_TEMPL_BYTES)
    }

    fn build_pb_with(&self, len: u64, template: &str) -> ProgressBar {
        let pb = ProgressBar::with_draw_target(len, self.to_pb_target());
        let style = Self::pb_style_common().template(template);
        pb.set_style(style);
        pb.enable_steady_tick(PB_TICK_INTERVAL_MS);
        pb
    }

    fn to_pb_target(&self) -> ProgressDrawTarget {
        match &self.inner {
            Inner::Term(term) => ProgressDrawTarget::to_term(term.clone(), None),
            _ => ProgressDrawTarget::hidden(),
        }
    }

    fn pb_style_common() -> ProgressStyle {
        ProgressStyle::default_bar().progress_chars(PB_PROGRESS_CHARS)
    }
}

impl Write for Console {
    #[inline(always)]
    fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
        self.as_mut_write().write(buf)
    }

    #[inline(always)]
    fn flush(&mut self) -> io::Result<()> {
        self.as_mut_write().flush()
    }
}

macro_rules! def_color {
    ($name:ident, $name_upper:ident, $style:expr) => {
        lazy_static! {
            static ref $name_upper: Style = $style;
        }

        pub fn $name<D>(val: D) -> console::StyledObject<D> {
            crate::console::$name_upper.apply_to(val)
        }
    };
}

def_color!(sty_none, STY_NONE, Style::new());
def_color!(sty_r, STY_R, Style::new().red());
def_color!(sty_g, STY_G, Style::new().green());
def_color!(sty_y, STY_Y, Style::new().yellow());
def_color!(sty_dim, STY_DIM, Style::new().dim());
def_color!(sty_r_under, STY_R_UNDER, Style::new().underlined().red());
def_color!(sty_g_under, STY_G_UNDER, Style::new().underlined().green());
def_color!(sty_y_under, STY_Y_UNDER, Style::new().underlined().yellow());
def_color!(sty_r_rev, STY_R_REV, Style::new().bold().reverse().red());
def_color!(sty_g_rev, STY_G_REV, Style::new().bold().reverse().green());
def_color!(sty_y_rev, STY_Y_REV, Style::new().bold().reverse().yellow());