~garritfra/rim

5f5c9fd92562e71fe770b8724cded9abd36174d8 — Garrit Franke 3 years ago 03f27f6
Rename Term struct to Editor
1 files changed, 18 insertions(+), 18 deletions(-)

M src/main.rs
M src/main.rs => src/main.rs +18 -18
@@ 9,7 9,7 @@ use termion::input::TermRead;
use termion::raw::IntoRawMode;
use termion::screen::*;

struct Term {
struct Editor {
    screen: Box<dyn Write>,
    mode: Mode,
}


@@ 20,7 20,7 @@ enum Mode {
    Insert,
}

impl Term {
impl Editor {
    pub fn new() -> Self {
        let mut screen = AlternateScreen::from(stdout().into_raw_mode().unwrap());



@@ 82,49 82,49 @@ impl Term {

fn main() {
    let stdin = stdin();
    let mut term = Term::new();
    term.enter_mode(Mode::Normal);
    let mut editor = Editor::new();
    editor.enter_mode(Mode::Normal);
    for c in stdin.keys() {
        let key = c.unwrap();
        log(format!("New key event: {:?}", &key));
        match key {
            Key::Left => term.write(&termion::cursor::Left(1)),
            Key::Right => term.write(&termion::cursor::Right(1)),
            Key::Up => term.write(&termion::cursor::Up(1)),
            Key::Down => term.write(&termion::cursor::Down(1)),
            Key::Esc => term.enter_mode(Mode::Normal),
            Key::Left => editor.write(&termion::cursor::Left(1)),
            Key::Right => editor.write(&termion::cursor::Right(1)),
            Key::Up => editor.write(&termion::cursor::Up(1)),
            Key::Down => editor.write(&termion::cursor::Down(1)),
            Key::Esc => editor.enter_mode(Mode::Normal),
            _ => {}
        };

        match term.mode {
        match editor.mode {
            Mode::Normal => {
                if bindings_normal(&mut term, &key) == false {
                if bindings_normal(&mut editor, &key) == false {
                    log("Should quit");
                    break;
                }
            }
            Mode::Insert => bindings_insert(&mut term, &key),
            Mode::Insert => bindings_insert(&mut editor, &key),
        };
        term.flush();
        editor.flush();
    }
    term.write(&termion::cursor::Show);
    term.enter_mode(Mode::Normal);
    editor.write(&termion::cursor::Show);
    editor.enter_mode(Mode::Normal);
}

/// Returns true if the program should continue running.
/// If false is returned, the Program should terminate
fn bindings_normal(term: &mut Term, k: &Key) -> bool {
fn bindings_normal(editor: &mut Editor, k: &Key) -> bool {
    log(format!("Normal Mode binding: {:?}", k));
    match k {
        Key::Char('q') => return false,
        Key::Char('i') => term.enter_mode(Mode::Insert),
        Key::Char('i') => editor.enter_mode(Mode::Insert),
        _ => {}
    };

    true
}

fn bindings_insert(term: &mut Term, k: &Key) {
fn bindings_insert(term: &mut Editor, k: &Key) {
    log(format!("Insert Mode binding: {:?}", k));
    match k {
        Key::Char('\n') => term.write_newline(),