on master help sign in
added move left&right, delete char
7d58378Jan 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− Prompt,
13+ Command,
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+10−7
@@ -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::Prompt => "PRO", // prompt?
240+ Mode::Command => "COM", // command?
241 };
241 };
242 // statusline
242 // statusline
243 self.surface.set_style(
243 self.surface.set_style(
@@ -249,14 +249,17 @@ impl Editor {
249 }
249 }
250
250
251 pub fn render_prompt(&mut self, text_color: Style) {
251 pub fn render_prompt(&mut self, text_color: Style) {
252+ // TODO: maybe name this render_commandline
252 use tui::backend::Backend;
253 use tui::backend::Backend;
253 let view = self.view.as_ref().unwrap();
254 let view = self.view.as_ref().unwrap();
254 // render buffer text
255 // render buffer text
255 let buffer_string;
256 let buffer_string;
256− if view.state.mode == Mode::Prompt {
257+ if view.state.mode == Mode::Command {
257 buffer_string = &self.prompt.buffer;
258 buffer_string = &self.prompt.buffer;
258 self.surface
259 self.surface
260+ .set_string(1, self.size.1 - 1, String::from(":"), text_color);
261+ self.surface
259− .set_string(1, self.size.1 - 1, buffer_string, text_color);
262+ .set_string(2, self.size.1 - 1, buffer_string, text_color);
260 } else {
263 } else {
261 buffer_string = &String::from("");
264 buffer_string = &String::from("");
262 }
265 }
@@ -277,8 +280,8 @@ impl Editor {
277 Mode::Insert => write!(stdout, "\x1B[6 q"),
280 Mode::Insert => write!(stdout, "\x1B[6 q"),
278 mode => write!(stdout, "\x1B[2 q"),
281 mode => write!(stdout, "\x1B[2 q"),
279 };
282 };
280− if view.state.mode() == Mode::Prompt {
283+ if view.state.mode() == Mode::Command {
281− pos = Position::new(self.size.0 as usize, 1 + self.prompt.buffer.len());
284+ pos = Position::new(self.size.0 as usize, 2 + self.prompt.cursor_loc);
282 } else {
285 } else {
283 if let Some(path) = view.state.path() {
286 if let Some(path) = view.state.path() {
284 self.surface
287 self.surface
@@ -346,8 +349,8 @@ impl Editor {
346 }
349 }
347 view.ensure_cursor_in_view();
350 view.ensure_cursor_in_view();
348 }
351 }
349− Mode::Prompt => {
352+ Mode::Command => {
350− self.prompt.handle_keyevent(event, view);
353+ self.prompt.handle_input(event, view);
351 }
354 }
352 mode => {
355 mode => {
353 if let Some(command) = keymap[&mode].get(&keys) {
356 if let Some(command) = keymap[&mode].get(&keys) {
helix-view/src/commands.rs+2−14
@@ -307,20 +307,8 @@ pub fn append_mode(view: &mut View, _count: usize) {
307 })
307 })
308}
308}
309
309
310−pub fn prompt_mode(view: &mut View, _count: usize) {
310+pub fn command_mode(view: &mut View, _count: usize) {
311− view.state.mode = Mode::Prompt;
311+ view.state.mode = Mode::Command;
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− }
324}
312}
325
313
326// TODO: I, A, o and O can share a lot of the primitives.
314// TODO: I, A, o and O can share a lot of the primitives.
helix-view/src/keymap.rs+1−7
@@ -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::prompt_mode,
166+ vec![key!(':')] => commands::command_mode,
167 vec![Key {
167 vec![Key {
168 code: KeyCode::Esc,
168 code: KeyCode::Esc,
169 modifiers: Modifiers::NONE
169 modifiers: Modifiers::NONE
@@ -209,11 +209,5 @@ 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::Prompt => hashmap!(
213− vec![Key {
214− code: KeyCode::Esc,
215− modifiers: Modifiers::NONE
216− }] => commands::normal_mode as Command,
217− )
218 )
212 )
219}
213}
helix-view/src/prompt.rs+35−3
@@ -11,17 +11,37 @@ pub struct Prompt {
11impl Prompt {
11impl Prompt {
12 pub fn new() -> Prompt {
12 pub fn new() -> Prompt {
13 let prompt = Prompt {
13 let prompt = Prompt {
14− buffer: String::from(":"), // starting prompt symbol
14+ buffer: String::from(""),
15 cursor_loc: 0,
15 cursor_loc: 0,
16 };
16 };
17 prompt
17 prompt
18 }
18 }
19
19
20 pub fn insert_char(&mut self, c: char) {
20 pub fn insert_char(&mut self, c: char) {
21− self.buffer.push(c);
21+ self.buffer.insert(self.cursor_loc, c);
22+ self.cursor_loc += 1;
22 }
23 }
23
24
25+ pub fn move_char_left_prompt(&mut self) {
26+ if self.cursor_loc > 1 {
27+ self.cursor_loc -= 1;
28+ }
29+ }
30+
31+ pub fn move_char_right_prompt(&mut self) {
32+ if self.cursor_loc < self.buffer.len() {
33+ self.cursor_loc += 1;
34+ }
35+ }
36+
37+ pub fn delete_char_backwards(&mut self) {
38+ if self.cursor_loc > 0 {
39+ self.buffer.remove(self.cursor_loc - 1);
40+ self.cursor_loc -= 1;
41+ }
42+ }
43+
24− pub fn handle_keyevent(&mut self, key_event: KeyEvent, view: &mut View) {
44+ pub fn handle_input(&mut self, key_event: KeyEvent, view: &mut View) {
25 match key_event {
45 match key_event {
26 KeyEvent {
46 KeyEvent {
27 code: KeyCode::Char(c),
47 code: KeyCode::Char(c),
@@ -30,6 +50,18 @@ impl Prompt {
30 KeyEvent {
50 KeyEvent {
31 code: KeyCode::Esc, ..
51 code: KeyCode::Esc, ..
32 } => commands::normal_mode(view, 1),
52 } => commands::normal_mode(view, 1),
53+ KeyEvent {
54+ code: KeyCode::Right,
55+ ..
56+ } => self.move_char_right_prompt(),
57+ KeyEvent {
58+ code: KeyCode::Left,
59+ ..
60+ } => self.move_char_left_prompt(),
61+ KeyEvent {
62+ code: KeyCode::Backspace,
63+ ..
64+ } => self.delete_char_backwards(),
33 _ => (),
65 _ => (),
34 }
66 }
35 }
67 }