diff --git a/src/editor.rs b/src/editor.rs index dba9831..205a6af 100644 --- a/src/editor.rs +++ b/src/editor.rs @@ -108,11 +108,12 @@ impl Editor { } self.status_line(); print!( - "{}", + "{}{}", cursor::Goto( self.physical_column() as u16 + 1, (self.cursor.line - self.scroll) as u16 + 1 - ) + ), + cursor::Show ); stdout().flush().unwrap(); } diff --git a/src/main.rs b/src/main.rs index c93e012..2df81d1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,7 +5,7 @@ use std::{ }; use termion::{ clear, color, - cursor::Goto, + cursor::{self, Goto}, event::{Event, Key}, input::TermRead, raw::{IntoRawMode, RawTerminal}, @@ -50,8 +50,9 @@ impl Navigator { fn draw(&self) { print!( - "{}{}Open editors: {}", + "{}{}{}Open editors: {}", clear::All, + cursor::Hide, Goto(1, 1), self.editors.len() ); @@ -73,20 +74,43 @@ impl Navigator { match key { Key::Esc => self.quit(), Key::Char('\n') => self.open_selected(), + Key::Ctrl('n') => self.new_editor(), + Key::Up => self.nav_up(), + Key::Down => self.nav_down(), _ => (), } } } } + fn nav_up(&mut self) { + if self.selected > Some(0) { + self.selected = Some(self.selected.unwrap() - 1); + } + } + + fn nav_down(&mut self) { + if let Some(index) = self.selected.as_mut() { + if *index < self.editors.len() - 1 { + *index += 1; + } + } + } + fn open_selected(&mut self) { if let Some(index) = self.selected { self.editors[index].open(); } } + fn new_editor(&mut self) { + self.selected = Some(self.editors.len()); + self.editors.push(Editor::new(None)); + self.open_selected(); + } + fn quit(&self) { - print!("{}", clear::All); + print!("{}{}", clear::All, cursor::Show); exit(0); } }