Skip to content

Commit

Permalink
Add 'is_terminal' function for importless checking. (#23)
Browse files Browse the repository at this point in the history
* Add 'is_terminal' function for importless checking.

* Document that 'is_terminal()' is a convenience.
  • Loading branch information
SergioBenitez authored Mar 28, 2023
1 parent 2a48188 commit cc90de8
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ 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.
///
Expand All @@ -53,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]
Expand Down

0 comments on commit cc90de8

Please sign in to comment.