diff --git a/term/Cargo.toml b/term/Cargo.toml index a0c9431f5..940f638f2 100644 --- a/term/Cargo.toml +++ b/term/Cargo.toml @@ -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" diff --git a/term/src/terminalstate.rs b/term/src/terminalstate.rs index 4013f53ca..a0e98a4e3 100644 --- a/term/src/terminalstate.rs +++ b/term/src/terminalstate.rs @@ -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, @@ -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(); diff --git a/term/src/test/mod.rs b/term/src/test/mod.rs index afc17e3a2..574a84a7a 100644 --- a/term/src/test/mod.rs +++ b/term/src/test/mod.rs @@ -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, diff --git a/term/src/test/selection.rs b/term/src/test/selection.rs index 482d69744..f9818433b 100644 --- a/term/src/test/selection.rs +++ b/term/src/test/selection.rs @@ -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 diff --git a/termwiz/src/cell.rs b/termwiz/src/cell.rs index 0284c7098..1b54d0763 100644 --- a/termwiz/src/cell.rs +++ b/termwiz/src/cell.rs @@ -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. diff --git a/termwiz/src/surface/line.rs b/termwiz/src/surface/line.rs index 8b604d0f7..c314bdd57 100644 --- a/termwiz/src/surface/line.rs +++ b/termwiz/src/surface/line.rs @@ -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)