Use term_size instead of ioctl

This commit is contained in:
Alexandre Bury 2018-06-18 16:00:58 -07:00
parent f3d822c00c
commit 26e5f51192
5 changed files with 24 additions and 38 deletions

View File

@ -24,10 +24,7 @@ unicode-width = "0.1"
xi-unicode = "0.1.0"
libc = "0.2"
chan = "0.1"
[dependencies.ioctl-rs]
optional = true
version = "0.2"
term_size = { version = "0.3.1", optional = true }
[dependencies.num]
default-features = false
@ -71,8 +68,8 @@ pretty-bytes = "0.2.2"
blt-backend = ["bear-lib-terminal"]
default = ["ncurses-backend"]
markdown = ["pulldown-cmark"]
ncurses-backend = ["ncurses", "maplit", "ioctl-rs"]
pancurses-backend = ["pancurses", "maplit", "ioctl-rs"]
ncurses-backend = ["ncurses", "maplit", "term_size"]
pancurses-backend = ["pancurses", "maplit", "term_size"]
termion-backend = ["termion"]
[lib]

View File

@ -1,20 +1,32 @@
//! Common module for the ncurses and pancurses backends.
//!
//! Requires either of `ncurses-backend` or `pancurses-backend`.
#![cfg(any(feature = "ncurses", feature = "pancurses"))]
#![cfg(any(feature = "ncurses-backend", feature = "pancurses-backend"))]
extern crate term_size;
use std::collections::HashMap;
use event::{Event, Key};
use std::collections::HashMap;
use theme::{BaseColor, Color, ColorPair};
mod sizes;
use vec::Vec2;
#[cfg(feature = "ncurses")]
#[cfg(feature = "ncurses-backend")]
pub mod n;
#[cfg(feature = "pancurses")]
#[cfg(feature = "pancurses-backend")]
pub mod pan;
/// Get the size of the terminal.
///
/// Usually ncurses can do that by himself, but because we're playing with
/// threads, ncurses' signal handler is confused and he can't keep track of
/// the terminal size. Poor ncurses.
fn terminal_size() -> Vec2 {
term_size::dimensions().unwrap_or((0,0)).into()
}
fn split_i32(code: i32) -> Vec<u8> {
(0..4).map(|i| ((code >> (8 * i)) & 0xFF) as u8).collect()
}

View File

@ -264,8 +264,8 @@ impl Backend {
///
/// We need to have ncurses update its representation of the screen.
fn on_resize() {
// Get size using ioctl
let size = super::sizes::terminal_size();
// Get size
let size = super::terminal_size();
// Send the size to ncurses
ncurses::resize_term(size.y as i32, size.x as i32);

View File

@ -357,8 +357,8 @@ impl Backend {
///
/// We need to have ncurses update its representation of the screen.
fn on_resize() {
// Get size using ioctl
let size = super::sizes::terminal_size();
// Get size
let size = super::terminal_size();
// Send the size to ncurses
pancurses::resize_term(size.y as i32, size.x as i32);

View File

@ -1,23 +0,0 @@
extern crate ioctl_rs as ioctl;
use libc::{c_ushort, STDOUT_FILENO};
use std::mem;
use vec::Vec2;
#[repr(C)]
struct TermSize {
row: c_ushort,
col: c_ushort,
_x: c_ushort,
_y: c_ushort,
}
/// Get the size of the terminal.
pub fn terminal_size() -> Vec2 {
unsafe {
let mut size: TermSize = mem::zeroed();
ioctl::ioctl(STDOUT_FILENO, ioctl::TIOCGWINSZ, &mut size as *mut _);
Vec2::new(size.col as usize, size.row as usize)
}
}