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

Enable or disable colored output according to CLICOLOR(_FORCE) #67177

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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/librustc_errors/emitter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use rustc_data_structures::fx::FxHashMap;
use rustc_data_structures::sync::Lrc;
use std::borrow::Cow;
use std::io::prelude::*;
use std::io;
use std::{env, io};
use std::cmp::{min, max, Reverse};
use std::path::Path;
use termcolor::{StandardStream, ColorChoice, ColorSpec, BufferWriter, Ansi};
Expand Down Expand Up @@ -458,7 +458,11 @@ impl ColorConfig {
}
}
ColorConfig::Never => ColorChoice::Never,
ColorConfig::Auto if atty::is(atty::Stream::Stderr) => {
ColorConfig::Auto
if env::var("CLICOLOR_FORCE").unwrap_or("0".to_string()) != "0"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This unnecessarily allocates a string. Something like env::var(...).as_ref().map(|s| &**s).unwrap_or("0") should work. Also I think it would be nicer to use ColorConfig::Auto => { if { ... } else { ... } } instead.

|| (atty::is(atty::Stream::Stderr)
&& env::var("CLICOLOR").unwrap_or("1".to_string()) != "0") =>
{
ColorChoice::Auto
}
ColorConfig::Auto => ColorChoice::Never,
Expand Down
1 change: 1 addition & 0 deletions src/libterm/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
html_playground_url = "https://play.rust-lang.org/",
test(attr(deny(warnings))))]
#![deny(missing_docs)]
#![feature(result_map_or)]

#![cfg_attr(windows, feature(libc))]

Expand Down
10 changes: 7 additions & 3 deletions src/libterm/terminfo/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use crate::color;
use crate::Terminal;

use searcher::get_dbpath_for_term;
use parser::compiled::{parse, msys_terminfo};
use parser::compiled::{parse, ansi_terminfo};
use parm::{expand, Variables, Param};

/// A parsed terminfo database entry.
Expand Down Expand Up @@ -69,14 +69,18 @@ impl fmt::Display for Error {
impl TermInfo {
/// Creates a TermInfo based on current environment.
pub fn from_env() -> Result<TermInfo, Error> {
if env::var("CLICOLOR_FORCE").unwrap_or("0".to_string()) != "0" {
return Ok(ansi_terminfo());
}
let term = match env::var("TERM") {
Ok(name) => TermInfo::from_name(&name),
Err(..) => return Err(Error::TermUnset),
};

if term.is_err() && env::var("MSYSCON").ok().map_or(false, |s| "mintty.exe" == s) {
if term.is_err() && (env::var("MSYSCON").map_or(false, |s| "mintty.exe" == s) ||
env::var("CLICOLOR").unwrap_or("0".to_string()) != "0") {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is confusing what the condition is. It is easy to overlook the start and end of (...).

// msys terminal
Ok(msys_terminfo())
Ok(ansi_terminfo())
} else {
term
}
Expand Down
5 changes: 3 additions & 2 deletions src/libterm/terminfo/parser/compiled.rs
Original file line number Diff line number Diff line change
Expand Up @@ -316,8 +316,9 @@ pub fn parse(file: &mut dyn io::Read, longnames: bool) -> Result<TermInfo, Strin
})
}

/// Creates a dummy TermInfo struct for msys terminals
pub fn msys_terminfo() -> TermInfo {
/// Create a dummy TermInfo struct which only supports ISO 6429 (ANSI) color sequences. This is
/// used for msys and when CLICOLOR(_FORCE) is set.
pub fn ansi_terminfo() -> TermInfo {
let mut strings = HashMap::new();
strings.insert("sgr0".to_string(), b"\x1B[0m".to_vec());
strings.insert("bold".to_string(), b"\x1B[1m".to_vec());
Expand Down