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
use grid::{Grid, Pt};

#[derive(PartialEq, Eq, Clone, Debug, Hash)]
pub struct Text {
    pub (crate) pt: Pt,
    pub (crate) content: String,
    pub (crate) id: Option<(Pt, String)>,
    pub (crate) attrs: Option<Vec<(String, String)>>,
}

impl Text {
    pub fn new(pt: Pt, content: String) -> Text {
        Text {
            pt: pt,
            content: content,
            id: None,
            attrs: None,
        }
    }

    pub fn infer_id(&mut self, grid: &Grid) {
        let mut letter = self.pt;
        for _ in 0..self.content.len() {
            let pt = letter.s();
            if let Some(s) = grid.match_id(pt) {
                self.id = Some((pt, s));
            }
            letter = letter.e();
        }
    }
}

impl Text {
    pub fn attach_attributes(&mut self, pt: Pt, grid: &Grid) {
        if let Some((_, ref id)) = self.id {
            if let Some(attr) = grid.find_attr(id) {
                ::attrs::input_attr(&mut self.attrs, attr);
            }
        }
        if let Some(attr) = grid.find_pt_attr(pt) {
            ::attrs::input_attr(&mut self.attrs, attr);
        }
    }
}

impl Grid {
    pub fn remove_text(&mut self, t: &Text) {
        let r = t.pt.row();
        let c = t.pt.col();
        let len = t.content.chars().count();
        for c_i in c..c+(len as i32) {
            self.mark_used(Pt::rowcol(r,c_i));
        }
        if let Some(ref pt_id) = t.id {
            self.clear_id(pt_id);
        }
    }
}