diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 022af6e..1aa33f6 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -16,6 +16,7 @@ jobs: strategy: matrix: build: [linux, macos, windows] + toolchain: ["1.48.0", "stable", "beta", "nightly"] include: - build: linux os: ubuntu-latest diff --git a/Cargo.toml b/Cargo.toml index e872d5f..c1bef60 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,8 +10,8 @@ license = "MIT OR Apache-2.0" edition = "2018" -[target.'cfg(not(windows))'.dependencies.libc] -version = "0.2" +[target.'cfg(not(windows))'.dependencies] +rustix = { version = "0.35.6", features = ["termios"] } [target.'cfg(windows)'.dependencies.windows-sys] version = "0.36.0" diff --git a/README.md b/README.md index f49765c..7257233 100644 --- a/README.md +++ b/README.md @@ -22,7 +22,7 @@ if let Some((Width(w), Height(h))) = size { ## Minimum Rust Version -This crate requires a minimum rust version of 1.31.0 (2018-12-06) +This crate requires a minimum rust version of 1.48.0 (2020-11-19) ## License diff --git a/examples/get_size.rs b/examples/get_size.rs index 7595c5d..9039b42 100644 --- a/examples/get_size.rs +++ b/examples/get_size.rs @@ -26,17 +26,19 @@ fn run() { #[cfg(not(windows))] fn run() { + use std::os::unix::io::AsRawFd; + println!( "Size from terminal_size_using_fd(stdout): {:?}", - terminal_size::terminal_size_using_fd(libc::STDOUT_FILENO) + terminal_size::terminal_size_using_fd(std::io::stdout().as_raw_fd()) ); println!( "Size from terminal_size_using_fd(stderr): {:?}", - terminal_size::terminal_size_using_fd(libc::STDERR_FILENO) + terminal_size::terminal_size_using_fd(std::io::stderr().as_raw_fd()) ); println!( "Size from terminal_size_using_fd(stdin): {:?}", - terminal_size::terminal_size_using_fd(libc::STDIN_FILENO) + terminal_size::terminal_size_using_fd(std::io::stdin().as_raw_fd()) ); } diff --git a/src/lib.rs b/src/lib.rs index 6ef79be..d4b73aa 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2,7 +2,7 @@ //! //! Supports both Linux, MacOS, and Windows. //! -//! This crate requires a minimum rust version of 1.31.0 (2018-12-06) +//! This crate requires a minimum rust version of 1.48.0 (2020-11-19) //! //! # Example //! diff --git a/src/unix.rs b/src/unix.rs index 59ac276..ddc2fa8 100644 --- a/src/unix.rs +++ b/src/unix.rs @@ -1,37 +1,31 @@ use super::{Height, Width}; use std::os::unix::io::RawFd; +use rustix::fd::{BorrowedFd, AsRawFd}; /// Returns the size of the terminal defaulting to STDOUT, if available. /// /// If STDOUT is not a tty, returns `None` pub fn terminal_size() -> Option<(Width, Height)> { - terminal_size_using_fd(libc::STDOUT_FILENO) + terminal_size_using_fd(std::io::stdout().as_raw_fd()) } /// Returns the size of the terminal using the given file descriptor, if available. /// /// If the given file descriptor is not a tty, returns `None` pub fn terminal_size_using_fd(fd: RawFd) -> Option<(Width, Height)> { - use libc::ioctl; - use libc::isatty; - use libc::{winsize as WinSize, TIOCGWINSZ}; - let is_tty: bool = unsafe { isatty(fd) == 1 }; + use rustix::termios::{isatty, tcgetwinsize}; - if !is_tty { - return None; - } + // TODO: Once I/O safety is stabilized, the enlosing function here should + // be unsafe due to taking a `RawFd`. We should then move the main + // logic here into a new function which takes a `BorrowedFd` and is safe. + let fd = unsafe { BorrowedFd::borrow_raw(fd) }; - let mut winsize = WinSize { - ws_row: 0, - ws_col: 0, - ws_xpixel: 0, - ws_ypixel: 0, - }; - - if unsafe { ioctl(fd, TIOCGWINSZ.into(), &mut winsize) } == -1 { + if !isatty(fd) { return None; } + let winsize = tcgetwinsize(fd).ok()?; + let rows = winsize.ws_row; let cols = winsize.ws_col;