From 3bc00f5f3f030acc8dbbf8018f6c2a99561ca709 Mon Sep 17 00:00:00 2001 From: tomsmeding Date: Sun, 10 Feb 2019 20:37:02 +0100 Subject: Update organisation and document --- src/lib.rs | 204 ++++++++----------------------------------------------------- 1 file changed, 26 insertions(+), 178 deletions(-) (limited to 'src/lib.rs') diff --git a/src/lib.rs b/src/lib.rs index 7a17a0f..b092b85 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,183 +1,31 @@ -use std::char; -use std::io; +//! This crate contains bindings to the C library [termio][1]. Most functions +//! wrapped are methods on the `Screen` or `Keyboard` structs, while others take +//! one of these structs as an argument. This organisation ensures that e.g. the +//! screen can only be redrawn while the double-buffered screen is actually +//! active, and that keys can only be read from the input while the input stream +//! is actually in raw mode. +//! +//! The `widgets` submodule contains the built-in widget modules in termio. +//! +//! All callbacks are `extern "C"` functions, and thus do not accept generic +//! `Fn` instances. This is due to a limitation of the original termio API. +//! +//! In this crate, terminal screen positions and sizes are represented using a +//! tuple `(x, y): (u32, u32)`. Positions are zero-based. +//! +//! The `KEY_` constants describe the return values from `Keyboard::get_key()`. +//! To create key combinations, e.g. ctrl-A, use addition, e.g. `KEY_CTRL + +//! 'A'`. +//! +//! **Please be aware that to get anything on the screen, you need to call +//! `redraw()`.** +//! +//! [1]: https://github.com/tomsmeding/termio mod bindings; mod util; -pub mod widgets; - -// #[cfg(test)] -// mod tests { -// #[test] -// fn it_works() { -// assert_eq!(2 + 2, 4); -// } -// } - -// All positions are represented using a tuple (x, y): (u32, u32). -// Positions are zero-based. - -pub const KEY_TAB: i32 = bindings::KEY_TAB as i32; -pub const KEY_LF: i32 = bindings::KEY_LF as i32; -pub const KEY_CR: i32 = bindings::KEY_CR as i32; -pub const KEY_ESC: i32 = bindings::KEY_ESC as i32; -pub const KEY_BACKSPACE: i32 = bindings::KEY_BACKSPACE as i32; - -pub const KEY_RIGHT: i32 = bindings::KEY_RIGHT as i32; -pub const KEY_UP: i32 = bindings::KEY_UP as i32; -pub const KEY_LEFT: i32 = bindings::KEY_LEFT as i32; -pub const KEY_DOWN: i32 = bindings::KEY_DOWN as i32; -pub const KEY_PAGEUP: i32 = bindings::KEY_PAGEUP as i32; -pub const KEY_PAGEDOWN: i32 = bindings::KEY_PAGEDOWN as i32; -pub const KEY_DELETE: i32 = bindings::KEY_DELETE as i32; -pub const KEY_SHIFTTAB: i32 = bindings::KEY_SHIFTTAB as i32; - -pub const KEY_CTRL: i32 = bindings::KEY_CTRL as i32; -pub const KEY_SHIFT: i32 = bindings::KEY_SHIFT as i32; -pub const KEY_ALT: i32 = bindings::KEY_ALT as i32; -pub const KEY_CTRLALT: i32 = bindings::KEY_CTRLALT as i32; -pub const KEY_CTRLSHIFT: i32 = bindings::KEY_CTRLSHIFT as i32; - -#[derive(Debug, Copy, Clone)] -pub struct Style { - pub fg: u8, // 0-7, 9 - pub bg: u8, // 0-7, 9 - pub bold: bool, - pub ul: bool -} - -pub fn init_keyboard(nosigkeys: bool) { - unsafe { bindings::initkeyboard(nosigkeys); } -} - -pub fn end_keyboard() { - unsafe { bindings::endkeyboard(); } -} - -pub fn init_screen() { - unsafe { bindings::initscreen(); } -} - -pub fn end_screen() { - unsafe { bindings::endscreen(); } -} - -pub fn install_cl_handler(install: bool) { - unsafe { bindings::installCLhandler(install); } -} - -pub fn install_redraw_handler(handler: extern "C" fn(full_redraw: bool)) { - unsafe { bindings::installredrawhandler(Some(handler)); } -} - -pub fn clear_screen() { - unsafe { bindings::clearscreen(); } -} - -pub fn get_term_size() -> (u32, u32) { - let sz = unsafe { bindings::gettermsize() }; - (sz.w as u32, sz.h as u32) -} - -pub fn set_style(style: Style) { - let sty = bindings::Style { - fg: style.fg as i32, - bg: style.bg as i32, - bold: style.bold, - ul: style.ul, - }; - unsafe { bindings::setstyle(&sty as *const bindings::Style); } -} - -pub fn set_fg(fg: u8) { - unsafe { bindings::setfg(fg as i32); } -} -pub fn set_bg(bg: u8) { - unsafe { bindings::setbg(bg as i32); } -} - -pub fn set_bold(bold: bool) { - unsafe { bindings::setbold(bold); } -} - -pub fn set_ul(ul: bool) { - unsafe { bindings::setul(ul); } -} - -pub fn putc(c: char) { - unsafe { bindings::tputc(c as i32); } -} - -pub fn print(s: &str) { - unsafe { bindings::tprintf( - "%s".as_bytes().as_ptr() as *const i8, - s.as_bytes().as_ptr() as *const i8); } -} - -pub fn fill_rect(lefttop: (u32, u32), size: (u32, u32), c: char) { - unsafe { bindings::fillrect( - lefttop.0 as i32, lefttop.1 as i32, - size.0 as i32, size.1 as i32, - c as i32); } -} - -pub fn redraw() { - unsafe { bindings::redraw(); } -} - -pub fn redraw_full() { - unsafe { bindings::redrawfull(); } -} - -pub fn scroll_term(lefttop: (u32, u32), size: (u32, u32), c: char) { - unsafe { bindings::scrollterm( - lefttop.0 as i32, lefttop.1 as i32, - size.0 as i32, size.1 as i32, - c as i32); } -} - -pub fn get_buffer_char(pos: (u32, u32)) -> char { - unsafe { - char::from_u32( - bindings::getbufferchar(pos.0 as i32, pos.1 as i32) as u32 - ).unwrap() - } -} - -pub fn move_to(pos: (u32, u32)) { - unsafe { bindings::moveto(pos.0 as i32, pos.1 as i32); } -} - -pub fn push_cursor() { - unsafe { bindings::pushcursor(); } -} - -pub fn pop_cursor() { - unsafe { bindings::popcursor(); } -} - -pub fn bel() { - unsafe { bindings::bel(); } -} - -pub fn cursor_visible(visible: bool) { - unsafe { bindings::cursorvisible(visible); } -} - -// None on EOF, otherwise a combination of the KEY_* constants -pub fn get_key() -> io::Result> { - match unsafe { bindings::tgetkey() } { - -2 => Err(io::Error::last_os_error()), - -1 => Ok(None), - value => Ok(Some(value)), - } -} +mod core; +pub mod widgets; -pub fn get_line() -> Option { - let ptr = unsafe { bindings::tgetline() }; - if ptr.is_null() { - None - } else { - unsafe { Some(util::string_from_utf8_charp(ptr)) } - } -} +pub use crate::core::*; -- cgit v1.2.3-54-g00ecf