Skip to content

Commit

Permalink
Fix Color Bug in Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
SuperHacker-liuan committed Jan 16, 2018
1 parent eb1377b commit a6be3f2
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion src/term.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,23 @@ use std::io::Write;
use std::sync::{Arc, Mutex};
use termcolor::{Color, ColorChoice, ColorSpec, StandardStream, WriteColor};

const WIN_DEF_BG: Option<Color> = Some(Color::Black);
const WIN_DEF_FG: Option<Color> = Some(Color::White);

pub struct ColorTerm {
stdout: Mutex<StandardStream>,
color_spec: Mutex<ColorSpec>,
}

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),
}
}

Expand All @@ -31,9 +38,19 @@ impl ColorTerm {
pub fn set_default_color(&self, fg: Option<Option<Color>>, bg: Option<Option<Color>>, bold: Option<bool>) {
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 {
Expand Down

0 comments on commit a6be3f2

Please sign in to comment.