Skip to content

Commit

Permalink
fix(color): disable when color feature missing
Browse files Browse the repository at this point in the history
This commit fixes the added color customization code so that it's
properly disabled if the color feature is missing.

It also updates the debug macro to use the updated signature for
Colorizer::new.

Signed-off-by: Alecto Irene Perez <[email protected]>
  • Loading branch information
codeinred committed Feb 5, 2022
1 parent f9f1dd7 commit 53a69b9
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 28 deletions.
24 changes: 16 additions & 8 deletions src/build/app/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand All @@ -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};
Expand Down Expand Up @@ -1335,17 +1340,20 @@ 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();
/// ```
#[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
}
Expand Down Expand Up @@ -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")]
Expand All @@ -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")]
Expand Down
6 changes: 3 additions & 3 deletions src/error/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -519,7 +519,7 @@ impl Error {
val,
err,
ColorChoice::Never,
StyleSpec::empty(),
StyleSpec::new(),
false,
);
match &mut err.inner.message {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -764,7 +764,7 @@ impl Message {
fn formatted(&self) -> Cow<Colorizer> {
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)
}
Expand Down
5 changes: 4 additions & 1 deletion src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
65 changes: 49 additions & 16 deletions src/output/fmt.rs
Original file line number Diff line number Diff line change
@@ -1,30 +1,50 @@
use crate::util::color::ColorChoice;

#[cfg(feature = "color")]
use termcolor::{Color, ColorSpec};

use std::{
fmt::{self, Display, Formatter},
io::{self, Write},
};

#[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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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();
Expand Down Expand Up @@ -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)
}

Expand Down

0 comments on commit 53a69b9

Please sign in to comment.