diff options
author | Ryan Dahl <ry@tinyclouds.org> | 2019-03-19 12:18:05 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-03-19 12:18:05 -0400 |
commit | fa3c35301aa44975776b96c85f200de8eb500c22 (patch) | |
tree | 76f8d5e6f42e1c306a40efd0b80dce18528952f4 /cli/ansi.rs | |
parent | c7d81fa9ff495986675c05e52e13acc9ffc85372 (diff) |
Rename //src/ to //cli/ (#1962)
To better distinguish the deno_core crate from the executable deno,
which will now be called "the cli" internally.
Diffstat (limited to 'cli/ansi.rs')
-rw-r--r-- | cli/ansi.rs | 70 |
1 files changed, 70 insertions, 0 deletions
diff --git a/cli/ansi.rs b/cli/ansi.rs new file mode 100644 index 000000000..95b5e0694 --- /dev/null +++ b/cli/ansi.rs @@ -0,0 +1,70 @@ +// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. +use ansi_term::Color::Fixed; +use ansi_term::Color::Red; +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. +#[cfg(test)] +pub fn strip_ansi_codes(s: &str) -> std::borrow::Cow<str> { + STRIP_ANSI_RE.replace_all(s, "") +} + +pub fn use_color() -> bool { + !(*NO_COLOR) +} + +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) +} + +pub fn yellow(s: String) -> impl fmt::Display { + let mut style = Style::new(); + if use_color() { + // matches TypeScript's ForegroundColorEscapeSequences.Yellow + style = style.fg(Fixed(11)); + } + style.paint(s) +} + +pub fn cyan(s: String) -> impl fmt::Display { + let mut style = Style::new(); + if use_color() { + // matches TypeScript's ForegroundColorEscapeSequences.Cyan + style = style.fg(Fixed(14)); + } + style.paint(s) +} + +pub fn bold(s: String) -> impl fmt::Display { + let mut style = Style::new(); + if use_color() { + style = style.bold(); + } + style.paint(s) +} |