Skip to content

Commit 86aafb0

Browse files
committed
feat: add is_dumb public function
1 parent 1426649 commit 86aafb0

4 files changed

Lines changed: 26 additions & 10 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: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use alloc::sync::Arc;
22
use core::fmt::{Debug, Display};
3+
use std::env;
34
use std::io::{self, Read, Write};
45
#[cfg(any(unix, all(target_os = "wasi", target_env = "p1")))]
56
use std::os::fd::{AsRawFd, RawFd};
@@ -568,6 +569,26 @@ impl Term {
568569
}
569570
}
570571

572+
/// A fast way to check if the application has a dumb terminal.
573+
///
574+
/// On Unix: `TERM` environment variable is not set or set to `dumb`.
575+
///
576+
/// On Windows: `TERM` environment variable is explicitly set to `dumb`.
577+
/// Native windows terminals typically do not set the `TERM` environment variable.
578+
#[inline]
579+
pub fn is_dumb() -> bool {
580+
#[cfg(windows)]
581+
let default = false;
582+
583+
#[cfg(not(windows))]
584+
let default = true;
585+
586+
match env::var("TERM") {
587+
Ok(term) => term == "dumb",
588+
Err(_) => default,
589+
}
590+
}
591+
571592
/// A fast way to check if the application has a user attended for stdout.
572593
///
573594
/// This means that stdout is connected to a terminal instead of a

src/unix_term.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use std::os::fd::{AsRawFd, RawFd};
99
#[cfg(not(target_os = "macos"))]
1010
use std::sync::OnceLock;
1111

12+
use crate::is_dumb;
1213
use crate::kb::Key;
1314
use crate::term::Term;
1415

@@ -30,10 +31,7 @@ pub(crate) fn is_a_color_terminal(out: &Term) -> bool {
3031
return false;
3132
}
3233

33-
match env::var("TERM") {
34-
Ok(term) => term != "dumb",
35-
Err(_) => false,
36-
}
34+
!is_dumb()
3735
}
3836

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

src/windows_term/mod.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ use windows_sys::Win32::System::Console::{
2424
};
2525
use windows_sys::Win32::UI::Input::KeyboardAndMouse::VIRTUAL_KEY;
2626

27-
use crate::common_term;
2827
use crate::kb::Key;
2928
use crate::term::{Term, TermTarget};
29+
use crate::{common_term, is_dumb};
3030

3131
#[cfg(feature = "windows-console-colors")]
3232
mod colors;
@@ -67,10 +67,7 @@ pub(crate) fn is_a_color_terminal(out: &Term) -> bool {
6767
return false;
6868
}
6969
if msys_tty_on(out) {
70-
return match env::var("TERM") {
71-
Ok(term) => term != "dumb",
72-
Err(_) => true,
73-
};
70+
return !is_dumb();
7471
}
7572
enable_ansi_on(out)
7673
}

0 commit comments

Comments
 (0)