Skip to content

Commit 6541be3

Browse files
committed
Replace use_colored_tty with iatty crate
1 parent 9a7fac6 commit 6541be3

File tree

7 files changed

+53
-41
lines changed

7 files changed

+53
-41
lines changed

Cargo.lock

Lines changed: 35 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ cargo-fmt = []
3232
rustfmt-format-diff = []
3333

3434
[dependencies]
35+
isatty = "0.1"
3536
itertools = "0.7"
3637
toml = "0.4"
3738
serde = "1.0"
@@ -53,9 +54,3 @@ failure = "0.1.1"
5354
[dev-dependencies]
5455
assert_cli = "0.6"
5556
lazy_static = "1.0.0"
56-
57-
[target.'cfg(unix)'.dependencies]
58-
libc = "0.2.11"
59-
60-
[target.'cfg(windows)'.dependencies]
61-
winapi = { version = "0.3" }

src/bin/main.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ use failure::err_msg;
2828
use getopts::{Matches, Options};
2929

3030
use rustfmt::{
31-
checkstyle_footer, checkstyle_header, format_input, load_config, use_colored_tty, CliOptions,
32-
Color, Config, EmitMode, ErrorKind, FileLines, FileName, Input, Summary, Verbosity,
31+
checkstyle_footer, checkstyle_header, format_input, load_config, CliOptions, Color, Config,
32+
EmitMode, ErrorKind, FileLines, FileName, Input, Summary, Verbosity,
3333
};
3434

3535
fn main() {
@@ -316,7 +316,7 @@ fn format_and_emit_report(input: Input, config: &Config) -> Result<Summary, fail
316316
if report.has_warnings() {
317317
match term::stderr() {
318318
Some(ref t)
319-
if use_colored_tty(config.color())
319+
if config.color().use_colored_tty()
320320
&& t.supports_color()
321321
&& t.supports_attr(term::Attr::Bold) =>
322322
{

src/config/options.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ use config::config_type::ConfigType;
1212
use config::lists::*;
1313
use config::{Config, FileName};
1414

15+
use isatty::stdout_isatty;
16+
1517
use std::collections::HashSet;
1618
use std::path::{Path, PathBuf};
1719

@@ -191,6 +193,16 @@ configuration_option_enum! { Color:
191193
Auto,
192194
}
193195

196+
impl Color {
197+
pub fn use_colored_tty(&self) -> bool {
198+
match self {
199+
Color::Always => true,
200+
Color::Never => false,
201+
Color::Auto => stdout_isatty(),
202+
}
203+
}
204+
}
205+
194206
configuration_option_enum! { Verbosity:
195207
// Emit more.
196208
Verbose,

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
extern crate derive_new;
2020
extern crate diff;
2121
extern crate failure;
22+
extern crate isatty;
2223
extern crate itertools;
2324
#[cfg(test)]
2425
#[macro_use]
@@ -61,7 +62,6 @@ pub use config::summary::Summary;
6162
pub use config::{
6263
load_config, CliOptions, Color, Config, EmitMode, FileLines, FileName, Verbosity,
6364
};
64-
pub use utils::use_colored_tty;
6565

6666
#[macro_use]
6767
mod utils;

src/rustfmt_diff.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ use diff;
1313
use std::collections::VecDeque;
1414
use std::io;
1515
use std::io::Write;
16-
use utils::use_colored_tty;
1716

1817
#[derive(Debug, PartialEq)]
1918
pub enum DiffLine {
@@ -53,7 +52,7 @@ impl OutputWriter {
5352
// for colorized output and the capabilities of the terminal.
5453
pub fn new(color: Color) -> Self {
5554
if let Some(t) = term::stdout() {
56-
if use_colored_tty(color) && t.supports_color() {
55+
if color.use_colored_tty() && t.supports_color() {
5756
return OutputWriter { terminal: Some(t) };
5857
}
5958
}

src/utils.rs

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ use syntax::ast::{
1818
use syntax::codemap::{BytePos, Span, NO_EXPANSION};
1919
use syntax::ptr;
2020

21-
use config::Color;
2221
use rewrite::RewriteContext;
2322
use shape::Shape;
2423

@@ -376,32 +375,6 @@ pub fn left_most_sub_expr(e: &ast::Expr) -> &ast::Expr {
376375
}
377376
}
378377

379-
// isatty shamelessly adapted from cargo.
380-
#[cfg(unix)]
381-
pub fn isatty() -> bool {
382-
extern crate libc;
383-
384-
unsafe { libc::isatty(libc::STDOUT_FILENO) != 0 }
385-
}
386-
#[cfg(windows)]
387-
pub fn isatty() -> bool {
388-
extern crate winapi;
389-
390-
unsafe {
391-
let handle = winapi::um::processenv::GetStdHandle(winapi::um::winbase::STD_OUTPUT_HANDLE);
392-
let mut out = 0;
393-
winapi::um::consoleapi::GetConsoleMode(handle, &mut out) != 0
394-
}
395-
}
396-
397-
pub fn use_colored_tty(color: Color) -> bool {
398-
match color {
399-
Color::Always => true,
400-
Color::Never => false,
401-
Color::Auto => isatty(),
402-
}
403-
}
404-
405378
pub fn starts_with_newline(s: &str) -> bool {
406379
s.starts_with('\n') || s.starts_with("\r\n")
407380
}

0 commit comments

Comments
 (0)