improve selection controls

This commit is contained in:
Crispy 2023-03-04 12:43:54 +01:00
parent f5da786053
commit 2d49302d0a

View file

@ -1,6 +1,6 @@
use crossterm::{ use crossterm::{
cursor::{self, MoveTo}, cursor::{self, MoveTo},
event::{self, Event, KeyCode, KeyModifiers}, event::{self, Event, KeyCode, KeyEvent, KeyModifiers},
queue, queue,
style::{Color, Colors, ResetColor, SetColors}, style::{Color, Colors, ResetColor, SetColors},
terminal::{self, Clear, ClearType}, terminal::{self, Clear, ClearType},
@ -89,12 +89,48 @@ impl Editor {
fn input(&mut self) { fn input(&mut self) {
match event::read() { match event::read() {
Ok(Event::Key(event)) => match event.modifiers { Ok(Event::Key(event)) => {
if self.input_movement(&event) {
return;
}
match event.modifiers {
KeyModifiers::NONE => match event.code { KeyModifiers::NONE => match event.code {
KeyCode::Esc => self.active = false, KeyCode::Esc => self.active = false,
KeyCode::Char(ch) => self.insert_char(ch), KeyCode::Char(ch) => self.insert_char(ch),
KeyCode::Enter => self.insert_char('\n'),
KeyCode::Backspace => self.backspace(), KeyCode::Backspace => self.backspace(),
KeyCode::Delete => self.delete(), KeyCode::Delete => self.delete(),
_ => (),
},
KeyModifiers::CONTROL => match event.code {
KeyCode::Char('s') => self.save(),
KeyCode::Char('c') => self.copy(),
KeyCode::Char('x') => self.cut(),
KeyCode::Char('v') => self.paste(),
_ => (),
},
_ => (),
}
}
_ => (),
}
}
/// Cursor movement logic, returns true if cursor moved (so consider the event consumed in that case)
fn input_movement(&mut self, event: &KeyEvent) -> bool {
if let KeyCode::Left
| KeyCode::Right
| KeyCode::Up
| KeyCode::Down
| KeyCode::Home
| KeyCode::End = event.code
{
if event.modifiers.contains(KeyModifiers::SHIFT) {
self.set_marker();
} else {
self.marker = None;
}
match event.code {
KeyCode::Left => self.move_left(), KeyCode::Left => self.move_left(),
KeyCode::Right => self.move_right(), KeyCode::Right => self.move_right(),
KeyCode::Up => self.move_up(), KeyCode::Up => self.move_up(),
@ -102,18 +138,10 @@ impl Editor {
KeyCode::Home => self.move_home(), KeyCode::Home => self.move_home(),
KeyCode::End => self.move_end(), KeyCode::End => self.move_end(),
_ => (), _ => (),
}, }
KeyModifiers::CONTROL => match event.code { true
KeyCode::Char('s') => self.save(), } else {
KeyCode::Char('p') => self.toggle_marker(), false
KeyCode::Char('c') => self.copy(),
KeyCode::Char('x') => self.cut(),
KeyCode::Char('v') => self.paste(),
_ => (),
},
_ => (),
},
_ => (),
} }
} }
@ -244,10 +272,8 @@ impl Editor {
} }
} }
fn toggle_marker(&mut self) { fn set_marker(&mut self) {
if self.marker.is_some() { if self.marker.is_none() {
self.marker = None;
} else {
self.marker = Some(self.char_index()); self.marker = Some(self.char_index());
} }
} }
@ -282,7 +308,6 @@ impl Editor {
fn insert_char(&mut self, ch: char) { fn insert_char(&mut self, ch: char) {
self.unsaved_changes = true; self.unsaved_changes = true;
// eprintln!("inserting {ch} at {}", self.index());
self.text.insert(self.char_index(), ch); self.text.insert(self.char_index(), ch);
self.find_lines(); self.find_lines();
self.move_right(); self.move_right();