Skip to content

Commit

Permalink
Merge pull request #58 from eminence/check_all_streams
Browse files Browse the repository at this point in the history
Check all streams (out/err/in) to find one that has a tty and size
  • Loading branch information
eminence authored Sep 4, 2023
2 parents 78bc3d3 + 08f0e73 commit 18c58b1
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 10 deletions.
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

0 comments on commit 18c58b1

Please sign in to comment.