cool_rust_input/
lib.rs

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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
use crossterm::event::{self, Event, KeyCode};
use crossterm::{
    cursor, execute,
    style::{Color, ResetColor, SetForegroundColor},
    terminal::{self, disable_raw_mode, enable_raw_mode},
};
use std::cmp;
use std::io::{self, stdout, Write};

pub enum KeyPressResult {
    Handled,
    Stop,
    Continue,
}

#[allow(unused_variables)]
pub trait CustomInput {
    fn handle_key_press(&mut self, key: &Event, current_text: String) -> KeyPressResult {
        if let Event::Key(key_event) = key {
            if let KeyCode::Esc = key_event.code {
                return KeyPressResult::Stop;
            }
        }
        KeyPressResult::Continue
    }
    fn before_draw_text(&mut self, terminal_size: (u16, u16), current_text: String) {
        let _ = execute!(stdout(), SetForegroundColor(Color::Blue));
    }
    fn after_draw_text(&mut self, terminal_size: (u16, u16), current_text: String) {
        let _ = execute!(stdout(), ResetColor);
    }
    fn get_offset(&mut self, terminal_size: (u16, u16), current_text: String) -> (u16, u16) {
        (0, 0)
    }
    fn get_size(&mut self, terminal_size: (u16, u16), current_text: String) -> (u16, u16) {
        terminal_size
    }
}
pub struct DefaultInputHandler;
impl CustomInput for DefaultInputHandler {}

pub struct CoolInput<H: CustomInput> {
    pub text: String,
    pub cursor_x: usize,
    pub cursor_y: usize,
    pub scroll_y: usize,
    pub listening: bool,
    pub custom_input: H,
    pub tab_width: usize,
}

pub fn set_terminal_line(
    text: &str,
    x: usize,
    y: usize,
    overwrite: bool,
) -> Result<(), std::io::Error> {
    execute!(stdout(), cursor::Hide)?;
    if overwrite {
        print!("\x1b[{};{}H\x1b[2K{}", y + 1, x + 1, text);
    } else {
        print!("\x1b[{};{}H{}", y + 1, x + 1, text);
    }
    Ok(())
}

