2
0
Fork 0
mirror of https://github.com/clap-rs/clap synced 2024-12-17 16:13:10 +00:00
clap/src/output/fmt.rs

189 lines
4.8 KiB
Rust
Raw Normal View History

2016-09-05 19:29:40 +00:00
#[cfg(all(feature = "color", not(target_os = "windows")))]
use ansi_term::ANSIString;
#[cfg(all(feature = "color", not(target_os = "windows")))]
2015-10-28 14:23:59 +00:00
use ansi_term::Colour::{Green, Red, Yellow};
use std::env;
2018-08-02 03:13:51 +00:00
use std::fmt;
#[derive(Debug, Copy, Clone, PartialEq)]
2020-03-19 07:17:52 +00:00
#[doc(hidden)]
pub enum ColorWhen {
Auto,
Always,
2016-09-05 19:29:40 +00:00
Never,
}
#[cfg(feature = "color")]
2020-03-19 07:17:52 +00:00
pub(crate) fn is_a_tty(stderr: bool) -> bool {
debugln!("is_a_tty: stderr={:?}", stderr);
let stream = if stderr {
atty::Stream::Stderr
} else {
atty::Stream::Stdout
};
atty::is(stream)
}
#[cfg(not(feature = "color"))]
2020-03-19 07:17:52 +00:00
pub(crate) fn is_a_tty(_: bool) -> bool {
debugln!("is_a_tty;");
false
}
2020-03-19 07:17:52 +00:00
pub(crate) fn is_term_dumb() -> bool {
2020-01-31 09:13:44 +00:00
env::var("TERM").ok() == Some(String::from("dumb"))
}
2020-03-19 07:17:52 +00:00
pub(crate) struct ColorizerOption {
pub(crate) use_stderr: bool,
pub(crate) when: ColorWhen,
}
2020-03-19 07:17:52 +00:00
pub(crate) struct Colorizer {
when: ColorWhen,
}
macro_rules! color {
($_self:ident, $c:ident, $m:expr) => {
match $_self.when {
ColorWhen::Auto => Format::$c($m),
ColorWhen::Always => Format::$c($m),
ColorWhen::Never => Format::None($m),
}
};
}
impl Colorizer {
2020-03-19 07:17:52 +00:00
pub(crate) fn new(option: &ColorizerOption) -> Colorizer {
let is_a_tty = is_a_tty(option.use_stderr);
let is_term_dumb = is_term_dumb();
Colorizer {
2017-05-27 16:10:42 +00:00
when: if is_a_tty && !is_term_dumb {
option.when
} else {
ColorWhen::Never
2017-05-27 16:10:42 +00:00
},
}
}
2020-03-19 07:17:52 +00:00
pub(crate) fn good<T>(&self, msg: T) -> Format<T>
2017-11-28 12:30:06 +00:00
where
T: fmt::Display + AsRef<str>,
2016-09-05 19:29:40 +00:00
{
debugln!("Colorizer::good;");
color!(self, Good, msg)
}
2020-03-19 07:17:52 +00:00
pub(crate) fn warning<T>(&self, msg: T) -> Format<T>
2017-11-28 12:30:06 +00:00
where
T: fmt::Display + AsRef<str>,
2016-09-05 19:29:40 +00:00
{
debugln!("Colorizer::warning;");
color!(self, Warning, msg)
}
2020-03-19 07:17:52 +00:00
pub(crate) fn error<T>(&self, msg: T) -> Format<T>
2017-11-28 12:30:06 +00:00
where
T: fmt::Display + AsRef<str>,
2016-09-05 19:29:40 +00:00
{
debugln!("Colorizer::error;");
color!(self, Error, msg)
}
2020-03-19 07:17:52 +00:00
pub(crate) fn none<T>(&self, msg: T) -> Format<T>
2017-11-28 12:30:06 +00:00
where
T: fmt::Display + AsRef<str>,
2016-09-05 19:29:40 +00:00
{
debugln!("Colorizer::none;");
Format::None(msg)
}
}
impl Default for Colorizer {
fn default() -> Self {
2018-11-14 17:12:34 +00:00
Colorizer::new(&ColorizerOption {
2017-05-27 16:10:42 +00:00
use_stderr: true,
when: ColorWhen::Auto,
})
}
}
/// Defines styles for different types of error messages. Defaults to Error=Red, Warning=Yellow,
/// and Good=Green
#[derive(Debug)]
2020-03-19 07:17:52 +00:00
pub(crate) enum Format<T> {
/// Defines the style used for errors, defaults to Red
2015-09-07 01:07:46 +00:00
Error(T),
/// Defines the style used for warnings, defaults to Yellow
2015-09-07 01:07:46 +00:00
Warning(T),
/// Defines the style used for good values, defaults to Green
2015-09-07 01:07:46 +00:00
Good(T),
/// Defines no formatting style
None(T),
}
#[cfg(all(feature = "color", not(target_os = "windows")))]
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()),
Format::None(ref e) => ANSIString::from(e.as_ref()),
}
}
}
#[cfg(any(not(feature = "color"), target_os = "windows"))]
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,
Format::None(ref e) => e,
}
}
}
#[cfg(all(feature = "color", not(target_os = "windows")))]
impl<T: AsRef<str>> fmt::Display for Format<T> {
2020-01-31 09:13:44 +00:00
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", &self.format())
}
}
#[cfg(any(not(feature = "color"), target_os = "windows"))]
impl<T: fmt::Display> fmt::Display for Format<T> {
2020-01-31 09:13:44 +00:00
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", &self.format())
}
}
2015-09-04 15:38:48 +00:00
#[cfg(all(test, feature = "color", not(target_os = "windows")))]
2015-09-04 15:38:48 +00:00
mod test {
2018-08-02 03:13:51 +00:00
use super::Format;
use ansi_term::ANSIString;
2016-09-05 19:29:40 +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");
2017-11-28 12:30:06 +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")));
let none = Format::None("none");
2017-11-28 12:30:06 +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
}