fixed append
This commit is contained in:
parent
e06d61317f
commit
1be955dc11
38
src/main.rs
38
src/main.rs
|
@ -113,14 +113,18 @@ struct Cursor {
|
|||
pub line: usize,
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, Debug)]
|
||||
struct InsertState {
|
||||
append: bool,
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, Debug, Default)]
|
||||
enum Mode {
|
||||
#[default]
|
||||
Normal,
|
||||
Command,
|
||||
Visual,
|
||||
Append,
|
||||
Insert,
|
||||
Insert(InsertState),
|
||||
}
|
||||
|
||||
impl Mode {
|
||||
|
@ -129,8 +133,7 @@ impl Mode {
|
|||
match self {
|
||||
Mode::Normal => Style::SteadyBlock,
|
||||
Mode::Visual => Style::BlinkingBlock,
|
||||
Mode::Append => Style::BlinkingBar,
|
||||
Mode::Insert => Style::BlinkingBar,
|
||||
Mode::Insert(_) => Style::BlinkingBar,
|
||||
Mode::Command => Style::SteadyUnderScore,
|
||||
}
|
||||
}
|
||||
|
@ -179,8 +182,7 @@ impl State {
|
|||
Mode::Normal => self.on_normal_event(event),
|
||||
Mode::Command => self.on_command_event(event),
|
||||
Mode::Visual => self.on_visual_event(event),
|
||||
Mode::Append => self.on_append_event(event),
|
||||
Mode::Insert => self.on_insert_event(event),
|
||||
Mode::Insert(state) => self.on_insert_event(event, state),
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -188,11 +190,13 @@ impl State {
|
|||
match event {
|
||||
Event::Key(KeyEvent { code, .. }) => match code {
|
||||
KeyCode::Char('i') => {
|
||||
self.mode = Mode::Insert;
|
||||
let state = InsertState { append: false };
|
||||
self.mode = Mode::Insert(state);
|
||||
}
|
||||
KeyCode::Char('a') => {
|
||||
let state = InsertState { append: true, };
|
||||
self.move_cursor(Direction::Right);
|
||||
self.mode = Mode::Append;
|
||||
self.mode = Mode::Insert(state);
|
||||
}
|
||||
KeyCode::Char(':') => {
|
||||
self.mode = Mode::Command;
|
||||
|
@ -233,20 +237,7 @@ impl State {
|
|||
}
|
||||
}
|
||||
|
||||
fn on_append_event(&mut self, event: Event) {
|
||||
match event {
|
||||
Event::Key(KeyEvent { code, .. }) => match code {
|
||||
KeyCode::Esc => {
|
||||
self.move_cursor(Direction::Left);
|
||||
self.mode = Mode::Normal;
|
||||
}
|
||||
_ => self.on_insert_event(event),
|
||||
},
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
fn on_insert_event(&mut self, event: Event) {
|
||||
fn on_insert_event(&mut self, event: Event, state: InsertState) {
|
||||
match event {
|
||||
Event::Key(KeyEvent { code, .. }) => match code {
|
||||
KeyCode::Char(c) => {
|
||||
|
@ -270,6 +261,9 @@ impl State {
|
|||
self.cursor.column = 0;
|
||||
}
|
||||
KeyCode::Esc => {
|
||||
if state.append {
|
||||
self.move_cursor(Direction::Left);
|
||||
}
|
||||
self.mode = Mode::Normal;
|
||||
}
|
||||
code => self.match_any_key(code),
|
||||
|
|
Loading…
Reference in New Issue