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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
use crossterm::event::{self, Event, KeyCode, KeyEvent, KeyModifiers};
use crossterm::{
    cursor, execute, queue,
    style::{Color, ResetColor, SetForegroundColor},
    terminal::{self, disable_raw_mode, enable_raw_mode},
};
use std::cmp;
use std::io::{self, stdout, Write};

/// Returned by [CustomInput's handle_key_press](CustomInput::handle_key_press) to signal how the key event should be handled.
pub enum KeyPressResult {
    /// Tells the input that this event has been handled, and shouldn't be further processed.
    Handled,
    /// Tells the input to stop, like it is finished / submitted.
    Stop,
    /// Continue handling event as normal.
    Continue,
}

/// Context given to [CustomInput]
pub struct HandlerContext<'a> {
    pub text_data: &'a mut TextInputData,
    pub terminal_size: &'a (u16, u16),
}

/// Trait that allows custom implementations / behaviour of an [input](CoolInput)
#[allow(unused_variables)]
pub trait CustomInputHandler {
    /// Called before handling of every key press.
    fn handle_key_press(&mut self, key: &Event, ctx: HandlerContext) -> KeyPressResult {
        if let Event::Key(key_event) = key {
            // Make pressing Escape stop the input
            if let KeyCode::Esc = key_event.code {
                return KeyPressResult::Stop;
            }

            // Make CTRL + C also stop
            if let KeyCode::Char(c) = key_event.code {
                if c == 'c' && key_event.modifiers.contains(KeyModifiers::CONTROL) {
                    return KeyPressResult::Stop;
                }
            }
        }
        KeyPressResult::Continue
    }
    /// Called before the user's text input is drawn. Here you can ex. change color of the inputted text
    fn before_draw_text(&mut self, ctx: HandlerContext) {
        let _ = queue!(stdout(), SetForegroundColor(Color::White));
    }
    /// Called after the user's text is drawn. Here you can ex. draw other text like information or a title of the document.
    fn after_draw_text(&mut self, ctx: HandlerContext) {}
    /// Called by the parent [input](CoolInput) to get the input area's offset, I.e. where the user will start typing.
    fn get_offset(&mut self, ctx: HandlerContext) -> (u16, u16) {
        (0, 0)
    }
    /// Called by the parent [input](CoolInput) to get the input area's size
    fn get_size(&mut self, ctx: HandlerContext) -> (u16, u16) {
        *ctx.terminal_size
    }
}
/// A basic default input handler that implements all default functions of the [CustomInput] trait.
pub struct DefaultInputHandler;
impl CustomInputHandler for DefaultInputHandler {}

fn get_slice_of_string(text: &str, start: usize, end: usize) -> String {
    let mut new_text = String::new();
    let length = text.chars().count();

    for i in start..end {
        if i >= length {
            break;
        }
        new_text.insert(
            new_text.len(),
            text.chars().nth(i).expect("Char at pos should exist"),
        );
    }
    new_text
}

pub struct TextInputData {
    pub text: String,
    pub cursor_x: usize,
    pub cursor_y: usize,
    pub tab_width: usize,
}

/// The main input type. Uses a custom input handler (a struct which implements [CustomInput])
pub struct CoolInput<H: CustomInputHandler> {
    pub text_data: TextInputData,
    pub scroll_x: usize,
    pub scroll_y: usize,
    pub listening: bool,
    pub custom_input: H,
}

/// Helper function to draw text to the screen by a coordinate
pub fn set_terminal_line(
    text: &str,
    x: usize,
    y: usize,
    overwrite: bool,
) -> Result<(), std::io::Error> {
    if overwrite {
        queue!(
            stdout(),
            cursor::MoveTo(x as u16, y as u16),
            terminal::Clear(terminal::ClearType::CurrentLine)
        )?;
        print!("{text}");
    } else {
        queue!(stdout(), cursor::MoveTo(x as u16, y as u16))?;
        print!("{text}");
    }
    Ok(())
}

