mirror of
https://github.com/Llewellynvdm/exa.git
synced 2025-01-14 17:19:56 +00:00
Make sure we check the ioctl term size result
This commit is contained in:
parent
d1e682b0c1
commit
65967355a8
@ -4,6 +4,7 @@
|
|||||||
|
|
||||||
#![warn(trivial_casts, trivial_numeric_casts)]
|
#![warn(trivial_casts, trivial_numeric_casts)]
|
||||||
#![warn(unused_extern_crates, unused_qualifications)]
|
#![warn(unused_extern_crates, unused_qualifications)]
|
||||||
|
#![warn(unused_results)]
|
||||||
|
|
||||||
extern crate ansi_term;
|
extern crate ansi_term;
|
||||||
extern crate datetime;
|
extern crate datetime;
|
||||||
|
@ -28,6 +28,7 @@ pub struct Options {
|
|||||||
impl Options {
|
impl Options {
|
||||||
|
|
||||||
/// Call getopts on the given slice of command-line strings.
|
/// Call getopts on the given slice of command-line strings.
|
||||||
|
#[allow(unused_results)]
|
||||||
pub fn getopts(args: &[String]) -> Result<(Options, Vec<String>), Misfire> {
|
pub fn getopts(args: &[String]) -> Result<(Options, Vec<String>), Misfire> {
|
||||||
let mut opts = getopts::Options::new();
|
let mut opts = getopts::Options::new();
|
||||||
opts.optflag("1", "oneline", "display one entry per line");
|
opts.optflag("1", "oneline", "display one entry per line");
|
||||||
|
68
src/term.rs
68
src/term.rs
@ -1,39 +1,53 @@
|
|||||||
mod c {
|
//! System calls for getting the terminal size.
|
||||||
use std::mem::zeroed;
|
//!
|
||||||
use libc::{c_int, c_ushort, c_ulong, STDOUT_FILENO};
|
//! Getting the terminal size is performed using an ioctl command that takes
|
||||||
|
//! the file handle to the terminal -- which in this case, is stdout -- and
|
||||||
|
//! populates a structure containing the values.
|
||||||
|
//!
|
||||||
|
//! The size is needed when the user wants the output formatted into columns:
|
||||||
|
//! the default grid view, or the hybrid grid-details view.
|
||||||
|
|
||||||
// Getting the terminal size is done using an ioctl command that
|
use std::mem::zeroed;
|
||||||
// takes the file handle to the terminal (which in our case is
|
use libc::{c_int, c_ushort, c_ulong, STDOUT_FILENO};
|
||||||
// stdout), and populates a structure with the values.
|
|
||||||
|
|
||||||
pub struct Winsize {
|
|
||||||
pub ws_row: c_ushort,
|
/// The number of rows and columns of a terminal.
|
||||||
pub ws_col: c_ushort,
|
struct Winsize {
|
||||||
|
ws_row: c_ushort,
|
||||||
|
ws_col: c_ushort,
|
||||||
|
}
|
||||||
|
|
||||||
|
// Unfortunately the actual command is not standardised...
|
||||||
|
|
||||||
|
#[cfg(any(target_os = "linux", target_os = "android"))]
|
||||||
|
static TIOCGWINSZ: c_ulong = 0x5413;
|
||||||
|
|
||||||
|
#[cfg(any(target_os = "macos", target_os = "ios", target_os = "dragonfly"))]
|
||||||
|
static TIOCGWINSZ: c_ulong = 0x40087468;
|
||||||
|
|
||||||
|
extern {
|
||||||
|
pub fn ioctl(fd: c_int, request: c_ulong, ...) -> c_int;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Runs the ioctl command. Returns (0, 0) if output is not to a terminal, or
|
||||||
|
/// there is an error. (0, 0) is an invalid size to have anyway, which is why
|
||||||
|
/// it can be used as a nil value.
|
||||||
|
unsafe fn get_dimensions() -> Winsize {
|
||||||
|
let mut window: Winsize = zeroed();
|
||||||
|
let result = ioctl(STDOUT_FILENO, TIOCGWINSZ, &mut window);
|
||||||
|
|
||||||
|
if result == -1 {
|
||||||
|
zeroed()
|
||||||
}
|
}
|
||||||
|
else {
|
||||||
// Unfortunately the actual command is not standardised...
|
|
||||||
|
|
||||||
#[cfg(any(target_os = "linux", target_os = "android"))]
|
|
||||||
static TIOCGWINSZ: c_ulong = 0x5413;
|
|
||||||
|
|
||||||
#[cfg(any(target_os = "macos", target_os = "ios", target_os = "dragonfly"))]
|
|
||||||
static TIOCGWINSZ: c_ulong = 0x40087468;
|
|
||||||
|
|
||||||
extern {
|
|
||||||
pub fn ioctl(fd: c_int, request: c_ulong, ...) -> c_int;
|
|
||||||
}
|
|
||||||
|
|
||||||
pub unsafe fn dimensions() -> Winsize {
|
|
||||||
let mut window: Winsize = zeroed();
|
|
||||||
ioctl(STDOUT_FILENO, TIOCGWINSZ, &mut window);
|
|
||||||
window
|
window
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Query the current processes's output, returning its width and height as a
|
/// Query the current processes's output, returning its width and height as a
|
||||||
/// number of characters. Returns None if the output isn't to a terminal.
|
/// number of characters. Returns `None` if the output isn't to a terminal.
|
||||||
pub fn dimensions() -> Option<(usize, usize)> {
|
pub fn dimensions() -> Option<(usize, usize)> {
|
||||||
let w = unsafe { c::dimensions() };
|
let w = unsafe { get_dimensions() };
|
||||||
|
|
||||||
if w.ws_col == 0 || w.ws_row == 0 {
|
if w.ws_col == 0 || w.ws_row == 0 {
|
||||||
None
|
None
|
||||||
|
Loading…
Reference in New Issue
Block a user