Skip to content

Commit

Permalink
If ioctl TIOCGWINSZ returns 0,0 try to use env vars instead.
Browse files Browse the repository at this point in the history
For some terminals (for example Emacs eshell/eterm) the ioctl with
TIOCGWINSZ might falsely return 0 columns and 0 rows.  If this happens
we now try to use LINES and COLUMNS environment variables.

Fixes crossterm-rs#891
  • Loading branch information
swilde committed May 17, 2024
1 parent fce58c8 commit d24f4d9
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/terminal/sys/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -53,6 +53,10 @@ pub(crate) fn window_size() -> io::Result<WindowSize> {
};

if wrap_with_result(unsafe { ioctl(fd, TIOCGWINSZ.into(), &mut size) }).is_ok() {
if size.ws_row == 0 && size.ws_col == 0 {
size.ws_row = env::var("LINES").unwrap_or("0".to_string()).parse::<u16>().unwrap();
size.ws_col = env::var("COLUMNS").unwrap_or("0".to_string()).parse::<u16>().unwrap();
}
return Ok(size.into());
}

Expand Down

0 comments on commit d24f4d9

Please sign in to comment.