Move logic to editor module.
This commit is contained in:
parent
a7fe16b007
commit
221512f80c
48
src/editor.rs
Normal file
48
src/editor.rs
Normal file
@ -0,0 +1,48 @@
|
||||
use std::io::{self, stdout};
|
||||
use termion::event::Key;
|
||||
use termion::input::TermRead;
|
||||
use termion::raw::IntoRawMode;
|
||||
|
||||
pub struct Editor {
|
||||
should_quit: bool,
|
||||
}
|
||||
|
||||
impl Editor {
|
||||
pub fn run(&mut self) {
|
||||
let _stdout = stdout().into_raw_mode().unwrap();
|
||||
|
||||
loop {
|
||||
if let Err(error) = self.process_keypress() {
|
||||
die(error);
|
||||
}
|
||||
if self.should_quit {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn default() -> Self {
|
||||
Self { should_quit: false }
|
||||
}
|
||||
|
||||
fn process_keypress(&mut self) -> Result<(), std::io::Error> {
|
||||
let pressed_key = read_key()?;
|
||||
match pressed_key {
|
||||
Key::Ctrl('q') => self.should_quit = true,
|
||||
_ => (),
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
fn read_key() -> Result<Key, std::io::Error> {
|
||||
loop {
|
||||
if let Some(key) = io::stdin().lock().keys().next() {
|
||||
return key;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn die(e: std::io::Error) {
|
||||
panic!(e);
|
||||
}
|
29
src/main.rs
29
src/main.rs
@ -1,29 +1,8 @@
|
||||
use std::io::{self, stdout};
|
||||
use termion::event::Key;
|
||||
use termion::input::TermRead;
|
||||
use termion::raw::IntoRawMode;
|
||||
#![warn(clippy::all, clippy::pedantic)]
|
||||
mod editor;
|
||||
|
||||
fn die(e: std::io::Error) {
|
||||
panic!(e);
|
||||
}
|
||||
use editor::Editor;
|
||||
|
||||
fn main() {
|
||||
let _stdout = stdout().into_raw_mode().unwrap();
|
||||
|
||||
for key in io::stdin().keys() {
|
||||
match key {
|
||||
Ok(key) => match key {
|
||||
Key::Char(c) => {
|
||||
if c.is_control() {
|
||||
println!("{:?}\r", c as u8);
|
||||
} else {
|
||||
println!("{:?} ({})\r", c as u8, c);
|
||||
}
|
||||
}
|
||||
Key::Ctrl('q') => break,
|
||||
_ => println!("{:?}\r", key),
|
||||
}
|
||||
Err(err) => die(err),
|
||||
}
|
||||
}
|
||||
Editor::default().run();
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user