2023-01-02 16:00:42 -05:00
|
|
|
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
|
2021-09-12 12:04:17 -04:00
|
|
|
|
2022-02-28 22:37:50 -05:00
|
|
|
use atty;
|
2021-12-18 16:14:42 -05:00
|
|
|
use once_cell::sync::Lazy;
|
2020-12-13 13:45:53 -05:00
|
|
|
use std::fmt;
|
|
|
|
use std::io::Write;
|
2023-01-14 23:18:58 -05:00
|
|
|
use termcolor::Ansi;
|
|
|
|
use termcolor::Color::Ansi256;
|
|
|
|
use termcolor::Color::Black;
|
|
|
|
use termcolor::Color::Blue;
|
|
|
|
use termcolor::Color::Cyan;
|
|
|
|
use termcolor::Color::Green;
|
|
|
|
use termcolor::Color::Red;
|
|
|
|
use termcolor::Color::White;
|
|
|
|
use termcolor::Color::Yellow;
|
|
|
|
use termcolor::ColorSpec;
|
|
|
|
use termcolor::WriteColor;
|
2020-12-13 13:45:53 -05:00
|
|
|
|
|
|
|
#[cfg(windows)]
|
2023-01-14 23:18:58 -05:00
|
|
|
use termcolor::BufferWriter;
|
|
|
|
#[cfg(windows)]
|
|
|
|
use termcolor::ColorChoice;
|
2020-12-13 13:45:53 -05:00
|
|
|
|
2021-12-18 16:14:42 -05:00
|
|
|
static NO_COLOR: Lazy<bool> =
|
|
|
|
Lazy::new(|| std::env::var_os("NO_COLOR").is_some());
|
2020-12-13 13:45:53 -05:00
|
|
|
|
2022-02-28 22:37:50 -05:00
|
|
|
static IS_TTY: Lazy<bool> = Lazy::new(|| atty::is(atty::Stream::Stdout));
|
|
|
|
|
|
|
|
pub fn is_tty() -> bool {
|
|
|
|
*IS_TTY
|
|
|
|
}
|
|
|
|
|
2020-12-13 13:45:53 -05:00
|
|
|
pub fn use_color() -> bool {
|
2021-12-11 10:23:30 -05:00
|
|
|
!(*NO_COLOR)
|
2020-12-13 13:45:53 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(windows)]
|
|
|
|
pub fn enable_ansi() {
|
|
|
|
BufferWriter::stdout(ColorChoice::AlwaysAnsi);
|
|
|
|
}
|
|
|
|
|
2021-09-12 12:04:17 -04:00
|
|
|
fn style<S: AsRef<str>>(s: S, colorspec: ColorSpec) -> impl fmt::Display {
|
2020-12-13 13:45:53 -05:00
|
|
|
if !use_color() {
|
2021-09-12 12:04:17 -04:00
|
|
|
return String::from(s.as_ref());
|
2020-12-13 13:45:53 -05:00
|
|
|
}
|
|
|
|
let mut v = Vec::new();
|
|
|
|
let mut ansi_writer = Ansi::new(&mut v);
|
|
|
|
ansi_writer.set_color(&colorspec).unwrap();
|
2021-09-12 12:04:17 -04:00
|
|
|
ansi_writer.write_all(s.as_ref().as_bytes()).unwrap();
|
2020-12-13 13:45:53 -05:00
|
|
|
ansi_writer.reset().unwrap();
|
|
|
|
String::from_utf8_lossy(&v).into_owned()
|
|
|
|
}
|
|
|
|
|
2021-09-12 12:04:17 -04:00
|
|
|
pub fn red_bold<S: AsRef<str>>(s: S) -> impl fmt::Display {
|
2020-12-13 13:45:53 -05:00
|
|
|
let mut style_spec = ColorSpec::new();
|
|
|
|
style_spec.set_fg(Some(Red)).set_bold(true);
|
2021-07-30 09:03:41 -04:00
|
|
|
style(s, style_spec)
|
2020-12-13 13:45:53 -05:00
|
|
|
}
|
|
|
|
|
2021-09-12 12:04:17 -04:00
|
|
|
pub fn green_bold<S: AsRef<str>>(s: S) -> impl fmt::Display {
|
2020-12-13 13:45:53 -05:00
|
|
|
let mut style_spec = ColorSpec::new();
|
|
|
|
style_spec.set_fg(Some(Green)).set_bold(true);
|
2021-07-30 09:03:41 -04:00
|
|
|
style(s, style_spec)
|
2020-12-13 13:45:53 -05:00
|
|
|
}
|
|
|
|
|
2021-09-12 12:04:17 -04: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-12-13 13:45:53 -05:00
|
|
|
let mut style_spec = ColorSpec::new();
|
|
|
|
style_spec.set_bold(true).set_italic(true);
|
2021-07-30 09:03:41 -04:00
|
|
|
style(s, style_spec)
|
2020-12-13 13:45:53 -05:00
|
|
|
}
|
|
|
|
|
2021-09-12 12:04:17 -04:00
|
|
|
pub fn white_on_red<S: AsRef<str>>(s: S) -> impl fmt::Display {
|
2020-12-13 13:45:53 -05:00
|
|
|
let mut style_spec = ColorSpec::new();
|
|
|
|
style_spec.set_bg(Some(Red)).set_fg(Some(White));
|
2021-07-30 09:03:41 -04:00
|
|
|
style(s, style_spec)
|
2020-12-13 13:45:53 -05:00
|
|
|
}
|
|
|
|
|
2021-09-12 12:04:17 -04:00
|
|
|
pub fn black_on_green<S: AsRef<str>>(s: S) -> impl fmt::Display {
|
2020-12-13 13:45:53 -05:00
|
|
|
let mut style_spec = ColorSpec::new();
|
|
|
|
style_spec.set_bg(Some(Green)).set_fg(Some(Black));
|
2021-07-30 09:03:41 -04:00
|
|
|
style(s, style_spec)
|
2020-12-13 13:45:53 -05:00
|
|
|
}
|
|
|
|
|
2021-09-12 12:04:17 -04:00
|
|
|
pub fn yellow<S: AsRef<str>>(s: S) -> impl fmt::Display {
|
2020-12-13 13:45:53 -05:00
|
|
|
let mut style_spec = ColorSpec::new();
|
|
|
|
style_spec.set_fg(Some(Yellow));
|
2021-07-30 09:03:41 -04:00
|
|
|
style(s, style_spec)
|
2020-12-13 13:45:53 -05:00
|
|
|
}
|
|
|
|
|
2021-09-12 12:04:17 -04:00
|
|
|
pub fn cyan<S: AsRef<str>>(s: S) -> impl fmt::Display {
|
2020-12-13 13:45:53 -05:00
|
|
|
let mut style_spec = ColorSpec::new();
|
|
|
|
style_spec.set_fg(Some(Cyan));
|
2021-07-30 09:03:41 -04:00
|
|
|
style(s, style_spec)
|
2020-12-13 13:45:53 -05:00
|
|
|
}
|
|
|
|
|
2021-09-12 12:04:17 -04:00
|
|
|
pub fn red<S: AsRef<str>>(s: S) -> impl fmt::Display {
|
2020-12-13 13:45:53 -05:00
|
|
|
let mut style_spec = ColorSpec::new();
|
|
|
|
style_spec.set_fg(Some(Red));
|
2021-07-30 09:03:41 -04:00
|
|
|
style(s, style_spec)
|
2020-12-13 13:45:53 -05:00
|
|
|
}
|
|
|
|
|
2021-09-12 12:04:17 -04:00
|
|
|
pub fn green<S: AsRef<str>>(s: S) -> impl fmt::Display {
|
2020-12-13 13:45:53 -05:00
|
|
|
let mut style_spec = ColorSpec::new();
|
|
|
|
style_spec.set_fg(Some(Green));
|
2021-07-30 09:03:41 -04:00
|
|
|
style(s, style_spec)
|
2020-12-13 13:45:53 -05:00
|
|
|
}
|
|
|
|
|
2021-09-12 12:04:17 -04:00
|
|
|
pub fn bold<S: AsRef<str>>(s: S) -> impl fmt::Display {
|
2020-12-13 13:45:53 -05:00
|
|
|
let mut style_spec = ColorSpec::new();
|
|
|
|
style_spec.set_bold(true);
|
2021-07-30 09:03:41 -04:00
|
|
|
style(s, style_spec)
|
2020-12-13 13:45:53 -05:00
|
|
|
}
|
|
|
|
|
2021-09-12 12:04:17 -04:00
|
|
|
pub fn gray<S: AsRef<str>>(s: S) -> impl fmt::Display {
|
2020-12-13 13:45:53 -05:00
|
|
|
let mut style_spec = ColorSpec::new();
|
2022-04-24 15:00:26 -04:00
|
|
|
style_spec.set_fg(Some(Ansi256(245)));
|
2021-07-30 09:03:41 -04:00
|
|
|
style(s, style_spec)
|
2020-12-13 13:45:53 -05:00
|
|
|
}
|
|
|
|
|
2021-09-12 12:04:17 -04:00
|
|
|
pub fn intense_blue<S: AsRef<str>>(s: S) -> impl fmt::Display {
|
2020-12-13 13:45:53 -05:00
|
|
|
let mut style_spec = ColorSpec::new();
|
|
|
|
style_spec.set_fg(Some(Blue)).set_intense(true);
|
2021-07-30 09:03:41 -04:00
|
|
|
style(s, style_spec)
|
2020-12-13 13:45:53 -05:00
|
|
|
}
|
2022-05-09 04:56:13 -04:00
|
|
|
|
|
|
|
pub fn white_bold_on_red<S: AsRef<str>>(s: S) -> impl fmt::Display {
|
|
|
|
let mut style_spec = ColorSpec::new();
|
|
|
|
style_spec
|
|
|
|
.set_bold(true)
|
|
|
|
.set_bg(Some(Red))
|
|
|
|
.set_fg(Some(White));
|
|
|
|
style(s, style_spec)
|
|
|
|
}
|