1
//! Text-input editor: a buffer with a cursor and the editing operations the
2
//! keymap names. It does no I/O, so it can be tested without a terminal; the only trace of
3
//! drawing it keeps is the scroll position of the text it is shown in.
4

            
5
use std::cell::Cell;
6

            
7
use tvui::Viewport;
8

            
9
mod lines;
10

            
11
pub use lines::TEXT_AREA_ROWS;
12

            
13
/// A text buffer with a cursor. Loaded text is kept verbatim, newlines
14
/// included; a single-line editor cannot type one, a multi-line one
15
/// ([`Editor::multiline`]) can and moves by lines.
16
#[derive(Debug, Clone, Default)]
17
pub struct Editor {
18
    buffer: String,
19
    cursor: usize,
20
    multiline: bool,
21
    scroll: Cell<Viewport>,
22
}
23

            
24
impl Editor {
25
    #[must_use]
26
2486
    pub fn with_buffer(buffer: impl Into<String>) -> Self {
27
2486
        let buf: String = buffer.into();
28
2486
        let cursor = buf.chars().count();
29
2486
        Self {
30
2486
            buffer: buf,
31
2486
            cursor,
32
2486
            multiline: false,
33
2486
            scroll: Cell::default(),
34
2486
        }
35
2486
    }
36

            
37
    #[must_use]
38
10950
    pub fn buffer(&self) -> &str {
39
10950
        &self.buffer
40
10950
    }
41

            
42
    /// The cell the draw of the field showing this editor keeps its scroll in.
43
    #[must_use]
44
1037
    pub fn scroll(&self) -> &Cell<Viewport> {
45
1037
        &self.scroll
46
1037
    }
47

            
48
    /// Where the field showing this editor is scrolled to.
49
    #[must_use]
50
361
    pub fn viewport(&self) -> Viewport {
51
361
        self.scroll.get()
52
361
    }
53

            
54
    /// Remember the scroll the drawing of the field ended at; the frame only borrows the editor.
55
305
    pub fn set_viewport(&self, viewport: Viewport) {
56
305
        self.scroll.set(viewport);
57
305
    }
58

            
59
    #[must_use]
60
1135
    pub fn cursor(&self) -> usize {
61
1135
        self.cursor
62
1135
    }
63

            
64
    /// Puts the cursor before character `index`, or at the end when the text is shorter.
65
31
    pub fn set_cursor(&mut self, index: usize) {
66
31
        self.cursor = index.min(self.buffer.chars().count());
67
31
    }
68

            
69
    /// Replace the entire buffer with `new`, placing the cursor at the end.
70
43
    pub fn replace_buffer(&mut self, new: impl Into<String>) {
71
43
        let buf: String = new.into();
72
43
        self.cursor = buf.chars().count();
73
43
        self.buffer = buf;
74
43
    }
75

            
76
11537
    fn chars(&self) -> Vec<char> {
77
11537
        self.buffer.chars().collect()
78
11537
    }
79

            
80
11213
    fn rebuild(&mut self, chars: &[char]) {
81
11213
        self.buffer = chars.iter().collect();
82
11213
        if self.cursor > chars.len() {
83
            self.cursor = chars.len();
84
11213
        }
85
11213
    }
86

            
87
    /// Insert a single character at the cursor.
88
11131
    pub fn insert_char(&mut self, c: char) {
89
11131
        let mut chars = self.chars();
90
11131
        chars.insert(self.cursor, c);
91
11131
        self.cursor += 1;
92
11131
        self.rebuild(&chars);
93
11131
    }
94

            
95
59
    pub fn delete_backward(&mut self) {
96
59
        let mut chars = self.chars();
97
59
        let span = self.terminator_span(&chars, self.cursor, true).unwrap_or(1);
98
59
        let Some(start) = self.cursor.checked_sub(span) else {
99
1
            return;
100
        };
101
58
        chars.drain(start..self.cursor);
102
58
        self.cursor = start;
103
58
        self.rebuild(&chars);
104
59
    }
105

            
106
14
    pub fn delete_forward(&mut self) {
107
14
        let mut chars = self.chars();
108
14
        if self.cursor < chars.len() {
109
13
            let span = self
110
13
                .terminator_span(&chars, self.cursor, false)
111
13
                .unwrap_or(1);
112
13
            chars.drain(self.cursor..self.cursor + span);
113
13
            self.rebuild(&chars);
114
1
        }
115
14
    }
116

            
117
114
    pub fn move_left(&mut self) {
118
114
        self.cursor = self.cursor.saturating_sub(1);
119
114
        if self.inside_crlf() {
120
1
            self.cursor -= 1;
121
113
        }
122
114
    }
123

            
124
29
    pub fn move_right(&mut self) {
125
29
        if self.cursor < self.buffer.chars().count() {
126
22
            self.cursor += 1;
127
7
        }
128
29
        if self.inside_crlf() {
129
1
            self.cursor += 1;
130
28
        }
131
29
    }
132

            
133
27
    pub fn move_home(&mut self) {
134
27
        self.cursor = self.home_target();
135
27
    }
136

            
137
26
    pub fn move_end(&mut self) {
138
26
        self.cursor = self.end_target();
139
26
    }
140

            
141
    /// Delete from the cursor to end-of-line (emacs C-k).
142
    /// In a multi-line editor it stops at the line end, and at the line end
143
    /// it deletes the line terminator instead.
144
9
    pub fn kill_to_end(&mut self) {
145
9
        let end = self.end_target();
146
9
        if self.multiline && self.cursor == end {
147
4
            self.delete_forward();
148
4
            return;
149
5
        }
150
5
        let mut chars = self.chars();
151
5
        chars.drain(self.cursor..end);
152
5
        self.rebuild(&chars);
153
9
    }
154

            
155
    /// Delete the word before the cursor (emacs C-w).
156
3
    pub fn kill_word_backward(&mut self) {
157
3
        let mut chars = self.chars();
158
3
        let mut i = self.cursor;
159
3
        while i > 0 && chars[i - 1].is_whitespace() {
160
            i -= 1;
161
        }
162
10
        while i > 0 && !chars[i - 1].is_whitespace() {
163
7
            i -= 1;
164
        }
165
3
        chars.drain(i..self.cursor);
166
3
        self.cursor = i;
167
3
        self.rebuild(&chars);
168
3
    }
169

            
170
5
    pub fn word_forward(&mut self) {
171
5
        let chars = self.chars();
172
5
        let mut i = self.cursor;
173
24
        while i < chars.len() && !chars[i].is_whitespace() {
174
19
            i += 1;
175
        }
176
8
        while i < chars.len() && chars[i].is_whitespace() {
177
3
            i += 1;
178
        }
179
5
        self.cursor = i;
180
5
    }
181

            
182
7
    pub fn word_backward(&mut self) {
183
7
        let chars = self.chars();
184
7
        let mut i = self.cursor;
185
8
        while i > 0 && chars[i - 1].is_whitespace() {
186
1
            i -= 1;
187
        }
188
23
        while i > 0 && !chars[i - 1].is_whitespace() {
189
16
            i -= 1;
190
        }
191
7
        self.cursor = i;
192
7
    }
193

            
194
3
    pub fn delete_word_forward(&mut self) {
195
3
        let mut chars = self.chars();
196
3
        let start = self.cursor;
197
3
        let mut i = start;
198
16
        while i < chars.len() && !chars[i].is_whitespace() {
199
13
            i += 1;
200
        }
201
4
        while i < chars.len() && chars[i].is_whitespace() {
202
1
            i += 1;
203
        }
204
3
        chars.drain(start..i);
205
3
        self.rebuild(&chars);
206
3
    }
207
}
208

            
209
#[cfg(test)]
210
#[path = "editor_tests.rs"]
211
mod tests;