2016-09-05 19:29:40 +00:00
|
|
|
#[cfg(all(feature = "color", not(target_os = "windows")))]
|
|
|
|
use ansi_term::ANSIString;
|
2015-05-22 22:17:57 +00:00
|
|
|
|
2015-07-18 18:31:09 +00:00
|
|
|
#[cfg(all(feature = "color", not(target_os = "windows")))]
|
2015-10-28 14:23:59 +00:00
|
|
|
use ansi_term::Colour::{Green, Red, Yellow};
|
2015-05-22 22:17:57 +00:00
|
|
|
|
2016-05-30 08:07:44 +00:00
|
|
|
#[cfg(feature = "color")]
|
2016-09-05 21:03:45 +00:00
|
|
|
use libc;
|
|
|
|
use std::fmt;
|
2016-05-30 08:07:44 +00:00
|
|
|
|
2016-06-06 22:43:35 +00:00
|
|
|
#[cfg(all(feature = "color", not(target_os = "windows")))]
|
2016-05-30 08:07:44 +00:00
|
|
|
const STDERR: i32 = libc::STDERR_FILENO;
|
2016-06-06 22:43:35 +00:00
|
|
|
#[cfg(all(feature = "color", not(target_os = "windows")))]
|
2016-05-30 08:07:44 +00:00
|
|
|
const STDOUT: i32 = libc::STDOUT_FILENO;
|
|
|
|
|
2016-10-05 17:32:59 +00:00
|
|
|
#[cfg(target_os = "windows")]
|
2016-06-06 22:43:35 +00:00
|
|
|
const STDERR: i32 = 0;
|
2016-10-05 17:32:59 +00:00
|
|
|
#[cfg(target_os = "windows")]
|
2016-06-06 22:43:35 +00:00
|
|
|
const STDOUT: i32 = 0;
|
|
|
|
|
2016-05-30 08:07:44 +00:00
|
|
|
#[doc(hidden)]
|
|
|
|
#[derive(Debug, Copy, Clone, PartialEq)]
|
|
|
|
pub enum ColorWhen {
|
|
|
|
Auto,
|
|
|
|
Always,
|
2016-09-05 19:29:40 +00:00
|
|
|
Never,
|
2016-05-30 08:07:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(feature = "color")]
|
|
|
|
pub fn is_a_tty(stderr: bool) -> bool {
|
2016-12-29 19:07:04 +00:00
|
|
|
debugln!("is_a_tty: stderr={:?}", stderr);
|
2016-05-30 08:07:44 +00:00
|
|
|
let fd = if stderr { STDERR } else { STDOUT };
|
|
|
|
unsafe { libc::isatty(fd) != 0 }
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(not(feature = "color"))]
|
|
|
|
pub fn is_a_tty(_: bool) -> bool {
|
2016-12-29 19:07:04 +00:00
|
|
|
debugln!("is_a_tty;");
|
2016-05-30 08:07:44 +00:00
|
|
|
false
|
|
|
|
}
|
|
|
|
|
|
|
|
#[doc(hidden)]
|
|
|
|
pub struct Colorizer {
|
|
|
|
pub use_stderr: bool,
|
2016-09-05 19:29:40 +00:00
|
|
|
pub when: ColorWhen,
|
2016-05-30 08:07:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! color {
|
|
|
|
($_self:ident, $c:ident, $m:expr) => {
|
|
|
|
match $_self.when {
|
|
|
|
ColorWhen::Auto => if is_a_tty($_self.use_stderr) {
|
|
|
|
Format::$c($m)
|
|
|
|
} else {
|
|
|
|
Format::None($m)
|
|
|
|
},
|
|
|
|
ColorWhen::Always => Format::$c($m),
|
|
|
|
ColorWhen::Never => Format::None($m),
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Colorizer {
|
2016-09-05 19:29:40 +00:00
|
|
|
pub fn good<T>(&self, msg: T) -> Format<T>
|
|
|
|
where T: fmt::Display + AsRef<str>
|
|
|
|
{
|
2016-12-29 19:07:04 +00:00
|
|
|
debugln!("Colorizer::good;");
|
2016-05-30 08:07:44 +00:00
|
|
|
color!(self, Good, msg)
|
|
|
|
}
|
|
|
|
|
2016-09-05 19:29:40 +00:00
|
|
|
pub fn warning<T>(&self, msg: T) -> Format<T>
|
|
|
|
where T: fmt::Display + AsRef<str>
|
|
|
|
{
|
2016-12-29 19:07:04 +00:00
|
|
|
debugln!("Colorizer::warning;");
|
2016-05-30 08:07:44 +00:00
|
|
|
color!(self, Warning, msg)
|
|
|
|
}
|
|
|
|
|
2016-09-05 19:29:40 +00:00
|
|
|
pub fn error<T>(&self, msg: T) -> Format<T>
|
|
|
|
where T: fmt::Display + AsRef<str>
|
|
|
|
{
|
2016-12-29 19:07:04 +00:00
|
|
|
debugln!("Colorizer::error;");
|
2016-05-30 08:07:44 +00:00
|
|
|
color!(self, Error, msg)
|
|
|
|
}
|
|
|
|
|
2016-09-05 19:29:40 +00:00
|
|
|
pub fn none<T>(&self, msg: T) -> Format<T>
|
|
|
|
where T: fmt::Display + AsRef<str>
|
|
|
|
{
|
2016-12-29 19:07:04 +00:00
|
|
|
debugln!("Colorizer::none;");
|
2016-05-30 08:07:44 +00:00
|
|
|
Format::None(msg)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for Colorizer {
|
|
|
|
fn default() -> Self {
|
|
|
|
Colorizer {
|
|
|
|
use_stderr: true,
|
2016-09-05 19:29:40 +00:00
|
|
|
when: ColorWhen::Auto,
|
2016-05-30 08:07:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-05-22 22:17:57 +00:00
|
|
|
|
2015-10-28 13:57:47 +00:00
|
|
|
/// Defines styles for different types of error messages. Defaults to Error=Red, Warning=Yellow,
|
|
|
|
/// and Good=Green
|
|
|
|
#[derive(Debug)]
|
2016-05-02 16:48:47 +00:00
|
|
|
#[doc(hidden)]
|
2015-05-22 22:17:57 +00:00
|
|
|
pub enum Format<T> {
|
2015-10-28 13:57:47 +00:00
|
|
|
/// Defines the style used for errors, defaults to Red
|
2015-09-07 01:07:46 +00:00
|
|
|
Error(T),
|
2015-10-28 13:57:47 +00:00
|
|
|
/// Defines the style used for warnings, defaults to Yellow
|
2015-09-07 01:07:46 +00:00
|
|
|
Warning(T),
|
2015-10-28 13:57:47 +00:00
|
|
|
/// Defines the style used for good values, defaults to Green
|
2015-09-07 01:07:46 +00:00
|
|
|
Good(T),
|
2016-05-30 08:07:44 +00:00
|
|
|
/// Defines no formatting style
|
|
|
|
None(T),
|
2015-05-22 22:17:57 +00:00
|
|
|
}
|
|
|
|
|
2015-07-18 18:31:09 +00:00
|
|
|
#[cfg(all(feature = "color", not(target_os = "windows")))]
|
2015-05-22 22:17:57 +00:00
|
|
|
impl<T: AsRef<str>> Format<T> {
|
|
|
|
fn format(&self) -> ANSIString {
|
|
|
|
match *self {
|
|
|
|
Format::Error(ref e) => Red.bold().paint(e.as_ref()),
|
|
|
|
Format::Warning(ref e) => Yellow.paint(e.as_ref()),
|
|
|
|
Format::Good(ref e) => Green.paint(e.as_ref()),
|
2016-05-30 08:07:44 +00:00
|
|
|
Format::None(ref e) => ANSIString::from(e.as_ref()),
|
2015-05-22 22:17:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-18 18:31:09 +00:00
|
|
|
#[cfg(any(not(feature = "color"), target_os = "windows"))]
|
2016-10-08 22:08:19 +00:00
|
|
|
#[cfg_attr(feature="lints", allow(match_same_arms))]
|
2015-05-22 22:17:57 +00:00
|
|
|
impl<T: fmt::Display> Format<T> {
|
|
|
|
fn format(&self) -> &T {
|
|
|
|
match *self {
|
|
|
|
Format::Error(ref e) => e,
|
|
|
|
Format::Warning(ref e) => e,
|
|
|
|
Format::Good(ref e) => e,
|
2016-05-30 08:07:44 +00:00
|
|
|
Format::None(ref e) => e,
|
2015-05-22 22:17:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-30 08:07:44 +00:00
|
|
|
|
|
|
|
#[cfg(all(feature = "color", not(target_os = "windows")))]
|
|
|
|
impl<T: AsRef<str>> fmt::Display for Format<T> {
|
2016-11-20 19:41:15 +00:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", &self.format()) }
|
2016-05-30 08:07:44 +00:00
|
|
|
}
|
|
|
|
|
2015-07-18 18:31:09 +00:00
|
|
|
#[cfg(any(not(feature = "color"), target_os = "windows"))]
|
2015-05-22 22:17:57 +00:00
|
|
|
impl<T: fmt::Display> fmt::Display for Format<T> {
|
2016-11-20 19:41:15 +00:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", &self.format()) }
|
2015-05-22 22:17:57 +00:00
|
|
|
}
|
2015-09-04 15:38:48 +00:00
|
|
|
|
2015-12-18 07:24:34 +00:00
|
|
|
#[cfg(all(test, feature = "color", not(target_os = "windows")))]
|
2015-09-04 15:38:48 +00:00
|
|
|
mod test {
|
2016-05-30 08:07:44 +00:00
|
|
|
use ansi_term::ANSIString;
|
2016-09-05 19:29:40 +00:00
|
|
|
use ansi_term::Colour::{Green, Red, Yellow};
|
|
|
|
use super::Format;
|
2015-09-04 15:38:48 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn colored_output() {
|
|
|
|
let err = Format::Error("error");
|
2015-10-28 14:23:59 +00:00
|
|
|
assert_eq!(&*format!("{}", err),
|
|
|
|
&*format!("{}", Red.bold().paint("error")));
|
2015-09-04 15:38:48 +00:00
|
|
|
let good = Format::Good("good");
|
|
|
|
assert_eq!(&*format!("{}", good), &*format!("{}", Green.paint("good")));
|
|
|
|
let warn = Format::Warning("warn");
|
|
|
|
assert_eq!(&*format!("{}", warn), &*format!("{}", Yellow.paint("warn")));
|
2016-05-30 08:07:44 +00:00
|
|
|
let none = Format::None("none");
|
2016-09-05 19:29:40 +00:00
|
|
|
assert_eq!(&*format!("{}", none),
|
|
|
|
&*format!("{}", ANSIString::from("none")));
|
2015-09-04 15:38:48 +00:00
|
|
|
}
|
2015-09-07 01:07:46 +00:00
|
|
|
}
|