diff --git a/src/build/app/mod.rs b/src/build/app/mod.rs index 117aadf68072..04e57429e3dc 100644 --- a/src/build/app/mod.rs +++ b/src/build/app/mod.rs @@ -5,7 +5,13 @@ mod settings; mod tests; pub use self::settings::{AppFlags, AppSettings}; + +#[cfg(feature = "color")] use termcolor::Color; +#[cfg(feature = "color")] +use termcolor::ColorSpec; +#[cfg(feature = "color")] +use crate::output::fmt::Style; // Std use std::{ @@ -28,7 +34,6 @@ use crate::build::{arg::ArgProvider, Arg, ArgGroup, ArgPredicate, ArgSettings}; use crate::error::ErrorKind; use crate::error::Result as ClapResult; use crate::mkeymap::MKeyMap; -use crate::output::fmt::Style; use crate::output::{fmt::Colorizer, fmt::StyleSpec, Help, HelpWriter, Usage}; use crate::parse::{ArgMatcher, ArgMatches, Input, Parser}; use crate::util::{color::ColorChoice, Id, Key}; @@ -1335,9 +1340,12 @@ impl<'help> App<'help> { /// /// ```no_run /// - /// # use clap::{App, Style}; + /// # use clap::{App, Style, Color, ColorSpec}; /// - /// // Do stuff to color + /// let mut color = ColorSpec::new(); + /// + /// color.set_fg(Some(Color::Green)).set_bold(true); + /// /// App::new("myprog") /// .style(Style::Good, color) /// .get_matches(); @@ -1345,7 +1353,7 @@ impl<'help> App<'help> { #[cfg(feature = "color")] #[inline] #[must_use] - pub fn style(mut self, style: Style, spec: termcolor::ColorSpec) -> Self { + pub fn style(mut self, style: Style, spec: ColorSpec) -> Self { self.output_style.set_style(style, spec); self } @@ -1473,11 +1481,11 @@ impl<'help> App<'help> { /// /// ```no_run /// - /// # use clap::{App, Style}; + /// # use clap::{App, Style, Color}; /// /// // Do stuff to color /// App::new("myprog") - /// .style_color(Style::Good, Some(Color::Green)) + /// .foreground(Style::Good, Some(Color::Green)) /// .get_matches(); /// ``` #[cfg(feature = "color")] @@ -1496,11 +1504,11 @@ impl<'help> App<'help> { /// /// ```no_run /// - /// # use clap::{App, Style}; + /// # use clap::{App, Style, Color}; /// /// // Do stuff to color /// App::new("myprog") - /// .style_color(Style::Good, Some(Color::Green)) + /// .background(Style::Good, Some(Color::Green)) /// .get_matches(); /// ``` #[cfg(feature = "color")] diff --git a/src/error/mod.rs b/src/error/mod.rs index f314d6f22a89..04f074e73d8e 100644 --- a/src/error/mod.rs +++ b/src/error/mod.rs @@ -519,7 +519,7 @@ impl Error { val, err, ColorChoice::Never, - StyleSpec::empty(), + StyleSpec::new(), false, ); match &mut err.inner.message { @@ -662,7 +662,7 @@ impl Error { } pub(crate) fn argument_not_found_auto(arg: String) -> Self { - let mut c = Colorizer::new(true, ColorChoice::Never, StyleSpec::empty()); + let mut c = Colorizer::new(true, ColorChoice::Never, StyleSpec::new()); start_error(&mut c, "The argument '"); c.warning(&*arg); @@ -764,7 +764,7 @@ impl Message { fn formatted(&self) -> Cow { match self { Message::Raw(s) => { - let mut c = Colorizer::new(true, ColorChoice::Never, StyleSpec::empty()); + let mut c = Colorizer::new(true, ColorChoice::Never, StyleSpec::new()); start_error(&mut c, s); Cow::Owned(c) } diff --git a/src/macros.rs b/src/macros.rs index a706c28cb026..067d4d808107 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -894,7 +894,10 @@ macro_rules! debug { ($($arg:tt)*) => ({ let prefix = format!("[{:>w$}] \t", module_path!(), w = 28); let body = format!($($arg)*); - let mut color = $crate::output::fmt::Colorizer::new(true, $crate::ColorChoice::Auto); + let mut color = $crate::output::fmt::Colorizer::new( + true, + $crate::ColorChoice::Auto, + $crate::output::fmt::StyleSpec::default()); color.hint(prefix); color.hint(body); color.none("\n"); diff --git a/src/output/fmt.rs b/src/output/fmt.rs index 889b574fb524..55bb3d501504 100644 --- a/src/output/fmt.rs +++ b/src/output/fmt.rs @@ -1,5 +1,8 @@ use crate::util::color::ColorChoice; +#[cfg(feature = "color")] +use termcolor::{Color, ColorSpec}; + use std::{ fmt::{self, Display, Formatter}, io::{self, Write}, @@ -7,24 +10,41 @@ use std::{ #[derive(Clone, Debug, PartialEq, Eq)] pub(crate) struct StyleSpec { - pub good_style: termcolor::ColorSpec, - pub warning_style: termcolor::ColorSpec, - pub error_style: termcolor::ColorSpec, - pub hint_style: termcolor::ColorSpec, - pub default_style: termcolor::ColorSpec, + #[cfg(feature = "color")] + pub good_style: ColorSpec, + + #[cfg(feature = "color")] + pub warning_style: ColorSpec, + + #[cfg(feature = "color")] + pub error_style: ColorSpec, + + #[cfg(feature = "color")] + pub hint_style: ColorSpec, + + #[cfg(feature = "color")] + pub default_style: ColorSpec, } impl StyleSpec { - pub(crate) fn empty() -> StyleSpec { + #[cfg(not(feature = "color"))] + pub(crate) fn new() -> StyleSpec { + StyleSpec {} + } + + #[cfg(feature = "color")] + pub(crate) fn new() -> StyleSpec { StyleSpec { - good_style: termcolor::ColorSpec::new(), - warning_style: termcolor::ColorSpec::new(), - error_style: termcolor::ColorSpec::new(), - hint_style: termcolor::ColorSpec::new(), - default_style: termcolor::ColorSpec::new(), + good_style: ColorSpec::new(), + warning_style: ColorSpec::new(), + error_style: ColorSpec::new(), + hint_style: ColorSpec::new(), + default_style: ColorSpec::new(), } } - pub(crate) fn get_style(&self, style: Style) -> &termcolor::ColorSpec { + + #[cfg(feature = "color")] + pub(crate) fn get_style(&self, style: Style) -> &ColorSpec { match style { Style::Good => &self.good_style, Style::Warning => &self.warning_style, @@ -33,7 +53,9 @@ impl StyleSpec { Style::Default => &self.default_style, } } - pub(crate) fn set_style(&mut self, style: Style, spec: termcolor::ColorSpec) -> &mut Self { + + #[cfg(feature = "color")] + pub(crate) fn set_style(&mut self, style: Style, spec: ColorSpec) -> &mut Self { match style { Style::Good => self.good_style = spec, Style::Warning => self.warning_style = spec, @@ -43,7 +65,9 @@ impl StyleSpec { } self } - pub(crate) fn style(&mut self, style: Style) -> &mut termcolor::ColorSpec { + + #[cfg(feature = "color")] + pub(crate) fn style(&mut self, style: Style) -> &mut ColorSpec { match style { Style::Good => &mut self.good_style, Style::Warning => &mut self.warning_style, @@ -55,8 +79,13 @@ impl StyleSpec { } impl Default for StyleSpec { + #[cfg(not(feature = "color"))] + fn default() -> StyleSpec { + StyleSpec {} + } + + #[cfg(feature = "color")] fn default() -> StyleSpec { - use termcolor::{Color, ColorSpec}; // Declare the styles let mut good_style = ColorSpec::new(); let mut warning_style = ColorSpec::new(); @@ -86,12 +115,16 @@ pub(crate) struct Colorizer { #[allow(unused)] color_when: ColorChoice, pieces: Vec<(String, Style)>, + + // If color is not enabled, then style_spec is never used + #[allow(dead_code)] style_spec: StyleSpec, } impl Colorizer { /// Get the `ColorSpec` used for a particular style - pub(crate) fn spec_for(&self, style: Style) -> &termcolor::ColorSpec { + #[cfg(feature = "color")] + pub(crate) fn spec_for(&self, style: Style) -> &ColorSpec { self.style_spec.get_style(style) }