diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-17 12:20:39 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-17 12:20:39 +0000 |
commit | 1376c5a617be5c25655d0d7cb63e3beaa5a6e026 (patch) | |
tree | 3bb8d61aee02bc7a15eab3f36e3b921afc2075d0 /vendor/is-terminal/src/lib.rs | |
parent | Releasing progress-linux version 1.69.0+dfsg1-1~progress7.99u1. (diff) | |
download | rustc-1376c5a617be5c25655d0d7cb63e3beaa5a6e026.tar.xz rustc-1376c5a617be5c25655d0d7cb63e3beaa5a6e026.zip |
Merging upstream version 1.70.0+dfsg1.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'vendor/is-terminal/src/lib.rs')
-rw-r--r-- | vendor/is-terminal/src/lib.rs | 52 |
1 files changed, 33 insertions, 19 deletions
diff --git a/vendor/is-terminal/src/lib.rs b/vendor/is-terminal/src/lib.rs index 18d41e14d..e363f14c1 100644 --- a/vendor/is-terminal/src/lib.rs +++ b/vendor/is-terminal/src/lib.rs @@ -31,11 +31,14 @@ use io_lifetimes::AsFilelike; #[cfg(windows)] use io_lifetimes::BorrowedHandle; +#[cfg(target_os = "hermit")] +use std::os::hermit::io::AsRawFd; #[cfg(windows)] use std::os::windows::io::AsRawHandle; #[cfg(windows)] use windows_sys::Win32::Foundation::HANDLE; +/// Extension trait to check whether something is a terminal. pub trait IsTerminal { /// Returns true if this is a terminal. /// @@ -51,6 +54,23 @@ pub trait IsTerminal { fn is_terminal(&self) -> bool; } +/// Returns `true` if `this` is a terminal. +/// +/// This is equivalent to calling `this.is_terminal()` and exists only as a +/// convenience to calling the trait method [`IsTerminal::is_terminal`] +/// without importing the trait. +/// +/// # Example +/// +/// ``` +/// if is_terminal::is_terminal(&std::io::stdout()) { +/// println!("stdout is a terminal") +/// } +/// ``` +pub fn is_terminal<T: IsTerminal>(this: &T) -> bool { + this.is_terminal() +} + #[cfg(not(target_os = "unknown"))] impl<Stream: AsFilelike> IsTerminal for Stream { #[inline] @@ -62,7 +82,7 @@ impl<Stream: AsFilelike> IsTerminal for Stream { #[cfg(target_os = "hermit")] { - hermit_abi::isatty(self.as_filelike().as_fd()) + hermit_abi::isatty(self.as_filelike().as_raw_fd()) } #[cfg(windows)] @@ -291,34 +311,28 @@ mod tests { #[test] #[cfg(any(unix, target_os = "wasi"))] fn stdin() { - unsafe { - assert_eq!( - atty::is(atty::Stream::Stdin), - rustix::io::stdin().is_terminal() - ) - } + assert_eq!( + atty::is(atty::Stream::Stdin), + rustix::io::stdin().is_terminal() + ) } #[test] #[cfg(any(unix, target_os = "wasi"))] fn stdout() { - unsafe { - assert_eq!( - atty::is(atty::Stream::Stdout), - rustix::io::stdout().is_terminal() - ) - } + assert_eq!( + atty::is(atty::Stream::Stdout), + rustix::io::stdout().is_terminal() + ) } #[test] #[cfg(any(unix, target_os = "wasi"))] fn stderr() { - unsafe { - assert_eq!( - atty::is(atty::Stream::Stderr), - rustix::io::stderr().is_terminal() - ) - } + assert_eq!( + atty::is(atty::Stream::Stderr), + rustix::io::stderr().is_terminal() + ) } #[test] |