Skip to content

Commit

Permalink
chore(IsTerminal): use the std trait IsTerminal (#880)
Browse files Browse the repository at this point in the history
The `is_terminal()` method is now part of the standard library since
rust 1.70.0, so we can remove the dependency to the `is-terminal` crate.
  • Loading branch information
xaf authored Jan 9, 2025
1 parent 6b2cd13 commit 9bbb0f5
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 22 deletions.
18 changes: 0 additions & 18 deletions Cargo.lock

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

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ globset = "0.4.14"
humantime = "2.1.0"
imara-diff = "0.1.6"
indicatif = "0.17.9"
is-terminal = "0.4.13"
itertools = "0.14.0"
lazy_static = "1.4.0"
libz-sys = { version = "1.1.21", features = ["static"] } # So we can force static linking
Expand Down
8 changes: 8 additions & 0 deletions src/internal/commands/builtin/status.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::collections::BTreeMap;
use std::collections::HashMap;
use std::io::IsTerminal;
use std::process::exit;

use regex::Regex;
Expand Down Expand Up @@ -243,6 +244,13 @@ impl StatusCommand {
yaml_lines.remove(0);
}

// If the output is not a terminal, return the yaml as is;
// this is required as usually we do not colorize when
// both stdout and stderr are not terminals
if !std::io::stdout().is_terminal() {
return yaml_lines.join("\n");
}

let pattern = r#"^(\s*)(\-\s*)?(("[^"]+"|([^:]|:[^ ])+)\s*:)(\s+|$)"#;
let regex_keys = Regex::new(pattern).unwrap();
// Replace the keys by themselves, colored
Expand Down
2 changes: 1 addition & 1 deletion src/internal/env.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::collections::HashMap;
use std::collections::HashSet;
use std::fs::OpenOptions;
use std::io::IsTerminal;
use std::io::Write;
use std::panic::catch_unwind;
use std::path::PathBuf;
Expand All @@ -12,7 +13,6 @@ use fs4::fs_std::FileExt;
use gethostname::gethostname;
use git2::Repository;
use git_url_parse::GitUrl;
use is_terminal::IsTerminal;
use lazy_static::lazy_static;
use once_cell::sync::OnceCell;
use petname::Generator;
Expand Down
6 changes: 4 additions & 2 deletions src/internal/user_interface/colors.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use is_terminal::IsTerminal;
use lazy_static::lazy_static;
use std::io::IsTerminal;

use lazy_static::lazy_static;
use regex::Regex;

use crate::internal::env::shell_is_interactive;
Expand Down Expand Up @@ -73,6 +73,8 @@ fn enable_colors() -> bool {
if std::env::var_os("CLICOLOR_FORCE").is_some() {
return true;
}
// TODO: find an approach to not depend on the stderr check for the
// PS1 colorization
std::io::stdout().is_terminal() || std::io::stderr().is_terminal()
}

Expand Down

0 comments on commit 9bbb0f5

Please sign in to comment.