summaryrefslogtreecommitdiff
path: root/src/ansi.rs
blob: f9cd39ac46d9791d940be7b4787ce7a793ee2332 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// 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::borrow::Cow;
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.
#[allow(dead_code)]
pub fn strip_ansi_codes(s: &str) -> 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)
}