From 060e1efc073fd26566f90f6db78264631e884623 Mon Sep 17 00:00:00 2001 From: Jack O'Connor Date: Mon, 8 Feb 2016 01:39:33 -0500 Subject: [PATCH] add .cargo/config settings to control output colors --- src/cargo/core/shell.rs | 43 ++++++++++++++++++++++++++++++++++------ src/cargo/util/config.rs | 21 ++++++++++++++++++++ 2 files changed, 58 insertions(+), 6 deletions(-) diff --git a/src/cargo/core/shell.rs b/src/cargo/core/shell.rs index 3b4c1955812..07ee9634a97 100644 --- a/src/cargo/core/shell.rs +++ b/src/cargo/core/shell.rs @@ -2,7 +2,7 @@ use std::fmt; use std::io::prelude::*; use std::io; -use term::color::{Color, BLACK, RED, GREEN, YELLOW}; +use term::color::{Color, BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE}; use term::{self, Terminal, TerminfoTerminal, color, Attr}; use self::AdequateTerminal::{NoColor, Colored}; @@ -10,6 +10,7 @@ use self::Verbosity::{Verbose, Normal, Quiet}; use self::ColorConfig::{Auto, Always, Never}; use util::errors::CargoResult; +use util::human; #[derive(Clone, Copy, PartialEq)] pub enum Verbosity { @@ -44,12 +45,20 @@ pub struct Shell { pub struct MultiShell { out: Shell, err: Shell, - verbosity: Verbosity + verbosity: Verbosity, + status_color: Option, + status_bold: bool, } impl MultiShell { pub fn new(out: Shell, err: Shell, verbosity: Verbosity) -> MultiShell { - MultiShell { out: out, err: err, verbosity: verbosity } + MultiShell { + out: out, + err: err, + verbosity: verbosity, + status_color: None, + status_bold: true, + } } pub fn out(&mut self) -> &mut Shell { @@ -71,9 +80,11 @@ impl MultiShell { pub fn status(&mut self, status: T, message: U) -> CargoResult<()> where T: fmt::Display, U: fmt::Display { + let color = self.status_color.unwrap_or(GREEN); + let bold = self.status_bold; match self.verbosity { Quiet => Ok(()), - _ => self.out().say_status(status, message, GREEN) + _ => self.out().say_status(status, message, color, bold), } } @@ -139,6 +150,26 @@ impl MultiShell { pub fn get_verbose(&self) -> Verbosity { self.verbosity } + + pub fn set_status_color(&mut self, color_name: &str) -> CargoResult<()> { + let color = match color_name { + "black" => BLACK, + "red" => RED, + "green" => GREEN, + "yellow" => YELLOW, + "blue" => BLUE, + "magenta" => MAGENTA, + "cyan" => CYAN, + "white" => WHITE, + _ => return Err(human(format!("invalid color name '{}'", color_name))), + }; + self.status_color = Some(color); + Ok(()) + } + + pub fn set_status_bold(&mut self, bold: bool) { + self.status_bold = bold + } } impl Shell { @@ -179,13 +210,13 @@ impl Shell { Ok(()) } - pub fn say_status(&mut self, status: T, message: U, color: Color) + pub fn say_status(&mut self, status: T, message: U, color: Color, bold: bool) -> CargoResult<()> where T: fmt::Display, U: fmt::Display { try!(self.reset()); if color != BLACK { try!(self.fg(color)); } - if self.supports_attr(Attr::Bold) { try!(self.attr(Attr::Bold)); } + if bold && self.supports_attr(Attr::Bold) { try!(self.attr(Attr::Bold)); } try!(write!(self, "{:>12}", status.to_string())); try!(self.reset()); try!(write!(self, " {}\n", message)); diff --git a/src/cargo/util/config.rs b/src/cargo/util/config.rs index 88a42383be0..795a9531952 100644 --- a/src/cargo/util/config.rs +++ b/src/cargo/util/config.rs @@ -50,6 +50,7 @@ impl Config { try!(cfg.scrape_tool_config()); try!(cfg.scrape_rustc_version()); try!(cfg.scrape_target_dir_config()); + try!(cfg.load_colors()); Ok(cfg) } @@ -265,6 +266,26 @@ impl Config { let tool = env::var_os(&var).unwrap_or_else(|| OsString::from(tool)); Ok(PathBuf::from(tool)) } + + fn load_colors(&self) -> CargoResult<()> { + if let Some((map, _)) = try!(self.get_table("color")) { + if let Some(val) = map.get("status_color") { + if let &ConfigValue::String(ref color, _) = val { + try!(self.shell.borrow_mut().set_status_color(color)); + } else { + return Err(human("invalid type for status_color")); + } + } + if let Some(val) = map.get("status_bold") { + if let &ConfigValue::Boolean(bold, _) = val { + self.shell.borrow_mut().set_status_bold(bold); + } else { + return Err(human("invalid type for status_bold")); + } + } + } + Ok(()) + } } #[derive(Eq, PartialEq, Clone, RustcEncodable, RustcDecodable, Copy)]