summaryrefslogtreecommitdiff
path: root/alacritty_terminal/src/term/mod.rs
diff options
context:
space:
mode:
authora5ob7r <12132068+a5ob7r@users.noreply.github.com>2022-01-05 06:46:20 +0900
committerGitHub <noreply@github.com>2022-01-04 21:46:20 +0000
commit2057ac243a9e38fe9789982ffe93d96d594652ef (patch)
tree0e4fe5b2014f0b80e1227ee9e7c91dce4a5c6fcf /alacritty_terminal/src/term/mod.rs
parent437f42f8d30fb6d4843738fbd46bb801679443a7 (diff)
downloadalacritty-2057ac243a9e38fe9789982ffe93d96d594652ef.tar.gz
alacritty-2057ac243a9e38fe9789982ffe93d96d594652ef.zip
Trim \n from hints/semantic-/simple selection
Before this patch a hint regex ending in the last column would append a newline, despite this newline being "invisible" to the user. To match the expected behavior, newlines are trimmed from regex hint matches. To ensure consistency the simple and semantic selection also do not include a newline at the end of the copied text anymore. Fixes #5697.
Diffstat (limited to 'alacritty_terminal/src/term/mod.rs')
-rw-r--r--alacritty_terminal/src/term/mod.rs40
1 files changed, 23 insertions, 17 deletions
diff --git a/alacritty_terminal/src/term/mod.rs b/alacritty_terminal/src/term/mod.rs
index c7c461f4..ef71f6f1 100644
--- a/alacritty_terminal/src/term/mod.rs
+++ b/alacritty_terminal/src/term/mod.rs
@@ -17,7 +17,7 @@ use crate::config::Config;
use crate::event::{Event, EventListener};
use crate::grid::{Dimensions, Grid, GridIterator, Scroll};
use crate::index::{self, Boundary, Column, Direction, Line, Point, Side};
-use crate::selection::{Selection, SelectionRange};
+use crate::selection::{Selection, SelectionRange, SelectionType};
use crate::term::cell::{Cell, Flags, LineLength};
use crate::term::color::{Colors, Rgb};
use crate::vi_mode::{ViModeCursor, ViMotion};
@@ -348,24 +348,30 @@ impl<T> Term<T> {
/// Convert the active selection to a String.
pub fn selection_to_string(&self) -> Option<String> {
let selection_range = self.selection.as_ref().and_then(|s| s.to_range(self))?;
- let SelectionRange { start, end, is_block } = selection_range;
+ let SelectionRange { start, end, .. } = selection_range;
let mut res = String::new();
- if is_block {
- for line in (start.line.0..end.line.0).map(Line::from) {
- res += self
- .line_to_string(line, start.column..end.column, start.column.0 != 0)
- .trim_end();
-
- // If the last column is included, newline is appended automatically.
- if end.column != self.columns() - 1 {
- res += "\n";
+ match self.selection.as_ref() {
+ Some(Selection { ty: SelectionType::Block, .. }) => {
+ for line in (start.line.0..end.line.0).map(Line::from) {
+ res += self
+ .line_to_string(line, start.column..end.column, start.column.0 != 0)
+ .trim_end();
+
+ // If the last column is included, newline is appended automatically.
+ if end.column != self.columns() - 1 {
+ res += "\n";
+ }
}
- }
- res += self.line_to_string(end.line, start.column..end.column, true).trim_end();
- } else {
- res = self.bounds_to_string(start, end);
+ res += self.line_to_string(end.line, start.column..end.column, true).trim_end();
+ },
+ Some(Selection { ty: SelectionType::Lines, .. }) => {
+ res = self.bounds_to_string(start, end) + "\n";
+ },
+ _ => {
+ res = self.bounds_to_string(start, end);
+ },
}
Some(res)
@@ -382,7 +388,7 @@ impl<T> Term<T> {
res += &self.line_to_string(line, start_col..end_col, line == end.line);
}
- res
+ res.strip_suffix('\n').map(str::to_owned).unwrap_or(res)
}
/// Convert a single line in the grid to a String.
@@ -2103,7 +2109,7 @@ mod tests {
);
selection.update(Point { line: Line(2), column: Column(2) }, Side::Right);
term.selection = Some(selection);
- assert_eq!(term.selection_to_string(), Some("aaa\n\naaa\n".into()));
+ assert_eq!(term.selection_to_string(), Some("aaa\n\naaa".into()));
}
/// Check that the grid can be serialized back and forth losslessly.