2015-05-22 22:17:57 +00:00
|
|
|
use std::fmt;
|
|
|
|
|
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-07-18 18:31:09 +00:00
|
|
|
#[cfg(all(feature = "color", not(target_os = "windows")))]
|
2015-05-22 22:17:57 +00:00
|
|
|
use ansi_term::ANSIString;
|
|
|
|
|
|
|
|
|
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),
|
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()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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>> fmt::Display for Format<T> {
|
2015-10-28 14:23:59 +00:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
2015-05-22 22:17:57 +00:00
|
|
|
write!(f, "{}", &self.format())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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> Format<T> {
|
|
|
|
fn format(&self) -> &T {
|
|
|
|
match *self {
|
|
|
|
Format::Error(ref e) => e,
|
|
|
|
Format::Warning(ref e) => e,
|
|
|
|
Format::Good(ref e) => e,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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> {
|
2015-10-28 14:23:59 +00:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
2015-05-22 22:17:57 +00:00
|
|
|
write!(f, "{}", &self.format())
|
|
|
|
}
|
|
|
|
}
|
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 {
|
|
|
|
use super::Format;
|
2015-10-28 14:23:59 +00:00
|
|
|
use ansi_term::Colour::{Green, Red, Yellow};
|
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")));
|
|
|
|
}
|
2015-09-07 01:07:46 +00:00
|
|
|
}
|