Skip to content
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

Check all streams (out/err/in) to find one that has a tty and size #58

Merged
merged 1 commit into from
Sep 4, 2023
Merged
Show file tree
Hide file tree
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
4 changes: 3 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,6 @@ mod windows;
pub use crate::windows::{terminal_size, terminal_size_using_handle};

#[cfg(not(any(unix, windows)))]
pub fn terminal_size() -> Option<(Width, Height)> { None }
pub fn terminal_size() -> Option<(Width, Height)> {
None
}
18 changes: 14 additions & 4 deletions src/unix.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
use super::{Height, Width};
use std::os::unix::io::RawFd;
use rustix::fd::BorrowedFd;
use std::os::unix::io::RawFd;

/// Returns the size of the terminal defaulting to STDOUT, if available.
/// Returns the size of the terminal.
///
/// If STDOUT is not a tty, returns `None`
/// This function checks the stdout, stderr, and stdin streams (in that order).
/// The size of the first stream that is a TTY will be returned. If nothing
/// is a TTY, then `None` is returned.
pub fn terminal_size() -> Option<(Width, Height)> {
terminal_size_using_fd(rustix::io::raw_stdout())
if let Some(size) = terminal_size_using_fd(rustix::io::raw_stdout()) {
Some(size)
} else if let Some(size) = terminal_size_using_fd(rustix::io::raw_stderr()) {
Some(size)
} else if let Some(size) = terminal_size_using_fd(rustix::io::raw_stdin()) {
Some(size)
} else {
None
}
}

/// Returns the size of the terminal using the given file descriptor, if available.
Expand Down
28 changes: 23 additions & 5 deletions src/windows.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,34 @@
use super::{Height, Width};
use std::os::windows::io::RawHandle;

/// Returns the size of the terminal defaulting to STDOUT, if available.
/// Returns the size of the terminal.
///
/// This function checks the stdout, stderr, and stdin streams (in that order).
/// The size of the first stream that is a TTY will be returned. If nothing
/// is a TTY, then `None` is returned.
///
/// Note that this returns the size of the actual command window, and
/// not the overall size of the command window buffer
pub fn terminal_size() -> Option<(Width, Height)> {
use windows_sys::Win32::System::Console::{GetStdHandle, STD_OUTPUT_HANDLE};

let handle = unsafe { GetStdHandle(STD_OUTPUT_HANDLE) as RawHandle };
use windows_sys::Win32::System::Console::{
GetStdHandle, STD_ERROR_HANDLE, STD_INPUT_HANDLE, STD_OUTPUT_HANDLE,
};

terminal_size_using_handle(handle)
if let Some(size) =
terminal_size_using_handle(unsafe { GetStdHandle(STD_OUTPUT_HANDLE) as RawHandle })
{
Some(size)
} else if let Some(size) =
terminal_size_using_handle(unsafe { GetStdHandle(STD_ERROR_HANDLE) as RawHandle })
{
Some(size)
} else if let Some(size) =
terminal_size_using_handle(unsafe { GetStdHandle(STD_INPUT_HANDLE) as RawHandle })
{
Some(size)
} else {
None
}
}

/// Returns the size of the terminal using the given handle, if available.
Expand Down