Skip to content

Commit

Permalink
Merge pull request #33 from sunfishcode/rustix
Browse files Browse the repository at this point in the history
Migrate from libc to rustix.
  • Loading branch information
eminence committed Jul 2, 2022
2 parents c018a71 + c07fbf4 commit f849618
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 23 deletions.
1 change: 1 addition & 0 deletions .github/workflows/main.yml
Expand Up @@ -16,6 +16,7 @@ jobs:
strategy:
matrix:
build: [linux, macos, windows]
toolchain: ["1.48.0", "stable", "beta", "nightly"]
include:
- build: linux
os: ubuntu-latest
Expand Down
4 changes: 2 additions & 2 deletions Cargo.toml
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -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

Expand Down
8 changes: 5 additions & 3 deletions examples/get_size.rs
Expand Up @@ -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())
);
}

Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Expand Up @@ -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
//!
Expand Down
26 changes: 10 additions & 16 deletions 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;

Expand Down

0 comments on commit f849618

Please sign in to comment.