aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAkshay <[email protected]>2021-03-18 15:34:00 +0000
committerAkshay <[email protected]>2021-03-18 15:34:00 +0000
commit68bd6c2dfb0ed593c5e6deebe1d9753db90f03e1 (patch)
treeca3f173addd185172acb2c915501c812dedc1384
parentdf95b239e78121ddf4314d47e1c20dad626752fb (diff)
render cursor to command box, add readline keybinds
-rw-r--r--src/app.rs33
-rw-r--r--src/command.rs45
2 files changed, 70 insertions, 8 deletions
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 @@
1use crate::{ 1use crate::{
2 bitmap::{MapPoint, Pixmap}, 2 bitmap::{MapPoint, Pixmap},
3 command::CommandBox, 3 command::CommandBox,
4 consts::colors::*, 4 consts::{colors::*, FONT_PATH},
5 dither, rect, 5 dither, rect,
6 symmetry::Symmetry, 6 symmetry::Symmetry,
7 undo::{ModifyRecord, OpKind, Operation, UndoStack}, 7 undo::{ModifyRecord, OpKind, Operation, UndoStack},
@@ -352,6 +352,20 @@ impl<'ctx> AppState<'ctx> {
352 BLACK, 352 BLACK,
353 (0, winsize_y - cmd_height), 353 (0, winsize_y - cmd_height),
354 ); 354 );
355
356 self.canvas.set_draw_color(PINK);
357 let mut font = self.ttf_context.load_font(FONT_PATH, 17).unwrap();
358 font.set_style(sdl2::ttf::FontStyle::NORMAL);
359 font.set_hinting(sdl2::ttf::Hinting::Mono);
360 let prev_text = &self.command_box.text[..self.command_box.cursor];
361 let prev_text_dim = font.size_of_latin1(prev_text.as_bytes()).unwrap();
362 let cursor = rect!(
363 prev_text_dim.0,
364 winsize_y - cmd_height + 2,
365 2,
366 cmd_height - 4
367 );
368 self.canvas.fill_rect(cursor).unwrap();
355 } 369 }
356 370
357 fn draw_mouse(&mut self) { 371 fn draw_mouse(&mut self) {
@@ -641,7 +655,9 @@ impl<'ctx> AppState<'ctx> {
641 } 655 }
642 } 656 }
643 if let Event::KeyDown { 657 if let Event::KeyDown {
644 keycode: Some(k), .. 658 keycode: Some(k),
659 keymod,
660 ..
645 } = event 661 } = event
646 { 662 {
647 match k { 663 match k {
@@ -651,9 +667,16 @@ impl<'ctx> AppState<'ctx> {
651 Keycode::Right => self.command_box.forward(), 667 Keycode::Right => self.command_box.forward(),
652 Keycode::Up => self.command_box.hist_prev(), 668 Keycode::Up => self.command_box.hist_prev(),
653 Keycode::Down => self.command_box.hist_next(), 669 Keycode::Down => self.command_box.hist_next(),
654 Keycode::Return => { 670 _ if keymod == Mod::LCTRLMOD => match k {
655 self.eval_command(); 671 Keycode::A => self.command_box.cursor_start(),
656 } 672 Keycode::E => self.command_box.cursor_end(),
673 Keycode::F => self.command_box.forward(),
674 Keycode::B => self.command_box.backward(),
675 Keycode::K => self.command_box.delete_to_end(),
676 Keycode::U => self.command_box.delete_to_start(),
677 _ => (),
678 },
679 Keycode::Return => self.eval_command(),
657 Keycode::Escape => { 680 Keycode::Escape => {
658 self.command_box.clear(); 681 self.command_box.clear();
659 self.mode = Mode::Draw; 682 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 {
22 } 22 }
23 } 23 }
24 24
25 fn cursor_end(&mut self) { 25 pub fn cursor_end(&mut self) {
26 self.cursor = self.text.len(); 26 self.cursor = self.text.len();
27 } 27 }
28 28
29 fn cursor_start(&mut self) { 29 pub fn cursor_start(&mut self) {
30 self.cursor = 0; 30 self.cursor = 0;
31 } 31 }
32 32
@@ -47,8 +47,17 @@ impl CommandBox {
47 } 47 }
48 } 48 }
49 49
50 pub fn delete_to_end(&mut self) {
51 self.text.truncate(self.cursor);
52 }
53
54 pub fn delete_to_start(&mut self) {
55 self.text = self.text.chars().skip(self.cursor).collect();
56 self.cursor = 0;
57 }
58
50 pub fn push_str(&mut self, v: &str) { 59 pub fn push_str(&mut self, v: &str) {
51 self.text.push_str(v); 60 self.text.insert_str(self.cursor, v);
52 self.cursor += v.len(); 61 self.cursor += v.len();
53 } 62 }
54 63
@@ -143,6 +152,36 @@ mod command_tests {
143 } 152 }
144 153
145 #[test] 154 #[test]
155 fn entering_text_between() {
156 let mut cmd = setup_with("save as file.png");
157 cmd.backward();
158 cmd.backward();
159 cmd.backward();
160 cmd.push_str("ext");
161 assert_eq!(&cmd.text, "save as file.extpng");
162 }
163
164 #[test]
165 fn delete_to_end() {
166 let mut cmd = setup_with("save as file.png");
167 cmd.backward();
168 cmd.backward();
169 cmd.backward();
170 cmd.delete_to_end();
171 assert_eq!(&cmd.text, "save as file.");
172 }
173
174 #[test]
175 fn delete_to_start() {
176 let mut cmd = setup_with("save as file.png");
177 cmd.backward();
178 cmd.backward();
179 cmd.backward();
180 cmd.delete_to_start();
181 assert_eq!(&cmd.text, "png");
182 }
183
184 #[test]
146 fn backspacing_from_end() { 185 fn backspacing_from_end() {
147 let mut cmd = setup_with("save"); 186 let mut cmd = setup_with("save");
148 cmd.backspace(); 187 cmd.backspace();