on master help sign in
added move left&right, delete char
commit7d58378374388c031ad09ceb2e27d4a0792d636e authorJan Hrastnik <jan.hrastnik2@gmail.com> dateTue 13 Oct 2020, 18:57 (6 years ago) parented03ec9
5 files changed+49−32
helix-core/src/state.rs+1−1 helix-term/src/editor.rs+10−7 helix-view/src/commands.rs+2−14 helix-view/src/keymap.rs+1−7 helix-view/src/prompt.rs+35−3
helix-core/src/state.rs+1−1
@@ -10,7 +10,7 @@ pub enum Mode {
1010 Normal,
1111 Insert,
1212 Goto,
13− Prompt,
13+ Command,
1414}
1515
1616/// A state represents the current editor state of a single buffer.
helix-term/src/editor.rs+10−7
@@ -237,7 +237,7 @@ impl Editor {
237237 Mode::Insert => "INS",
238238 Mode::Normal => "NOR",
239239 Mode::Goto => "GOTO",
240− Mode::Prompt => "PRO", // prompt?
240+ Mode::Command => "COM", // command?
241241 };
242242 // statusline
243243 self.surface.set_style(
@@ -249,14 +249,17 @@ impl Editor {
249249 }
250250
251251 pub fn render_prompt(&mut self, text_color: Style) {
252+ // TODO: maybe name this render_commandline
252253 use tui::backend::Backend;
253254 let view = self.view.as_ref().unwrap();
254255 // render buffer text
255256 let buffer_string;
256− if view.state.mode == Mode::Prompt {
257+ if view.state.mode == Mode::Command {
257258 buffer_string = &self.prompt.buffer;
258259 self.surface
259− .set_string(1, self.size.1 - 1, buffer_string, text_color);
260+ .set_string(1, self.size.1 - 1, String::from(":"), text_color);
261+ self.surface
262+ .set_string(2, self.size.1 - 1, buffer_string, text_color);
260263 } else {
261264 buffer_string = &String::from("");
262265 }
@@ -277,8 +280,8 @@ impl Editor {
277280 Mode::Insert => write!(stdout, "\x1B[6 q"),
278281 mode => write!(stdout, "\x1B[2 q"),
279282 };
280− if view.state.mode() == Mode::Prompt {
281− pos = Position::new(self.size.0 as usize, 1 + self.prompt.buffer.len());
283+ if view.state.mode() == Mode::Command {
284+ pos = Position::new(self.size.0 as usize, 2 + self.prompt.cursor_loc);
282285 } else {
283286 if let Some(path) = view.state.path() {
284287 self.surface
@@ -346,8 +349,8 @@ impl Editor {
346349 }
347350 view.ensure_cursor_in_view();
348351 }
349− Mode::Prompt => {
350− self.prompt.handle_keyevent(event, view);
352+ Mode::Command => {
353+ self.prompt.handle_input(event, view);
351354 }
352355 mode => {
353356 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) {
307307 })
308308}
309309
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− }
310+pub fn command_mode(view: &mut View, _count: usize) {
311+ view.state.mode = Mode::Command;
324312}
325313
326314// 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 {
163163 vec![key!('p')] => commands::paste,
164164 vec![key!('>')] => commands::indent,
165165 vec![key!('<')] => commands::unindent,
166− vec![key!(':')] => commands::prompt_mode,
166+ vec![key!(':')] => commands::command_mode,
167167 vec![Key {
168168 code: KeyCode::Esc,
169169 modifiers: Modifiers::NONE
@@ -209,11 +209,5 @@ 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::Prompt => hashmap!(
213− vec![Key {
214− code: KeyCode::Esc,
215− modifiers: Modifiers::NONE
216− }] => commands::normal_mode as Command,
217− )
218212 )
219213}
helix-view/src/prompt.rs+35−3
@@ -11,17 +11,37 @@ pub struct Prompt {
1111impl Prompt {
1212 pub fn new() -> Prompt {
1313 let prompt = Prompt {
14− buffer: String::from(":"), // starting prompt symbol
14+ buffer: String::from(""),
1515 cursor_loc: 0,
1616 };
1717 prompt
1818 }
1919
2020 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;
2223 }
2324
24− pub fn handle_keyevent(&mut self, key_event: KeyEvent, view: &mut View) {
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+
44+ pub fn handle_input(&mut self, key_event: KeyEvent, view: &mut View) {
2545 match key_event {
2646 KeyEvent {
2747 code: KeyCode::Char(c),
@@ -30,6 +50,18 @@ impl Prompt {
3050 KeyEvent {
3151 code: KeyCode::Esc, ..
3252 } => 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(),
3365 _ => (),
3466 }
3567 }