improve selection controls
This commit is contained in:
parent
f5da786053
commit
2d49302d0a
1 changed files with 54 additions and 29 deletions
|
@ -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,34 +89,62 @@ 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)) => {
|
||||||
KeyModifiers::NONE => match event.code {
|
if self.input_movement(&event) {
|
||||||
KeyCode::Esc => self.active = false,
|
return;
|
||||||
KeyCode::Char(ch) => self.insert_char(ch),
|
}
|
||||||
KeyCode::Backspace => self.backspace(),
|
match event.modifiers {
|
||||||
KeyCode::Delete => self.delete(),
|
KeyModifiers::NONE => match event.code {
|
||||||
KeyCode::Left => self.move_left(),
|
KeyCode::Esc => self.active = false,
|
||||||
KeyCode::Right => self.move_right(),
|
KeyCode::Char(ch) => self.insert_char(ch),
|
||||||
KeyCode::Up => self.move_up(),
|
KeyCode::Enter => self.insert_char('\n'),
|
||||||
KeyCode::Down => self.move_down(),
|
KeyCode::Backspace => self.backspace(),
|
||||||
KeyCode::Home => self.move_home(),
|
KeyCode::Delete => self.delete(),
|
||||||
KeyCode::End => self.move_end(),
|
_ => (),
|
||||||
|
},
|
||||||
|
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(),
|
||||||
|
_ => (),
|
||||||
|
},
|
||||||
_ => (),
|
_ => (),
|
||||||
},
|
}
|
||||||
KeyModifiers::CONTROL => match event.code {
|
}
|
||||||
KeyCode::Char('s') => self.save(),
|
|
||||||
KeyCode::Char('p') => self.toggle_marker(),
|
|
||||||
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::Right => self.move_right(),
|
||||||
|
KeyCode::Up => self.move_up(),
|
||||||
|
KeyCode::Down => self.move_down(),
|
||||||
|
KeyCode::Home => self.move_home(),
|
||||||
|
KeyCode::End => self.move_end(),
|
||||||
|
_ => (),
|
||||||
|
}
|
||||||
|
true
|
||||||
|
} else {
|
||||||
|
false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn draw(&self) {
|
fn draw(&self) {
|
||||||
queue!(stdout(), Clear(ClearType::All)).unwrap();
|
queue!(stdout(), Clear(ClearType::All)).unwrap();
|
||||||
|
|
||||||
|
@ -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();
|
||||||
|
|
Loading…
Reference in a new issue