Termsize fallback (#398)

* Fallback if /dev/tty is missing
This commit is contained in:
Atanas Yankov 2020-03-11 09:12:15 +00:00 committed by GitHub
parent 9c9479deb2
commit 52b9d479c1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 6 deletions

View File

@ -1,5 +1,6 @@
# Version 0.16.1
- Add support for converting chars to StylizedContent
- Make terminal size function fallback to `STDOUT_FILENO` if `/dev/tty` is missing.
# Version 0.16.0
- Make terminal size function work on `/dev/tty` instead of `STDOUT_FILENO`.

View File

@ -4,8 +4,8 @@ use std::{io, mem, process, sync::Mutex};
use crate::event::sys::unix::file_descriptor::FileDesc;
use lazy_static::lazy_static;
use libc::{
cfmakeraw, ioctl, tcgetattr, tcsetattr, termios as Termios, winsize, STDIN_FILENO, TCSANOW,
TIOCGWINSZ,
cfmakeraw, ioctl, tcgetattr, tcsetattr, termios as Termios, winsize, STDIN_FILENO,
STDOUT_FILENO, TCSANOW, TIOCGWINSZ,
};
use crate::error::{ErrorKind, Result};
@ -32,11 +32,14 @@ pub(crate) fn size() -> Result<(u16, u16)> {
ws_ypixel: 0,
};
let file = File::open("/dev/tty")?;
let fd = if let Ok(file) = File::open("/dev/tty") {
FileDesc::new(file.into_raw_fd(), true).raw_fd()
} else {
// Fallback to libc::STDOUT_FILENO if /dev/tty is missing
STDOUT_FILENO
};
let fd = FileDesc::new(file.into_raw_fd(), true);
if let Ok(true) = wrap_with_result(unsafe { ioctl(fd.raw_fd(), TIOCGWINSZ.into(), &mut size) })
{
if let Ok(true) = wrap_with_result(unsafe { ioctl(fd, TIOCGWINSZ.into(), &mut size) }) {
Ok((size.ws_col, size.ws_row))
} else {
tput_size().ok_or_else(|| std::io::Error::last_os_error().into())