Skip to content

Commit 2da4bef

Browse files
committed
Merge pull request #394 from peschkaj/378-rustc-colors
Autodetect TTY and add colors
2 parents 403a27a + 4e616ef commit 2da4bef

File tree

9 files changed

+46
-4
lines changed

9 files changed

+46
-4
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/rustup-cli/download_tracker.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use time::precise_time_s;
55
use rustup::Notification;
66
use rustup_dist::Notification as In;
77
use rustup_utils::Notification as Un;
8-
use tty;
8+
use rustup_utils::tty;
99

1010
/// Keep track of this many past download amounts
1111
const DOWNLOAD_TRACK_COUNT: usize = 5;

src/rustup-cli/main.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ mod proxy_mode;
4040
mod setup_mode;
4141
mod rustup_mode;
4242
mod self_update;
43-
mod tty;
4443
mod job;
4544
mod term2;
4645
mod errors;

src/rustup-cli/term2.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
55
use std::io;
66
use term;
7-
use tty;
7+
use rustup_utils::tty;
88

99
pub use term::color;
1010
pub use term::Attr;

src/rustup-utils/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ hyper = "0.7.0"
1717
rand = "0.3.11"
1818
scopeguard = "0.1.2"
1919
error-chain = { path = "../error-chain", version = "0.1.9" }
20+
libc = "0.2.0"
2021

2122
[target.x86_64-pc-windows-gnu.dependencies]
2223
winapi = "0.2.4"

src/rustup-utils/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,14 @@ extern crate advapi32;
2424
#[cfg(windows)]
2525
extern crate userenv;
2626

27+
#[cfg(unix)]
28+
extern crate libc;
29+
2730
pub mod notify;
2831
pub mod errors;
2932
pub mod notifications;
3033
pub mod raw;
34+
pub mod tty;
3135
pub mod utils;
3236

3337
pub use errors::*;
File renamed without changes.

src/rustup/command.rs

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,19 @@ pub fn run_command_for_dir<S: AsRef<OsStr>>(cmd: Command,
3030

3131
fn telemetry_rustc<S: AsRef<OsStr>>(mut cmd: Command, args: &[S], cfg: &Cfg) -> Result<()> {
3232
let now = Instant::now();
33-
33+
3434
cmd.args(&args[1..]);
35+
36+
let has_color_args = (&args).iter().any(|e| {
37+
let e = e.as_ref().to_str().unwrap_or("");
38+
e.starts_with("--color")
39+
});
40+
41+
if stderr_isatty() && !has_color_args
42+
{
43+
cmd.arg("--color");
44+
cmd.arg("always");
45+
}
3546

3647
// FIXME rust-lang/rust#32254. It's not clear to me
3748
// when and why this is needed.
@@ -132,3 +143,27 @@ fn run_command_for_dir_without_telemetry<S: AsRef<OsStr>>(mut cmd: Command, args
132143
}
133144
}
134145
}
146+
147+
#[cfg(unix)]
148+
fn stderr_isatty() -> bool {
149+
use libc;
150+
unsafe { libc::isatty(libc::STDERR_FILENO) != 0 }
151+
}
152+
153+
#[cfg(windows)]
154+
fn stderr_isatty() -> bool {
155+
type DWORD = u32;
156+
type BOOL = i32;
157+
type HANDLE = *mut u8;
158+
const STD_ERROR_HANDLE: DWORD = -12i32 as DWORD;
159+
extern "system" {
160+
fn GetStdHandle(which: DWORD) -> HANDLE;
161+
fn GetConsoleMode(hConsoleHandle: HANDLE,
162+
lpMode: *mut DWORD) -> BOOL;
163+
}
164+
unsafe {
165+
let handle = GetStdHandle(STD_ERROR_HANDLE);
166+
let mut out = 0;
167+
GetConsoleMode(handle, &mut out) != 0
168+
}
169+
}

src/rustup/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ extern crate regex;
1010
extern crate itertools;
1111
extern crate rustc_serialize;
1212
extern crate time;
13+
#[cfg(unix)]
14+
extern crate libc;
1315

1416
pub use errors::*;
1517
pub use notifications::*;

0 commit comments

Comments
 (0)