diff --git a/src/editor.rs b/src/editor.rs index 059bb60..7502f2f 100644 --- a/src/editor.rs +++ b/src/editor.rs @@ -109,6 +109,7 @@ impl Editor { KeyCode::Char('c') => self.copy(config), KeyCode::Char('x') => self.cut(config), KeyCode::Char('v') => self.paste(config), + KeyCode::Char('g') => self.go_to_line(), KeyCode::Char('l') => config.line_numbers = !config.line_numbers, _ => (), }, @@ -440,4 +441,18 @@ impl Editor { } } } + + fn go_to_line(&mut self) { + let max = self.lines.len(); + let prompt = format!("Go to line (1-{max}): "); + if let Some(target) = read_line(&prompt).and_then(|t| t.parse::().ok()) { + if (1..=max).contains(&target) { + self.cursor.line = target - 1; + self.cursor.column = 0; + self.scroll_to_cursor(); + } else { + self.set_message(format!("Line {target} not in range 1-{max}")); + } + } + } } diff --git a/src/main.rs b/src/main.rs index 7bbcc68..6e0ca1f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -105,7 +105,8 @@ impl Navigator { queue!(stdout(), MoveTo(0, offset)).unwrap(); print!("Current dir: {}", self.path.to_string_lossy()); - let max_rows = terminal::size().unwrap().1 as usize - self.editors.len() - 4; + let height = terminal::size().unwrap().1; + let max_rows = height as usize - self.editors.len() - 4; let end = (self.scroll + max_rows).min(self.files.len()); let visible_rows = self.scroll..end; @@ -117,7 +118,7 @@ impl Navigator { if let Some(name) = path.file_name() { print!("{}", name.to_string_lossy()); } else { - print!("{}", path.to_string_lossy()); + print!(".."); } if path.is_dir() { print!("/"); @@ -126,7 +127,7 @@ impl Navigator { } if let Some(text) = &self.message { - queue!(stdout(), MoveTo(0, terminal::size().unwrap().1)).unwrap(); + queue!(stdout(), MoveTo(0, height)).unwrap(); print!("{text}"); }