Skip to content

Commit

Permalink
Use terminal_size for determining term size
Browse files Browse the repository at this point in the history
  • Loading branch information
pksunkara committed Apr 26, 2020
1 parent 84ac5c0 commit d345166
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 27 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ ansi-parsing = ["regex"]
clicolors-control = "1.0.1"
lazy_static = "1"
libc = "0.2"
terminal_size = "0.1.11"
regex = { version = "1.3.1", optional = true, default-features = false, features = ["std"] }
unicode-width = { version = "0.1", optional = true }

Expand Down
20 changes: 2 additions & 18 deletions src/unix_term.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use std::fmt::Display;
use std::fs;
use std::io;
use std::io::{BufRead, BufReader};
use std::mem;
use std::os::unix::io::AsRawFd;
use std::str;

Expand All @@ -18,24 +17,9 @@ pub fn is_a_terminal(out: &Term) -> bool {
unsafe { libc::isatty(out.as_raw_fd()) != 0 }
}

#[inline]
pub fn terminal_size() -> Option<(u16, u16)> {
unsafe {
if libc::isatty(libc::STDOUT_FILENO) != 1 {
return None;
}

let mut winsize: libc::winsize = mem::zeroed();

// FIXME: ".into()" used as a temporary fix for a libc bug
// https://github.com/rust-lang/libc/pull/704
#[allow(clippy::identity_conversion)]
libc::ioctl(libc::STDOUT_FILENO, libc::TIOCGWINSZ.into(), &mut winsize);
if winsize.ws_row > 0 && winsize.ws_col > 0 {
Some((winsize.ws_row as u16, winsize.ws_col as u16))
} else {
None
}
}
terminal_size::terminal_size().map(|x| ((x.0).0, (x.1).0))
}

pub fn read_secure() -> io::Result<String> {
Expand Down
2 changes: 2 additions & 0 deletions src/wasm_term.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ pub fn is_a_terminal(_out: &Term) -> bool {
false
}

#[inline]
pub fn terminal_size() -> Option<(u16, u16)> {
None
}
Expand All @@ -31,6 +32,7 @@ pub fn read_single_key() -> io::Result<Key> {
))
}

#[inline]
pub fn wants_emoji() -> bool {
false
}
Expand Down
11 changes: 2 additions & 9 deletions src/windows_term.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,16 +74,9 @@ unsafe fn console_on_any(fds: &[DWORD]) -> bool {
false
}

#[inline]
pub fn terminal_size() -> Option<(u16, u16)> {
let hand = unsafe { GetStdHandle(STD_OUTPUT_HANDLE) };
if let Some((_, csbi)) = get_console_screen_buffer_info(hand) {
Some((
(csbi.srWindow.Bottom - csbi.srWindow.Top) as u16,
(csbi.srWindow.Right - csbi.srWindow.Left) as u16,
))
} else {
None
}
terminal_size::terminal_size().map(|x| ((x.0).0, (x.1).0))
}

pub fn move_cursor_to(out: &Term, x: usize, y: usize) -> io::Result<()> {
Expand Down

0 comments on commit d345166

Please sign in to comment.