From b704dafb2420df6f7fca64980a2f52c1a00bcef5 Mon Sep 17 00:00:00 2001 From: Joe Wilm Date: Thu, 29 Dec 2016 20:39:30 -0500 Subject: Fix some bugs with selections Moving the window on macOS would cause a panic in certain circumstances. --- src/term/mod.rs | 53 +++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 39 insertions(+), 14 deletions(-) (limited to 'src/term') diff --git a/src/term/mod.rs b/src/term/mod.rs index a166b23f..29bf6b83 100644 --- a/src/term/mod.rs +++ b/src/term/mod.rs @@ -260,7 +260,7 @@ impl SizeInfo { Column((self.width / self.cell_width) as usize) } - pub fn pixels_to_coords(&self, x: usize, y: usize) -> Option<(Line, Column)> { + pub fn pixels_to_coords(&self, x: usize, y: usize) -> Option { if x > self.width as usize || y > self.height as usize { return None; } @@ -268,7 +268,10 @@ impl SizeInfo { let col = Column(x / (self.cell_width as usize)); let line = Line(y / (self.cell_height as usize)); - Some((line, col)) + Some(Point { + line: cmp::min(line, self.lines() - 1), + col: cmp::min(col, self.cols() - 1) + }) } } @@ -328,45 +331,67 @@ impl Term { } } trait Append : PushChar { - fn append(&mut self, grid: &Grid, line: Line, cols: T) -> Range ; + fn append(&mut self, grid: &Grid, line: Line, cols: T) -> Option>; } use std::ops::{Range, RangeTo, RangeFrom, RangeFull}; impl Append> for String { - fn append(&mut self, grid: &Grid, line: Line, cols: Range) -> Range { + fn append( + &mut self, + grid: &Grid, + line: Line, + cols: Range + ) -> Option> { let line = &grid[line]; let line_length = line.line_length(); let line_end = cmp::min(line_length, cols.end + 1); - for cell in &line[cols.start..line_end] { - self.push(cell.c); - } - cols.start..line_end + if cols.start >= line_end { + None + } else { + for cell in &line[cols.start..line_end] { + self.push(cell.c); + } + + Some(cols.start..line_end) + } } } impl Append> for String { #[inline] - fn append(&mut self, grid: &Grid, line: Line, cols: RangeTo) -> Range { + fn append(&mut self, grid: &Grid, line: Line, cols: RangeTo) -> Option> { self.append(grid, line, Column(0)..cols.end) } } impl Append> for String { #[inline] - fn append(&mut self, grid: &Grid, line: Line, cols: RangeFrom) -> Range { + fn append( + &mut self, + grid: &Grid, + line: Line, + cols: RangeFrom + ) -> Option> { let range = self.append(grid, line, cols.start..Column(usize::max_value() - 1)); - self.maybe_newline(grid, line, range.end); + range.as_ref() + .map(|range| self.maybe_newline(grid, line, range.end)); range } } impl Append for String { #[inline] - fn append(&mut self, grid: &Grid, line: Line, _: RangeFull) -> Range { + fn append( + &mut self, + grid: &Grid, + line: Line, + _: RangeFull + ) -> Option> { let range = self.append(grid, line, Column(0)..Column(usize::max_value() - 1)); - self.maybe_newline(grid, line, range.end); + range.as_ref() + .map(|range| self.maybe_newline(grid, line, range.end)); range } } @@ -415,7 +440,7 @@ impl Term { /// line and column returned are also relative to the top left. /// /// Returns None if the coordinates are outside the screen - pub fn pixels_to_coords(&self, x: usize, y: usize) -> Option<(Line, Column)> { + pub fn pixels_to_coords(&self, x: usize, y: usize) -> Option { self.size_info().pixels_to_coords(x, y) } -- cgit