Better documentation, fix TIOCGWINSZ

This commit is contained in:
Ticki 2016-03-08 10:08:50 +01:00
parent 34f1d6a44c
commit 54ce18f17d
5 changed files with 23 additions and 6 deletions

7
examples/size.rs Normal file
View File

@ -0,0 +1,7 @@
extern crate libterm;
use libterm::terminal_size;
fn main() {
println!("Size is {:?}", terminal_size().unwrap())
}

View File

@ -4,6 +4,9 @@ use {IntoRawMode, TerminalError};
/// Extension to `Read` trait. /// Extension to `Read` trait.
pub trait ReadExt { pub trait ReadExt {
/// Read a password. /// Read a password.
///
/// EOT and ETX will abort the prompt, returning `None`. Newline or carriage return will
/// complete the password input.
fn read_passwd<W: Write>(&mut self, writer: &mut W) -> Result<Option<String>, TerminalError>; fn read_passwd<W: Write>(&mut self, writer: &mut W) -> Result<Option<String>, TerminalError>;
} }

View File

@ -18,8 +18,11 @@ pub use error::TerminalError;
mod raw; mod raw;
pub use raw::{IntoRawMode, TerminalRestorer}; pub use raw::{IntoRawMode, TerminalRestorer};
// TODO Redox terminal size
#[cfg(not(target_os = "redox"))]
mod size; mod size;
pub use size::termsize; #[cfg(not(target_os = "redox"))]
pub use size::terminal_size;
mod color; mod color;
pub use color::Color; pub use color::Color;

View File

@ -3,7 +3,7 @@ use libc::{c_ushort, STDOUT_FILENO};
use std::mem; use std::mem;
use termios::tiocgwinsz; use termios::TIOCGWINSZ;
use TerminalError; use TerminalError;
#[repr(C)] #[repr(C)]
@ -16,11 +16,11 @@ struct TermSize {
/// Get the size of the terminal. If the program isn't running in a terminal, it will return /// Get the size of the terminal. If the program isn't running in a terminal, it will return
/// `None`. /// `None`.
pub fn termsize() -> Result<(usize, usize), TerminalError> { pub fn terminal_size() -> Result<(usize, usize), TerminalError> {
unsafe { unsafe {
let mut size: TermSize = mem::zeroed(); let mut size: TermSize = mem::zeroed();
if ioctl(STDOUT_FILENO, tiocgwinsz as u64, &mut size as *mut _) == 0 { if ioctl(STDOUT_FILENO, TIOCGWINSZ, &mut size as *mut _) == 0 {
Ok((size.col as usize, size.row as usize)) Ok((size.col as usize, size.row as usize))
} else { } else {
Err(TerminalError::TermSizeError) Err(TerminalError::TermSizeError)

View File

@ -1,8 +1,12 @@
use libc::{c_int, c_uint, c_uchar}; use libc::{c_int, c_uint, c_uchar};
extern { #[cfg(not(target_os = "macos"))]
pub static tiocgwinsz: c_int; pub const TIOCGWINSZ: u64 = 0x00005413;
#[cfg(target_os = "macos")]
pub const TIOCGWINSZ: u64 = 0x40087468;
extern {
pub fn tcgetattr(filedes: c_int, termptr: *mut Termios) -> c_int; pub fn tcgetattr(filedes: c_int, termptr: *mut Termios) -> c_int;
pub fn tcsetattr(filedes: c_int, opt: c_int, termptr: *mut Termios) -> c_int; pub fn tcsetattr(filedes: c_int, opt: c_int, termptr: *mut Termios) -> c_int;
pub fn cfmakeraw(termptr: *mut Termios); pub fn cfmakeraw(termptr: *mut Termios);