Skip to content

Fix zero size on TTY over SSH #24

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 26 additions & 2 deletions src/nix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ extern crate libc;

use std::io::IsTerminal;

use std::ffi::{c_ushort, CString};

use self::{
super::Size,
libc::{c_ushort, ioctl, STDOUT_FILENO, TIOCGWINSZ},
libc::{ioctl, O_RDONLY, STDOUT_FILENO, TIOCGWINSZ},
};

/// A representation of the size of the current terminal
Expand All @@ -31,7 +33,29 @@ pub fn get() -> Option<Size> {
x: 0,
y: 0,
};
let r = unsafe { ioctl(STDOUT_FILENO, TIOCGWINSZ, &mut us) };

let fd = if let Ok(ssh_term) = std::env::var("SSH_TTY") {
// Convert path to a C-compatible string
let c_path = CString::new(ssh_term).expect("Failed to convert path to CString");

// Open the terminal device
let fd = unsafe { libc::open(c_path.as_ptr(), O_RDONLY) };
if fd < 0 {
return None; // Failed to open the terminal device
}

fd
} else {
STDOUT_FILENO
};

let r = unsafe { ioctl(fd, TIOCGWINSZ, &mut us) };

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should there be a libc::close call here after r?


// Closing the open file descriptor
if fd != STDOUT_FILENO {
unsafe { libc::close(fd); }
}

if r == 0 {
Some(Size {
rows: us.rows,
Expand Down