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

Autodetect TTY and add colors #394

Merged
merged 2 commits into from
May 6, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
35 changes: 34 additions & 1 deletion src/rustup/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,17 @@ pub fn run_command_for_dir<S: AsRef<OsStr>>(cmd: Command,

fn telemetry_rustc<S: AsRef<OsStr>>(mut cmd: Command, args: &[S], cfg: &Cfg) -> Result<()> {
let now = Instant::now();

cmd.args(&args[1..]);

if stderr_isatty() && !(&args).iter().any(|e| {
let e = e.as_ref().to_str().unwrap_or("");
e == "--color"
})
{
Copy link
Contributor

Choose a reason for hiding this comment

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

Please pull out this expression from (&args).iter() onward into a local variable with a descriptive name like, has_color_arg.

I believe this check should be e.starts_with("--color") since it could see e.g. --color=always.

cmd.arg("--color");
cmd.arg("always");
}

// FIXME rust-lang/rust#32254. It's not clear to me
// when and why this is needed.
Expand Down Expand Up @@ -132,3 +141,27 @@ fn run_command_for_dir_without_telemetry<S: AsRef<OsStr>>(mut cmd: Command, args
}
}
}

#[cfg(unix)]
fn stderr_isatty() -> bool {
use libc;
unsafe { libc::isatty(libc::STDERR_FILENO) != 0 }
}

#[cfg(windows)]
fn stderr_isatty() -> bool {
type DWORD = u32;
type BOOL = i32;
type HANDLE = *mut u8;
const STD_ERROR_HANDLE: DWORD = -12i32 as DWORD;
extern "system" {
fn GetStdHandle(which: DWORD) -> HANDLE;
fn GetConsoleMode(hConsoleHandle: HANDLE,
lpMode: *mut DWORD) -> BOOL;
}
unsafe {
let handle = GetStdHandle(STD_ERROR_HANDLE);
let mut out = 0;
GetConsoleMode(handle, &mut out) != 0
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

This function already exists in rustup_cli::tty. Can you extract that module to rustup_utils::tty so it can be reused here?

2 changes: 2 additions & 0 deletions src/rustup/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ extern crate regex;
extern crate itertools;
extern crate rustc_serialize;
extern crate time;
#[cfg(unix)]
extern crate libc;

pub use errors::*;
pub use notifications::*;
Expand Down