1#[cfg(all(feature = "color", not(target_os = "windows")))]
2use ansi_term::ANSIString;
3
4#[cfg(all(feature = "color", not(target_os = "windows")))]
5use ansi_term::Colour::{Green, Red, Yellow};
6
7#[cfg(feature = "color")]
8use atty;
9use std::fmt;
10use std::env;
11
12#[doc(hidden)]
13#[derive(Debug, Copy, Clone, PartialEq)]
14pub enum ColorWhen {
15 Auto,
16 Always,
17 Never,
18}
19
20#[cfg(feature = "color")]
21pub fn is_a_tty(stderr: bool) -> bool {
22 debugln!("is_a_tty: stderr={:?}", stderr);
23 let stream = if stderr {
24 atty::Stream::Stderr
25 } else {
26 atty::Stream::Stdout
27 };
28 atty::is(stream)
29}
30
31#[cfg(not(feature = "color"))]
32pub fn is_a_tty(_: bool) -> bool {
33 debugln!("is_a_tty;");
34 false
35}
36
37pub fn is_term_dumb() -> bool { env::var("TERM").ok() == Some(String::from("dumb")) }
38
39#[doc(hidden)]
40pub struct ColorizerOption {
41 pub use_stderr: bool,
42 pub when: ColorWhen,
43}
44
45#[doc(hidden)]
46pub struct Colorizer {
47 when: ColorWhen,
48}
49
50macro_rules! color {
51 ($_self:ident, $c:ident, $m:expr) => {
52 match $_self.when {
53 ColorWhen::Auto => Format::$c($m),
54 ColorWhen::Always => Format::$c($m),
55 ColorWhen::Never => Format::None($m),
56 }
57 };
58}
59
60impl Colorizer {
61 pub fn new(option: ColorizerOption) -> Colorizer {
62 let is_a_tty = is_a_tty(option.use_stderr);
63 let is_term_dumb = is_term_dumb();
64 Colorizer {
65 when: match option.when {
66 ColorWhen::Auto if is_a_tty && !is_term_dumb => ColorWhen::Auto,
67 ColorWhen::Auto => ColorWhen::Never,
68 when => when,
69 }
70 }
71 }
72
73 pub fn good<T>(&self, msg: T) -> Format<T>
74 where
75 T: fmt::Display + AsRef<str>,
76 {
77 debugln!("Colorizer::good;");
78 color!(self, Good, msg)
79 }
80
81 pub fn warning<T>(&self, msg: T) -> Format<T>
82 where
83 T: fmt::Display + AsRef<str>,
84 {
85 debugln!("Colorizer::warning;");
86 color!(self, Warning, msg)
87 }
88
89 pub fn error<T>(&self, msg: T) -> Format<T>
90 where
91 T: fmt::Display + AsRef<str>,
92 {
93 debugln!("Colorizer::error;");
94 color!(self, Error, msg)
95 }
96
97 pub fn none<T>(&self, msg: T) -> Format<T>
98 where
99 T: fmt::Display + AsRef<str>,
100 {
101 debugln!("Colorizer::none;");
102 Format::None(msg)
103 }
104}
105
106impl Default for Colorizer {
107 fn default() -> Self {
108 Colorizer::new(ColorizerOption {
109 use_stderr: true,
110 when: ColorWhen::Auto,
111 })
112 }
113}
114
115#[derive(Debug)]
118#[doc(hidden)]
119pub enum Format<T> {
120 Error(T),
122 Warning(T),
124 Good(T),
126 None(T),
128}
129
130#[cfg(all(feature = "color", not(target_os = "windows")))]
131impl<T: AsRef<str>> Format<T> {
132 fn format(&self) -> ANSIString {
133 match *self {
134 Format::Error(ref e) => Red.bold().paint(e.as_ref()),
135 Format::Warning(ref e) => Yellow.paint(e.as_ref()),
136 Format::Good(ref e) => Green.paint(e.as_ref()),
137 Format::None(ref e) => ANSIString::from(e.as_ref()),
138 }
139 }
140}
141
142#[cfg(any(not(feature = "color"), target_os = "windows"))]
143#[cfg_attr(feature = "lints", allow(match_same_arms))]
144impl<T: fmt::Display> Format<T> {
145 fn format(&self) -> &T {
146 match *self {
147 Format::Error(ref e) => e,
148 Format::Warning(ref e) => e,
149 Format::Good(ref e) => e,
150 Format::None(ref e) => e,
151 }
152 }
153}
154
155
156#[cfg(all(feature = "color", not(target_os = "windows")))]
157impl<T: AsRef<str>> fmt::Display for Format<T> {
158 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", &self.format()) }
159}
160
161#[cfg(any(not(feature = "color"), target_os = "windows"))]
162impl<T: fmt::Display> fmt::Display for Format<T> {
163 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", &self.format()) }
164}
165
166#[cfg(all(test, feature = "color", not(target_os = "windows")))]
167mod test {
168 use ansi_term::ANSIString;
169 use ansi_term::Colour::{Green, Red, Yellow};
170 use super::Format;
171
172 #[test]
173 fn colored_output() {
174 let err = Format::Error("error");
175 assert_eq!(
176 &*format!("{}", err),
177 &*format!("{}", Red.bold().paint("error"))
178 );
179 let good = Format::Good("good");
180 assert_eq!(&*format!("{}", good), &*format!("{}", Green.paint("good")));
181 let warn = Format::Warning("warn");
182 assert_eq!(&*format!("{}", warn), &*format!("{}", Yellow.paint("warn")));
183 let none = Format::None("none");
184 assert_eq!(
185 &*format!("{}", none),
186 &*format!("{}", ANSIString::from("none"))
187 );
188 }
189}