Skip to content

io: implement is_terminal for stdio types #7114

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
3 changes: 2 additions & 1 deletion spellcheck.dic
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
299
300
&
+
<
@@ -263,6 +263,7 @@ tokio
Tokio
tokio's
Tokio's
tty
tuple
Tuple
tx
7 changes: 7 additions & 0 deletions tokio/src/io/blocking.rs
Original file line number Diff line number Diff line change
@@ -50,6 +50,13 @@ cfg_io_blocking! {
}
}

impl<T> Blocking<T> {
#[cfg_attr(not(feature = "io-std"), allow(dead_code))]
pub(crate) fn inner(&self) -> Option<&T> {
self.inner.as_ref()
}
}

impl<T> AsyncRead for Blocking<T>
where
T: Read + Unpin + Send + 'static,
12 changes: 12 additions & 0 deletions tokio/src/io/stderr.rs
Original file line number Diff line number Diff line change
@@ -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;
@@ -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()
Comment on lines +138 to +144
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will incorrectly return false if stderr is a terminal but there is an ongoing IO operation. Can we instead do this?

Suggested change
/// 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()
/// Returns true if the descriptor/handle refers to a terminal/tty.
pub fn is_terminal(&self) -> bool {
std::io::stderr().is_terminal()

}
}
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;
@@ -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
@@ -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.
12 changes: 12 additions & 0 deletions tokio/src/io/stdout.rs
Original file line number Diff line number Diff line change
@@ -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;
@@ -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
@@ -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 {
@@ -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 {
@@ -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 {
6 changes: 5 additions & 1 deletion tokio/src/process/unix/mod.rs
Original file line number Diff line number Diff line change
@@ -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};
@@ -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 {
15 changes: 14 additions & 1 deletion tokio/src/process/windows.rs
Original file line number Diff line number Diff line change
@@ -24,7 +24,10 @@ 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::io::IsTerminal;
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};
@@ -199,6 +202,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 {
@@ -207,6 +214,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>,