Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "console"
description = "A terminal and console abstraction for Rust"
version = "0.16.3"
version = "0.16.4"
keywords = ["cli", "terminal", "colors", "console", "ansi"]
license = "MIT"
edition = "2021"
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ extern crate alloc;
pub use crate::kb::Key;
#[cfg(feature = "std")]
pub use crate::term::{
user_attended, user_attended_stderr, Term, TermFamily, TermFeatures, TermTarget,
is_dumb, user_attended, user_attended_stderr, Term, TermFamily, TermFeatures, TermTarget,
};
#[cfg(feature = "std")]
pub use crate::utils::{
Expand Down
21 changes: 21 additions & 0 deletions src/term.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use alloc::sync::Arc;
use core::fmt::{Debug, Display};
use std::env;
use std::io::{self, Read, Write};
#[cfg(any(unix, all(target_os = "wasi", target_env = "p1")))]
use std::os::fd::{AsRawFd, RawFd};
Expand Down Expand Up @@ -568,6 +569,26 @@ impl Term {
}
}

/// A fast way to check if the application has a dumb terminal.
///
/// On Unix: `TERM` environment variable is not set or set to `dumb`.
///
/// On Windows: `TERM` environment variable is explicitly set to `dumb`.
/// Native windows terminals typically do not set the `TERM` environment variable.
#[inline]
pub fn is_dumb() -> bool {
#[cfg(windows)]
let default = false;

#[cfg(not(windows))]
let default = true;

match env::var("TERM") {
Ok(term) => term == "dumb",
Err(_) => default,
}
}

/// A fast way to check if the application has a user attended for stdout.
///
/// This means that stdout is connected to a terminal instead of a
Expand Down
6 changes: 2 additions & 4 deletions src/unix_term.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use std::os::fd::{AsRawFd, RawFd};
#[cfg(not(target_os = "macos"))]
use std::sync::OnceLock;

use crate::is_dumb;
use crate::kb::Key;
use crate::term::Term;

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

match env::var("TERM") {
Ok(term) => term != "dumb",
Err(_) => false,
}
!is_dumb()
}

pub(crate) fn is_a_true_color_terminal(out: &Term) -> bool {
Expand Down
7 changes: 2 additions & 5 deletions src/windows_term/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ use windows_sys::Win32::System::Console::{
};
use windows_sys::Win32::UI::Input::KeyboardAndMouse::VIRTUAL_KEY;

use crate::common_term;
use crate::kb::Key;
use crate::term::{Term, TermTarget};
use crate::{common_term, is_dumb};

#[cfg(feature = "windows-console-colors")]
mod colors;
Expand Down Expand Up @@ -67,10 +67,7 @@ pub(crate) fn is_a_color_terminal(out: &Term) -> bool {
return false;
}
if msys_tty_on(out) {
return match env::var("TERM") {
Ok(term) => term != "dumb",
Err(_) => true,
};
return !is_dumb();
}
enable_ansi_on(out)
}
Expand Down
Loading