Skip to content

Commit d345166

Browse files
committed
Use terminal_size for determining term size
1 parent 84ac5c0 commit d345166

File tree

4 files changed

+7
-27
lines changed

4 files changed

+7
-27
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ ansi-parsing = ["regex"]
1919
clicolors-control = "1.0.1"
2020
lazy_static = "1"
2121
libc = "0.2"
22+
terminal_size = "0.1.11"
2223
regex = { version = "1.3.1", optional = true, default-features = false, features = ["std"] }
2324
unicode-width = { version = "0.1", optional = true }
2425

src/unix_term.rs

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ use std::fmt::Display;
22
use std::fs;
33
use std::io;
44
use std::io::{BufRead, BufReader};
5-
use std::mem;
65
use std::os::unix::io::AsRawFd;
76
use std::str;
87

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

20+
#[inline]
2121
pub fn terminal_size() -> Option<(u16, u16)> {
22-
unsafe {
23-
if libc::isatty(libc::STDOUT_FILENO) != 1 {
24-
return None;
25-
}
26-
27-
let mut winsize: libc::winsize = mem::zeroed();
28-
29-
// FIXME: ".into()" used as a temporary fix for a libc bug
30-
// https://github.com/rust-lang/libc/pull/704
31-
#[allow(clippy::identity_conversion)]
32-
libc::ioctl(libc::STDOUT_FILENO, libc::TIOCGWINSZ.into(), &mut winsize);
33-
if winsize.ws_row > 0 && winsize.ws_col > 0 {
34-
Some((winsize.ws_row as u16, winsize.ws_col as u16))
35-
} else {
36-
None
37-
}
38-
}
22+
terminal_size::terminal_size().map(|x| ((x.0).0, (x.1).0))
3923
}
4024

4125
pub fn read_secure() -> io::Result<String> {

src/wasm_term.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ pub fn is_a_terminal(_out: &Term) -> bool {
1313
false
1414
}
1515

16+
#[inline]
1617
pub fn terminal_size() -> Option<(u16, u16)> {
1718
None
1819
}
@@ -31,6 +32,7 @@ pub fn read_single_key() -> io::Result<Key> {
3132
))
3233
}
3334

35+
#[inline]
3436
pub fn wants_emoji() -> bool {
3537
false
3638
}

src/windows_term.rs

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -74,16 +74,9 @@ unsafe fn console_on_any(fds: &[DWORD]) -> bool {
7474
false
7575
}
7676

77+
#[inline]
7778
pub fn terminal_size() -> Option<(u16, u16)> {
78-
let hand = unsafe { GetStdHandle(STD_OUTPUT_HANDLE) };
79-
if let Some((_, csbi)) = get_console_screen_buffer_info(hand) {
80-
Some((
81-
(csbi.srWindow.Bottom - csbi.srWindow.Top) as u16,
82-
(csbi.srWindow.Right - csbi.srWindow.Left) as u16,
83-
))
84-
} else {
85-
None
86-
}
79+
terminal_size::terminal_size().map(|x| ((x.0).0, (x.1).0))
8780
}
8881

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

0 commit comments

Comments
 (0)