Skip to content

Commit

Permalink
implement is_terminal for stdio types
Browse files Browse the repository at this point in the history
  • Loading branch information
maminrayej committed Jan 19, 2025
1 parent a82bdee commit af267bb
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 3 deletions.
6 changes: 6 additions & 0 deletions tokio/src/io/blocking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ cfg_io_blocking! {
}
}

impl<T> Blocking<T> {
pub(crate) fn inner(&self) -> Option<&T> {
self.inner.as_ref()
}
}

impl<T> AsyncRead for Blocking<T>
where
T: Read + Unpin + Send + 'static,
Expand Down
12 changes: 12 additions & 0 deletions tokio/src/io/stderr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use crate::io::stdio_common::SplitByUtf8BoundaryIfWindows;
use crate::io::AsyncWrite;

use std::io;
use std::io::IsTerminal;
use std::pin::Pin;
use std::task::Context;
use std::task::Poll;
Expand Down Expand Up @@ -132,3 +133,14 @@ impl AsyncWrite for Stderr {
Pin::new(&mut self.std).poll_shutdown(cx)
}
}

impl Stderr {
/// Returns true if the descriptor/handle refers to a terminal/tty.
pub fn is_terminal(&self) -> bool {
self.std
.inner()
.inner()
.map(|stderr| stderr.is_terminal())
.unwrap_or_default()
}
}
12 changes: 11 additions & 1 deletion tokio/src/io/stdin.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::io::blocking::Blocking;
use crate::io::{AsyncRead, ReadBuf};

use std::io;
use std::io::{self, IsTerminal};
use std::pin::Pin;
use std::task::Context;
use std::task::Poll;
Expand Down Expand Up @@ -96,3 +96,13 @@ impl AsyncRead for Stdin {
Pin::new(&mut self.std).poll_read(cx, buf)
}
}

impl Stdin {
/// Returns true if the descriptor/handle refers to a terminal/tty.
pub fn is_terminal(&self) -> bool {
self.std
.inner()
.map(|stdin| stdin.is_terminal())
.unwrap_or_default()
}
}
4 changes: 4 additions & 0 deletions tokio/src/io/stdio_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ impl<W> SplitByUtf8BoundaryIfWindows<W> {
pub(crate) fn new(inner: W) -> Self {
Self { inner }
}

pub(crate) fn inner(&self) -> &W {
&self.inner
}
}

// this constant is defined by Unicode standard.
Expand Down
12 changes: 12 additions & 0 deletions tokio/src/io/stdout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::io::blocking::Blocking;
use crate::io::stdio_common::SplitByUtf8BoundaryIfWindows;
use crate::io::AsyncWrite;
use std::io;
use std::io::IsTerminal;
use std::pin::Pin;
use std::task::Context;
use std::task::Poll;
Expand Down Expand Up @@ -181,3 +182,14 @@ impl AsyncWrite for Stdout {
Pin::new(&mut self.std).poll_shutdown(cx)
}
}

impl Stdout {
/// Returns true if the descriptor/handle refers to a terminal/tty.
pub fn is_terminal(&self) -> bool {
self.std
.inner()
.inner()
.map(|stdout| stdout.is_terminal())
.unwrap_or_default()
}
}
15 changes: 15 additions & 0 deletions tokio/src/process/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1381,6 +1381,11 @@ impl ChildStdin {
inner: imp::stdio(inner)?,
})
}

/// Returns true if the descriptor/handle refers to a terminal/tty.
pub fn is_terminal(&self) -> bool {
self.inner.is_terminal()
}
}

impl ChildStdout {
Expand All @@ -1396,6 +1401,11 @@ impl ChildStdout {
inner: imp::stdio(inner)?,
})
}

/// Returns true if the descriptor/handle refers to a terminal/tty.
pub fn is_terminal(&self) -> bool {
self.inner.is_terminal()
}
}

impl ChildStderr {
Expand All @@ -1411,6 +1421,11 @@ impl ChildStderr {
inner: imp::stdio(inner)?,
})
}

/// Returns true if the descriptor/handle refers to a terminal/tty.
pub fn is_terminal(&self) -> bool {
self.inner.is_terminal()
}
}

impl AsyncWrite for ChildStdin {
Expand Down
6 changes: 5 additions & 1 deletion tokio/src/process/unix/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ use mio::unix::SourceFd;
use std::fmt;
use std::fs::File;
use std::future::Future;
use std::io;
use std::io::{self, IsTerminal};
use std::os::unix::io::{AsFd, AsRawFd, BorrowedFd, FromRawFd, IntoRawFd, OwnedFd, RawFd};
use std::pin::Pin;
use std::process::{Child as StdChild, ExitStatus, Stdio};
Expand Down Expand Up @@ -279,6 +279,10 @@ impl ChildStdio {
pub(super) fn into_owned_fd(self) -> io::Result<OwnedFd> {
convert_to_blocking_file(self).map(OwnedFd::from)
}

pub(crate) fn is_terminal(&self) -> bool {
self.as_fd().is_terminal()
}
}

impl fmt::Debug for ChildStdio {
Expand Down
14 changes: 13 additions & 1 deletion tokio/src/process/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ use std::fmt;
use std::fs::File as StdFile;
use std::future::Future;
use std::io;
use std::os::windows::prelude::{AsRawHandle, IntoRawHandle, OwnedHandle, RawHandle};
use std::os::windows::prelude::{
AsHandle, AsRawHandle, BorrowedHandle, IntoRawHandle, OwnedHandle, RawHandle,
};
use std::pin::Pin;
use std::process::Stdio;
use std::process::{Child as StdChild, Command as StdCommand, ExitStatus};
Expand Down Expand Up @@ -199,6 +201,10 @@ impl ChildStdio {
pub(super) fn into_owned_handle(self) -> io::Result<OwnedHandle> {
convert_to_file(self).map(OwnedHandle::from)
}

pub(crate) fn is_terminal(&self) -> bool {
self.as_handle().is_terminal()
}
}

impl AsRawHandle for ChildStdio {
Expand All @@ -207,6 +213,12 @@ impl AsRawHandle for ChildStdio {
}
}

impl AsHandle for ChildStdio {
fn as_handle(&self) -> BorrowedHandle<'_> {
self.raw.as_handle()
}
}

impl AsyncRead for ChildStdio {
fn poll_read(
mut self: Pin<&mut Self>,
Expand Down

0 comments on commit af267bb

Please sign in to comment.