diff options
author | Jesse Luehrs <doy@tozt.net> | 2016-04-27 01:50:04 -0400 |
---|---|---|
committer | Jesse Luehrs <doy@tozt.net> | 2016-04-27 01:50:36 -0400 |
commit | 85213a843566b82b05626efd97c48644b04935d2 (patch) | |
tree | 1897cb51bd0519aa955310ca049a09064e9a476d /src | |
parent | f0486657754c485f8211c6d00f9bf5ba6953dd67 (diff) | |
download | vt100-rust-85213a843566b82b05626efd97c48644b04935d2.tar.gz vt100-rust-85213a843566b82b05626efd97c48644b04935d2.zip |
start implementing cells
Diffstat (limited to 'src')
-rw-r--r-- | src/cell.rs | 30 | ||||
-rw-r--r-- | src/ffi.rs | 6 | ||||
-rw-r--r-- | src/lib.rs | 2 | ||||
-rw-r--r-- | src/screen.rs | 12 | ||||
-rw-r--r-- | src/types.rs | 1 |
5 files changed, 51 insertions, 0 deletions
diff --git a/src/cell.rs b/src/cell.rs new file mode 100644 index 0000000..7590e98 --- /dev/null +++ b/src/cell.rs @@ -0,0 +1,30 @@ +use libc; +use std; + +use types; + +pub struct Cell(*mut types::CellImpl); + +#[repr(C)] +struct CellPrefix { + pub contents: [libc::c_char; 8], + pub len: libc::size_t, +} + +impl Cell { + pub fn new(cell_impl: *mut types::CellImpl) -> Cell { + Cell(cell_impl) + } + + pub fn contents(&self) -> &str { + let Cell(cell_impl) = *self; + let contents: &[u8] = unsafe { + let prefix: *mut CellPrefix = std::mem::transmute(cell_impl); + std::slice::from_raw_parts( + &(*prefix).contents as *const i8 as *const u8, + (*prefix).len + ) + }; + std::str::from_utf8(contents).unwrap() + } +} @@ -32,6 +32,12 @@ extern "C" { rows: libc::c_int, ); + pub fn vt100_screen_cell_at( + screen: *mut types::ScreenImpl, + row: libc::c_int, + col: libc::c_int, + ) -> *mut types::CellImpl; + pub fn vt100_wrapper_rows(screen: *mut types::ScreenImpl) -> libc::c_int; pub fn vt100_wrapper_cols(screen: *mut types::ScreenImpl) -> libc::c_int; } @@ -1,7 +1,9 @@ extern crate libc; +mod cell; mod ffi; mod screen; mod types; pub use screen::Screen; +pub use cell::Cell; diff --git a/src/screen.rs b/src/screen.rs index da96a6a..d3e026f 100644 --- a/src/screen.rs +++ b/src/screen.rs @@ -1,6 +1,7 @@ use libc; use std; +use cell; use ffi; use types; @@ -91,6 +92,17 @@ impl Screen { }.to_vec(); std::string::String::from_utf8(rust_plaintext).unwrap() } + + pub fn cell(&self, row: i32, col: i32) -> Option<cell::Cell> { + let Screen(screen_impl) = *self; + if row < 0 || row >= self.rows() || col < 0 || col >= self.cols() { + return None + } + let cell_impl = unsafe { + ffi::vt100_screen_cell_at(screen_impl, row, col) + }; + Some(cell::Cell::new(cell_impl)) + } } impl Drop for Screen { diff --git a/src/types.rs b/src/types.rs index a5ac590..fca7f97 100644 --- a/src/types.rs +++ b/src/types.rs @@ -1,6 +1,7 @@ use libc; pub enum ScreenImpl {} +pub enum CellImpl {} #[repr(C)] pub struct Loc { |