1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-28 18:19:08 -05:00
denoland-deno/runtime/colors.rs

168 lines
4.4 KiB
Rust
Raw Normal View History

// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
use once_cell::sync::Lazy;
use std::fmt;
use std::io::IsTerminal;
use std::io::Write;
use termcolor::Ansi;
use termcolor::Color::Ansi256;
use termcolor::Color::Black;
use termcolor::Color::Blue;
use termcolor::Color::Cyan;
use termcolor::Color::Green;
2023-03-25 10:29:46 -04:00
use termcolor::Color::Magenta;
use termcolor::Color::Red;
use termcolor::Color::White;
use termcolor::Color::Yellow;
use termcolor::ColorSpec;
use termcolor::WriteColor;
#[cfg(windows)]
use termcolor::BufferWriter;
#[cfg(windows)]
use termcolor::ColorChoice;
static NO_COLOR: Lazy<bool> = Lazy::new(|| {
std::env::var_os("NO_COLOR")
.map(|v| !v.is_empty())
.unwrap_or(false)
});
static IS_TTY: Lazy<bool> = Lazy::new(|| std::io::stdout().is_terminal());
pub fn is_tty() -> bool {
*IS_TTY
}
pub fn use_color() -> bool {
!(*NO_COLOR)
}
#[cfg(windows)]
pub fn enable_ansi() {
BufferWriter::stdout(ColorChoice::AlwaysAnsi);
}
fn style<S: AsRef<str>>(s: S, colorspec: ColorSpec) -> impl fmt::Display {
if !use_color() {
return String::from(s.as_ref());
}
let mut v = Vec::new();
let mut ansi_writer = Ansi::new(&mut v);
ansi_writer.set_color(&colorspec).unwrap();
ansi_writer.write_all(s.as_ref().as_bytes()).unwrap();
ansi_writer.reset().unwrap();
String::from_utf8_lossy(&v).into_owned()
}
pub fn red_bold<S: AsRef<str>>(s: S) -> impl fmt::Display {
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)
}
pub fn green_bold<S: AsRef<str>>(s: S) -> impl fmt::Display {
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)
}
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 {
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)
}
pub fn white_on_red<S: AsRef<str>>(s: S) -> impl fmt::Display {
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)
}
pub fn black_on_green<S: AsRef<str>>(s: S) -> impl fmt::Display {
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)
}
pub fn yellow<S: AsRef<str>>(s: S) -> impl fmt::Display {
let mut style_spec = ColorSpec::new();
style_spec.set_fg(Some(Yellow));
2021-07-30 09:03:41 -04:00
style(s, style_spec)
}
pub fn cyan<S: AsRef<str>>(s: S) -> impl fmt::Display {
let mut style_spec = ColorSpec::new();
style_spec.set_fg(Some(Cyan));
2021-07-30 09:03:41 -04:00
style(s, style_spec)
}
pub fn cyan_with_underline<S: AsRef<str>>(s: S) -> impl fmt::Display {
let mut style_spec = ColorSpec::new();
style_spec.set_fg(Some(Cyan)).set_underline(true);
style(s, style_spec)
}
2023-03-25 10:29:46 -04:00
pub fn cyan_bold<S: AsRef<str>>(s: S) -> impl fmt::Display {
let mut style_spec = ColorSpec::new();
style_spec.set_fg(Some(Cyan)).set_bold(true);
style(s, style_spec)
}
pub fn magenta<S: AsRef<str>>(s: S) -> impl fmt::Display {
let mut style_spec = ColorSpec::new();
style_spec.set_fg(Some(Magenta));
style(s, style_spec)
}
pub fn red<S: AsRef<str>>(s: S) -> impl fmt::Display {
let mut style_spec = ColorSpec::new();
style_spec.set_fg(Some(Red));
2021-07-30 09:03:41 -04:00
style(s, style_spec)
}
pub fn green<S: AsRef<str>>(s: S) -> impl fmt::Display {
let mut style_spec = ColorSpec::new();
style_spec.set_fg(Some(Green));
2021-07-30 09:03:41 -04:00
style(s, style_spec)
}
pub fn bold<S: AsRef<str>>(s: S) -> impl fmt::Display {
let mut style_spec = ColorSpec::new();
style_spec.set_bold(true);
2021-07-30 09:03:41 -04:00
style(s, style_spec)
}
pub fn gray<S: AsRef<str>>(s: S) -> impl fmt::Display {
let mut style_spec = ColorSpec::new();
style_spec.set_fg(Some(Ansi256(245)));
2021-07-30 09:03:41 -04:00
style(s, style_spec)
}
pub fn intense_blue<S: AsRef<str>>(s: S) -> impl fmt::Display {
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)
}
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)
}