Skip to content

Commit

Permalink
Merge pull request #28 from epage/debug
Browse files Browse the repository at this point in the history
Color debug output
  • Loading branch information
epage authored Nov 24, 2021
2 parents e6fca54 + 4b72d3c commit af76c21
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 35 deletions.
9 changes: 7 additions & 2 deletions src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
})
}

Expand Down
75 changes: 53 additions & 22 deletions src/output/fmt.rs
Original file line number Diff line number Diff line change
@@ -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<Color>)>,
pieces: Vec<(String, Style)>,
}

impl Colorizer {
Expand All @@ -37,22 +24,28 @@ impl Colorizer {

#[inline]
pub(crate) fn good(&mut self, msg: impl Into<String>) {
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<String>) {
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<String>) {
self.pieces.push((msg.into(), Some(Color::Red)));
self.pieces.push((msg.into(), Style::Error));
}

#[inline]
#[allow(dead_code)]
pub(crate) fn hint(&mut self, msg: impl Into<String>) {
self.pieces.push((msg.into(), Style::Hint));
}

#[inline]
pub(crate) fn none(&mut self, msg: impl Into<String>) {
self.pieces.push((msg.into(), None));
self.pieces.push((msg.into(), Style::Default));
}
}

Expand All @@ -78,9 +71,21 @@ 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::Hint => {
color.set_dimmed(true);
}
Style::Default => {}
}

buffer.set_color(&color)?;
Expand Down Expand Up @@ -117,3 +122,29 @@ impl Display for Colorizer {
Ok(())
}
}

#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum Style {
Good,
Warning,
Error,
Hint,
Default,
}

impl Default for Style {
fn default() -> Self {
Self::Default
}
}

#[cfg(feature = "color")]
fn is_a_tty(stderr: bool) -> bool {
let stream = if stderr {
atty::Stream::Stderr
} else {
atty::Stream::Stdout
};

atty::is(stream)
}
11 changes: 0 additions & 11 deletions src/util/color.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}

0 comments on commit af76c21

Please sign in to comment.