diff --git a/src/editor.rs b/src/editor.rs index b1ea346..a5b1866 100644 --- a/src/editor.rs +++ b/src/editor.rs @@ -279,10 +279,17 @@ impl Editor { } fn scroll_to_cursor(&mut self) { + // while self.cursor.line < self.scroll { + // self.scroll -= 1; + // } + // while self.cursor.line > (self.scroll + terminal::size().unwrap().1 as usize - 2) { + // self.scroll += 1; + // } + self.scroll = self.scroll.min(self.cursor.line); let height = terminal::size().unwrap().1 as usize - 2; self.scroll = self .scroll - .clamp(self.cursor.line.saturating_sub(height), self.cursor.line); + .max(self.scroll + self.cursor.line.saturating_sub(self.scroll + height)); } fn move_home(&mut self) { diff --git a/src/main.rs b/src/main.rs index 407e77d..1b2f593 100644 --- a/src/main.rs +++ b/src/main.rs @@ -167,44 +167,41 @@ impl Navigator { fn enter(&mut self) { if self.selected < self.editors.len() { self.editors[self.selected].enter(); - return; - } - - let i = self.selected - self.editors.len(); - // top entry is hardcoded to be ../ - if i == 0 { - if let Some(parent) = self.path.parent() { - self.set_path(self.path.join(parent)); - } - return; - } - - let path = &self.files[i]; - if path.is_dir() { - self.set_path(self.path.join(path)); - return; - } - if path.is_file() { - let path = path.canonicalize().unwrap(); - let mut selected = self.editors.len(); - for (i, editor) in self.editors.iter().enumerate() { - if editor.path() == Some(&path) { - selected = i; - break; + } else { + let i = self.selected - self.editors.len(); + if i == 0 { + if let Some(parent) = self.path.parent() { + self.set_path(self.path.join(parent)); } - } - // no editor exists with this path - if selected == self.editors.len() { - match Editor::open_file(self.clipboard.clone(), path) { - Ok(editor) => self.editors.push(editor), - Err(err) => { - self.message(format!("Could not open file: {err}")); - return; + } else { + let path = &self.files[i]; + if path.is_dir() { + self.set_path(self.path.join(path)); + } else if path.is_file() { + let path = path.canonicalize().unwrap(); + let mut editor_index = self.editors.len(); + for (i, editor) in self.editors.iter().enumerate() { + if editor.path() == Some(&path) { + editor_index = i; + break; + } } + if editor_index == self.editors.len() { + match Editor::open_file( + self.clipboard.clone(), + path.canonicalize().unwrap(), + ) { + Ok(editor) => self.editors.push(editor), + Err(err) => { + self.message(format!("Could not open file: {err}")); + return; + } + } + } + self.selected = editor_index; + self.open_selected(); } } - self.selected = selected; - self.open_selected(); } }