Replace use_colored_tty with iatty crate

This commit is contained in:
Nick Cameron 2018-05-21 15:32:27 +12:00
parent 9a7fac63c8
commit 6541be3c6f
7 changed files with 53 additions and 41 deletions

37
Cargo.lock generated
View File

@ -238,6 +238,17 @@ dependencies = [
"quick-error 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "isatty"
version = "0.1.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.2.40 (registry+https://github.com/rust-lang/crates.io-index)",
"redox_syscall 0.1.37 (registry+https://github.com/rust-lang/crates.io-index)",
"winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "itertools"
version = "0.7.8"
@ -251,6 +262,15 @@ name = "itoa"
version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "kernel32-sys"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)",
"winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "lazy_static"
version = "0.2.11"
@ -523,9 +543,9 @@ dependencies = [
"env_logger 0.5.10 (registry+https://github.com/rust-lang/crates.io-index)",
"failure 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
"getopts 0.2.17 (registry+https://github.com/rust-lang/crates.io-index)",
"isatty 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)",
"itertools 0.7.8 (registry+https://github.com/rust-lang/crates.io-index)",
"lazy_static 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.2.40 (registry+https://github.com/rust-lang/crates.io-index)",
"log 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)",
"regex 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
"rustc-ap-rustc_target 134.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
@ -536,7 +556,6 @@ dependencies = [
"term 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)",
"toml 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)",
"unicode-segmentation 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
"winapi 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
@ -722,6 +741,11 @@ name = "void"
version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "winapi"
version = "0.2.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "winapi"
version = "0.3.4"
@ -731,6 +755,11 @@ dependencies = [
"winapi-x86_64-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "winapi-build"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "winapi-i686-pc-windows-gnu"
version = "0.4.0"
@ -780,8 +809,10 @@ dependencies = [
"checksum fuchsia-zircon-sys 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "3dcaa9ae7725d12cdb85b3ad99a434db70b468c09ded17e012d86b5c1010f7a7"
"checksum getopts 0.2.17 (registry+https://github.com/rust-lang/crates.io-index)" = "b900c08c1939860ce8b54dc6a89e26e00c04c380fd0e09796799bd7f12861e05"
"checksum humantime 1.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "0484fda3e7007f2a4a0d9c3a703ca38c71c54c55602ce4660c419fd32e188c9e"
"checksum isatty 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)" = "a118a53ba42790ef25c82bb481ecf36e2da892646cccd361e69a6bb881e19398"
"checksum itertools 0.7.8 (registry+https://github.com/rust-lang/crates.io-index)" = "f58856976b776fedd95533137617a02fb25719f40e7d9b01c7043cd65474f450"
"checksum itoa 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)" = "c069bbec61e1ca5a596166e55dfe4773ff745c3d16b700013bcaff9a6df2c682"
"checksum kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7507624b29483431c0ba2d82aece8ca6cdba9382bff4ddd0f7490560c056098d"
"checksum lazy_static 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)" = "76f033c7ad61445c5b347c7382dd1237847eb1bce590fe50365dcb33d546be73"
"checksum lazy_static 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "c8f31047daa365f19be14b47c29df4f7c3b581832407daabe6ae77397619237d"
"checksum libc 0.2.40 (registry+https://github.com/rust-lang/crates.io-index)" = "6fd41f331ac7c5b8ac259b8bf82c75c0fb2e469bbf37d2becbba9a6a2221965b"
@ -839,7 +870,9 @@ dependencies = [
"checksum unreachable 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "382810877fe448991dfc7f0dd6e3ae5d58088fd0ea5e35189655f84e6814fa56"
"checksum utf8-ranges 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "662fab6525a98beff2921d7f61a39e7d59e0b425ebc7d0d9e66d316e55124122"
"checksum void 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "6a02e4885ed3bc0f2de90ea6dd45ebcbb66dacffe03547fadbb0eeae2770887d"
"checksum winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)" = "167dc9d6949a9b857f3451275e911c3f44255842c1f7a76f33c55103a909087a"
"checksum winapi 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)" = "04e3bd221fcbe8a271359c04f21a76db7d0c6028862d1bb5512d85e1e2eb5bb3"
"checksum winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "2d315eee3b34aca4797b2da6b13ed88266e6d612562a0c46390af8299fc699bc"
"checksum winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
"checksum winapi-x86_64-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
"checksum wincolor 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "eeb06499a3a4d44302791052df005d5232b927ed1a9658146d842165c4de7767"

View File

@ -32,6 +32,7 @@ cargo-fmt = []
rustfmt-format-diff = []
[dependencies]
isatty = "0.1"
itertools = "0.7"
toml = "0.4"
serde = "1.0"
@ -53,9 +54,3 @@ failure = "0.1.1"
[dev-dependencies]
assert_cli = "0.6"
lazy_static = "1.0.0"
[target.'cfg(unix)'.dependencies]
libc = "0.2.11"
[target.'cfg(windows)'.dependencies]
winapi = { version = "0.3" }

View File

@ -28,8 +28,8 @@ use failure::err_msg;
use getopts::{Matches, Options};
use rustfmt::{
checkstyle_footer, checkstyle_header, format_input, load_config, use_colored_tty, CliOptions,
Color, Config, EmitMode, ErrorKind, FileLines, FileName, Input, Summary, Verbosity,
checkstyle_footer, checkstyle_header, format_input, load_config, CliOptions, Color, Config,
EmitMode, ErrorKind, FileLines, FileName, Input, Summary, Verbosity,
};
fn main() {
@ -316,7 +316,7 @@ fn format_and_emit_report(input: Input, config: &Config) -> Result<Summary, fail
if report.has_warnings() {
match term::stderr() {
Some(ref t)
if use_colored_tty(config.color())
if config.color().use_colored_tty()
&& t.supports_color()
&& t.supports_attr(term::Attr::Bold) =>
{

View File

@ -12,6 +12,8 @@ use config::config_type::ConfigType;
use config::lists::*;
use config::{Config, FileName};
use isatty::stdout_isatty;
use std::collections::HashSet;
use std::path::{Path, PathBuf};
@ -191,6 +193,16 @@ configuration_option_enum! { Color:
Auto,
}
impl Color {
pub fn use_colored_tty(&self) -> bool {
match self {
Color::Always => true,
Color::Never => false,
Color::Auto => stdout_isatty(),
}
}
}
configuration_option_enum! { Verbosity:
// Emit more.
Verbose,

View File

@ -19,6 +19,7 @@
extern crate derive_new;
extern crate diff;
extern crate failure;
extern crate isatty;
extern crate itertools;
#[cfg(test)]
#[macro_use]
@ -61,7 +62,6 @@ pub use config::summary::Summary;
pub use config::{
load_config, CliOptions, Color, Config, EmitMode, FileLines, FileName, Verbosity,
};
pub use utils::use_colored_tty;
#[macro_use]
mod utils;

View File

@ -13,7 +13,6 @@ use diff;
use std::collections::VecDeque;
use std::io;
use std::io::Write;
use utils::use_colored_tty;
#[derive(Debug, PartialEq)]
pub enum DiffLine {
@ -53,7 +52,7 @@ impl OutputWriter {
// for colorized output and the capabilities of the terminal.
pub fn new(color: Color) -> Self {
if let Some(t) = term::stdout() {
if use_colored_tty(color) && t.supports_color() {
if color.use_colored_tty() && t.supports_color() {
return OutputWriter { terminal: Some(t) };
}
}

View File

@ -18,7 +18,6 @@ use syntax::ast::{
use syntax::codemap::{BytePos, Span, NO_EXPANSION};
use syntax::ptr;
use config::Color;
use rewrite::RewriteContext;
use shape::Shape;
@ -376,32 +375,6 @@ pub fn left_most_sub_expr(e: &ast::Expr) -> &ast::Expr {
}
}
// isatty shamelessly adapted from cargo.
#[cfg(unix)]
pub fn isatty() -> bool {
extern crate libc;
unsafe { libc::isatty(libc::STDOUT_FILENO) != 0 }
}
#[cfg(windows)]
pub fn isatty() -> bool {
extern crate winapi;
unsafe {
let handle = winapi::um::processenv::GetStdHandle(winapi::um::winbase::STD_OUTPUT_HANDLE);
let mut out = 0;
winapi::um::consoleapi::GetConsoleMode(handle, &mut out) != 0
}
}
pub fn use_colored_tty(color: Color) -> bool {
match color {
Color::Always => true,
Color::Never => false,
Color::Auto => isatty(),
}
}
pub fn starts_with_newline(s: &str) -> bool {
s.starts_with('\n') || s.starts_with("\r\n")
}