on master help sign in
moved prompt command matching to prompt.rs
ed03ec9Jan Hrastnik
helix-core/src/state.rs+1−1
@@ -10,7 +10,7 @@ pub enum Mode {
10 Normal,
10 Normal,
11 Insert,
11 Insert,
12 Goto,
12 Goto,
13− Command,
13+ Prompt,
14}
14}
15
15
16/// A state represents the current editor state of a single buffer.
16/// A state represents the current editor state of a single buffer.
helix-term/src/editor.rs+13−16
@@ -237,7 +237,7 @@ impl Editor {
237 Mode::Insert => "INS",
237 Mode::Insert => "INS",
238 Mode::Normal => "NOR",
238 Mode::Normal => "NOR",
239 Mode::Goto => "GOTO",
239 Mode::Goto => "GOTO",
240− Mode::Command => "COM", // command mode i guess
240+ Mode::Prompt => "PRO", // prompt?
241 };
241 };
242 // statusline
242 // statusline
243 self.surface.set_style(
243 self.surface.set_style(
@@ -252,9 +252,14 @@ impl Editor {
252 use tui::backend::Backend;
252 use tui::backend::Backend;
253 let view = self.view.as_ref().unwrap();
253 let view = self.view.as_ref().unwrap();
254 // render buffer text
254 // render buffer text
255− let buffer_string = &self.prompt.buffer;
255+ let buffer_string;
256+ if view.state.mode == Mode::Prompt {
257+ buffer_string = &self.prompt.buffer;
256− self.surface
258+ self.surface
257− .set_string(2, self.size.1 - 1, buffer_string, text_color);
259+ .set_string(1, self.size.1 - 1, buffer_string, text_color);
260+ } else {
261+ buffer_string = &String::from("");
262+ }
258
263
259 // TODO: theres probably a better place for this
264 // TODO: theres probably a better place for this
260 self.terminal
265 self.terminal
@@ -272,8 +277,8 @@ impl Editor {
272 Mode::Insert => write!(stdout, "\x1B[6 q"),
277 Mode::Insert => write!(stdout, "\x1B[6 q"),
273 mode => write!(stdout, "\x1B[2 q"),
278 mode => write!(stdout, "\x1B[2 q"),
274 };
279 };
275− if view.state.mode() == Mode::Command {
280+ if view.state.mode() == Mode::Prompt {
276− pos = Position::new(self.size.0 as usize, 2 + self.prompt.buffer.len());
281+ pos = Position::new(self.size.0 as usize, 1 + self.prompt.buffer.len());
277 } else {
282 } else {
278 if let Some(path) = view.state.path() {
283 if let Some(path) = view.state.path() {
279 self.surface
284 self.surface
@@ -341,16 +346,8 @@ impl Editor {
341 }
346 }
342 view.ensure_cursor_in_view();
347 view.ensure_cursor_in_view();
343 }
348 }
344− Mode::Command => {
349+ Mode::Prompt => {
345− if let Some(command) = keymap[&Mode::Command].get(&keys) {
350+ self.prompt.handle_keyevent(event, view);
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− }
354 }
351 }
355 mode => {
352 mode => {
356 if let Some(command) = keymap[&mode].get(&keys) {
353 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) {
307 })
307 })
308}
308}
309
309
310−pub fn command_mode(view: &mut View, _count: usize) {
310+pub fn prompt_mode(view: &mut View, _count: usize) {
311− view.state.mode = Mode::Command;
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+ }
312}
324}
313
325
314// TODO: I, A, o and O can share a lot of the primitives.
326// 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 {
163 vec![key!('p')] => commands::paste,
163 vec![key!('p')] => commands::paste,
164 vec![key!('>')] => commands::indent,
164 vec![key!('>')] => commands::indent,
165 vec![key!('<')] => commands::unindent,
165 vec![key!('<')] => commands::unindent,
166− vec![key!(':')] => commands::command_mode,
166+ vec![key!(':')] => commands::prompt_mode,
167 vec![Key {
167 vec![Key {
168 code: KeyCode::Esc,
168 code: KeyCode::Esc,
169 modifiers: Modifiers::NONE
169 modifiers: Modifiers::NONE
@@ -209,7 +209,7 @@ pub fn default() -> Keymaps {
209 vec![key!('g')] => commands::move_file_start as Command,
209 vec![key!('g')] => commands::move_file_start as Command,
210 vec![key!('e')] => commands::move_file_end as Command,
210 vec![key!('e')] => commands::move_file_end as Command,
211 ),
211 ),
212− state::Mode::Command => hashmap!(
212+ state::Mode::Prompt => hashmap!(
213 vec![Key {
213 vec![Key {
214 code: KeyCode::Esc,
214 code: KeyCode::Esc,
215 modifiers: Modifiers::NONE
215 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};
1use std::string::String;
4use std::string::String;
2
5
3pub struct Prompt {
6pub struct Prompt {
4 pub buffer: String,
7 pub buffer: String,
8+ pub cursor_loc: usize,
5}
9}
6
10
7impl Prompt {
11impl Prompt {
8 pub fn new() -> Prompt {
12 pub fn new() -> Prompt {
9 let prompt = Prompt {
13 let prompt = Prompt {
10− buffer: String::from(""),
14+ buffer: String::from(":"), // starting prompt symbol
15+ cursor_loc: 0,
11 };
16 };
12 prompt
17 prompt
13 }
18 }
@@ -15,4 +20,17 @@ impl Prompt {
15 pub fn insert_char(&mut self, c: char) {
20 pub fn insert_char(&mut self, c: char) {
16 self.buffer.push(c);
21 self.buffer.push(c);
17 }
22 }
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+ }
18}
36}