From 3f5b917c6ced370d940774b51ff89cec0d03c562 Mon Sep 17 00:00:00 2001 From: Akshay Date: Tue, 6 Apr 2021 09:03:37 +0530 Subject: draw grid and pixmap boundary separately --- src/app.rs | 52 ++++++++++++++++++++++++++++++++-------------------- src/command.rs | 26 ++++++++++++++++++++++++++ src/lisp/lex.rs | 4 ++-- src/lisp/std.lisp | 2 +- 4 files changed, 61 insertions(+), 23 deletions(-) (limited to 'src') diff --git a/src/app.rs b/src/app.rs index 56fcac7..33d4934 100644 --- a/src/app.rs +++ b/src/app.rs @@ -336,14 +336,14 @@ impl<'ctx> AppState<'ctx> { let (width, height) = (self.width(), self.height()); let canvas = &mut self.canvas; canvas.set_draw_color(self.grid.color); - for i in 0..=width { + for i in 1..width { let x = (i * cs) as i32; let y = (height * cs) as i32; let start = self.start + Point::new(x, 0); let end = self.start + Point::new(x, y); canvas.draw_line(start, end).unwrap(); } - for j in 0..=height { + for j in 1..height { let x = (width * cs) as i32; let y = (j * cs) as i32; let start = self.start + Point::new(0, y); @@ -560,17 +560,26 @@ impl<'ctx> AppState<'ctx> { for (color, length) in utils::compress(scan) { if color { self.canvas - .fill_rect(Rect::new( + .fill_rect(rect!( (pass as u32 * cs) as i32 + start.x(), (line_nr as u32 * cs) as i32 + start.y(), cs * length as u32, - cs, + cs )) .unwrap(); } pass += length; } } + self.canvas.set_draw_color(GRID_COLOR); + self.canvas + .draw_rect(rect!( + start.x(), + start.y(), + self.width() * cs, + self.height() * cs + )) + .unwrap(); if grid_enabled { self.draw_grid(); } @@ -884,19 +893,24 @@ impl<'ctx> AppState<'ctx> { .push_str(&clipboard.clipboard_text().unwrap()); } } - if let Event::KeyDown { - keycode: Some(k), - keymod, - .. - } = event - { - match k { + match event { + Event::KeyDown { + keycode: Some(k), + keymod, + .. + } => match k { Keycode::Backspace => self.command_box.backspace(), Keycode::Delete => self.command_box.delete(), Keycode::Left => self.command_box.backward(), 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(), + Keycode::Escape => { + self.command_box.clear(); + self.message.text.clear(); + self.mode = Mode::Draw; + } _ if keymod == Mod::LCTRLMOD => match k { Keycode::A => self.command_box.cursor_start(), Keycode::E => self.command_box.cursor_end(), @@ -906,16 +920,14 @@ impl<'ctx> AppState<'ctx> { Keycode::U => self.command_box.delete_to_start(), _ => (), }, - Keycode::Return => self.eval_command(), - Keycode::Escape => { - self.command_box.clear(); - self.message.text = format!(" "); - self.mode = Mode::Draw; - } + // how does one handle alt keys + // _ if keymod == Mod::LALTMOD => match k { + // Keycode::B => self.command_box.cursor_back_word(), + // Keycode::F => self.command_box.cursor_forward_word(), + // _ => (), + // }, _ => (), - } - } - match event { + }, Event::TextInput { text, .. } => { self.command_box.push_str(&text[..]); } diff --git a/src/command.rs b/src/command.rs index f5a24c9..383389e 100644 --- a/src/command.rs +++ b/src/command.rs @@ -30,6 +30,32 @@ impl CommandBox { self.cursor = 0; } + pub fn cursor_back_word(&mut self) { + let mut prev_word_idx = 0; + { + let sl = &self.text[0..self.cursor]; + let idx = sl.rfind(|c: char| !c.is_alphanumeric() && c != '_'); + if let Some(i) = idx { + prev_word_idx = i; + } + } + self.cursor = prev_word_idx; + } + + pub fn cursor_forward_word(&mut self) { + let mut next_word_idx = self.cursor; + { + if self.cursor != self.text.len() { + let sl = &self.text[self.cursor..]; + let idx = sl.find(|c: char| !c.is_alphanumeric() && c != '_'); + if let Some(i) = idx { + next_word_idx = i; + } + } + } + self.cursor = next_word_idx; + } + pub fn backward(&mut self) { self.cursor = self.cursor.saturating_sub(1); } diff --git a/src/lisp/lex.rs b/src/lisp/lex.rs index 5d3030b..2088421 100644 --- a/src/lisp/lex.rs +++ b/src/lisp/lex.rs @@ -120,8 +120,8 @@ impl<'a> Lexer<'a> { while let Some((ind, chr)) = chars.next() { let low = self.cur_pos; let res = match chr { - '(' => Ok((1, Token::LeftParen)), - ')' => Ok((1, Token::RightParen)), + '(' | '[' => Ok((1, Token::LeftParen)), + ')' | ']' => Ok((1, Token::RightParen)), '\'' => Ok((1, Token::Quote)), '`' => Ok((1, Token::BackQuote)), ',' => match chars.next() { diff --git a/src/lisp/std.lisp b/src/lisp/std.lisp index 97c3984..2a042a0 100644 --- a/src/lisp/std.lisp +++ b/src/lisp/std.lisp @@ -47,7 +47,7 @@ (define (product ls) (fold 1 * ls)) (define (enumerate start stop step) - (if (> start stop) + (if (>= start stop) '() (cons start (enumerate (+ start step) stop step)))) -- cgit v1.2.3