Skip to content

Commit 854c535

Browse files
committed
feat: add is_dumb public function
1 parent 1426649 commit 854c535

5 files changed

Lines changed: 36 additions & 9 deletions

File tree

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ extern crate alloc;
8989
pub use crate::kb::Key;
9090
#[cfg(feature = "std")]
9191
pub use crate::term::{
92-
user_attended, user_attended_stderr, Term, TermFamily, TermFeatures, TermTarget,
92+
is_dumb, user_attended, user_attended_stderr, Term, TermFamily, TermFeatures, TermTarget,
9393
};
9494
#[cfg(feature = "std")]
9595
pub use crate::utils::{

src/term.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -568,6 +568,17 @@ impl Term {
568568
}
569569
}
570570

571+
/// A fast way to check if the application has a dumb terminal.
572+
///
573+
/// On Unix: `TERM` environment variable is not set or set to `dumb`.
574+
///
575+
/// On Windows: `TERM` environment variable is explicitly set to `dumb`.
576+
/// Native windows terminals typically do not set the `TERM` environment variable.
577+
#[inline]
578+
pub fn is_dumb() -> bool {
579+
is_a_dumb_terminal()
580+
}
581+
571582
/// A fast way to check if the application has a user attended for stdout.
572583
///
573584
/// This means that stdout is connected to a terminal instead of a

src/unix_term.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,13 @@ pub(crate) fn is_a_terminal(out: &impl AsRawFd) -> bool {
2121
unsafe { libc::isatty(out.as_raw_fd()) != 0 }
2222
}
2323

24+
pub(crate) fn is_a_dumb_terminal() -> bool {
25+
match env::var("TERM") {
26+
Ok(term) => term == "dumb",
27+
Err(_) => true,
28+
}
29+
}
30+
2431
pub(crate) fn is_a_color_terminal(out: &Term) -> bool {
2532
if !is_a_terminal(out) {
2633
return false;
@@ -30,10 +37,7 @@ pub(crate) fn is_a_color_terminal(out: &Term) -> bool {
3037
return false;
3138
}
3239

33-
match env::var("TERM") {
34-
Ok(term) => term != "dumb",
35-
Err(_) => false,
36-
}
40+
!is_a_dumb_terminal()
3741
}
3842

3943
pub(crate) fn is_a_true_color_terminal(out: &Term) -> bool {

src/wasm_term.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ pub(crate) fn is_a_terminal(_out: &Term) -> bool {
2121
}
2222
}
2323

24+
#[inline]
25+
pub(crate) fn is_a_dumb_terminal() -> bool {
26+
false
27+
}
28+
2429
#[inline]
2530
pub(crate) fn is_a_color_terminal(_out: &Term) -> bool {
2631
// We currently never report color terminals. For discussion see

src/windows_term/mod.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,16 @@ pub(crate) fn is_a_terminal(out: &Term) -> bool {
5959
msys_tty_on(out)
6060
}
6161

62+
pub(crate) fn is_a_dumb_terminal() -> bool {
63+
// Native windows terminals typically do not set the TERM.
64+
// We can only assume that if it is explicitly set to `dumb` then it is a dumb terminal.
65+
// Otherwise, we assume it is not.
66+
match env::var("TERM") {
67+
Ok(term) => term == "dumb",
68+
Err(_) => false,
69+
}
70+
}
71+
6272
pub(crate) fn is_a_color_terminal(out: &Term) -> bool {
6373
if !is_a_terminal(out) {
6474
return false;
@@ -67,10 +77,7 @@ pub(crate) fn is_a_color_terminal(out: &Term) -> bool {
6777
return false;
6878
}
6979
if msys_tty_on(out) {
70-
return match env::var("TERM") {
71-
Ok(term) => term != "dumb",
72-
Err(_) => true,
73-
};
80+
return !is_a_dumb_terminal();
7481
}
7582
enable_ansi_on(out)
7683
}

0 commit comments

Comments
 (0)