aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/ffi.c12
-rw-r--r--src/ffi.rs3
-rw-r--r--src/screen.rs52
3 files changed, 49 insertions, 18 deletions
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 <stdlib.h>
+#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) };
}
}