@@ -4,60 +4,81 @@ use crossterm::{
44 event::{self, read, Event, EventStream, KeyCode, KeyEvent},
55 execute, queue, style,
66 terminal::{self, disable_raw_mode, enable_raw_mode},
7− Result,
87};
98use futures::{future::FutureExt, select, StreamExt};
109use std::io::{self, stdout, Write};
1110use std::path::PathBuf;
1211use std::time::Duration;
1312
14−const HELP: &str = r#"
15− - Use q to quit
16− - Move cursor with h, j, k, l
17−"#;
13+use anyhow::Error;
14+
15+use crate::{keymap, Args};
16+use helix_core::{Buffer, State};
1817
1918pub struct Editor {
20− file: PathBuf,
19+ state: Option<State>,
2120}
2221
2322impl Editor {
24− pub async fn print_events() {
23+ pub fn new(mut args: Args) -> Result<Self, Error> {
24+ let mut editor = Editor { state: None };
25+
26+ if let Some(file) = args.files.pop() {
27+ editor.open(file)?;
28+ }
29+
30+ Ok(editor)
31+ }
32+
33+ pub fn open(&mut self, path: PathBuf) -> Result<(), Error> {
34+ let buffer = Buffer::load(path)?;
35+ let state = State::new(buffer);
36+ self.state = Some(state);
37+ Ok(())
38+ }
39+
40+ fn render(&self) {
41+ // TODO:
42+ }
43+
44+ pub async fn print_events(&mut self) {
2545 let mut reader = EventStream::new();
46+ let keymap = keymap::default();
47+
48+ self.render();
49+
2650 loop {
2751 // Handle key events
2852 let mut event = reader.next().await;
2953 match event {
30− Some(Ok(x)) => {
31− if let Event::Key(KeyEvent {
32− code: KeyCode::Char(c),
33− ..
34− }) = x
35− {
36− match c {
37− 'h' => execute!(io::stdout(), cursor::MoveLeft(1)).unwrap(),
38− 'j' => execute!(io::stdout(), cursor::MoveDown(1)).unwrap(),
39− 'k' => execute!(io::stdout(), cursor::MoveUp(1)).unwrap(),
40− 'l' => execute!(io::stdout(), cursor::MoveRight(1)).unwrap(),
41− 'q' => {
42− execute!(
43− io::stdout(),
44− style::ResetColor,
45− cursor::Show,
46− terminal::LeaveAlternateScreen
47− );
48− break;
49− }
50− _ => println!("{:?}", x),
54+ // TODO: handle resize events
55+ Some(Ok(Event::Key(KeyEvent {
56+ code: KeyCode::Char('q'),
57+ ..
58+ }))) => {
59+ break;
60+ }
61+ Some(Ok(Event::Key(event))) => {
62+ // TODO: handle modes and sequences (`gg`)
63+ if let Some(command) = keymap.get(&event) {
64+ if let Some(state) = &mut self.state {
65+ // TODO: handle count other than 1
66+ command(state, 1);
67+ self.render();
5168 }
5269 }
5370 }
71+ Some(Ok(_)) => {
72+ // unhandled event
73+ ()
74+ }
5475 Some(Err(x)) => panic!(x),
5576 None => break,
5677 }
5778 }
5879 }
5980
60− pub fn run() -> Result<()> {
81+ pub fn run(&mut self) -> Result<(), Error> {
6182 enable_raw_mode()?;
6283
6384 let mut stdout = stdout();
@@ -81,7 +102,7 @@ impl Editor {
81102 }
82103
83104 // No need to `run()`, now we can just block on the main future.
84− smol::block_on(Editor::print_events());
105+ smol::block_on(self.print_events());
85106
86107 // Send a shutdown signal.
87108 drop(s);
@@ -93,6 +114,8 @@ impl Editor {
93114 t.join().unwrap();
94115 }
95116
96− disable_raw_mode()
117+ disable_raw_mode()?;
118+
119+ Ok(())
97120 }
98121}