Skip to content

Commit

Permalink
Use std::io::IsTerminal instead of atty (#1129)
Browse files Browse the repository at this point in the history
This is a new feature of Rust 1.71.0 and enables dropping a small
dependency in favor of built-in functionality.
  • Loading branch information
alexcrichton committed Jul 17, 2023
1 parent 07229fe commit ff0ceda
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 6 deletions.
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ wit-smith = { version = "0.1.6", path = "crates/wit-smith" }
[dependencies]
anyhow = { workspace = true }
env_logger = { workspace = true }
atty = "0.2"
log = { workspace = true }
clap = { workspace = true }
tempfile = "3.2.0"
Expand Down
4 changes: 2 additions & 2 deletions src/bin/wasm-tools/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use anyhow::Result;
use clap::Parser;
use std::io::{self, Write};
use std::io::{self, IsTerminal, Write};
use std::process::ExitCode;
use termcolor::{Color, ColorChoice, ColorSpec, StandardStream, WriteColor};

Expand Down Expand Up @@ -100,7 +100,7 @@ fn main() -> ExitCode {
}

fn print_error(color: ColorChoice, err: anyhow::Error) -> Result<()> {
let color = if color == ColorChoice::Auto && !atty::is(atty::Stream::Stderr) {
let color = if color == ColorChoice::Auto && !io::stderr().is_terminal() {
ColorChoice::Never
} else {
color
Expand Down
9 changes: 6 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use anyhow::{bail, Context, Result};
use std::fs::File;
use std::io::IsTerminal;
use std::io::{BufWriter, Read, Write};
use std::path::{Path, PathBuf};
use termcolor::{Ansi, ColorChoice, NoColor, StandardStream, WriteColor};
Expand Down Expand Up @@ -135,10 +136,11 @@ impl OutputArg {
.context(format!("failed to write `{}`", path.display()))?;
}
None => {
if atty::is(atty::Stream::Stdout) {
let mut stdout = std::io::stdout();
if stdout.is_terminal() {
bail!("cannot print binary wasm output to a terminal, pass the `-t` flag to print the text format");
}
std::io::stdout()
stdout
.write_all(bytes)
.context("failed to write to stdout")?;
}
Expand Down Expand Up @@ -176,7 +178,8 @@ impl OutputArg {
}
}
None => {
if color == ColorChoice::Auto && !atty::is(atty::Stream::Stdout) {
let stdout = std::io::stdout();
if color == ColorChoice::Auto && !stdout.is_terminal() {
Ok(Box::new(StandardStream::stdout(ColorChoice::Never)))
} else {
Ok(Box::new(StandardStream::stdout(color)))
Expand Down

0 comments on commit ff0ceda

Please sign in to comment.