impl<H: CustomInput> CoolInput<H> {
    pub fn new(handler: H, tab_width: usize) -> Self {
        CoolInput {
            text: String::new(),
            cursor_x: 0,
            cursor_y: 0,
            listening: false,
            scroll_y: 0,
            tab_width: tab_width,
            custom_input: handler,
        }
    }
    pub fn render(&mut self) -> Result<(), std::io::Error> {
        self.update_text()?;
        self.update_cursor()?;
        Ok(())
    }
    fn update_cursor(&mut self) -> Result<(), std::io::Error> {
        execute!(stdout(), cursor::Show)?;
        let terminal_size = self.get_terminal_size()?;
        let (width, height) = self
            .custom_input
            .get_size(terminal_size, self.text.to_string());
        let (offset_x, offset_y) = self
            .custom_input
            .get_offset(terminal_size, self.text.to_string());
        let x = cmp::min((self.cursor_x as u16) + offset_x, offset_x + width);
        let target_y = (self.cursor_y as u16) + offset_y;
        let target_y = target_y.checked_sub(self.scroll_y as u16).unwrap_or(0);
        let y = cmp::min(
            cmp::min(target_y, offset_y + height - 1),
            terminal_size.1 - 1,
        );
        execute!(stdout(), cursor::MoveTo(x, y))?;
        Ok(())
    }
    pub fn get_terminal_size(&mut self) -> Result<(u16, u16), std::io::Error> {
        let mut terminal_size = terminal::size()?;
        terminal_size.1 -= 1;
        Ok(terminal_size)
    }
    fn insert_string(&mut self, c: char, x: usize, y: usize) {
        let mut new = String::new();
        let mut cur_x = 0;
        let mut cur_y = 0;

        if x == 0 && y == 0 {
            self.text.insert(0, c);
        } else {
            let mut found = false;
            for char in self.text.chars() {
                cur_x += 1;
                if char == '\n' {
                    cur_y += 1;
                    cur_x = 0;
                }
                new.insert(new.len(), char);
                if cur_x == x && cur_y == y {
                    new.insert(new.len(), c);
                    found = true;
                }
            }
            if !found {
                println!("{}, {}", x, y);
                std::process::exit(1);
            }
            self.text = new;
        }
    }
    fn remove_character(&mut self, x: usize, y: usize) -> Result<(), std::io::Error> {
        let mut new = String::new();
        let mut cur_x = 0;
        let mut cur_y = 0;

        if x == 0 {
            self.move_cursor_up()?;
            self.cursor_x = self.get_current_line_length()?;
        } else {
            self.cursor_x -= 1;
        }

        if !self.text.is_empty() {
            for char in self.text.chars() {
                cur_x += 1;
                if char == '\n' {
                    cur_y += 1;
                    cur_x = 0;
                }
                if cur_x != x || cur_y != y {
                    new.insert(new.len(), char);
                }
            }
        }
        self.text = new;
        Ok(())
    }
    fn update_text(&mut self) -> Result<(), std::io::Error> {
        let terminal_size = self.get_terminal_size()?;
        let (_width, height) = self
            .custom_input
            .get_size(terminal_size, self.text.to_string());
        let (offset_x, offset_y) = self
            .custom_input
            .get_offset(terminal_size, self.text.to_string());
        self.custom_input
            .before_draw_text(terminal_size, self.text.to_string());
        let offset_y = offset_y as i16;
        for y in offset_y..offset_y + (height as i16) {
            let y_line_index = y - offset_y + (self.scroll_y as i16);
            if y_line_index >= 0 && y_line_index < (self.text.lines().count() as i16) {
                let line = self.get_line_at(y_line_index as usize)?;
                set_terminal_line(&line, offset_x as usize, y as usize, true)?;
            } else {
                set_terminal_line("", offset_x as usize, y as usize, true)?;
            }
        }
        self.custom_input
            .after_draw_text(terminal_size, self.text.to_string());
        io::stdout().flush()?;
        Ok(())
    }
    fn move_cursor_end(&mut self) -> Result<(), std::io::Error> {
        if self.get_amt_lines() > 0 {
            self.cursor_x = self.get_current_line_length()?;
            self.update_cursor()?;
        }
        Ok(())
    }
    fn move_cursor_up(&mut self) -> Result<(), std::io::Error> {
        if self.cursor_y > 0 {
            self.cursor_y -= 1;
            self.cursor_x = cmp::min(self.get_current_line_length()?, self.cursor_x);
            if self.cursor_y < self.scroll_y {
                self.scroll_y -= 1;
            }
            self.update_text()?;
        } else {
            self.cursor_x = 0;
        }
        Ok(())
    }
    fn move_cursor_down(&mut self) -> Result<(), std::io::Error> {
        if self.cursor_y < self.get_amt_lines() - 1 {
            self.cursor_y += 1;
            self.cursor_x = cmp::min(self.get_current_line_length()?, self.cursor_x);
            let text = self.text.to_string();
            let terminal_size = self.get_terminal_size()?;
            let input_height = self.custom_input.get_size(terminal_size, text).1;
            if self.cursor_y >= (input_height as usize) + self.scroll_y {
                self.scroll_y += 1;
            }
            self.update_text()?;
        } else {
            self.move_cursor_end()?;
        }
        Ok(())
    }
    fn get_amt_lines(&mut self) -> usize {
        let mut amt = self.text.lines().count();
        if self.text.ends_with("\n") {
            amt += 1;
        }
        amt
    }
    fn get_line_at(&mut self, y: usize) -> Result<String, std::io::Error> {
        Ok(self.text.lines().nth(y).unwrap_or("").to_string())
    }
    fn get_current_line_length(&mut self) -> Result<usize, std::io::Error> {
        Ok(self.get_line_at(self.cursor_y)?.chars().count())
    }
    pub fn handle_key_press(&mut self, key: Event) -> Result<(), std::io::Error> {
        match self
            .custom_input
            .handle_key_press(&key, self.text.to_string())
        {
            KeyPressResult::Handled => {
                return Ok(());
            }
            KeyPressResult::Stop => {
                self.listening = false;
                return Ok(());
            }
            KeyPressResult::Continue => {
                if let Event::Key(key_event) = key {
                    if key_event.kind == crossterm::event::KeyEventKind::Press {
                        match key_event.code {
                            KeyCode::Char(c) => {
                                self.insert_string(c, self.cursor_x, self.cursor_y);
                                self.cursor_x += 1;
                                self.update_text()?;
                                self.update_cursor()?;
                            }
                            KeyCode::Enter => {
                                self.insert_string('\n', self.cursor_x, self.cursor_y);
                                self.cursor_y += 1;
                                self.cursor_x = 0;
                                self.update_text()?;
                                self.update_cursor()?;
                            }
                            KeyCode::Backspace => {
                                if self.cursor_x > 0 || self.cursor_y != 0 {
                                    self.remove_character(self.cursor_x, self.cursor_y)?;
                                    self.update_text()?;
                                    self.update_cursor()?;
                                }
                            }
                            KeyCode::Tab => {
                                for _ in 0..self.tab_width {
                                    self.insert_string(' ', self.cursor_x, self.cursor_y);
                                }
                                self.cursor_x += self.tab_width;
                                self.update_text()?;
                                self.update_cursor()?;
                            }
                            KeyCode::Delete => {
                                if self.get_amt_lines() > 0 {
                                    let line_length = self.get_current_line_length()?;
                                    if self.cursor_x < line_length
                                        || self.cursor_y != self.get_amt_lines() - 1
                                    {
                                        if self.cursor_x == line_length {
                                            self.cursor_x = 0;
                                            self.cursor_y += 1;
                                        } else {
                                            self.cursor_x += 1;
                                        }
                                        self.remove_character(self.cursor_x, self.cursor_y)?;
                                        self.update_text()?;
                                        self.update_cursor()?;
                                    }
                                }
                            }
                            KeyCode::Up => {
                                self.move_cursor_up()?;
                                self.update_cursor()?;
                            }
                            KeyCode::Down => {
                                if self.get_amt_lines() > 0 {
                                    self.move_cursor_down()?;
                                    self.update_cursor()?;
                                }
                            }
                            KeyCode::Left => {
                                if self.cursor_x > 0 || self.cursor_y != 0 {
                                    if self.cursor_x > 0 {
                                        self.cursor_x -= 1;
                                    } else {
                                        self.cursor_y -= 1;
                                        self.cursor_x = self.get_current_line_length()?;
                                    }
                                }
                                self.update_text()?;
                                self.update_cursor()?;
                            }
                            KeyCode::Right if self.get_amt_lines() > 0 => {
                                if self.cursor_y != self.get_amt_lines() - 1
                                    || self.cursor_x < self.get_current_line_length()?
                                {
                                    if self.cursor_x != self.get_current_line_length()? {
                                        self.cursor_x += 1;
                                    } else {
                                        self.cursor_y += 1;
                                        self.cursor_x = 0;
                                    }
                                    self.update_text()?;
                                    self.update_cursor()?;
                                }
                            }
                            KeyCode::Home => {
                                self.cursor_x = 0;
                                self.update_text()?;
                                self.update_cursor()?;
                            }
                            KeyCode::End => {
                                self.update_text()?;
                                self.move_cursor_end()?;
                            }
                            _ => {}
                        }
                    }
                }
            }
        }
        Ok(())
    }
    pub fn listen_quiet(&mut self) -> Result<(), std::io::Error> {
        self.listening = true;
        while self.listening {
            self.handle_key_press(event::read()?)?;
        }
        Ok(())
    }
    pub fn pre_listen(&mut self) -> Result<(), std::io::Error> {
        let terminal_size = self.get_terminal_size()?;
        let (offset_x, offset_y) = self
            .custom_input
            .get_offset(terminal_size, self.text.to_string());
        enable_raw_mode()?;
        execute!(
            stdout(),
            terminal::Clear(terminal::ClearType::All),
            cursor::MoveTo(
                (self.cursor_x as u16) + offset_x,
                (self.cursor_y as u16) + offset_y
            )
        )?;
        Ok(())
    }
    pub fn post_listen(&mut self) -> Result<(), std::io::Error> {
        execute!(
            stdout(),
            ResetColor,
            terminal::Clear(terminal::ClearType::All),
            cursor::MoveTo(0, 0)
        )?;
        disable_raw_mode()?;
        Ok(())
    }
    pub fn listen(&mut self) -> Result<(), std::io::Error> {
        self.pre_listen()?;
        self.render()?;
        self.listen_quiet()?;
        self.post_listen()?;
        Ok(())
    }
}