improve double-click selection of wrapped lines

This one has been bugging me for a while; we now know when we've
wrapped a line and can join it without a line break when double-clicking
to select a word.

This commit introduces a wrapped attribute to help record this
information, which could potentially help with when it comes
to looking at nicer resize behavior in refs: https://github.com/wez/wezterm/issues/14
This commit is contained in:
Wez Furlong committed 2019-05-26 16:05:51 -07:00
1 parent 09f168a0db
commit 0bb3714435
6 files changed
+56 -20

No files matched your search

+3
View File
@@ -14,6 +14,9 @@ unicode-width = "~0.1"
serde = {version="~1.0", features = ["rc"]}
serde_derive = "~1.0"
[dev-dependencies]
pretty_assertions = "0.6"
[dependencies.termwiz]
path = "../termwiz"
+20 -9
View File
@@ -15,6 +15,7 @@ use termwiz::escape::osc::{ITermFileData, ITermProprietary};
use termwiz::escape::{Action, ControlCode, Esc, EscCode, OneBased, OperatingSystemCommand, CSI};
use termwiz::hyperlink::Rule as HyperlinkRule;
use termwiz::image::{ImageCell, ImageData, TextureCoordinate};
use unicode_width::UnicodeWidthStr;
struct TabStop {
tabs: Vec<bool>,
@@ -295,13 +296,19 @@ impl TerminalState {
if let Some(sel) = self.selection_range.as_ref().map(|r| r.normalize()) {
let screen = self.screen();
let mut last_was_wrapped = false;
for y in sel.rows() {
let idx = screen.scrollback_or_visible_row(y);
let cols = sel.cols_for_row(y);
if !s.is_empty() {
let last_col_idx = cols.end.min(screen.lines[idx].cells().len()) - 1;
if !s.is_empty() && !last_was_wrapped {
s.push('\n');
}
s.push_str(screen.lines[idx].columns_as_str(cols).trim_end());
let last_cell = &screen.lines[idx].cells()[last_col_idx];
// TODO: should really test for any unicode whitespace
last_was_wrapped = last_cell.attrs().wrapped() && last_cell.str() != " ";
}
}
@@ -452,7 +459,7 @@ impl TerminalState {
y,
},
end: SelectionCoordinate {
x: click_range.end,
x: click_range.end - 1,
y,
},
},
@@ -463,7 +470,7 @@ impl TerminalState {
};
let mut end_coord = SelectionCoordinate {
x: range_start.end,
x: range_start.end - 1,
y,
};
@@ -474,7 +481,7 @@ impl TerminalState {
DoubleClickRange::Range(range_end) => {
if range_end.end > range_end.start {
end_coord = SelectionCoordinate {
x: range_end.end,
x: range_end.end - 1,
y: y + (y_cont - idx) as i32,
};
}
@@ -482,7 +489,7 @@ impl TerminalState {
}
DoubleClickRange::RangeWithWrap(range_end) => {
end_coord = SelectionCoordinate {
x: range_end.end,
x: range_end.end - 1,
y: y + (y_cont - idx) as i32,
};
}
@@ -1932,15 +1939,19 @@ impl<'a> Performer<'a> {
let y = self.cursor.y;
let width = self.screen().physical_cols;
let pen = self.pen.clone();
let cell = Cell::new_grapheme(g, pen.clone());
let mut pen = self.pen.clone();
// the max(1) here is to ensure that we advance to the next cell
// position for zero-width graphemes. We want to make sure that
// they occupy a cell so that we can re-emit them when we output them.
// If we didn't do this, then we'd effectively filter them out from
// the model, which seems like a lossy design choice.
let print_width = cell.width().max(1);
let print_width = UnicodeWidthStr::width(g).max(1);
if !self.insert && x + print_width >= width {
pen.set_wrapped(true);
}
let cell = Cell::new_grapheme(g, pen);
if self.insert {
let screen = self.screen_mut();
+17 -9
View File
@@ -7,6 +7,7 @@ use bitflags::bitflags;
mod c1;
mod csi;
mod selection;
use pretty_assertions::assert_eq;
use std::sync::Arc;
use termwiz::escape::csi::{Edit, EraseInDisplay, EraseInLine};
use termwiz::escape::{OneBased, OperatingSystemCommand, CSI};
@@ -275,7 +276,13 @@ fn assert_lines_equal(lines: &[Line], expect_lines: &[Line], compare: Compare) {
if compare.contains(Compare::ATTRS) {
let line_attrs: Vec<_> = line.cells().iter().map(|c| c.attrs().clone()).collect();
let expect_attrs: Vec<_> = expect.cells().iter().map(|c| c.attrs().clone()).collect();
assert_eq!(expect_attrs, line_attrs, "line {} attrs didn't match", idx,);
assert_eq!(
expect_attrs,
line_attrs,
"line {} `{}` attrs didn't match (left=expected, right=actual)",
idx,
line.as_str()
);
}
if compare.contains(Compare::TEXT) {
let line_str = line.as_str();
@@ -519,9 +526,9 @@ fn test_hyperlinks() {
assert_lines_equal(
&term.screen().visible_lines(),
&[
Line::from_text("hello", &linked),
" ".into(),
" ".into(),
Line::from_text_with_wrapped_last_col("hello", &linked),
Line::from_text(" ", &CellAttributes::default()),
Line::from_text(" ", &CellAttributes::default()),
],
Compare::TEXT | Compare::ATTRS,
);
@@ -535,8 +542,8 @@ fn test_hyperlinks() {
assert_lines_equal(
&term.screen().visible_lines(),
&[
Line::from_text("hello", &linked),
Line::from_text("hey!!", &linked),
Line::from_text_with_wrapped_last_col("hello", &linked),
Line::from_text_with_wrapped_last_col("hey!!", &linked),
" ".into(),
],
Compare::TEXT | Compare::ATTRS,
@@ -551,7 +558,8 @@ fn test_hyperlinks() {
term.soft_reset();
term.print("00t");
let mut partial_line: Line = "wo00t".into();
let mut partial_line =
Line::from_text_with_wrapped_last_col("wo00t", &CellAttributes::default());
partial_line.set_cell(
0,
Cell::new(
@@ -574,8 +582,8 @@ fn test_hyperlinks() {
assert_lines_equal(
&term.screen().visible_lines(),
&[
Line::from_text("hello", &linked),
Line::from_text("hey!!", &linked),
Line::from_text_with_wrapped_last_col("hello", &linked),
Line::from_text_with_wrapped_last_col("hey!!", &linked),
partial_line,
],
Compare::TEXT | Compare::ATTRS,
+2 -1
View File
@@ -1,4 +1,5 @@
use super::*;
use pretty_assertions::assert_eq;
/// Test basic dragging to select some text
#[test]
@@ -71,7 +72,7 @@ fn double_click_wrapped_selection() {
assert_visible_contents(&term, &["hello worl", "d ", " "]);
term.click_n(7, 0, MouseButton::Left, 2);
assert_eq!(term.get_clipboard().unwrap(), "worl\nd");
assert_eq!(term.get_clipboard().unwrap(), "world");
}
/// Make sure that we adjust for the viewport offset when scrolling
+5
View File
@@ -141,6 +141,7 @@ impl CellAttributes {
bitfield!(reverse, set_reverse, 7);
bitfield!(strikethrough, set_strikethrough, 8);
bitfield!(invisible, set_invisible, 9);
bitfield!(wrapped, set_wrapped, 10);
/// Returns true if the attribute bits in both objects are equal.
/// This can be used to cheaply test whether the styles of the two
@@ -294,6 +295,10 @@ impl Cell {
pub fn attrs(&self) -> &CellAttributes {
&self.attrs
}
pub fn attrs_mut(&mut self) -> &mut CellAttributes {
&mut self.attrs
}
}
/// Models a change in the attributes of a cell in a stream of changes.
+9 -1
View File
@@ -61,6 +61,14 @@ impl Line {
}
}
pub fn from_text_with_wrapped_last_col(s: &str, attrs: &CellAttributes) -> Line {
let mut line = Self::from_text(s, attrs);
line.cells
.last_mut()
.map(|cell| cell.attrs_mut().set_wrapped(true));
line
}
pub fn resize_and_clear(&mut self, width: usize) {
let blank = Cell::default();
self.cells.clear();
@@ -213,7 +221,7 @@ impl Line {
lower = idx;
}
if upper == self.cells().len() {
if upper > lower && self.cells[upper - 1].attrs().wrapped() {
DoubleClickRange::RangeWithWrap(lower..upper)
} else {
DoubleClickRange::Range(lower..upper)