<commit_before>extern crate libc;
use std::os::raw::*;

use super::{Width, Height};

const TIOCGWINSZ: c_int = 0x00005413;

#[derive(Debug)]
struct WinSize {
    ws_row: c_ushort,
    ws_col: c_ushort,
    ws_xpixel: c_ushort,
    ws_ypixel: c_ushort
}

/// Returns the size of the terminal, if available.
///
/// If STDIN is not a tty, returns `None`
pub fn terminal_size() -> Option<(Width, Height)> {
    use self::libc::{isatty, STDIN_FILENO};
    use self::libc::funcs::bsd44::ioctl;
    let is_tty: bool = unsafe{isatty(STDIN_FILENO) == 1};

    if !is_tty { return None; }

    let (rows, cols) = unsafe {
        let mut winsize = WinSize{ws_row: 0, ws_col: 0, ws_xpixel: 0, ws_ypixel: 0};
        ioctl(STDIN_FILENO, TIOCGWINSZ, &mut winsize);
        println!("{:?}", winsize);
        let rows = if winsize.ws_row > 0 { winsize.ws_row } else { 0 };
        let cols = if winsize.ws_col > 0 { winsize.ws_col } else { 0 };
        (rows as u16, cols as u16)
    };

    if rows > 0 && cols > 0 {
        Some((Width(cols), Height(rows)))
    } else {
        None
    }
}<commit_msg>Add test on linux to compare with the output of stty<commit_after>extern crate libc;
use std::os::raw::*;

use super::{Width, Height};

const TIOCGWINSZ: c_int = 0x00005413;

#[derive(Debug)]
struct WinSize {
    ws_row: c_ushort,
    ws_col: c_ushort,
    ws_xpixel: c_ushort,
    ws_ypixel: c_ushort
}

/// Returns the size of the terminal, if available.
///
/// If STDIN is not a tty, returns `None`
pub fn terminal_size() -> Option<(Width, Height)> {
    use self::libc::{isatty, STDIN_FILENO};
    use self::libc::funcs::bsd44::ioctl;
    let is_tty: bool = unsafe{isatty(STDIN_FILENO) == 1};

    if !is_tty { return None; }

    let (rows, cols) = unsafe {
        let mut winsize = WinSize{ws_row: 0, ws_col: 0, ws_xpixel: 0, ws_ypixel: 0};
        ioctl(STDIN_FILENO, TIOCGWINSZ, &mut winsize);
        println!("{:?}", winsize);
        let rows = if winsize.ws_row > 0 { winsize.ws_row } else { 0 };
        let cols = if winsize.ws_col > 0 { winsize.ws_col } else { 0 };
        (rows as u16, cols as u16)
    };

    if rows > 0 && cols > 0 {
        Some((Width(cols), Height(rows)))
    } else {
        None
    }
}

#[test]
/// Compare with the output of `stty size`
fn compare_with_stty() {
    use std::process::Command;
    use std::process::Stdio;

    let output = Command::new("stty").arg("size").arg("-F").arg("/dev/stderr").stderr(Stdio::inherit()).output().unwrap();
    let stdout = String::from_utf8(output.stdout).unwrap();
    assert!(output.status.success());
    // stdout is "rows cols"
    let mut data = stdout.split_whitespace();
    let rows = u16::from_str_radix(data.next().unwrap(), 10).unwrap();
    let cols = u16::from_str_radix(data.next().unwrap(), 10).unwrap();
    println!("{}", stdout);
    println!("{} {}", rows, cols);

    if let Some((Width(w), Height(h))) = terminal_size() {
        assert_eq!(rows, h);
        assert_eq!(cols, w);
    }


}
{'fraction_non_alphanumeric': 0.1642140468227425, 'fraction_numerical': 0.02040133779264214, 'mean_word_length': 3.047361299052774, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1139742', 'n_tokens_mistral': 1191, 'n_tokens_neox': 1112, 'n_words': 281}