diff --git a/src/terminal/sys/unix.rs b/src/terminal/sys/unix.rs index ed545c5b..ed43f808 100644 --- a/src/terminal/sys/unix.rs +++ b/src/terminal/sys/unix.rs @@ -13,7 +13,7 @@ use std::fs::File; use std::os::unix::io::{IntoRawFd, RawFd}; -use std::{io, mem, process}; +use std::{env, io, mem, process}; // Some(Termios) -> we're in the raw mode and this is the previous mode // None -> we're not in the raw mode @@ -53,6 +53,14 @@ pub(crate) fn window_size() -> io::Result { }; if wrap_with_result(unsafe { ioctl(fd, TIOCGWINSZ.into(), &mut size) }).is_ok() { + // For some terminals (for example Emacs eshell/eterm) the + // ioctl with TIOCGWINSZ might falsely return 0 columns and 0 + // rows. If this happens we try to use environment variables + // to determine the window size. + if size.ws_row == 0 && size.ws_col == 0 { + size.ws_row = env::var("LINES").map_or(Ok(0), |v| v.parse()).unwrap_or(0); + size.ws_col = env::var("COLUMNS").map_or(Ok(0), |v| v.parse()).unwrap_or(0); + } return Ok(size.into()); }