1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
|
use libc;
use std;
use cell;
use ffi;
use types;
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(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 set_window_size(&self, rows: i32, cols: i32) {
let Screen(screen_impl) = *self;
unsafe { ffi::vt100_screen_set_window_size(screen_impl, rows, cols) };
}
pub fn set_scrollback_length(&self, rows: i32) {
let Screen(screen_impl) = *self;
unsafe { ffi::vt100_screen_set_scrollback_length(screen_impl, rows) };
}
pub fn process(&mut self, s: &[u8]) -> u64 {
let Screen(screen_impl) = *self;
unsafe {
ffi::vt100_screen_process_string(
screen_impl,
s.as_ptr() as *const libc::c_char,
s.len()
) as u64
}
}
pub fn window_contents(&self,
row_start: i32,
col_start: i32,
row_end: i32,
col_end: i32
) -> String {
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 };
let mut plaintext: *mut libc::c_char = unsafe { std::mem::uninitialized() };
let mut len: libc::size_t = unsafe { std::mem::uninitialized() };
unsafe {
ffi::vt100_screen_get_string_plaintext(
screen_impl,
&start_loc as *const types::Loc,
&end_loc as *const types::Loc,
&mut plaintext as *mut *mut libc::c_char,
&mut len as *mut libc::size_t,
)
};
let rust_plaintext = unsafe {
std::slice::from_raw_parts(
plaintext as *mut libc::c_uchar,
len
)
}.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 {
fn drop(&mut self) {
let Screen(screen_impl) = *self;
unsafe { ffi::vt100_screen_delete(screen_impl) };
}
}
|