add ctrl+g to go to line

This commit is contained in:
Crispy 2023-03-15 23:11:53 +01:00
parent b37026bae8
commit b9468ec7da
2 changed files with 19 additions and 3 deletions

View file

@ -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::<usize>().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}"));
}
}
}
}

View file

@ -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}");
}