Skip to content

Commit

Permalink
Remove color and style duplication from colored crate
Browse files Browse the repository at this point in the history
  • Loading branch information
andresovela committed Aug 2, 2023
1 parent 0e317e7 commit 9b7fad4
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 80 deletions.
81 changes: 25 additions & 56 deletions decoder/src/log/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ use nom::{
IResult, Parser,
};

use std::str::FromStr;

#[derive(Debug, PartialEq, Clone)]
#[non_exhaustive]
pub(super) enum LogMetadata {
Expand All @@ -23,22 +25,8 @@ pub(super) enum LogMetadata {

#[derive(Debug, PartialEq, Clone, Copy)]
pub(super) enum LogColor {
Black,
Red,
Green,
Yellow,
Blue,
Magenta,
Cyan,
White,
BrightBlack,
BrightRed,
BrightGreen,
BrightYellow,
BrightBlue,
BrightMagenta,
BrightCyan,
BrightWhite,
/// User-defined color
Color(colored::Color),

/// Color matching the default color for the log level
SeverityLevel,
Expand All @@ -48,15 +36,6 @@ pub(super) enum LogColor {
WarnError,
}

#[derive(Debug, PartialEq, Clone, Copy)]
pub(super) enum LogStyle {
Bold,
Italic,
Underline,
Strikethrough,
Dimmed,
}

#[derive(Debug, PartialEq, Clone, Copy)]
pub(super) enum Alignment {
Center,
Expand All @@ -74,7 +53,7 @@ pub(super) struct LogSegment {
pub(super) struct LogFormat {
pub(super) width: Option<usize>,
pub(super) color: Option<LogColor>,
pub(super) style: Option<LogStyle>,
pub(super) style: Option<colored::Styles>,
pub(super) alignment: Option<Alignment>,
}

Expand All @@ -83,7 +62,7 @@ enum IntermediateOutput {
Metadata(LogMetadata),
WidthAndAlignment((usize, Option<Alignment>)),
Color(LogColor),
Style(LogStyle),
Style(colored::Styles),
}

impl LogSegment {
Expand All @@ -106,7 +85,7 @@ impl LogSegment {
}

#[allow(dead_code)]
const fn with_style(mut self, style: LogStyle) -> Self {
const fn with_style(mut self, style: colored::Styles) -> Self {
self.format.style = Some(style);
self
}
Expand Down Expand Up @@ -141,37 +120,21 @@ fn parse_metadata(input: &str) -> IResult<&str, IntermediateOutput, ()> {

fn parse_color(input: &str) -> IResult<&str, IntermediateOutput, ()> {
let mut parse_type = map_res(take_while(char::is_alphabetic), move |s| match s {
"black" => Ok(IntermediateOutput::Color(LogColor::Black)),
"red" => Ok(IntermediateOutput::Color(LogColor::Red)),
"green" => Ok(IntermediateOutput::Color(LogColor::Green)),
"yellow" => Ok(IntermediateOutput::Color(LogColor::Yellow)),
"blue" => Ok(IntermediateOutput::Color(LogColor::Blue)),
"magenta" => Ok(IntermediateOutput::Color(LogColor::Magenta)),
"cyan" => Ok(IntermediateOutput::Color(LogColor::Cyan)),
"white" => Ok(IntermediateOutput::Color(LogColor::White)),
"brightblack" => Ok(IntermediateOutput::Color(LogColor::BrightBlack)),
"brightred" => Ok(IntermediateOutput::Color(LogColor::BrightRed)),
"brightgreen" => Ok(IntermediateOutput::Color(LogColor::BrightGreen)),
"brightyellow" => Ok(IntermediateOutput::Color(LogColor::BrightYellow)),
"brightblue" => Ok(IntermediateOutput::Color(LogColor::BrightBlue)),
"brightmagenta" => Ok(IntermediateOutput::Color(LogColor::BrightMagenta)),
"brightcyan" => Ok(IntermediateOutput::Color(LogColor::BrightCyan)),
"brightwhite" => Ok(IntermediateOutput::Color(LogColor::BrightWhite)),
"severity" => Ok(IntermediateOutput::Color(LogColor::SeverityLevel)),
"werror" => Ok(IntermediateOutput::Color(LogColor::WarnError)),
_ => Err(()),
s => colored::Color::from_str(s).map(|c| IntermediateOutput::Color(LogColor::Color(c))),
});

parse_type.parse(input)
}

fn parse_style(input: &str) -> IResult<&str, IntermediateOutput, ()> {
let mut parse_type = map_res(take_while(char::is_alphabetic), move |s| match s {
"bold" => Ok(IntermediateOutput::Style(LogStyle::Bold)),
"italic" => Ok(IntermediateOutput::Style(LogStyle::Italic)),
"underline" => Ok(IntermediateOutput::Style(LogStyle::Underline)),
"strike" => Ok(IntermediateOutput::Style(LogStyle::Strikethrough)),
"dimmed" => Ok(IntermediateOutput::Style(LogStyle::Dimmed)),
"bold" => Ok(IntermediateOutput::Style(colored::Styles::Bold)),
"italic" => Ok(IntermediateOutput::Style(colored::Styles::Italic)),
"underline" => Ok(IntermediateOutput::Style(colored::Styles::Underline)),
"strike" => Ok(IntermediateOutput::Style(colored::Styles::Strikethrough)),
"dimmed" => Ok(IntermediateOutput::Style(colored::Styles::Dimmed)),
_ => Err(()),
});

Expand Down Expand Up @@ -342,7 +305,7 @@ mod tests {
let expected_output = LogSegment::new(LogMetadata::Timestamp)
.with_width(8)
.with_alignment(Alignment::Right)
.with_color(LogColor::White);
.with_color(LogColor::Color(colored::Color::White));
assert_eq!(result, Ok(("", expected_output)));
}

Expand Down Expand Up @@ -386,7 +349,13 @@ mod tests {
#[test]
fn test_parse_color() {
let result = parse_color("blue");
assert_eq!(result, Ok(("", IntermediateOutput::Color(LogColor::Blue))));
assert_eq!(
result,
Ok((
"",
IntermediateOutput::Color(LogColor::Color(colored::Color::Blue))
))
);
}

#[test]
Expand All @@ -401,14 +370,14 @@ mod tests {
LogSegment::new(LogMetadata::String(" [".to_string())),
LogSegment::new(LogMetadata::LogLevel)
.with_color(LogColor::SeverityLevel)
.with_style(LogStyle::Bold),
.with_style(colored::Styles::Bold),
LogSegment::new(LogMetadata::String("] ".to_string())),
LogSegment::new(LogMetadata::FileName)
.with_color(LogColor::White)
.with_style(LogStyle::Underline),
.with_color(LogColor::Color(colored::Color::White))
.with_style(colored::Styles::Underline),
LogSegment::new(LogMetadata::String(":".to_string())),
LogSegment::new(LogMetadata::LineNumber)
.with_color(LogColor::White)
.with_color(LogColor::Color(colored::Color::White))
.with_width(3),
LogSegment::new(LogMetadata::String(" ".to_string())),
LogSegment::new(LogMetadata::Log).with_color(LogColor::WarnError),
Expand Down
37 changes: 13 additions & 24 deletions decoder/src/log/stdout_logger.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use colored::{Color, ColoredString, Colorize};
use colored::{Color, ColoredString, Colorize, Styles};
use dissimilar::Chunk;
use log::{Level, Log, Metadata, Record as LogRecord};

Expand All @@ -10,7 +10,7 @@ use std::{
};

use super::{
format::{self, Alignment, LogColor, LogFormat, LogMetadata, LogSegment, LogStyle},
format::{self, Alignment, LogColor, LogFormat, LogMetadata, LogSegment},
DefmtLoggerInfo, DefmtRecord,
};

Expand Down Expand Up @@ -399,22 +399,7 @@ fn apply_color(
) -> ColoredString {
if let Some(log_color) = log_color {
match log_color {
LogColor::Black => s.black(),
LogColor::Red => s.red(),
LogColor::Green => s.green(),
LogColor::Yellow => s.yellow(),
LogColor::Blue => s.blue(),
LogColor::Magenta => s.magenta(),
LogColor::Cyan => s.cyan(),
LogColor::White => s.white(),
LogColor::BrightBlack => s.bright_black(),
LogColor::BrightRed => s.bright_red(),
LogColor::BrightGreen => s.bright_green(),
LogColor::BrightYellow => s.bright_yellow(),
LogColor::BrightBlue => s.bright_blue(),
LogColor::BrightMagenta => s.bright_magenta(),
LogColor::BrightCyan => s.bright_cyan(),
LogColor::BrightWhite => s.bright_white(),
LogColor::Color(color) => s.color(color),
LogColor::SeverityLevel => {
if let Some(level) = level {
let color = color_for_log_level(level);
Expand All @@ -437,14 +422,18 @@ fn apply_color(
}
}

fn apply_style(s: ColoredString, log_style: Option<LogStyle>) -> ColoredString {
fn apply_style(s: ColoredString, log_style: Option<Styles>) -> ColoredString {
if let Some(log_style) = log_style {
match log_style {
LogStyle::Bold => s.bold(),
LogStyle::Italic => s.italic(),
LogStyle::Underline => s.underline(),
LogStyle::Strikethrough => s.strikethrough(),
LogStyle::Dimmed => s.dimmed(),
Styles::Bold => s.bold(),
Styles::Italic => s.italic(),
Styles::Underline => s.underline(),
Styles::Strikethrough => s.strikethrough(),
Styles::Dimmed => s.dimmed(),
Styles::Clear => s.clear(),
Styles::Reversed => s.reversed(),
Styles::Blink => s.blink(),
Styles::Hidden => s.hidden(),
}
} else {
s
Expand Down

0 comments on commit 9b7fad4

Please sign in to comment.