Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add .cargo/config settings to control output colors #2367

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
43 changes: 37 additions & 6 deletions src/cargo/core/shell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ 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};
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 {
Expand Down Expand Up @@ -44,12 +45,20 @@ pub struct Shell {
pub struct MultiShell {
out: Shell,
err: Shell,
verbosity: Verbosity
verbosity: Verbosity,
status_color: Option<Color>,
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 {
Expand All @@ -71,9 +80,11 @@ impl MultiShell {
pub fn status<T, U>(&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),
}
}

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -179,13 +210,13 @@ impl Shell {
Ok(())
}

pub fn say_status<T, U>(&mut self, status: T, message: U, color: Color)
pub fn say_status<T, U>(&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));
Expand Down
21 changes: 21 additions & 0 deletions src/cargo/util/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)]
Expand Down