From a6be3f25bff7883c63f4add3656b5521961f122e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E5=AE=89?= Date: Tue, 16 Jan 2018 19:57:51 +0800 Subject: [PATCH] Fix Color Bug in Windows --- src/term.rs | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/src/term.rs b/src/term.rs index 73b834e..4c884ed 100644 --- a/src/term.rs +++ b/src/term.rs @@ -2,6 +2,9 @@ use std::io::Write; use std::sync::{Arc, Mutex}; use termcolor::{Color, ColorChoice, ColorSpec, StandardStream, WriteColor}; +const WIN_DEF_BG: Option = Some(Color::Black); +const WIN_DEF_FG: Option = Some(Color::White); + pub struct ColorTerm { stdout: Mutex, color_spec: Mutex, @@ -9,9 +12,13 @@ pub struct ColorTerm { impl ColorTerm { fn new() -> ColorTerm { + let mut color_spec = ColorSpec::new(); + if cfg!(target_os = "windows") { + color_spec.set_bg(WIN_DEF_BG).set_fg(WIN_DEF_FG); + } ColorTerm { stdout: Mutex::new(StandardStream::stdout(ColorChoice::Always)), - color_spec: Mutex::new(ColorSpec::new()), + color_spec: Mutex::new(color_spec), } } @@ -31,9 +38,19 @@ impl ColorTerm { pub fn set_default_color(&self, fg: Option>, bg: Option>, bold: Option) { let mut spec = self.color_spec.lock().unwrap(); if let Some(fg) = fg { + let fg = if cfg!(target_os = "windows") && fg.is_none() { + WIN_DEF_FG + } else { + fg + }; spec.set_fg(fg); } if let Some(bg) = bg { + let bg = if cfg!(target_os = "windows") && bg.is_none() { + WIN_DEF_BG + } else { + bg + }; spec.set_bg(bg); } if let Some(bold) = bold {