Skip to content

Commit

Permalink
Update owo-colors
Browse files Browse the repository at this point in the history
The goal here is to remove a transitive dependency on the deprecated
`atty` crate, which trips `cargo deny` because it's unmaintained.

This currently does not eliminate the dependency completely, as we're
still waiting on `color-eyre` to release [1], and merging this PR as-is
would result in `cargo deny` being even more angry at using different
versions of the same crate, so this PR is a draft for now.

The update is non-trivial because `owo-colors` no longer supports
checking whether stdin supports colors, which is, well, reasonable.

Semantically, this cements the `Stream` trait as something that can be
printed onto, rather than a generic-purpose stream. Uses of `as_tty` are
replaced with a direct call to the `std` `is_terminal` function, and
`Stream` (now called `OwoStream`) now exclusively handles mapping `std`
I/O streams to `owo-colors` streams.

[1]: eyre-rs/eyre#215
  • Loading branch information
purplesyringa committed Jan 17, 2025
1 parent 4090bec commit 084d300
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 73 deletions.
52 changes: 24 additions & 28 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ signal-hook = { version = "0.3.15" }
directories = "4.0.1"
walkdir = { version = "2.3.2", optional = true }
tempfile = "3.3.0"
owo-colors = { version = "3.5.0", features = ["supports-colors"] }
owo-colors = { version = "4.0", features = ["supports-colors"] }
semver = "1.0.16"
is_ci = "1.1.1"

Expand Down
5 changes: 0 additions & 5 deletions deny.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,6 @@ targets = [

[advisories]
version = 2
# FIXME: remove this if/when clap changes to is-terminal, atty is
# patched, or we migrated to an MSRV of 1.66.0.
ignore = [
"RUSTSEC-2021-0145",
]

[bans]
multiple-versions = "deny"
Expand Down
6 changes: 4 additions & 2 deletions src/bin/commands/containers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ use std::io;
use clap::{Args, Subcommand};
use cross::docker::ImagePlatform;
use cross::rustc::{QualifiedToolchain, Toolchain};
use cross::shell::{MessageInfo, Stream};
use cross::shell::MessageInfo;
use cross::{docker, CommandExt, TargetTriple};
use is_terminal::IsTerminal;

#[derive(Args, Debug)]
pub struct ListVolumes {
Expand Down Expand Up @@ -327,7 +328,8 @@ pub fn create_persistent_volume(
docker.arg("--rm");
docker.args(["-v", &format!("{}:{}", volume_id, mount_prefix)]);
docker.arg("-d");
let is_tty = io::Stdin::is_atty() && io::Stdout::is_atty() && io::Stderr::is_atty();
let is_tty =
io::stdin().is_terminal() && io::stdout().is_terminal() && io::stderr().is_terminal();
if is_tty {
docker.arg("-t");
}
Expand Down
5 changes: 3 additions & 2 deletions src/docker/local.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ use super::shared::*;
use crate::errors::Result;
use crate::extensions::CommandExt;
use crate::file::{PathExt, ToUtf8};
use crate::shell::{MessageInfo, Stream};
use crate::shell::MessageInfo;
use eyre::Context;
use is_terminal::IsTerminal;

// NOTE: host path must be absolute
fn mount(
Expand Down Expand Up @@ -136,7 +137,7 @@ pub(crate) fn run(
]);
}

if io::Stdin::is_atty() && io::Stdout::is_atty() && io::Stderr::is_atty() {
if io::stdin().is_terminal() && io::stdout().is_terminal() && io::stderr().is_terminal() {
docker.arg("-t");
}

Expand Down
6 changes: 4 additions & 2 deletions src/docker/remote.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use std::process::{Command, ExitStatus};
use std::{env, fs, time};

use eyre::Context;
use is_terminal::IsTerminal;

use super::engine::Engine;
use super::shared::*;
Expand All @@ -13,7 +14,7 @@ use crate::errors::Result;
use crate::extensions::CommandExt;
use crate::file::{self, PathExt, ToUtf8};
use crate::rustc::{self, QualifiedToolchain, VersionMetaExt};
use crate::shell::{MessageInfo, Stream};
use crate::shell::MessageInfo;
use crate::temp;
use crate::TargetTriple;

Expand Down Expand Up @@ -774,7 +775,8 @@ pub(crate) fn run(
}

docker.arg("-d");
let is_tty = io::Stdin::is_atty() && io::Stdout::is_atty() && io::Stderr::is_atty();
let is_tty =
io::stdin().is_terminal() && io::stdout().is_terminal() && io::stderr().is_terminal();
if is_tty {
docker.arg("-t");
}
Expand Down
42 changes: 9 additions & 33 deletions src/shell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use std::str::FromStr;

use crate::config::bool_from_envvar;
use crate::errors::Result;
use is_terminal::IsTerminal;
use owo_colors::{self, OwoColorize};

// get the prefix for stderr messages
Expand Down Expand Up @@ -201,7 +200,7 @@ impl MessageInfo {
self.as_verbosity(call, Verbosity::Verbose(2))
}

fn erase_line<S: Stream + Write>(&mut self, stream: &mut S) -> Result<()> {
fn erase_line<S: OwoStream + Write>(&mut self, stream: &mut S) -> Result<()> {
// this is the Erase in Line sequence
stream.write_all(b"\x1B[K").map_err(Into::into)
}
Expand Down Expand Up @@ -449,42 +448,19 @@ fn get_verbosity(
})
}

pub trait Stream {
type TTY: IsTerminal;
const OWO: owo_colors::Stream;

#[must_use]
fn is_atty() -> bool;

fn owo(&self) -> owo_colors::Stream {
Self::OWO
}
trait OwoStream {
fn owo(&self) -> owo_colors::Stream;
}

impl Stream for io::Stdin {
type TTY = io::Stdin;
const OWO: owo_colors::Stream = owo_colors::Stream::Stdin;

fn is_atty() -> bool {
io::stdin().is_terminal()
}
}

impl Stream for io::Stdout {
type TTY = io::Stdout;
const OWO: owo_colors::Stream = owo_colors::Stream::Stdout;

fn is_atty() -> bool {
io::stdout().is_terminal()
impl OwoStream for io::Stdout {
fn owo(&self) -> owo_colors::Stream {
owo_colors::Stream::Stdout
}
}

impl Stream for io::Stderr {
type TTY = io::Stderr;
const OWO: owo_colors::Stream = owo_colors::Stream::Stderr;

fn is_atty() -> bool {
io::stderr().is_terminal()
impl OwoStream for io::Stderr {
fn owo(&self) -> owo_colors::Stream {
owo_colors::Stream::Stderr
}
}

Expand Down

0 comments on commit 084d300

Please sign in to comment.