diff options
Diffstat (limited to 'src/screen.rs')
-rw-r--r-- | src/screen.rs | 52 |
1 files changed, 34 insertions, 18 deletions
diff --git a/src/screen.rs b/src/screen.rs index 698a6af..296f2dc 100644 --- a/src/screen.rs +++ b/src/screen.rs @@ -4,29 +4,31 @@ use std; use ffi; use types; -pub struct Screen { - pub rows: i32, - pub cols: i32, - - screen_impl: *mut types::ScreenImpl, -} +pub struct Screen(*mut types::ScreenImpl); impl Screen { pub fn new(rows: i32, cols: i32) -> Screen { let screen_impl = unsafe { ffi::vt100_screen_new(rows as libc::c_int, cols as libc::c_int) }; - Screen { - rows: rows, - cols: cols, - screen_impl: screen_impl, - } + Screen(screen_impl) + } + + pub fn rows(&self) -> i32 { + let Screen(screen_impl) = *self; + unsafe { ffi::vt100_wrapper_rows(screen_impl) } + } + + pub fn cols(&self) -> i32 { + let Screen(screen_impl) = *self; + unsafe { ffi::vt100_wrapper_cols(screen_impl) } } pub fn process(&mut self, s: &str) -> u64 { + let Screen(screen_impl) = *self; unsafe { ffi::vt100_screen_process_string( - self.screen_impl, + screen_impl, s.as_ptr() as *const libc::c_char, s.len() ) as u64 @@ -39,10 +41,23 @@ impl Screen { row_end: i32, col_end: i32 ) -> String { - let row_start = std::cmp::min(std::cmp::max(row_start, 0), self.rows - 1); - let col_start = std::cmp::min(std::cmp::max(col_start, 0), self.cols - 1); - let row_end = std::cmp::min(std::cmp::max(row_end, 0), self.rows - 1); - let col_end = std::cmp::min(std::cmp::max(col_end, 0), self.cols - 1); + let Screen(screen_impl) = *self; + let row_start = std::cmp::min( + std::cmp::max(row_start, 0), + self.rows() - 1 + ); + let col_start = std::cmp::min( + std::cmp::max(col_start, 0), + self.cols() - 1 + ); + let row_end = std::cmp::min( + std::cmp::max(row_end, 0), + self.rows() - 1 + ); + let col_end = std::cmp::min( + std::cmp::max(col_end, 0), + self.cols() - 1 + ); let start_loc = types::Loc { row: row_start, col: col_start }; let end_loc = types::Loc { row: row_end, col: col_end }; @@ -51,7 +66,7 @@ impl Screen { let mut len: libc::size_t = unsafe { std::mem::uninitialized() }; unsafe { ffi::vt100_screen_get_string_plaintext( - self.screen_impl, + screen_impl, &start_loc as *const types::Loc, &end_loc as *const types::Loc, &mut plaintext as *mut *mut libc::c_char, @@ -70,6 +85,7 @@ impl Screen { impl Drop for Screen { fn drop(&mut self) { - unsafe { ffi::vt100_screen_delete(self.screen_impl) }; + let Screen(screen_impl) = *self; + unsafe { ffi::vt100_screen_delete(screen_impl) }; } } |