Skip to content

Commit

Permalink
Migrate from atty to is-terminal (#4382)
Browse files Browse the repository at this point in the history
  • Loading branch information
souzaguilhermea committed Feb 21, 2023
1 parent 4e7ae2d commit 7d7b9eb
Show file tree
Hide file tree
Showing 18 changed files with 160 additions and 69 deletions.
160 changes: 116 additions & 44 deletions Cargo.lock

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,6 @@ feat_os_windows_legacy = [
test = [ "uu_test" ]

[workspace.dependencies]
atty = "0.2"
bigdecimal = "0.3"
binary-heap-plus = "0.5.0"
bstr = "1.0"
Expand All @@ -287,6 +286,7 @@ gcd = "2.2"
glob = "0.3.0"
half = "2.1"
indicatif = "0.17"
is-terminal = "0.4.3"
itertools = "0.10.0"
libc = "0.2.139"
lscolors = { version = "0.13.0", default-features=false, features = ["nu-ansi-term"] }
Expand Down Expand Up @@ -476,7 +476,7 @@ time = { workspace=true, features=["local-offset"] }
unindent = "0.1"
uucore = { workspace=true, features=["entries", "process", "signals"] }
walkdir = { workspace=true }
atty = { workspace=true }
is-terminal = { workspace=true }
hex-literal = "0.3.1"
rstest = "0.16.0"

Expand Down
14 changes: 13 additions & 1 deletion deny.toml
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,24 @@ highlight = "all"
# For each duplicate dependency, indicate the name of the dependency which
# introduces it.
# spell-checker: disable
skip = []
skip = [
# is-terminal
{ name = "hermit-abi", version = "0.3.1" },
# is-terminal
{ name = "rustix", version = "0.36.8" },
# is-terminal (via rustix)
{ name = "io-lifetimes", version = "1.0.5" },
# is-terminal
{ name = "linux-raw-sys", version = "0.1.4" },
# is-terminal
{ name = "windows-sys", version = "0.45.0" },
]
# spell-checker: enable

# This section is considered when running `cargo deny check sources`.
# More documentation about the 'sources' section can be found here:
# https://embarkstudios.github.io/cargo-deny/checks/sources/cfg.html

[sources]
unknown-registry = "warn"
unknown-git = "warn"
Expand Down
2 changes: 1 addition & 1 deletion src/uu/cat/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ path = "src/cat.rs"
[dependencies]
clap = { workspace=true }
thiserror = { workspace = true }
atty = { workspace=true }
is-terminal = { workspace = true }
uucore = { workspace=true, features=["fs", "pipes"] }

[target.'cfg(unix)'.dependencies]
Expand Down
3 changes: 2 additions & 1 deletion src/uu/cat/src/cat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

// last synced with: cat (GNU coreutils) 8.13
use clap::{crate_version, Arg, ArgAction, Command};
use is_terminal::IsTerminal;
use std::fs::{metadata, File};
use std::io::{self, Read, Write};
use thiserror::Error;
Expand Down Expand Up @@ -332,7 +333,7 @@ fn cat_path(
let stdin = io::stdin();
let mut handle = InputHandle {
reader: stdin,
is_interactive: atty::is(atty::Stream::Stdin),
is_interactive: std::io::stdin().is_terminal(),
};
cat_handle(&mut handle, options, state)
}
Expand Down
2 changes: 1 addition & 1 deletion src/uu/cut/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ clap = { workspace=true }
uucore = { workspace=true }
memchr = { workspace=true }
bstr = { workspace=true }
atty = { workspace=true }
is-terminal = { workspace=true }

[[bin]]
name = "cut"
Expand Down
3 changes: 2 additions & 1 deletion src/uu/cut/src/cut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

use bstr::io::BufReadExt;
use clap::{crate_version, Arg, ArgAction, Command};
use is_terminal::IsTerminal;
use std::fs::File;
use std::io::{stdin, stdout, BufReader, BufWriter, Read, Write};
use std::path::Path;
Expand Down Expand Up @@ -136,7 +137,7 @@ enum Mode {
}

fn stdout_writer() -> Box<dyn Write> {
if atty::is(atty::Stream::Stdout) {
if std::io::stdout().is_terminal() {
Box::new(stdout())
} else {
Box::new(BufWriter::new(stdout())) as Box<dyn Write>
Expand Down
2 changes: 1 addition & 1 deletion src/uu/ls/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ glob = { workspace=true }
lscolors = { workspace=true }
uucore = { workspace=true, features = ["entries", "fs"] }
once_cell = { workspace=true }
atty = { workspace=true }
is-terminal = { workspace=true }
selinux = { workspace=true, optional = true }

[[bin]]
Expand Down
9 changes: 5 additions & 4 deletions src/uu/ls/src/ls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use clap::{
crate_version, Arg, ArgAction, Command,
};
use glob::{MatchOptions, Pattern};
use is_terminal::IsTerminal;
use lscolors::LsColors;
use number_prefix::NumberPrefix;
use once_cell::unsync::OnceCell;
Expand Down Expand Up @@ -451,7 +452,7 @@ impl Config {
(Format::Commas, Some(options::format::COMMAS))
} else if options.get_flag(options::format::COLUMNS) {
(Format::Columns, Some(options::format::COLUMNS))
} else if atty::is(atty::Stream::Stdout) {
} else if std::io::stdout().is_terminal() {
(Format::Columns, None)
} else {
(Format::OneLine, None)
Expand Down Expand Up @@ -557,7 +558,7 @@ impl Config {
None => options.contains_id(options::COLOR),
Some(val) => match val.as_str() {
"" | "always" | "yes" | "force" => true,
"auto" | "tty" | "if-tty" => atty::is(atty::Stream::Stdout),
"auto" | "tty" | "if-tty" => std::io::stdout().is_terminal(),
/* "never" | "no" | "none" | */ _ => false,
},
};
Expand Down Expand Up @@ -678,7 +679,7 @@ impl Config {
} else if options.get_flag(options::SHOW_CONTROL_CHARS) {
true
} else {
!atty::is(atty::Stream::Stdout)
!std::io::stdout().is_terminal()
};

let opt_quoting_style = options
Expand Down Expand Up @@ -750,7 +751,7 @@ impl Config {
"never" | "no" | "none" => IndicatorStyle::None,
"always" | "yes" | "force" => IndicatorStyle::Classify,
"auto" | "tty" | "if-tty" => {
if atty::is(atty::Stream::Stdout) {
if std::io::stdout().is_terminal() {
IndicatorStyle::Classify
} else {
IndicatorStyle::None
Expand Down
2 changes: 1 addition & 1 deletion src/uu/more/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ path = "src/more.rs"
clap = { workspace=true }
uucore = { workspace=true }
crossterm = { workspace=true }
atty = { workspace=true }
is-terminal = { workspace=true }
unicode-width = { workspace=true }
unicode-segmentation = { workspace=true }

Expand Down
3 changes: 2 additions & 1 deletion src/uu/more/src/more.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ use crossterm::{
terminal,
};

use is_terminal::IsTerminal;
use unicode_segmentation::UnicodeSegmentation;
use unicode_width::UnicodeWidthStr;
use uucore::display::Quotable;
Expand Down Expand Up @@ -83,7 +84,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
buff.clear();
}
reset_term(&mut stdout);
} else if atty::isnt(atty::Stream::Stdin) {
} else if !std::io::stdin().is_terminal() {
stdin().read_to_string(&mut buff).unwrap();
let mut stdout = setup_term();
more(&buff, &mut stdout, None, silent)?;
Expand Down
2 changes: 1 addition & 1 deletion src/uu/nohup/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ path = "src/nohup.rs"
[dependencies]
clap = { workspace=true }
libc = { workspace=true }
atty = { workspace=true }
is-terminal = { workspace=true }
uucore = { workspace=true, features=["fs"] }

[[bin]]
Expand Down
7 changes: 4 additions & 3 deletions src/uu/nohup/src/nohup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
// spell-checker:ignore (ToDO) execvp SIGHUP cproc vprocmgr cstrs homeout

use clap::{crate_version, Arg, ArgAction, Command};
use is_terminal::IsTerminal;
use libc::{c_char, dup2, execvp, signal};
use libc::{SIGHUP, SIG_IGN};
use std::env;
Expand Down Expand Up @@ -129,15 +130,15 @@ pub fn uu_app() -> Command {
}

fn replace_fds() -> UResult<()> {
if atty::is(atty::Stream::Stdin) {
if std::io::stdin().is_terminal() {
let new_stdin = File::open(Path::new("/dev/null"))
.map_err(|e| NohupError::CannotReplace("STDIN", e))?;
if unsafe { dup2(new_stdin.as_raw_fd(), 0) } != 0 {
return Err(NohupError::CannotReplace("STDIN", Error::last_os_error()).into());
}
}

if atty::is(atty::Stream::Stdout) {
if std::io::stdout().is_terminal() {
let new_stdout = find_stdout()?;
let fd = new_stdout.as_raw_fd();

Expand All @@ -146,7 +147,7 @@ fn replace_fds() -> UResult<()> {
}
}

if atty::is(atty::Stream::Stderr) && unsafe { dup2(1, 2) } != 2 {
if std::io::stderr().is_terminal() && unsafe { dup2(1, 2) } != 2 {
return Err(NohupError::CannotReplace("STDERR", Error::last_os_error()).into());
}
Ok(())
Expand Down
2 changes: 1 addition & 1 deletion src/uu/tail/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ memchr = { workspace=true }
notify = { workspace=true }
uucore = { workspace=true }
same-file = { workspace=true }
atty = { workspace=true }
is-terminal = { workspace=true }
fundu = { workspace=true }

[target.'cfg(windows)'.dependencies]
Expand Down
4 changes: 2 additions & 2 deletions src/uu/tail/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@

use crate::paths::Input;
use crate::{parse, platform, Quotable};
use atty::Stream;
use clap::crate_version;
use clap::{parser::ValueSource, Arg, ArgAction, ArgMatches, Command};
use fundu::DurationParser;
use is_terminal::IsTerminal;
use same_file::Handle;
use std::collections::VecDeque;
use std::ffi::OsString;
Expand Down Expand Up @@ -274,7 +274,7 @@ impl Settings {
.map_or(false, |meta| !meta.is_file())
});

if !blocking_stdin && atty::is(Stream::Stdin) {
if !blocking_stdin && std::io::stdin().is_terminal() {
show_warning!("following standard input indefinitely is ineffective");
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/uu/tty/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ path = "src/tty.rs"
[dependencies]
clap = { workspace=true }
nix = { workspace=true, features=["term"] }
atty = { workspace=true }
is-terminal = { workspace=true }
uucore = { workspace=true, features=["fs"] }

[[bin]]
Expand Down
3 changes: 2 additions & 1 deletion src/uu/tty/src/tty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
// spell-checker:ignore (ToDO) ttyname filedesc

use clap::{crate_version, Arg, ArgAction, Command};
use is_terminal::IsTerminal;
use std::io::Write;
use std::os::unix::io::AsRawFd;
use uucore::error::{set_exit_code, UResult};
Expand All @@ -30,7 +31,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {

// If silent, we don't need the name, only whether or not stdin is a tty.
if silent {
return if atty::is(atty::Stream::Stdin) {
return if std::io::stdin().is_terminal() {
Ok(())
} else {
Err(1.into())
Expand Down
5 changes: 3 additions & 2 deletions tests/by-util/test_more.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use crate::common::util::*;
use is_terminal::IsTerminal;

#[test]
fn test_more_no_arg() {
// Reading from stdin is now supported, so this must succeed
if atty::is(atty::Stream::Stdout) {
if std::io::stdout().is_terminal() {
new_ucmd!().succeeds();
} else {
}
Expand All @@ -14,7 +15,7 @@ fn test_more_dir_arg() {
// Run the test only if there's a valid terminal, else do nothing
// Maybe we could capture the error, i.e. "Device not found" in that case
// but I am leaving this for later
if atty::is(atty::Stream::Stdout) {
if std::io::stdout().is_terminal() {
new_ucmd!()
.arg(".")
.fails()
Expand Down

0 comments on commit 7d7b9eb

Please sign in to comment.