2019-04-11 15:23:14 -07:00
|
|
|
use crate::common::*;
|
2017-06-01 18:01:35 -07:00
|
|
|
|
2019-04-11 15:23:14 -07:00
|
|
|
use ansi_term::Color::*;
|
|
|
|
use ansi_term::{ANSIGenericString, Prefix, Style, Suffix};
|
|
|
|
use atty::Stream;
|
2017-06-01 18:01:35 -07:00
|
|
|
|
|
|
|
#[derive(Copy, Clone)]
|
|
|
|
pub struct Color {
|
|
|
|
use_color: UseColor,
|
2018-12-08 14:29:41 -08:00
|
|
|
atty: bool,
|
|
|
|
style: Style,
|
2017-06-01 18:01:35 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Color {
|
|
|
|
fn restyle(self, style: Style) -> Color {
|
2018-12-08 14:29:41 -08:00
|
|
|
Color { style, ..self }
|
2017-06-01 18:01:35 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
fn redirect(self, stream: Stream) -> Color {
|
|
|
|
Color {
|
2019-04-15 22:40:02 -07:00
|
|
|
atty: atty::is(stream),
|
2017-06-01 18:01:35 -07:00
|
|
|
..self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn effective_style(&self) -> Style {
|
|
|
|
if self.active() {
|
|
|
|
self.style
|
|
|
|
} else {
|
|
|
|
Style::new()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-11 15:23:14 -07:00
|
|
|
pub fn fmt(fmt: &Formatter) -> Color {
|
2017-06-01 18:01:35 -07:00
|
|
|
if fmt.alternate() {
|
|
|
|
Color::always()
|
|
|
|
} else {
|
|
|
|
Color::never()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn auto() -> Color {
|
|
|
|
Color {
|
|
|
|
use_color: UseColor::Auto,
|
|
|
|
..default()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn always() -> Color {
|
|
|
|
Color {
|
|
|
|
use_color: UseColor::Always,
|
|
|
|
..default()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn never() -> Color {
|
|
|
|
Color {
|
|
|
|
use_color: UseColor::Never,
|
|
|
|
..default()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn stderr(self) -> Color {
|
|
|
|
self.redirect(Stream::Stderr)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn stdout(self) -> Color {
|
|
|
|
self.redirect(Stream::Stdout)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn doc(self) -> Color {
|
|
|
|
self.restyle(Style::new().fg(Blue))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn error(self) -> Color {
|
|
|
|
self.restyle(Style::new().fg(Red).bold())
|
|
|
|
}
|
|
|
|
|
2019-04-18 11:48:02 -07:00
|
|
|
pub fn warning(self) -> Color {
|
|
|
|
self.restyle(Style::new().fg(Yellow).bold())
|
|
|
|
}
|
|
|
|
|
2017-06-01 18:01:35 -07:00
|
|
|
pub fn banner(self) -> Color {
|
|
|
|
self.restyle(Style::new().fg(Cyan).bold())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn command(self) -> Color {
|
|
|
|
self.restyle(Style::new().bold())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn parameter(self) -> Color {
|
|
|
|
self.restyle(Style::new().fg(Cyan))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn message(self) -> Color {
|
|
|
|
self.restyle(Style::new().bold())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn annotation(self) -> Color {
|
|
|
|
self.restyle(Style::new().fg(Purple))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn string(self) -> Color {
|
|
|
|
self.restyle(Style::new().fg(Green))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn active(&self) -> bool {
|
|
|
|
match self.use_color {
|
|
|
|
UseColor::Always => true,
|
2018-12-08 14:29:41 -08:00
|
|
|
UseColor::Never => false,
|
|
|
|
UseColor::Auto => self.atty,
|
2017-06-01 18:01:35 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn paint<'a>(&self, text: &'a str) -> ANSIGenericString<'a, str> {
|
|
|
|
self.effective_style().paint(text)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn prefix(&self) -> Prefix {
|
|
|
|
self.effective_style().prefix()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn suffix(&self) -> Suffix {
|
|
|
|
self.effective_style().suffix()
|
|
|
|
}
|
|
|
|
}
|
2019-04-15 22:40:02 -07:00
|
|
|
|
|
|
|
impl Default for Color {
|
|
|
|
fn default() -> Color {
|
|
|
|
Color {
|
|
|
|
use_color: UseColor::Never,
|
|
|
|
atty: false,
|
|
|
|
style: Style::new(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|