From 463d75474e246fdeee67cd575929227ca0d793c0 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Wed, 24 Nov 2021 10:49:57 -0600 Subject: [PATCH 1/2] refactor(color): Track style, rather than details This makes it easier for us to compose. Before, we had to infer things like "bold" based on the color. Now we just say "error" and get all of the formatting specific to that. --- src/output/fmt.rs | 67 +++++++++++++++++++++++++++++++---------------- src/util/color.rs | 11 -------- 2 files changed, 45 insertions(+), 33 deletions(-) diff --git a/src/output/fmt.rs b/src/output/fmt.rs index 9c15b1340d6..e7da8919dc5 100644 --- a/src/output/fmt.rs +++ b/src/output/fmt.rs @@ -1,28 +1,15 @@ -use crate::util::color::{Color, ColorChoice}; +use crate::util::color::ColorChoice; use std::{ fmt::{self, Display, Formatter}, io::{self, Write}, }; -#[cfg(feature = "color")] -fn is_a_tty(stderr: bool) -> bool { - debug!("is_a_tty: stderr={:?}", stderr); - - let stream = if stderr { - atty::Stream::Stderr - } else { - atty::Stream::Stdout - }; - - atty::is(stream) -} - #[derive(Clone, Debug)] pub(crate) struct Colorizer { use_stderr: bool, color_when: ColorChoice, - pieces: Vec<(String, Option)>, + pieces: Vec<(String, Style)>, } impl Colorizer { @@ -37,22 +24,22 @@ impl Colorizer { #[inline] pub(crate) fn good(&mut self, msg: impl Into) { - self.pieces.push((msg.into(), Some(Color::Green))); + self.pieces.push((msg.into(), Style::Good)); } #[inline] pub(crate) fn warning(&mut self, msg: impl Into) { - self.pieces.push((msg.into(), Some(Color::Yellow))); + self.pieces.push((msg.into(), Style::Warning)); } #[inline] pub(crate) fn error(&mut self, msg: impl Into) { - self.pieces.push((msg.into(), Some(Color::Red))); + self.pieces.push((msg.into(), Style::Error)); } #[inline] pub(crate) fn none(&mut self, msg: impl Into) { - self.pieces.push((msg.into(), None)); + self.pieces.push((msg.into(), Style::Default)); } } @@ -78,9 +65,18 @@ impl Colorizer { for piece in &self.pieces { let mut color = ColorSpec::new(); - color.set_fg(piece.1); - if piece.1 == Some(Color::Red) { - color.set_bold(true); + match piece.1 { + Style::Good => { + color.set_fg(Some(termcolor::Color::Green)); + } + Style::Warning => { + color.set_fg(Some(termcolor::Color::Yellow)); + } + Style::Error => { + color.set_fg(Some(termcolor::Color::Red)); + color.set_bold(true); + } + Style::Default => {} } buffer.set_color(&color)?; @@ -117,3 +113,30 @@ impl Display for Colorizer { Ok(()) } } + +#[derive(Copy, Clone, Debug, PartialEq, Eq)] +pub enum Style { + Good, + Warning, + Error, + Default, +} + +impl Default for Style { + fn default() -> Self { + Self::Default + } +} + +#[cfg(feature = "color")] +fn is_a_tty(stderr: bool) -> bool { + debug!("is_a_tty: stderr={:?}", stderr); + + let stream = if stderr { + atty::Stream::Stderr + } else { + atty::Stream::Stdout + }; + + atty::is(stream) +} diff --git a/src/util/color.rs b/src/util/color.rs index 41b5cb79df1..272a6c91688 100644 --- a/src/util/color.rs +++ b/src/util/color.rs @@ -60,14 +60,3 @@ impl Default for ColorChoice { Self::Auto } } - -#[cfg(feature = "color")] -pub(crate) use termcolor::Color; - -#[cfg(not(feature = "color"))] -#[derive(Copy, Clone, Debug)] -pub(crate) enum Color { - Green, - Yellow, - Red, -} From 4b72d3cc7f10e3ddadc71b5aa37e87a7c2541962 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Wed, 24 Nov 2021 10:59:16 -0600 Subject: [PATCH 2/2] feat: Color debug output --- src/macros.rs | 9 +++++++-- src/output/fmt.rs | 12 ++++++++++-- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/src/macros.rs b/src/macros.rs index 776431c9b44..9ee3a3a9ff4 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -805,8 +805,13 @@ macro_rules! wlnerr { #[cfg(feature = "debug")] macro_rules! debug { ($($arg:tt)*) => ({ - print!("[{:>w$}] \t", module_path!(), w = 28); - println!($($arg)*); + 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); + color.hint(prefix); + color.hint(body); + color.none("\n"); + let _ = color.print(); }) } diff --git a/src/output/fmt.rs b/src/output/fmt.rs index e7da8919dc5..e4317f799d1 100644 --- a/src/output/fmt.rs +++ b/src/output/fmt.rs @@ -37,6 +37,12 @@ impl Colorizer { self.pieces.push((msg.into(), Style::Error)); } + #[inline] + #[allow(dead_code)] + pub(crate) fn hint(&mut self, msg: impl Into) { + self.pieces.push((msg.into(), Style::Hint)); + } + #[inline] pub(crate) fn none(&mut self, msg: impl Into) { self.pieces.push((msg.into(), Style::Default)); @@ -76,6 +82,9 @@ impl Colorizer { color.set_fg(Some(termcolor::Color::Red)); color.set_bold(true); } + Style::Hint => { + color.set_dimmed(true); + } Style::Default => {} } @@ -119,6 +128,7 @@ pub enum Style { Good, Warning, Error, + Hint, Default, } @@ -130,8 +140,6 @@ impl Default for Style { #[cfg(feature = "color")] fn is_a_tty(stderr: bool) -> bool { - debug!("is_a_tty: stderr={:?}", stderr); - let stream = if stderr { atty::Stream::Stderr } else {