2021-01-11 12:13:41 -05:00
|
|
|
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
|
2020-09-05 20:34:02 -04:00
|
|
|
|
2019-02-07 20:07:20 -05:00
|
|
|
use regex::Regex;
|
|
|
|
use std::fmt;
|
2020-02-24 19:30:17 -05:00
|
|
|
use std::io::Write;
|
2020-10-12 07:25:25 -04:00
|
|
|
use termcolor::Color::{Ansi256, Black, Blue, Cyan, Green, Red, White, Yellow};
|
2020-02-24 19:30:17 -05:00
|
|
|
use termcolor::{Ansi, ColorSpec, WriteColor};
|
|
|
|
|
|
|
|
#[cfg(windows)]
|
|
|
|
use termcolor::{BufferWriter, ColorChoice};
|
2019-02-07 20:07:20 -05:00
|
|
|
|
2021-03-26 12:34:25 -04:00
|
|
|
lazy_static::lazy_static! {
|
2020-02-24 19:30:17 -05:00
|
|
|
// 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 = {
|
2021-03-26 12:34:25 -04:00
|
|
|
std::env::var_os("NO_COLOR").is_some()
|
2020-02-24 19:30:17 -05:00
|
|
|
};
|
2019-02-07 20:07:20 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Helper function to strip ansi codes.
|
2020-10-12 07:25:25 -04:00
|
|
|
#[cfg(test)]
|
2019-03-14 19:17:52 -04:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-02-24 19:30:17 -05:00
|
|
|
#[cfg(windows)]
|
|
|
|
pub fn enable_ansi() {
|
|
|
|
BufferWriter::stdout(ColorChoice::AlwaysAnsi);
|
|
|
|
}
|
|
|
|
|
2021-03-01 06:49:58 -05:00
|
|
|
fn style<S: AsRef<str>>(s: S, colorspec: ColorSpec) -> impl fmt::Display {
|
2020-03-06 08:48:38 -05:00
|
|
|
if !use_color() {
|
2021-03-01 06:49:58 -05:00
|
|
|
return String::from(s.as_ref());
|
2020-03-06 08:48:38 -05:00
|
|
|
}
|
2020-02-24 19:30:17 -05:00
|
|
|
let mut v = Vec::new();
|
|
|
|
let mut ansi_writer = Ansi::new(&mut v);
|
2020-03-06 08:48:38 -05:00
|
|
|
ansi_writer.set_color(&colorspec).unwrap();
|
2021-03-01 06:49:58 -05:00
|
|
|
ansi_writer.write_all(s.as_ref().as_bytes()).unwrap();
|
2020-02-24 19:30:17 -05:00
|
|
|
ansi_writer.reset().unwrap();
|
|
|
|
String::from_utf8_lossy(&v).into_owned()
|
|
|
|
}
|
|
|
|
|
2021-03-01 06:49:58 -05:00
|
|
|
pub fn red_bold<S: AsRef<str>>(s: S) -> impl fmt::Display {
|
2020-02-24 19:30:17 -05:00
|
|
|
let mut style_spec = ColorSpec::new();
|
|
|
|
style_spec.set_fg(Some(Red)).set_bold(true);
|
2021-03-01 06:49:58 -05:00
|
|
|
style(s, style_spec)
|
2019-02-07 20:07:20 -05:00
|
|
|
}
|
|
|
|
|
2021-03-01 06:49:58 -05:00
|
|
|
pub fn green_bold<S: AsRef<str>>(s: S) -> impl fmt::Display {
|
2020-05-23 09:22:08 -04:00
|
|
|
let mut style_spec = ColorSpec::new();
|
2020-09-08 06:04:02 -04:00
|
|
|
style_spec.set_fg(Some(Green)).set_bold(true);
|
2021-03-01 06:49:58 -05:00
|
|
|
style(s, style_spec)
|
2020-05-23 09:22:08 -04:00
|
|
|
}
|
|
|
|
|
2021-03-01 06:49:58 -05:00
|
|
|
pub fn italic<S: AsRef<str>>(s: S) -> impl fmt::Display {
|
|
|
|
let mut style_spec = ColorSpec::new();
|
|
|
|
style_spec.set_italic(true);
|
|
|
|
style(s, style_spec)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn italic_gray<S: AsRef<str>>(s: S) -> impl fmt::Display {
|
|
|
|
let mut style_spec = ColorSpec::new();
|
|
|
|
style_spec.set_fg(Some(Ansi256(8))).set_italic(true);
|
|
|
|
style(s, style_spec)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn italic_bold<S: AsRef<str>>(s: S) -> impl fmt::Display {
|
2020-02-24 19:30:17 -05:00
|
|
|
let mut style_spec = ColorSpec::new();
|
|
|
|
style_spec.set_bold(true).set_italic(true);
|
2021-03-01 06:49:58 -05:00
|
|
|
style(s, style_spec)
|
2019-02-07 20:07:20 -05:00
|
|
|
}
|
|
|
|
|
2021-03-01 06:49:58 -05:00
|
|
|
pub fn white_on_red<S: AsRef<str>>(s: S) -> impl fmt::Display {
|
2020-05-23 09:22:08 -04:00
|
|
|
let mut style_spec = ColorSpec::new();
|
|
|
|
style_spec.set_bg(Some(Red)).set_fg(Some(White));
|
2021-03-01 06:49:58 -05:00
|
|
|
style(s, style_spec)
|
2020-05-23 09:22:08 -04:00
|
|
|
}
|
|
|
|
|
2021-03-01 06:49:58 -05:00
|
|
|
pub fn black_on_green<S: AsRef<str>>(s: S) -> impl fmt::Display {
|
2020-09-17 21:48:08 -04:00
|
|
|
let mut style_spec = ColorSpec::new();
|
|
|
|
style_spec.set_bg(Some(Green)).set_fg(Some(Black));
|
2021-03-01 06:49:58 -05:00
|
|
|
style(s, style_spec)
|
2020-09-17 21:48:08 -04:00
|
|
|
}
|
|
|
|
|
2021-03-01 06:49:58 -05:00
|
|
|
pub fn yellow<S: AsRef<str>>(s: S) -> impl fmt::Display {
|
2020-02-24 19:30:17 -05:00
|
|
|
let mut style_spec = ColorSpec::new();
|
2020-09-08 06:04:02 -04:00
|
|
|
style_spec.set_fg(Some(Yellow));
|
2021-03-01 06:49:58 -05:00
|
|
|
style(s, style_spec)
|
2019-02-07 20:07:20 -05:00
|
|
|
}
|
|
|
|
|
2021-03-01 06:49:58 -05:00
|
|
|
pub fn cyan<S: AsRef<str>>(s: S) -> impl fmt::Display {
|
2020-02-24 19:30:17 -05:00
|
|
|
let mut style_spec = ColorSpec::new();
|
2020-09-08 06:04:02 -04:00
|
|
|
style_spec.set_fg(Some(Cyan));
|
2021-03-01 06:49:58 -05:00
|
|
|
style(s, style_spec)
|
2019-02-07 20:07:20 -05:00
|
|
|
}
|
|
|
|
|
2021-03-01 06:49:58 -05:00
|
|
|
pub fn red<S: AsRef<str>>(s: S) -> impl fmt::Display {
|
2020-02-24 19:30:17 -05:00
|
|
|
let mut style_spec = ColorSpec::new();
|
|
|
|
style_spec.set_fg(Some(Red));
|
2021-03-01 06:49:58 -05:00
|
|
|
style(s, style_spec)
|
2019-06-04 09:03:56 -04:00
|
|
|
}
|
|
|
|
|
2021-03-01 06:49:58 -05:00
|
|
|
pub fn green<S: AsRef<str>>(s: S) -> impl fmt::Display {
|
2020-02-24 19:30:17 -05:00
|
|
|
let mut style_spec = ColorSpec::new();
|
2020-09-08 06:04:02 -04:00
|
|
|
style_spec.set_fg(Some(Green));
|
2021-03-01 06:49:58 -05:00
|
|
|
style(s, style_spec)
|
2020-02-18 10:08:18 -05:00
|
|
|
}
|
|
|
|
|
2021-03-01 06:49:58 -05:00
|
|
|
pub fn bold<S: AsRef<str>>(s: S) -> impl fmt::Display {
|
2020-02-24 19:30:17 -05:00
|
|
|
let mut style_spec = ColorSpec::new();
|
|
|
|
style_spec.set_bold(true);
|
2021-03-01 06:49:58 -05:00
|
|
|
style(s, style_spec)
|
2019-02-07 20:07:20 -05:00
|
|
|
}
|
2020-03-01 16:17:45 -05:00
|
|
|
|
2021-03-01 06:49:58 -05:00
|
|
|
pub fn gray<S: AsRef<str>>(s: S) -> impl fmt::Display {
|
2020-03-01 16:17:45 -05:00
|
|
|
let mut style_spec = ColorSpec::new();
|
|
|
|
style_spec.set_fg(Some(Ansi256(8)));
|
2021-03-01 06:49:58 -05:00
|
|
|
style(s, style_spec)
|
2020-03-01 16:17:45 -05:00
|
|
|
}
|
|
|
|
|
2021-03-01 06:49:58 -05:00
|
|
|
pub fn intense_blue<S: AsRef<str>>(s: S) -> impl fmt::Display {
|
2020-07-12 08:16:33 -04:00
|
|
|
let mut style_spec = ColorSpec::new();
|
|
|
|
style_spec.set_fg(Some(Blue)).set_intense(true);
|
2021-03-01 06:49:58 -05:00
|
|
|
style(s, style_spec)
|
2020-07-12 08:16:33 -04:00
|
|
|
}
|