aboutsummaryrefslogtreecommitdiffstats
path: root/src/line/mod.rs
blob: 75a4508e4326ad3c522fda7cb49dbf8714610437 (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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
mod buf;

use std::io::{self, Write};

use crate::cursor::{self, Direction};
pub use buf::LineBuf;

pub struct Line {
    buf: LineBuf,
    dirty: bool,
}

impl std::ops::Deref for Line {
    type Target = LineBuf;

    fn deref(&self) -> &Self::Target {
        &self.buf
    }
}

impl Line {
    pub fn new() -> Self {
        Self {
            buf: LineBuf::new(),
            dirty: false,
        }
    }

    pub fn mark_clean(&mut self) {
        self.dirty = false;
    }

    pub fn mark_dirty(&mut self) {
        self.dirty = true;
    }

    pub fn is_dirty(&self) -> bool {
        self.dirty
    }

    pub fn had_contents(&self) -> bool {
        self.buf.is_dirty()
    }

    pub fn is_empty(&self) -> bool {
        self.buf.is_empty()
    }

    pub fn cursor_left(&mut self) -> io::Result<()> {
        if self.buf.left() {
            let mut stdout = io::stdout().lock();
            cursor::fmove_cursor(Direction::Left, 1, &mut stdout)?;
            stdout.flush()?;
        }
        Ok(())
    }

    pub fn cursor_right(&mut self) -> io::Result<()> {
        if self.buf.right() {
            let mut stdout = io::stdout().lock();
            cursor::fmove_cursor(Direction::Right, 1, &mut stdout)?;
            stdout.flush()?;
        }
        Ok(())
    }

    pub fn move_to_begin(&mut self) -> io::Result<()> {
        let mut stdout = io::stdout().lock();
        cursor::fmove_cursor(Direction::Left, self.buf.all_left(), &mut stdout)?;
        stdout.flush()
    }

    pub fn move_to_end(&mut self) -> io::Result<()> {
        let mut stdout = io::stdout().lock();
        cursor::fmove_cursor(Direction::Right, self.buf.all_right(), &mut stdout)?;
        stdout.flush()
    }

    // move to next
    pub fn cursor_left_word(&mut self) -> io::Result<()> {
        let mut i = 0;

        // find word
        while let Some(b' ') = self.buf.get_left() {
            self.buf.left();
            i += 1;
        }

        // skip it
        while let Some(x) = self.buf.get_left()
            && !x.is_ascii_whitespace()
        {
            self.buf.left();
            i += 1;
        }

        let mut stdout = io::stdout().lock();
        cursor::fmove_cursor(Direction::Left, i, &mut stdout)?;
        stdout.flush()
    }

    pub fn cursor_right_word(&mut self) -> io::Result<()> {
        let mut i = 0;

        // find word
        while let Some(b' ') = self.buf.get_right() {
            self.buf.right();
            i += 1;
        }

        // skip it
        while let Some(x) = self.buf.get_right()
            && !x.is_ascii_whitespace()
        {
            self.buf.right();
            i += 1;
        }

        let mut stdout = io::stdout().lock();
        cursor::fmove_cursor(Direction::Right, i, &mut stdout)?;
        stdout.flush()
    }
}

macro_rules! mutating_impls {
    ($(pub fn $fun:ident(&mut $self:ident $($arg:tt)*) -> $ty:ty $body:block )*) => {
        impl Line {$(
            pub fn $fun(&mut $self $($arg)*) -> $ty {
                $self.dirty = true;
                $body
            }
        )*}
    };
}

mutating_impls! {
    pub fn clear_prompt(&mut self) -> io::Result<()> {
        let mut stdout = io::stdout().lock();
        cursor::fmove_cursor(
            Direction::Right,
            self.buf.distance_from_right_end(),
            &mut stdout,
        )?;
        for _ in 0..self.buf.len() {
            stdout.write_all(b"\x08 \x08")?;
        }
        self.buf.clear();
        stdout.flush()
    }

    pub fn type_bytes(&mut self, bs: &[u8]) -> io::Result<()> {
        for b in bs.iter() {
            self.buf.add(*b);
        }
        io::stdout().lock().write_all(&bs)?;
        self.buf.display_post(b"");
        Ok(())
    }

    pub fn type_byte(&mut self, b: u8) -> io::Result<()> {
        self.type_bytes(&[b])
    }

    pub fn del_left(&mut self) -> io::Result<()> {
        if self.buf.del_left().is_some() {
            cursor::fmove_cursor(Direction::Left, 1, &mut io::stdout())?;
            self.buf.display_post(b" ");
        }
        Ok(())
    }

    pub fn del_right(&mut self) -> io::Result<()> {
        self.buf.del_right();
        self.buf.display_post(b" ");
        Ok(())
    }

    pub fn del_left_word(&mut self) -> io::Result<()> {
        let mut del = 0;
        while let Some(x) = self.buf.get_left()
            && x == b' '
        {
            self.buf.del_left();
            del += 1;
        }
        while let Some(x) = self.buf.get_left()
            && x != b' '
        {
            self.buf.del_left();
            del += 1;
        }
        cursor::fmove_cursor(Direction::Left, del, &mut io::stdout())?;
        self.buf.display_post(&vec![b' '; del]);
        Ok(())
    }

    pub fn del_right_word(&mut self) -> io::Result<()> {
        let mut del = 0;
        while let Some(x) = self.buf.get_right()
            && x == b' '
        {
            self.buf.del_right();
            del += 1;
        }
        while let Some(x) = self.buf.get_right()
            && x != b' '
        {
            self.buf.del_right();
            del += 1;
        }
        self.buf.display_post(&vec![b' '; del]);
        Ok(())
    }

    pub fn set_content(&mut self, content: Vec<u8>) -> io::Result<()> {
        self.buf.set_content(content);
        Ok(())
    }

    pub fn dump(&mut self) -> Vec<u8> {
        self.buf.dump()
    }
}