Trait IsTerminal
trait IsTerminal: crate::sealed::Sealed
Trait to determine if a descriptor/handle refers to a terminal/tty.
Required Methods
fn is_terminal(self: &Self) -> boolReturns
trueif the descriptor/handle refers to a terminal/tty.On platforms where Rust does not know how to detect a terminal yet, this will return
false. This will also returnfalseif an unexpected error occurred, such as from passing an invalid file descriptor.Platform-specific behavior
On Windows, in addition to detecting consoles, this currently uses some heuristics to detect older msys/cygwin/mingw pseudo-terminals based on device name: devices with names starting with
msys-orcygwin-and ending in-ptywill be considered terminals. Note that this may change in the future.Examples
An example of a type for which
IsTerminalis implemented isStdin:use std::io::{self, IsTerminal, Write}; fn main() -> io::Result<()> { let stdin = io::stdin(); // Indicate that the user is prompted for input, if this is a terminal. if stdin.is_terminal() { print!("> "); io::stdout().flush()?; } let mut name = String::new(); let _ = stdin.read_line(&mut name)?; println!("Hello {}", name.trim_end()); Ok(()) }The example can be run in two ways:
- If you run this example by piping some text to it, e.g.
echo "foo" | path/to/executableit will print:Hello foo. - If you instead run the example interactively by running
path/to/executabledirectly, it will prompt for input.
- If you run this example by piping some text to it, e.g.
Implementors
impl IsTerminal for Stdinimpl IsTerminal for OwnedFdimpl IsTerminal for BorrowedHandle<'_>impl IsTerminal for Stderrimpl IsTerminal for StderrLock<'_>impl IsTerminal for BorrowedFd<'_>impl IsTerminal for Stdoutimpl IsTerminal for StdoutLock<'_>impl IsTerminal for Fileimpl IsTerminal for StdinLock<'_>impl IsTerminal for OwnedHandle