summaryrefslogtreecommitdiff
path: root/runtime/colors.rs
blob: ff9ee24dbc7dfc7d33639ba23955e9578664a837 (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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.

use once_cell::sync::Lazy;
use std::fmt;
use std::fmt::Write as _;
use std::io::IsTerminal;
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::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);
}

/// A struct that can adapt a `fmt::Write` to a `std::io::Write`. If anything
/// that can not be represented as UTF-8 is written to this writer, it will
/// return an error.
struct StdFmtStdIoWriter<'a>(&'a mut dyn fmt::Write);

impl std::io::Write for StdFmtStdIoWriter<'_> {
  fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
    let str = std::str::from_utf8(buf).map_err(|_| {
      std::io::Error::new(
        std::io::ErrorKind::Other,
        "failed to convert bytes to str",
      )
    })?;
    match self.0.write_str(str) {
      Ok(_) => Ok(buf.len()),
      Err(_) => Err(std::io::Error::new(
        std::io::ErrorKind::Other,
        "failed to write to fmt::Write",
      )),
    }
  }

  fn flush(&mut self) -> std::io::Result<()> {
    Ok(())
  }
}

/// A struct that can adapt a `std::io::Write` to a `fmt::Write`.
struct StdIoStdFmtWriter<'a>(&'a mut dyn std::io::Write);

impl fmt::Write for StdIoStdFmtWriter<'_> {
  fn write_str(&mut self, s: &str) -> fmt::Result {
    self.0.write_all(s.as_bytes()).map_err(|_| fmt::Error)?;
    Ok(())
  }
}

pub struct Style<I: fmt::Display> {
  colorspec: ColorSpec,
  inner: I,
}

impl<I: fmt::Display> fmt::Display for Style<I> {
  fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
    if !use_color() {
      return fmt::Display::fmt(&self.inner, f);
    }
    let mut ansi_writer = Ansi::new(StdFmtStdIoWriter(f));
    ansi_writer
      .set_color(&self.colorspec)
      .map_err(|_| fmt::Error)?;
    write!(StdIoStdFmtWriter(&mut ansi_writer), "{}", self.inner)?;
    ansi_writer.reset().map_err(|_| fmt::Error)?;
    Ok(())
  }
}

#[inline]
fn style<'a, S: fmt::Display + 'a>(s: S, colorspec: ColorSpec) -> Style<S> {
  Style {
    colorspec,
    inner: s,
  }
}

pub fn red_bold<'a, S: fmt::Display + 'a>(s: S) -> Style<S> {
  let mut style_spec = ColorSpec::new();
  style_spec.set_fg(Some(Red)).set_bold(true);
  style(s, style_spec)
}

pub fn green_bold<'a, S: fmt::Display + 'a>(s: S) -> Style<S> {
  let mut style_spec = ColorSpec::new();
  style_spec.set_fg(Some(Green)).set_bold(true);
  style(s, style_spec)
}

pub fn yellow_bold<'a, S: fmt::Display + 'a>(s: S) -> Style<S> {
  let mut style_spec = ColorSpec::new();
  style_spec.set_fg(Some(Yellow)).set_bold(true);
  style(s, style_spec)
}

pub fn italic<'a, S: fmt::Display + 'a>(s: S) -> Style<S> {
  let mut style_spec = ColorSpec::new();
  style_spec.set_italic(true);
  style(s, style_spec)
}

pub fn italic_gray<'a, S: fmt::Display + 'a>(s: S) -> Style<S> {
  let mut style_spec = ColorSpec::new();
  style_spec.set_fg(Some(Ansi256(8))).set_italic(true);
  style(s, style_spec)
}

pub fn italic_bold<'a, S: fmt::Display + 'a>(s: S) -> Style<S> {
  let mut style_spec = ColorSpec::new();
  style_spec.set_bold(true).set_italic(true);
  style(s, style_spec)
}

pub fn white_on_red<'a, S: fmt::Display + 'a>(s: S) -> Style<S> {
  let mut style_spec = ColorSpec::new();
  style_spec.set_bg(Some(Red)).set_fg(Some(White));
  style(s, style_spec)
}

pub fn black_on_green<'a, S: fmt::Display + 'a>(s: S) -> Style<S> {
  let mut style_spec = ColorSpec::new();
  style_spec.set_bg(Some(Green)).set_fg(Some(Black));
  style(s, style_spec)
}

pub fn yellow<'a, S: fmt::Display + 'a>(s: S) -> Style<S> {
  let mut style_spec = ColorSpec::new();
  style_spec.set_fg(Some(Yellow));
  style(s, style_spec)
}

pub fn cyan<'a, S: fmt::Display + 'a>(s: S) -> Style<S> {
  let mut style_spec = ColorSpec::new();
  style_spec.set_fg(Some(Cyan));
  style(s, style_spec)
}

pub fn cyan_with_underline<'a>(
  s: impl fmt::Display + 'a,
) -> impl fmt::Display + 'a {
  let mut style_spec = ColorSpec::new();
  style_spec.set_fg(Some(Cyan)).set_underline(true);
  style(s, style_spec)
}

pub fn cyan_bold<'a, S: fmt::Display + 'a>(s: S) -> Style<S> {
  let mut style_spec = ColorSpec::new();
  style_spec.set_fg(Some(Cyan)).set_bold(true);
  style(s, style_spec)
}

pub fn magenta<'a, S: fmt::Display + 'a>(s: S) -> Style<S> {
  let mut style_spec = ColorSpec::new();
  style_spec.set_fg(Some(Magenta));
  style(s, style_spec)
}

pub fn red<'a, S: fmt::Display + 'a>(s: S) -> Style<S> {
  let mut style_spec = ColorSpec::new();
  style_spec.set_fg(Some(Red));
  style(s, style_spec)
}

pub fn green<'a, S: fmt::Display + 'a>(s: S) -> Style<S> {
  let mut style_spec = ColorSpec::new();
  style_spec.set_fg(Some(Green));
  style(s, style_spec)
}

pub fn bold<'a, S: fmt::Display + 'a>(s: S) -> Style<S> {
  let mut style_spec = ColorSpec::new();
  style_spec.set_bold(true);
  style(s, style_spec)
}

pub fn gray<'a, S: fmt::Display + 'a>(s: S) -> Style<S> {
  let mut style_spec = ColorSpec::new();
  style_spec.set_fg(Some(Ansi256(245)));
  style(s, style_spec)
}

pub fn intense_blue<'a, S: fmt::Display + 'a>(s: S) -> Style<S> {
  let mut style_spec = ColorSpec::new();
  style_spec.set_fg(Some(Blue)).set_intense(true);
  style(s, style_spec)
}

pub fn white_bold_on_red<'a>(
  s: impl fmt::Display + 'a,
) -> impl fmt::Display + 'a {
  let mut style_spec = ColorSpec::new();
  style_spec
    .set_bold(true)
    .set_bg(Some(Red))
    .set_fg(Some(White));
  style(s, style_spec)
}