Archived
1
0
forked from mars/breed

Merge pull request 'Add a entering Insert mode with appending cursor instead' (#2) from emma/breed:append into main

Reviewed-on: mars/breed#2
This commit is contained in:
mars 2023-04-11 21:48:29 +00:00
commit 7821969324

View File

@ -20,8 +20,10 @@ use std::io::{stdout, Stdout, Write};
use crossterm::{ use crossterm::{
cursor, cursor,
event::{Event, KeyCode, KeyEvent}, event::{ Event, KeyCode, KeyEvent, read },
terminal, ExecutableCommand, Result, terminal,
ExecutableCommand,
Result
}; };
use ropey::Rope; use ropey::Rope;
@ -124,13 +126,18 @@ struct Cursor {
pub line: usize, pub line: usize,
} }
#[derive(Copy, Clone, Debug)]
struct InsertState {
append: bool,
}
#[derive(Copy, Clone, Debug, Default)] #[derive(Copy, Clone, Debug, Default)]
enum Mode { enum Mode {
#[default] #[default]
Normal, Normal,
Command, Command,
Visual, Visual,
Insert, Insert(InsertState),
} }
impl Mode { impl Mode {
@ -139,7 +146,7 @@ impl Mode {
match self { match self {
Mode::Normal => Style::SteadyBlock, Mode::Normal => Style::SteadyBlock,
Mode::Visual => Style::BlinkingBlock, Mode::Visual => Style::BlinkingBlock,
Mode::Insert => Style::BlinkingBar, Mode::Insert(_) => Style::BlinkingBar,
Mode::Command => Style::SteadyUnderScore, Mode::Command => Style::SteadyUnderScore,
} }
} }
@ -195,7 +202,7 @@ impl State {
Mode::Normal => self.on_normal_event(event), Mode::Normal => self.on_normal_event(event),
Mode::Command => self.on_command_event(event), Mode::Command => self.on_command_event(event),
Mode::Visual => self.on_visual_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 { match event {
Event::Key(KeyEvent { code, .. }) => match code { Event::Key(KeyEvent { code, .. }) => match code {
KeyCode::Char('i') => { 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(':') => { KeyCode::Char(':') => {
self.mode = Mode::Command; 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 { match event {
Event::Key(KeyEvent { code, .. }) => match code { Event::Key(KeyEvent { code, .. }) => match code {
KeyCode::Char(c) => { KeyCode::Char(c) => {
@ -268,6 +281,9 @@ impl State {
self.cursor.column = 0; self.cursor.column = 0;
} }
KeyCode::Esc => { KeyCode::Esc => {
if state.append {
self.move_cursor(Direction::Left);
}
self.mode = Mode::Normal; self.mode = Mode::Normal;
} }
code => self.on_any_key(code), code => self.on_any_key(code),
@ -317,7 +333,7 @@ impl State {
fn screen_main(stdout: &mut Stdout, mut state: State) -> Result<()> { fn screen_main(stdout: &mut Stdout, mut state: State) -> Result<()> {
while !state.quit { while !state.quit {
state.draw(stdout)?; state.draw(stdout)?;
let event = crossterm::event::read()?; let event = read()?;
state.on_event(event); state.on_event(event);
} }