impl TextInputData {
    pub fn write_char(&mut self, c: char) -> Result<(), std::io::Error> {
        self.insert_char(c, self.cursor_x, self.cursor_y);
        self.move_cursor_right()?;
        Ok(())
    }
    pub fn insert_char(&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;
        }
    }
    pub 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.move_cursor_left()?;
        }

        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 move_cursor_end(&mut self) -> Result<(), std::io::Error> {
        if self.get_amt_lines() > 0 {
            self.cursor_x = self.get_current_line_length()?;
        }
        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);
        } 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);
        } else {
            self.move_cursor_end()?;
        }
        Ok(())
    }
    fn move_cursor_left(&mut self) -> Result<(), std::io::Error> {
        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()?;
            }
        }
        Ok(())
    }
    fn move_cursor_right(&mut self) -> Result<(), std::io::Error> {
        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;
            }
        }

        Ok(())
    }
    pub fn get_amt_lines(&mut self) -> usize {
        let mut amt = self.text.lines().count();
        if self.text.ends_with("\n") {
            amt += 1;
        }
        amt
    }
    pub fn get_line_at(&mut self, y: usize) -> Option<&str> {
        self.text.lines().nth(y)
    }
    pub fn get_current_line_length(&mut self) -> Result<usize, std::io::Error> {
        let line = self.get_line_at(self.cursor_y);
        match line {
            Some(text) => Ok(text.chars().count()),
            None => Err(std::io::Error::new(
                io::ErrorKind::Other,
                "Couldn't get length of current line because it doesn't exist.",
            )),
        }
    }
    fn handle_key_press(&mut self, key_event: KeyEvent) -> Result<(), std::io::Error> {
        match key_event.code {
            KeyCode::Char(c) => {
                self.insert_char(c, self.cursor_x, self.cursor_y);
                self.move_cursor_right()?;
            }
            KeyCode::Enter => {
                self.insert_char('\n', self.cursor_x, self.cursor_y);
                self.cursor_y += 1;
                self.cursor_x = 0;
            }
            KeyCode::Backspace => {
                if self.cursor_x > 0 || self.cursor_y != 0 {
                    self.remove_character(self.cursor_x, self.cursor_y)?;
                }
            }
            KeyCode::Tab => {
                for _ in 0..self.tab_width {
                    self.insert_char(' ', self.cursor_x, self.cursor_y);
                }
                self.cursor_x += self.tab_width;
            }
            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)?;
                    }
                }
            }
            KeyCode::Up => {
                self.move_cursor_up()?;
            }
            KeyCode::Down => {
                if self.get_amt_lines() > 0 {
                    self.move_cursor_down()?;
                }
            }
            KeyCode::Left => {
                self.move_cursor_left()?;
            }
            KeyCode::Right if self.get_amt_lines() > 0 => {
                self.move_cursor_right()?;
            }
            KeyCode::Home => {
                self.cursor_x = 0;
            }
            KeyCode::End => {
                self.move_cursor_end()?;
            }
            _ => {}
        }
        Ok(())
    }
}

