summaryrefslogtreecommitdiff
path: root/src/term
diff options
context:
space:
mode:
authorDustin <dustin1114@gmail.com>2017-12-23 00:17:04 -0500
committerJoe Wilm <jwilm@users.noreply.github.com>2017-12-22 21:17:04 -0800
commitea8cf10b2fa06f6573acec14f52816574bb0eb47 (patch)
treed0a7d0f9f4390ab124c6d130370526af7ea31e97 /src/term
parent7f7ec0682c3e186f28ee6c1e7502cf3151788d02 (diff)
downloadalacritty-ea8cf10b2fa06f6573acec14f52816574bb0eb47.tar.gz
alacritty-ea8cf10b2fa06f6573acec14f52816574bb0eb47.zip
Assure that newlines are in selections (#777)
When a selection was made, the last line of the selection did not include a new line character when the line ending was selected. This would occur if only one line was selected; if multiple lines were selected, only the final line did not include a newline. This commit updates the `string_from_selection` function to attempt to append a newline character in all places where they are suitable. Because of the simplification of newline insertion, several trait implementations were removed.
Diffstat (limited to 'src/term')
-rw-r--r--src/term/mod.rs75
1 files changed, 23 insertions, 52 deletions
diff --git a/src/term/mod.rs b/src/term/mod.rs
index 3484bae1..eb25efd6 100644
--- a/src/term/mod.rs
+++ b/src/term/mod.rs
@@ -866,71 +866,41 @@ impl Term {
self.push(c);
}
}
- trait Append<T> : PushChar {
- fn append(&mut self, grid: &Grid<Cell>, line: Line, cols: T) -> Option<Range<Column>>;
- }
- use std::ops::{Range, RangeTo, RangeFrom, RangeFull};
+ use std::ops::Range;
+
+ trait Append : PushChar {
+ fn append(&mut self, grid: &Grid<Cell>, line: Line, cols: Range<Column>) -> Option<Range<Column>>;
+ }
- impl Append<Range<Column>> for String {
+ impl Append for String {
fn append(
&mut self,
grid: &Grid<Cell>,
line: Line,
cols: Range<Column>
) -> Option<Range<Column>> {
- let line = &grid[line];
- let line_length = line.line_length();
+ let grid_line = &grid[line];
+ let line_length = grid_line.line_length();
let line_end = min(line_length, cols.end + 1);
if cols.start >= line_end {
None
} else {
- for cell in &line[cols.start..line_end] {
+ for cell in &grid_line[cols.start..line_end] {
if !cell.flags.contains(cell::WIDE_CHAR_SPACER) {
self.push(cell.c);
}
}
- Some(cols.start..line_end)
- }
- }
- }
-
- impl Append<RangeTo<Column>> for String {
- #[inline]
- fn append(&mut self, grid: &Grid<Cell>, line: Line, cols: RangeTo<Column>) -> Option<Range<Column>> {
- self.append(grid, line, Column(0)..cols.end)
- }
- }
-
- impl Append<RangeFrom<Column>> for String {
- #[inline]
- fn append(
- &mut self,
- grid: &Grid<Cell>,
- line: Line,
- cols: RangeFrom<Column>
- ) -> Option<Range<Column>> {
- let range = self.append(grid, line, cols.start..Column(usize::max_value() - 1));
- range.as_ref()
- .map(|range| self.maybe_newline(grid, line, range.end));
- range
- }
- }
+ let range = Some(cols.start..line_end);
+ if cols.end >= grid.num_cols() - 1 {
+ range.as_ref()
+ .map(|range| self.maybe_newline(grid, line, range.end));
+ }
- impl Append<RangeFull> for String {
- #[inline]
- fn append(
- &mut self,
- grid: &Grid<Cell>,
- line: Line,
- _: RangeFull
- ) -> Option<Range<Column>> {
- let range = self.append(grid, line, Column(0)..Column(usize::max_value() - 1));
- range.as_ref()
- .map(|range| self.maybe_newline(grid, line, range.end));
- range
+ range
+ }
}
}
@@ -938,6 +908,7 @@ impl Term {
let (start, end) = span.to_locations();
let line_count = end.line - start.line;
+ let max_col = Column(usize::max_value() - 1);
match line_count {
// Selection within single line
@@ -948,24 +919,24 @@ impl Term {
// Selection ends on line following start
Line(1) => {
// Starting line
- res.append(&self.grid, start.line, start.col..);
+ res.append(&self.grid, start.line, start.col..max_col);
// Ending line
- res.append(&self.grid, end.line, ..end.col);
+ res.append(&self.grid, end.line, Column(0)..end.col);
},
// Multi line selection
_ => {
// Starting line
- res.append(&self.grid, start.line, start.col..);
+ res.append(&self.grid, start.line, start.col..max_col);
let middle_range = IndexRange::from((start.line + 1)..(end.line));
for line in middle_range {
- res.append(&self.grid, line, ..);
+ res.append(&self.grid, line, Column(0)..max_col);
}
// Ending line
- res.append(&self.grid, end.line, ..end.col);
+ res.append(&self.grid, end.line, Column(0)..end.col);
}
}
@@ -1963,7 +1934,7 @@ mod tests {
let selection = Selection::lines(Point { line: Line(0), col: Column(3) });
match selection.to_span(&term) {
- Some(span) => assert_eq!(term.string_from_selection(&span), "\"aa\"a"),
+ Some(span) => assert_eq!(term.string_from_selection(&span), "\"aa\"a\n"),
_ => ()
}
}