Fixed bug with gutter size not updating as line numbers get wider.

This commit is contained in:
Nathan Vegdahl 2018-07-20 01:31:56 -07:00
parent db5f6c7e64
commit 75742c7aa4
2 changed files with 24 additions and 22 deletions

View File

@ -248,22 +248,26 @@ impl<T: LineFormatter> Editor<T> {
} }
} }
pub fn update_dim(&mut self, h: usize, w: usize) { /// Updates the view dimensions, and returns whether that
self.editor_dim = (h, w); /// actually changed anything.
self.update_view_dim(); pub fn update_dim(&mut self, h: usize, w: usize) -> bool {
}
pub fn update_view_dim(&mut self) {
// TODO: generalize for non-terminal UI. Maybe this isn't where it
// belongs, in fact. But for now, this is the easiest place to put
// it.
let line_count_digits = digit_count(self.buffer.line_count() as u32, 10) as usize; let line_count_digits = digit_count(self.buffer.line_count() as u32, 10) as usize;
if self.editor_dim.0 != h || self.editor_dim.1 != w {
self.editor_dim = (h, w);
// Minus 1 vertically for the header, minus one more than the digits in // Minus 1 vertically for the header, minus one more than the digits in
// the line count for the gutter. // the line count for the gutter.
self.view_dim = ( self.view_dim = (
self.editor_dim.0 - 1, self.editor_dim.0 - 1,
self.editor_dim.1 - line_count_digits - 1, self.editor_dim.1 - line_count_digits - 1,
); );
return true;
} else if self.view_dim.1 != (self.editor_dim.1 - line_count_digits - 1) {
self.view_dim.1 = self.editor_dim.1 - line_count_digits - 1;
return true;
} else {
return false;
}
} }
pub fn undo(&mut self) { pub fn undo(&mut self) {

View File

@ -59,18 +59,17 @@ macro_rules! ui_loop {
// Check for screen resize // Check for screen resize
let (w, h) = termion::terminal_size().unwrap(); let (w, h) = termion::terminal_size().unwrap();
if $term_ui.width != w as usize || $term_ui.height != h as usize { let needs_update = $term_ui
.editor
.update_dim(h as usize - 1, w as usize);
if needs_update {
$term_ui.width = w as usize; $term_ui.width = w as usize;
$term_ui.height = h as usize; $term_ui.height = h as usize;
$term_ui $term_ui.screen.resize(w as usize, h as usize);
.editor
.update_dim($term_ui.height - 1, $term_ui.width);
$term_ui.editor.update_view_dim();
$term_ui $term_ui
.editor .editor
.formatter .formatter
.set_wrap_width($term_ui.editor.view_dim.1); .set_wrap_width($term_ui.editor.view_dim.1);
$term_ui.screen.resize(w as usize, h as usize);
should_redraw = true; should_redraw = true;
} }
@ -132,7 +131,6 @@ impl TermUI {
self.width = w as usize; self.width = w as usize;
self.height = h as usize; self.height = h as usize;
self.editor.update_dim(self.height - 1, self.width); self.editor.update_dim(self.height - 1, self.width);
self.editor.update_view_dim();
self.editor.formatter.set_wrap_width(self.editor.view_dim.1); self.editor.formatter.set_wrap_width(self.editor.view_dim.1);
self.screen.resize(w as usize, h as usize); self.screen.resize(w as usize, h as usize);