Skip to content

Commit

Permalink
Use is-terminal instead
Browse files Browse the repository at this point in the history
  • Loading branch information
lifegpc authored Oct 6, 2023
1 parent c3d7a90 commit 2b0acae
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 38 deletions.
35 changes: 13 additions & 22 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 @@ -8,7 +8,6 @@ edition = "2021"
[dependencies]
anyhow = { version = "1.0", optional = true }
async-trait = { version = "0.1", optional = true }
atty = "0.2"
base64 = { version = "0.21", optional = true }
bytes = { version = "1.4", optional = true }
c_fixed_string = { version = "0.2", optional = true }
Expand All @@ -28,6 +27,7 @@ http-content-range = "0.1"
hyper = { version="0.14", features = ["server"], optional = true }
indicatif = "0.17.3"
int-enum = "0.5"
is-terminal = "0.4"
itertools = "0.10"
json = "0.12"
lazy_static = "1.4"
Expand Down
38 changes: 24 additions & 14 deletions src/opt/use_progress_bar.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,29 @@
use crate::ext::use_or_not::ToBool;
use crate::ext::use_or_not::UseOrNot;
use is_terminal::IsTerminal;

#[derive(Clone, Copy, Debug)]
#[allow(dead_code)]
pub enum StreamType {
Stdout,
Stderr,
Stdin,
}

impl StreamType {
pub fn is_terminal(&self) -> bool {
match self {
Self::Stdout => std::io::stdout().is_terminal(),
Self::Stderr => std::io::stderr().is_terminal(),
Self::Stdin => std::io::stdin().is_terminal(),
}
}
}

#[derive(Clone, Copy, Debug)]
pub struct UseProgressBar {
v: UseOrNot,
stream: atty::Stream,
stream: StreamType,
}

impl AsRef<Self> for UseProgressBar {
Expand All @@ -23,7 +42,7 @@ impl Default for UseProgressBar {
fn default() -> Self {
Self {
v: UseOrNot::Auto,
stream: atty::Stream::Stdout,
stream: StreamType::Stdout,
}
}
}
Expand All @@ -32,16 +51,7 @@ impl From<bool> for UseProgressBar {
fn from(v: bool) -> Self {
Self {
v: UseOrNot::from(v),
stream: atty::Stream::Stdout,
}
}
}

impl From<atty::Stream> for UseProgressBar {
fn from(stream: atty::Stream) -> Self {
Self {
v: UseOrNot::Auto,
stream,
stream: StreamType::Stdout,
}
}
}
Expand All @@ -50,13 +60,13 @@ impl From<UseOrNot> for UseProgressBar {
fn from(v: UseOrNot) -> Self {
Self {
v,
stream: atty::Stream::Stdout,
stream: StreamType::Stdout,
}
}
}

impl ToBool for UseProgressBar {
fn detect(&self) -> bool {
atty::is(self.stream)
self.stream.is_terminal()
}
}
3 changes: 2 additions & 1 deletion src/opthelper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use crate::server::cors::CorsEntry;
use crate::settings::SettingStore;
#[cfg(feature = "ugoira")]
use crate::ugoira::X264Profile;
use is_terminal::IsTerminal;
#[cfg(feature = "server")]
use std::net::IpAddr;
#[cfg(feature = "server")]
Expand Down Expand Up @@ -462,7 +463,7 @@ impl OptHelper {
if self._use_progress_bar.get_ref().is_some() {
return self._use_progress_bar.get_ref().as_ref().unwrap().to_bool();
}
atty::is(atty::Stream::Stdout)
std::io::stdout().is_terminal()
}

/// Return progress bar's template
Expand Down

0 comments on commit 2b0acae

Please sign in to comment.