2019-02-28 16:19:04 -05:00
|
|
|
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
|
2019-05-30 11:07:58 -04:00
|
|
|
// TODO(ry) Rename this file to colors.rs
|
|
|
|
// TODO(ry) Replace ansi_term with termcolor.
|
2019-06-04 09:03:56 -04:00
|
|
|
use ansi_term::Color::Black;
|
2019-02-18 19:20:07 -05:00
|
|
|
use ansi_term::Color::Fixed;
|
2019-02-07 20:07:20 -05:00
|
|
|
use ansi_term::Color::Red;
|
2019-06-04 09:03:56 -04:00
|
|
|
use ansi_term::Color::White;
|
2019-02-07 20:07:20 -05:00
|
|
|
use ansi_term::Style;
|
|
|
|
use regex::Regex;
|
|
|
|
use std::env;
|
|
|
|
use std::fmt;
|
|
|
|
|
|
|
|
lazy_static! {
|
|
|
|
// STRIP_ANSI_RE and strip_ansi_codes are lifted from the "console" crate.
|
|
|
|
// Copyright 2017 Armin Ronacher <armin.ronacher@active-4.com>. MIT License.
|
|
|
|
static ref STRIP_ANSI_RE: Regex = Regex::new(
|
|
|
|
r"[\x1b\x9b][\[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-PRZcf-nqry=><]"
|
|
|
|
).unwrap();
|
|
|
|
static ref NO_COLOR: bool = {
|
|
|
|
env::var_os("NO_COLOR").is_some()
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Helper function to strip ansi codes.
|
2019-03-14 19:17:52 -04:00
|
|
|
#[cfg(test)]
|
|
|
|
pub fn strip_ansi_codes(s: &str) -> std::borrow::Cow<str> {
|
2019-02-07 20:07:20 -05:00
|
|
|
STRIP_ANSI_RE.replace_all(s, "")
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn use_color() -> bool {
|
2019-02-08 11:14:33 -05:00
|
|
|
!(*NO_COLOR)
|
2019-02-07 20:07:20 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn red_bold(s: String) -> impl fmt::Display {
|
|
|
|
let mut style = Style::new();
|
|
|
|
if use_color() {
|
|
|
|
style = style.bold().fg(Red);
|
|
|
|
}
|
|
|
|
style.paint(s)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn italic_bold(s: String) -> impl fmt::Display {
|
|
|
|
let mut style = Style::new();
|
|
|
|
if use_color() {
|
|
|
|
style = style.italic().bold();
|
|
|
|
}
|
|
|
|
style.paint(s)
|
|
|
|
}
|
|
|
|
|
2019-06-04 09:03:56 -04:00
|
|
|
pub fn black_on_white(s: String) -> impl fmt::Display {
|
|
|
|
let mut style = Style::new();
|
|
|
|
if use_color() {
|
|
|
|
style = style.on(White).fg(Black);
|
|
|
|
}
|
|
|
|
style.paint(s)
|
|
|
|
}
|
|
|
|
|
2019-02-07 20:07:20 -05:00
|
|
|
pub fn yellow(s: String) -> impl fmt::Display {
|
|
|
|
let mut style = Style::new();
|
|
|
|
if use_color() {
|
2019-02-18 19:20:07 -05:00
|
|
|
// matches TypeScript's ForegroundColorEscapeSequences.Yellow
|
|
|
|
style = style.fg(Fixed(11));
|
2019-02-07 20:07:20 -05:00
|
|
|
}
|
|
|
|
style.paint(s)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn cyan(s: String) -> impl fmt::Display {
|
|
|
|
let mut style = Style::new();
|
|
|
|
if use_color() {
|
2019-02-18 19:20:07 -05:00
|
|
|
// matches TypeScript's ForegroundColorEscapeSequences.Cyan
|
|
|
|
style = style.fg(Fixed(14));
|
2019-02-07 20:07:20 -05:00
|
|
|
}
|
|
|
|
style.paint(s)
|
|
|
|
}
|
|
|
|
|
2019-06-04 09:03:56 -04:00
|
|
|
pub fn red(s: String) -> impl fmt::Display {
|
|
|
|
let mut style = Style::new();
|
|
|
|
if use_color() {
|
|
|
|
style = style.fg(Red);
|
|
|
|
}
|
|
|
|
style.paint(s)
|
|
|
|
}
|
|
|
|
|
2019-02-07 20:07:20 -05:00
|
|
|
pub fn bold(s: String) -> impl fmt::Display {
|
|
|
|
let mut style = Style::new();
|
|
|
|
if use_color() {
|
|
|
|
style = style.bold();
|
|
|
|
}
|
|
|
|
style.paint(s)
|
|
|
|
}
|