on master help sign in
moved prompt command matching to prompt.rs
commited03ec92a825b8e42005f2f8506b0f3db4923fa5 authorJan Hrastnik <jan.hrastnik2@gmail.com> dateTue 13 Oct 2020, 00:23 (6 years ago) parent7208c86
5 files changed+49−22
helix-core/src/state.rs+1−1 helix-term/src/editor.rs+13−16 helix-view/src/commands.rs+14−2 helix-view/src/keymap.rs+2−2 helix-view/src/prompt.rs+19−1
helix-core/src/state.rs+1−1
@@ -10,7 +10,7 @@ pub enum Mode {
1010 Normal,
1111 Insert,
1212 Goto,
13− Command,
13+ Prompt,
1414}
1515
1616/// A state represents the current editor state of a single buffer.
helix-term/src/editor.rs+13−16
@@ -237,7 +237,7 @@ impl Editor {
237237 Mode::Insert => "INS",
238238 Mode::Normal => "NOR",
239239 Mode::Goto => "GOTO",
240− Mode::Command => "COM", // command mode i guess
240+ Mode::Prompt => "PRO", // prompt?
241241 };
242242 // statusline
243243 self.surface.set_style(
@@ -252,9 +252,14 @@ impl Editor {
252252 use tui::backend::Backend;
253253 let view = self.view.as_ref().unwrap();
254254 // render buffer text
255− let buffer_string = &self.prompt.buffer;
256− self.surface
257− .set_string(2, self.size.1 - 1, buffer_string, text_color);
255+ let buffer_string;
256+ if view.state.mode == Mode::Prompt {
257+ buffer_string = &self.prompt.buffer;
258+ self.surface
259+ .set_string(1, self.size.1 - 1, buffer_string, text_color);
260+ } else {
261+ buffer_string = &String::from("");
262+ }
258263
259264 // TODO: theres probably a better place for this
260265 self.terminal
@@ -272,8 +277,8 @@ impl Editor {
272277 Mode::Insert => write!(stdout, "\x1B[6 q"),
273278 mode => write!(stdout, "\x1B[2 q"),
274279 };
275− if view.state.mode() == Mode::Command {
276− pos = Position::new(self.size.0 as usize, 2 + self.prompt.buffer.len());
280+ if view.state.mode() == Mode::Prompt {
281+ pos = Position::new(self.size.0 as usize, 1 + self.prompt.buffer.len());
277282 } else {
278283 if let Some(path) = view.state.path() {
279284 self.surface
@@ -341,16 +346,8 @@ impl Editor {
341346 }
342347 view.ensure_cursor_in_view();
343348 }
344− Mode::Command => {
345− if let Some(command) = keymap[&Mode::Command].get(&keys) {
346− command(view, 1);
347− } else if let KeyEvent {
348− code: KeyCode::Char(c),
349− ..
350− } = event
351− {
352− commands::insert_char_prompt(&mut self.prompt, c)
353− }
349+ Mode::Prompt => {
350+ self.prompt.handle_keyevent(event, view);
354351 }
355352 mode => {
356353 if let Some(command) = keymap[&mode].get(&keys) {
helix-view/src/commands.rs+14−2
@@ -307,8 +307,20 @@ pub fn append_mode(view: &mut View, _count: usize) {
307307 })
308308}
309309
310−pub fn command_mode(view: &mut View, _count: usize) {
311− view.state.mode = Mode::Command;
310+pub fn prompt_mode(view: &mut View, _count: usize) {
311+ view.state.mode = Mode::Prompt;
312+}
313+
314+pub fn move_char_left_prompt(prompt: &mut Prompt, _char: char) {
315+ if prompt.cursor_loc > 1 {
316+ prompt.cursor_loc -= 1;
317+ }
318+}
319+
320+pub fn move_char_right_prompt(prompt: &mut Prompt, _char: char) {
321+ if prompt.cursor_loc < prompt.buffer.len() {
322+ prompt.cursor_loc += 1;
323+ }
312324}
313325
314326// TODO: I, A, o and O can share a lot of the primitives.
helix-view/src/keymap.rs+2−2
@@ -163,7 +163,7 @@ pub fn default() -> Keymaps {
163163 vec![key!('p')] => commands::paste,
164164 vec![key!('>')] => commands::indent,
165165 vec![key!('<')] => commands::unindent,
166− vec![key!(':')] => commands::command_mode,
166+ vec![key!(':')] => commands::prompt_mode,
167167 vec![Key {
168168 code: KeyCode::Esc,
169169 modifiers: Modifiers::NONE
@@ -209,7 +209,7 @@ pub fn default() -> Keymaps {
209209 vec![key!('g')] => commands::move_file_start as Command,
210210 vec![key!('e')] => commands::move_file_end as Command,
211211 ),
212− state::Mode::Command => hashmap!(
212+ state::Mode::Prompt => hashmap!(
213213 vec![Key {
214214 code: KeyCode::Esc,
215215 modifiers: Modifiers::NONE
helix-view/src/prompt.rs+19−1
@@ -1,13 +1,18 @@
1+use crate::commands;
2+use crate::View;
3+use crossterm::event::{KeyCode, KeyEvent};
14use std::string::String;
25
36pub struct Prompt {
47 pub buffer: String,
8+ pub cursor_loc: usize,
59}
610
711impl Prompt {
812 pub fn new() -> Prompt {
913 let prompt = Prompt {
10− buffer: String::from(""),
14+ buffer: String::from(":"), // starting prompt symbol
15+ cursor_loc: 0,
1116 };
1217 prompt
1318 }
@@ -15,4 +20,17 @@ impl Prompt {
1520 pub fn insert_char(&mut self, c: char) {
1621 self.buffer.push(c);
1722 }
23+
24+ pub fn handle_keyevent(&mut self, key_event: KeyEvent, view: &mut View) {
25+ match key_event {
26+ KeyEvent {
27+ code: KeyCode::Char(c),
28+ ..
29+ } => self.insert_char(c),
30+ KeyEvent {
31+ code: KeyCode::Esc, ..
32+ } => commands::normal_mode(view, 1),
33+ _ => (),
34+ }
35+ }
1836}