impl<H: CustomInputHandler> CoolInput<H> {
    pub fn new(handler: H, tab_width: usize) -> Self {
        CoolInput {
            text_data: TextInputData {
                text: String::new(),
                cursor_x: 0,
                cursor_y: 0,
                tab_width,
            },
            listening: false,
            scroll_x: 0,
            scroll_y: 0,
            custom_input: handler,
        }
    }
    /// Get the size of the terminal running the program
    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)
    }
    /// Render all text and update cursor
    pub fn render(&mut self) -> Result<(), std::io::Error> {
        self.update_text()?;
        self.update_cursor()?;
        io::stdout().flush()?;
        Ok(())
    }
    fn update_cursor(&mut self) -> Result<(), std::io::Error> {
        let terminal_size = self.get_terminal_size()?;
        let (width, height) = self.custom_input.get_size(HandlerContext {
            text_data: &mut self.text_data,
            terminal_size: &terminal_size,
        });
        let (offset_x, offset_y) = self.custom_input.get_offset(HandlerContext {
            text_data: &mut self.text_data,
            terminal_size: &terminal_size,
        });

        let x = self.text_data.cursor_x as i16 + offset_x as i16 - self.scroll_x as i16;
        let x: u16 = cmp::max(x, 0_i16) as u16;
        let x = cmp::min(x, offset_x + width);
        let target_y = (self.text_data.cursor_y as u16) + offset_y;
        let target_y = target_y.saturating_sub(self.scroll_y as u16);
        let y = cmp::min(
            cmp::min(target_y, offset_y + height - 1),
            terminal_size.1 - 1,
        );
        queue!(stdout(), cursor::Show)?;
        queue!(stdout(), cursor::MoveTo(x, y))?;
        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(HandlerContext {
            text_data: &mut self.text_data,
            terminal_size: &terminal_size,
        });
        let (offset_x, offset_y) = self.custom_input.get_offset(HandlerContext {
            text_data: &mut self.text_data,
            terminal_size: &terminal_size,
        });
        self.custom_input.before_draw_text(HandlerContext {
            text_data: &mut self.text_data,
            terminal_size: &terminal_size,
        });
        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_data.text.lines().count() as i16) {
                if let Some(line) = self.text_data.get_line_at(y_line_index as usize) {
                    let text =
                        get_slice_of_string(line, self.scroll_x, self.scroll_x + width as usize);
                    set_terminal_line(&text, 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(HandlerContext {
            text_data: &mut self.text_data,
            terminal_size: &terminal_size,
        });
        Ok(())
    }
    fn scroll_in_view(
        &mut self,
        moving_right: bool,
        moving_down: bool,
    ) -> Result<(), std::io::Error> {
        let terminal_size = self.get_terminal_size()?;
        let input_size = self.custom_input.get_size(HandlerContext {
            text_data: &mut self.text_data,
            terminal_size: &terminal_size,
        });
        self.scroll_x = self.keep_scroll_axis_in_view(
            self.scroll_x,
            self.text_data.cursor_x,
            input_size.0 as usize,
            moving_right,
        )?;
        self.scroll_y = self.keep_scroll_axis_in_view(
            self.scroll_y,
            self.text_data.cursor_y,
            input_size.1 as usize,
            moving_down,
        )?;
        Ok(())
    }
    fn keep_scroll_axis_in_view(
        &mut self,
        scroll_amt: usize,
        cursor_pos: usize,
        bounds: usize,
        moving_direction: bool,
    ) -> Result<usize, std::io::Error> {
        let mut scroll_amt = scroll_amt;
        if moving_direction {
            if cursor_pos > bounds - 1 {
                scroll_amt = cmp::max(scroll_amt, cursor_pos - bounds + 1);
            }
        } else if cursor_pos < scroll_amt {
            scroll_amt = cursor_pos;
        }
        Ok(scroll_amt)
    }
    /// Handle a key event
    pub fn handle_key_press(&mut self, key: Event) -> Result<(), std::io::Error> {
        let terminal_size = self.get_terminal_size()?;
        let old_cursor_x = self.text_data.cursor_x;
        let old_cursor_y = self.text_data.cursor_y;
        match self.custom_input.handle_key_press(
            &key,
            HandlerContext {
                text_data: &mut self.text_data,
                terminal_size: &terminal_size,
            },
        ) {
            KeyPressResult::Handled => {
                self.scroll_in_view(
                    self.text_data.cursor_x > old_cursor_x,
                    self.text_data.cursor_y > old_cursor_y,
                )?;
                self.render()?;
                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 {
                        self.text_data.handle_key_press(key_event)?;
                        self.scroll_in_view(
                            self.text_data.cursor_x > old_cursor_x,
                            self.text_data.cursor_y > old_cursor_y,
                        )?;
                        self.render()?;
                    }
                }
            }
        }
        Ok(())
    }
    /// Start listening for key presses without preparing the terminal
    pub fn listen_quiet(&mut self) -> Result<(), std::io::Error> {
        self.listening = true;
        while self.listening {
            self.handle_key_press(event::read()?)?;
        }
        Ok(())
    }
    /// Prepare the terminal for input
    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(HandlerContext {
            text_data: &mut self.text_data,
            terminal_size: &terminal_size,
        });
        enable_raw_mode()?;
        execute!(
            stdout(),
            terminal::Clear(terminal::ClearType::All),
            cursor::MoveTo(
                (self.text_data.cursor_x as u16) + offset_x,
                (self.text_data.cursor_y as u16) + offset_y
            )
        )?;
        Ok(())
    }
    /// Restore the terminal after input is finished.
    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(())
    }
    /// Prepare terminal and start to listen for key presses until finished.
    pub fn listen(&mut self) -> Result<(), std::io::Error> {
        self.pre_listen()?;
        self.render()?;
        self.listen_quiet()?;
        self.post_listen()?;
        Ok(())
    }
}