From 7d58378374388c031ad09ceb2e27d4a0792d636e Mon Sep 17 00:00:00 2001 From: Jan Hrastnik Date: Tue, 13 Oct 2020 18:57:55 +0200 Subject: [PATCH] added move left&right, delete char --- helix-core/src/state.rs | 2 +- helix-term/src/editor.rs | 17 ++++++++++------- helix-view/src/commands.rs | 16 ++-------------- helix-view/src/keymap.rs | 8 +------- helix-view/src/prompt.rs | 38 +++++++++++++++++++++++++++++++++++--- 5 files changed, 49 insertions(+), 32 deletions(-) diff --git a/helix-core/src/state.rs b/helix-core/src/state.rs index 805940fb..bd14850f 100644 --- a/helix-core/src/state.rs +++ b/helix-core/src/state.rs @@ -10,7 +10,7 @@ pub enum Mode { Normal, Insert, Goto, - Prompt, + Command, } /// A state represents the current editor state of a single buffer. diff --git a/helix-term/src/editor.rs b/helix-term/src/editor.rs index 22ede486..78456654 100644 --- a/helix-term/src/editor.rs +++ b/helix-term/src/editor.rs @@ -237,7 +237,7 @@ impl Editor { Mode::Insert => "INS", Mode::Normal => "NOR", Mode::Goto => "GOTO", - Mode::Prompt => "PRO", // prompt? + Mode::Command => "COM", // command? }; // statusline self.surface.set_style( @@ -249,14 +249,17 @@ impl Editor { } pub fn render_prompt(&mut self, text_color: Style) { + // TODO: maybe name this render_commandline use tui::backend::Backend; let view = self.view.as_ref().unwrap(); // render buffer text let buffer_string; - if view.state.mode == Mode::Prompt { + if view.state.mode == Mode::Command { buffer_string = &self.prompt.buffer; self.surface - .set_string(1, self.size.1 - 1, buffer_string, text_color); + .set_string(1, self.size.1 - 1, String::from(":"), text_color); + self.surface + .set_string(2, self.size.1 - 1, buffer_string, text_color); } else { buffer_string = &String::from(""); } @@ -277,8 +280,8 @@ impl Editor { Mode::Insert => write!(stdout, "\x1B[6 q"), mode => write!(stdout, "\x1B[2 q"), }; - if view.state.mode() == Mode::Prompt { - pos = Position::new(self.size.0 as usize, 1 + self.prompt.buffer.len()); + if view.state.mode() == Mode::Command { + pos = Position::new(self.size.0 as usize, 2 + self.prompt.cursor_loc); } else { if let Some(path) = view.state.path() { self.surface @@ -346,8 +349,8 @@ impl Editor { } view.ensure_cursor_in_view(); } - Mode::Prompt => { - self.prompt.handle_keyevent(event, view); + Mode::Command => { + self.prompt.handle_input(event, view); } mode => { if let Some(command) = keymap[&mode].get(&keys) { diff --git a/helix-view/src/commands.rs b/helix-view/src/commands.rs index 888317e7..6efbf98d 100644 --- a/helix-view/src/commands.rs +++ b/helix-view/src/commands.rs @@ -307,20 +307,8 @@ pub fn append_mode(view: &mut View, _count: usize) { }) } -pub fn prompt_mode(view: &mut View, _count: usize) { - view.state.mode = Mode::Prompt; -} - -pub fn move_char_left_prompt(prompt: &mut Prompt, _char: char) { - if prompt.cursor_loc > 1 { - prompt.cursor_loc -= 1; - } -} - -pub fn move_char_right_prompt(prompt: &mut Prompt, _char: char) { - if prompt.cursor_loc < prompt.buffer.len() { - prompt.cursor_loc += 1; - } +pub fn command_mode(view: &mut View, _count: usize) { + view.state.mode = Mode::Command; } // TODO: I, A, o and O can share a lot of the primitives. diff --git a/helix-view/src/keymap.rs b/helix-view/src/keymap.rs index d6d44779..69e6cabb 100644 --- a/helix-view/src/keymap.rs +++ b/helix-view/src/keymap.rs @@ -163,7 +163,7 @@ pub fn default() -> Keymaps { vec![key!('p')] => commands::paste, vec![key!('>')] => commands::indent, vec![key!('<')] => commands::unindent, - vec![key!(':')] => commands::prompt_mode, + vec![key!(':')] => commands::command_mode, vec![Key { code: KeyCode::Esc, modifiers: Modifiers::NONE @@ -209,11 +209,5 @@ pub fn default() -> Keymaps { vec![key!('g')] => commands::move_file_start as Command, vec![key!('e')] => commands::move_file_end as Command, ), - state::Mode::Prompt => hashmap!( - vec![Key { - code: KeyCode::Esc, - modifiers: Modifiers::NONE - }] => commands::normal_mode as Command, - ) ) } diff --git a/helix-view/src/prompt.rs b/helix-view/src/prompt.rs index f1920daa..98389a97 100644 --- a/helix-view/src/prompt.rs +++ b/helix-view/src/prompt.rs @@ -11,17 +11,37 @@ pub struct Prompt { impl Prompt { pub fn new() -> Prompt { let prompt = Prompt { - buffer: String::from(":"), // starting prompt symbol + buffer: String::from(""), cursor_loc: 0, }; prompt } pub fn insert_char(&mut self, c: char) { - self.buffer.push(c); + self.buffer.insert(self.cursor_loc, c); + self.cursor_loc += 1; } - pub fn handle_keyevent(&mut self, key_event: KeyEvent, view: &mut View) { + 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), @@ -30,6 +50,18 @@ impl Prompt { KeyEvent { code: KeyCode::Esc, .. } => commands::normal_mode(view, 1), + 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(), _ => (), } }