added move start&end

imgbot
Jan Hrastnik 4 years ago committed by Blaž Hrastnik
parent 7d58378374
commit 3c0f187c5b

@ -1,6 +1,6 @@
use crate::commands; use crate::commands;
use crate::View; use crate::View;
use crossterm::event::{KeyCode, KeyEvent}; use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
use std::string::String; use std::string::String;
pub struct Prompt { pub struct Prompt {
@ -22,18 +22,26 @@ impl Prompt {
self.cursor_loc += 1; self.cursor_loc += 1;
} }
pub fn move_char_left_prompt(&mut self) { pub fn move_char_left(&mut self) {
if self.cursor_loc > 1 { if self.cursor_loc > 1 {
self.cursor_loc -= 1; self.cursor_loc -= 1;
} }
} }
pub fn move_char_right_prompt(&mut self) { pub fn move_char_right(&mut self) {
if self.cursor_loc < self.buffer.len() { if self.cursor_loc < self.buffer.len() {
self.cursor_loc += 1; self.cursor_loc += 1;
} }
} }
pub fn move_start(&mut self) {
self.cursor_loc = 0;
}
pub fn move_end(&mut self) {
self.cursor_loc = self.buffer.len();
}
pub fn delete_char_backwards(&mut self) { pub fn delete_char_backwards(&mut self) {
if self.cursor_loc > 0 { if self.cursor_loc > 0 {
self.buffer.remove(self.cursor_loc - 1); self.buffer.remove(self.cursor_loc - 1);
@ -41,11 +49,15 @@ impl Prompt {
} }
} }
pub fn success_fn() {
// TODO:
}
pub fn handle_input(&mut self, key_event: KeyEvent, view: &mut View) { pub fn handle_input(&mut self, key_event: KeyEvent, view: &mut View) {
match key_event { match key_event {
KeyEvent { KeyEvent {
code: KeyCode::Char(c), code: KeyCode::Char(c),
.. modifiers: KeyModifiers::NONE,
} => self.insert_char(c), } => self.insert_char(c),
KeyEvent { KeyEvent {
code: KeyCode::Esc, .. code: KeyCode::Esc, ..
@ -53,11 +65,19 @@ impl Prompt {
KeyEvent { KeyEvent {
code: KeyCode::Right, code: KeyCode::Right,
.. ..
} => self.move_char_right_prompt(), } => self.move_char_right(),
KeyEvent { KeyEvent {
code: KeyCode::Left, code: KeyCode::Left,
.. ..
} => self.move_char_left_prompt(), } => self.move_char_left(),
KeyEvent {
code: KeyCode::Char('e'),
modifiers: KeyModifiers::CONTROL,
} => self.move_end(),
KeyEvent {
code: KeyCode::Char('a'),
modifiers: KeyModifiers::CONTROL,
} => self.move_start(),
KeyEvent { KeyEvent {
code: KeyCode::Backspace, code: KeyCode::Backspace,
.. ..

Loading…
Cancel
Save