aboutsummaryrefslogtreecommitdiffstats
path: root/src/cell.rs
blob: 9156299381a58d70713e9f33b14c4b749ac7171e (plain) (blame)
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
use libc;
use std;

use color;
use ffi;
use types;

pub struct Cell(*mut types::CellImpl);

#[repr(C)]
struct CellPrefix {
    pub contents: [libc::c_char; 8],
    pub len: libc::size_t,
    pub attrs: types::CellAttrs,
}

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 prefix: *mut CellPrefix = unsafe {
            std::mem::transmute(cell_impl)
        };
        let contents: &[u8] = unsafe {
            std::slice::from_raw_parts(
                &(*prefix).contents as *const i8 as *const u8,
                (*prefix).len
            )
        };
        std::str::from_utf8(contents).unwrap()
    }

    pub fn fgcolor(&self) -> color::Color {
        let Cell(cell_impl) = *self;
        let prefix: *mut CellPrefix = unsafe {
            std::mem::transmute(cell_impl)
        };
        let attrs = unsafe { &(*prefix).attrs };
        color::Color::new(&attrs.fgcolor)
    }

    pub fn bgcolor(&self) -> color::Color {
        let Cell(cell_impl) = *self;
        let prefix: *mut CellPrefix = unsafe {
            std::mem::transmute(cell_impl)
        };
        let attrs = unsafe { &(*prefix).attrs };
        color::Color::new(&attrs.bgcolor)
    }

    pub fn is_wide(&self) -> bool {
        let Cell(cell_impl) = *self;
        unsafe { ffi::vt100_wrapper_cell_is_wide(cell_impl) != 0 }
    }

    pub fn bold(&self) -> bool {
        let Cell(cell_impl) = *self;
        let prefix: *mut CellPrefix = unsafe {
            std::mem::transmute(cell_impl)
        };
        unsafe {
            ffi::vt100_wrapper_cell_attrs_bold(&mut (*prefix).attrs) != 0
        }
    }

    pub fn italic(&self) -> bool {
        let Cell(cell_impl) = *self;
        let prefix: *mut CellPrefix = unsafe {
            std::mem::transmute(cell_impl)
        };
        unsafe {
            ffi::vt100_wrapper_cell_attrs_italic(&mut (*prefix).attrs) != 0
        }
    }

    pub fn underline(&self) -> bool {
        let Cell(cell_impl) = *self;
        let prefix: *mut CellPrefix = unsafe {
            std::mem::transmute(cell_impl)
        };
        unsafe {
            ffi::vt100_wrapper_cell_attrs_underline(&mut (*prefix).attrs) != 0
        }
    }

    pub fn inverse(&self) -> bool {
        let Cell(cell_impl) = *self;
        let prefix: *mut CellPrefix = unsafe {
            std::mem::transmute(cell_impl)
        };
        unsafe {
            ffi::vt100_wrapper_cell_attrs_inverse(&mut (*prefix).attrs) != 0
        }
    }

    pub fn check_dirty(&self) -> bool {
        let Cell(cell_impl) = *self;
        let ret = unsafe {
            ffi::vt100_wrapper_cell_was_drawn(cell_impl) == 0
        };
        unsafe {
            ffi::vt100_wrapper_cell_set_was_drawn(cell_impl)
        };
        ret
    }
}