From 60a7ec2ecf621ebb6f3376f71b27cdc61d7fc5a1 Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Tue, 26 Apr 2016 04:15:33 -0400 Subject: stop tracking terminal size separately --- src/ffi.c | 12 ++++++++++++ src/ffi.rs | 3 +++ src/screen.rs | 52 ++++++++++++++++++++++++++++++++++------------------ 3 files changed, 49 insertions(+), 18 deletions(-) create mode 100644 src/ffi.c (limited to 'src') diff --git a/src/ffi.c b/src/ffi.c new file mode 100644 index 0000000..a4af5ba --- /dev/null +++ b/src/ffi.c @@ -0,0 +1,12 @@ +#include +#include "../libvt100/src/vt100.h" + +int vt100_wrapper_rows(VT100Screen *vt) +{ + return vt->grid->max.row; +} + +int vt100_wrapper_cols(VT100Screen *vt) +{ + return vt->grid->max.col; +} diff --git a/src/ffi.rs b/src/ffi.rs index ceb8f35..47281ba 100644 --- a/src/ffi.rs +++ b/src/ffi.rs @@ -21,6 +21,9 @@ extern "C" { outp: *mut *mut libc::c_char, outlen: *mut libc::size_t, ); + + pub fn vt100_wrapper_rows(screen: *mut types::ScreenImpl) -> libc::c_int; + pub fn vt100_wrapper_cols(screen: *mut types::ScreenImpl) -> libc::c_int; } #[cfg(test)] 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) }; } } -- cgit v1.2.3-54-g00ecf