helix-mods/helix-view/src/prompt.rs

69 lines
1.7 KiB
Rust
Raw Normal View History

use crate::commands;
use crate::View;
use crossterm::event::{KeyCode, KeyEvent};
2020-10-09 22:55:45 +02:00
use std::string::String;
pub struct Prompt {
pub buffer: String,
pub cursor_loc: usize,
2020-10-09 22:55:45 +02:00
}
impl Prompt {
pub fn new() -> Prompt {
let prompt = Prompt {
2020-10-13 18:57:55 +02:00
buffer: String::from(""),
cursor_loc: 0,
2020-10-09 22:55:45 +02:00
};
prompt
}
pub fn insert_char(&mut self, c: char) {
2020-10-13 18:57:55 +02:00
self.buffer.insert(self.cursor_loc, c);
self.cursor_loc += 1;
2020-10-09 22:55:45 +02:00
}
2020-10-13 18:57:55 +02:00
pub fn move_char_left_prompt(&mut self) {
if self.cursor_loc > 1 {
self.cursor_loc -= 1;
}
}
pub fn move_char_right_prompt(&mut self) {
if self.cursor_loc < self.buffer.len() {
self.cursor_loc += 1;
}
}
pub fn delete_char_backwards(&mut self) {
if self.cursor_loc > 0 {
self.buffer.remove(self.cursor_loc - 1);
self.cursor_loc -= 1;
}
}
pub fn handle_input(&mut self, key_event: KeyEvent, view: &mut View) {
match key_event {
KeyEvent {
code: KeyCode::Char(c),
..
} => self.insert_char(c),
KeyEvent {
code: KeyCode::Esc, ..
} => commands::normal_mode(view, 1),
2020-10-13 18:57:55 +02:00
KeyEvent {
code: KeyCode::Right,
..
} => self.move_char_right_prompt(),
KeyEvent {
code: KeyCode::Left,
..
} => self.move_char_left_prompt(),
KeyEvent {
code: KeyCode::Backspace,
..
} => self.delete_char_backwards(),
_ => (),
}
}
2020-10-09 22:55:45 +02:00
}