Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Review feedback
  • Loading branch information
bkchr committed Nov 12, 2020
commit a628e4a966f2fdb561a2311d56cac21dc6859fea
2 changes: 0 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion client/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ targets = ["x86_64-unknown-linux-gnu"]
log = "0.4.11"
atty = "0.2.13"
regex = "1.4.2"
lazy_static = "1.4.0"
ansi_term = "0.12.1"
tokio = { version = "0.2.21", features = [ "signal", "rt-core", "rt-threaded", "blocking" ] }
futures = "0.3.4"
Expand Down
27 changes: 18 additions & 9 deletions client/cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ pub fn init_logger(

let subscriber = FmtSubscriber::builder()
.with_env_filter(env_filter)
.with_writer(MaybeColorWriter(enable_color))
.with_writer(MaybeColorWriter::new(enable_color))
.event_format(logging::EventFormat {
timer,
ansi: enable_color,
Expand Down Expand Up @@ -351,18 +351,27 @@ pub fn init_logger(
///
/// This is used by the logging to kill colors when they are disabled.
///
/// If the inner is `false`, the colors will be removed before writing to stderr.
/// If the `enable_colors` is `false`, the colors will be removed before writing to stderr.
#[derive(Clone)]
struct MaybeColorWriter(bool);
struct MaybeColorWriter{
enable_colors: bool,
remove_color_regex: Regex,
}

impl std::io::Write for MaybeColorWriter {
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
lazy_static::lazy_static! {
static ref RE: Regex = Regex::new("\x1b\\[[^m]+m").expect("Error initializing color regex");
impl MaybeColorWriter {
fn new(enable_colors: bool) -> Self {
Self {
enable_colors,
remove_color_regex: Regex::new("\x1b\\[[^m]+m")
.expect("Error initializing color regex"),
}
}
}

if !self.0 {
let replaced = RE.replace_all(buf, &b""[..]);
impl std::io::Write for MaybeColorWriter {
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
if !self.enable_colors {
let replaced = self.remove_color_regex.replace_all(buf, &b""[..]);
std::io::stderr().write(&replaced)?;
Ok(buf.len())
} else {
Expand Down