From 68bd6c2dfb0ed593c5e6deebe1d9753db90f03e1 Mon Sep 17 00:00:00 2001 From: Akshay Date: Thu, 18 Mar 2021 21:04:00 +0530 Subject: render cursor to command box, add readline keybinds --- src/app.rs | 33 ++++++++++++++++++++++++++++----- src/command.rs | 45 ++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 70 insertions(+), 8 deletions(-) (limited to 'src') diff --git a/src/app.rs b/src/app.rs index 9ba93f4..3571436 100644 --- a/src/app.rs +++ b/src/app.rs @@ -1,7 +1,7 @@ use crate::{ bitmap::{MapPoint, Pixmap}, command::CommandBox, - consts::colors::*, + consts::{colors::*, FONT_PATH}, dither, rect, symmetry::Symmetry, undo::{ModifyRecord, OpKind, Operation, UndoStack}, @@ -352,6 +352,20 @@ impl<'ctx> AppState<'ctx> { BLACK, (0, winsize_y - cmd_height), ); + + self.canvas.set_draw_color(PINK); + let mut font = self.ttf_context.load_font(FONT_PATH, 17).unwrap(); + font.set_style(sdl2::ttf::FontStyle::NORMAL); + font.set_hinting(sdl2::ttf::Hinting::Mono); + let prev_text = &self.command_box.text[..self.command_box.cursor]; + let prev_text_dim = font.size_of_latin1(prev_text.as_bytes()).unwrap(); + let cursor = rect!( + prev_text_dim.0, + winsize_y - cmd_height + 2, + 2, + cmd_height - 4 + ); + self.canvas.fill_rect(cursor).unwrap(); } fn draw_mouse(&mut self) { @@ -641,7 +655,9 @@ impl<'ctx> AppState<'ctx> { } } if let Event::KeyDown { - keycode: Some(k), .. + keycode: Some(k), + keymod, + .. } = event { match k { @@ -651,9 +667,16 @@ impl<'ctx> AppState<'ctx> { Keycode::Right => self.command_box.forward(), Keycode::Up => self.command_box.hist_prev(), Keycode::Down => self.command_box.hist_next(), - Keycode::Return => { - self.eval_command(); - } + _ if keymod == Mod::LCTRLMOD => match k { + Keycode::A => self.command_box.cursor_start(), + Keycode::E => self.command_box.cursor_end(), + Keycode::F => self.command_box.forward(), + Keycode::B => self.command_box.backward(), + Keycode::K => self.command_box.delete_to_end(), + Keycode::U => self.command_box.delete_to_start(), + _ => (), + }, + Keycode::Return => self.eval_command(), Keycode::Escape => { self.command_box.clear(); self.mode = Mode::Draw; diff --git a/src/command.rs b/src/command.rs index 064d767..d80f1f2 100644 --- a/src/command.rs +++ b/src/command.rs @@ -22,11 +22,11 @@ impl CommandBox { } } - fn cursor_end(&mut self) { + pub fn cursor_end(&mut self) { self.cursor = self.text.len(); } - fn cursor_start(&mut self) { + pub fn cursor_start(&mut self) { self.cursor = 0; } @@ -47,8 +47,17 @@ impl CommandBox { } } + pub fn delete_to_end(&mut self) { + self.text.truncate(self.cursor); + } + + pub fn delete_to_start(&mut self) { + self.text = self.text.chars().skip(self.cursor).collect(); + self.cursor = 0; + } + pub fn push_str(&mut self, v: &str) { - self.text.push_str(v); + self.text.insert_str(self.cursor, v); self.cursor += v.len(); } @@ -142,6 +151,36 @@ mod command_tests { assert_eq!(cmd.cursor, 16) } + #[test] + fn entering_text_between() { + let mut cmd = setup_with("save as file.png"); + cmd.backward(); + cmd.backward(); + cmd.backward(); + cmd.push_str("ext"); + assert_eq!(&cmd.text, "save as file.extpng"); + } + + #[test] + fn delete_to_end() { + let mut cmd = setup_with("save as file.png"); + cmd.backward(); + cmd.backward(); + cmd.backward(); + cmd.delete_to_end(); + assert_eq!(&cmd.text, "save as file."); + } + + #[test] + fn delete_to_start() { + let mut cmd = setup_with("save as file.png"); + cmd.backward(); + cmd.backward(); + cmd.backward(); + cmd.delete_to_start(); + assert_eq!(&cmd.text, "png"); + } + #[test] fn backspacing_from_end() { let mut cmd = setup_with("save"); -- cgit v1.2.3