Compare commits

...

7 Commits

1 changed files with 24 additions and 8 deletions

View File

@ -20,8 +20,10 @@ use std::io::{stdout, Stdout, Write};
use crossterm::{
cursor,
event::{Event, KeyCode, KeyEvent},
terminal, ExecutableCommand, Result,
event::{ Event, KeyCode, KeyEvent, read },
terminal,
ExecutableCommand,
Result
};
use ropey::Rope;
@ -124,13 +126,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,
Insert,
Insert(InsertState),
}
impl Mode {
@ -139,7 +146,7 @@ impl Mode {
match self {
Mode::Normal => Style::SteadyBlock,
Mode::Visual => Style::BlinkingBlock,
Mode::Insert => Style::BlinkingBar,
Mode::Insert(_) => Style::BlinkingBar,
Mode::Command => Style::SteadyUnderScore,
}
}
@ -195,7 +202,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::Insert => self.on_insert_event(event),
Mode::Insert(state) => self.on_insert_event(event, state),
}
}
@ -203,7 +210,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::Insert(state);
}
KeyCode::Char(':') => {
self.mode = Mode::Command;
@ -244,7 +257,7 @@ impl State {
}
}
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) => {
@ -268,6 +281,9 @@ impl State {
self.cursor.column = 0;
}
KeyCode::Esc => {
if state.append {
self.move_cursor(Direction::Left);
}
self.mode = Mode::Normal;
}
code => self.on_any_key(code),
@ -317,7 +333,7 @@ impl State {
fn screen_main(stdout: &mut Stdout, mut state: State) -> Result<()> {
while !state.quit {
state.draw(stdout)?;
let event = crossterm::event::read()?;
let event = read()?;
state.on_event(event);
}