~hime/linetest

558575a9ee97707f39065c400e840925aff06705 — drbawb 4 years ago 9239b6c
(ui) clean up cursor handling code

- add delete() operation
- combine cursor_left/right w/ advance/retreat
1 files changed, 24 insertions(+), 25 deletions(-)

M src/app.rs
M src/app.rs => src/app.rs +24 -25
@@ 31,13 31,29 @@ impl InputBuffer {
    }

    pub fn insert(&mut self, ch: char) {
        self.buffer.push(ch);
        self.advance();
        if self.pos < self.buffer.len() {
            self.buffer.insert(self.pos, ch);    
        } else {
            self.buffer.push(ch);
        }

        self.cursor_right();
    }

    pub fn backspace(&mut self) {
        self.buffer.pop();
        self.retreat();
        if self.pos < self.buffer.len() {
            self.buffer.remove(self.pos - 1);
        } else {
            self.buffer.pop();
        }
        
        self.cursor_left();
    }

    pub fn delete(&mut self) {
        if self.pos < self.buffer.len() {
            self.buffer.remove(self.pos);
        }
    }

    pub fn cursor_left(&mut self) {


@@ 62,27 78,6 @@ impl InputBuffer {
        }
    }

    fn advance(&mut self) {
        self.pos += 1;

        if self.pos > self.end {
            // move viewport forwards
            self.start += 1;
            self.end += 1;
        }
    }

    fn retreat(&mut self) {
        if self.pos <= 0 { return; } // ... no.

        self.pos -= 1;

        if self.start > 0 {
            self.start -= 1;
            self.end -= 1;
        }
    }

    fn span_len(&self) -> usize {
        self.end - self.start
    }


@@ 248,6 243,10 @@ impl Shell {
                    self.buffer.backspace();
                },

                event::KeyCode::Delete => {
                    self.buffer.delete();
                },

                event::KeyCode::Left => {
                    self.buffer.cursor_left();
                },