termwiz: fix hyperlink matching issue with double wide chars
refs: https://github.com/wez/wezterm/issues/221
This commit is contained in:
1 parent
bd3844d665
commit
e30f42a868
3 files changed
+106
-16
No files matched your search
Generated
+1
@@ -3184,6 +3184,7 @@ dependencies = [
|
||||
"num-derive",
|
||||
"num-traits 0.2.12",
|
||||
"ordered-float",
|
||||
"pretty_assertions",
|
||||
"regex",
|
||||
"semver",
|
||||
"serde",
|
||||
|
||||
@@ -39,6 +39,7 @@ use_serde = ["serde"]
|
||||
|
||||
[dev-dependencies]
|
||||
varbincode = "0.1"
|
||||
pretty_assertions = "0.6"
|
||||
|
||||
[dependencies.num-derive]
|
||||
features = ["full-syntax"]
|
||||
|
||||
+104
-16
@@ -190,25 +190,30 @@ impl Line {
|
||||
self.bits |= LineBits::SCANNED_IMPLICIT_HYPERLINKS;
|
||||
self.bits &= !LineBits::HAS_IMPLICIT_HYPERLINKS;
|
||||
|
||||
for m in Rule::match_hyperlinks(&line, rules) {
|
||||
// The capture range is measured in bytes but we need to translate
|
||||
// that to the char index of the column.
|
||||
for (cell_idx, (byte_idx, _char)) in line.char_indices().enumerate() {
|
||||
if self.cells[cell_idx].attrs().hyperlink.is_some() {
|
||||
// Don't replace existing links
|
||||
continue;
|
||||
}
|
||||
let matches = Rule::match_hyperlinks(&line, rules);
|
||||
if matches.is_empty() {
|
||||
return;
|
||||
}
|
||||
|
||||
// The capture range is measured in bytes but we need to translate
|
||||
// that to the index of the column. This is complicated a bit further
|
||||
// because double wide sequences have a blank column cell after them
|
||||
// in the cells array, but the string we match against excludes that
|
||||
// string.
|
||||
let mut cell_idx = 0;
|
||||
for (byte_idx, _grapheme) in line.grapheme_indices(true) {
|
||||
let cell = &mut self.cells[cell_idx];
|
||||
for m in &matches {
|
||||
if m.range.contains(&byte_idx) {
|
||||
let attrs = self.cells[cell_idx]
|
||||
.attrs()
|
||||
.clone()
|
||||
.set_hyperlink(Some(Arc::clone(&m.link)))
|
||||
.clone();
|
||||
let cell = Cell::new_grapheme(self.cells[cell_idx].str(), attrs);
|
||||
self.cells[cell_idx] = cell;
|
||||
self.bits |= LineBits::HAS_IMPLICIT_HYPERLINKS;
|
||||
let attrs = cell.attrs_mut();
|
||||
// Don't replace existing links
|
||||
if !attrs.hyperlink.is_some() {
|
||||
attrs.set_hyperlink(Some(Arc::clone(&m.link)));
|
||||
self.bits |= LineBits::HAS_IMPLICIT_HYPERLINKS;
|
||||
}
|
||||
}
|
||||
}
|
||||
cell_idx += cell.width();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -512,3 +517,86 @@ impl<'a> From<&'a str> for Line {
|
||||
Line::from_text(s, &CellAttributes::default())
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use super::*;
|
||||
use crate::hyperlink::*;
|
||||
use pretty_assertions::assert_eq;
|
||||
|
||||
#[test]
|
||||
fn hyperlinks() {
|
||||
let text =
|
||||
"❤ 😍🤢 http://example.com \u{1f468}\u{1f3fe}\u{200d}\u{1f9b0} http://example.com";
|
||||
|
||||
let rules = vec![
|
||||
Rule::new(r"\b\w+://(?:[\w.-]+)\.[a-z]{2,15}\S*\b", "$0").unwrap(),
|
||||
Rule::new(r"\b\w+@[\w-]+(\.[\w-]+)+\b", "mailto:$0").unwrap(),
|
||||
];
|
||||
|
||||
let hyperlink = Arc::new(Hyperlink::new_implicit("http://example.com"));
|
||||
let hyperlink_attr = CellAttributes::default()
|
||||
.set_hyperlink(Some(hyperlink.clone()))
|
||||
.clone();
|
||||
|
||||
let mut line: Line = text.into();
|
||||
line.scan_and_create_hyperlinks(&rules);
|
||||
assert!(line.has_hyperlink());
|
||||
assert_eq!(
|
||||
line.cells().to_vec(),
|
||||
vec![
|
||||
Cell::new_grapheme("❤", CellAttributes::default()),
|
||||
Cell::new(' ', CellAttributes::default()), // double width spacer
|
||||
Cell::new_grapheme("😍", CellAttributes::default()),
|
||||
Cell::new(' ', CellAttributes::default()), // double width spacer
|
||||
Cell::new_grapheme("🤢", CellAttributes::default()),
|
||||
Cell::new(' ', CellAttributes::default()), // double width spacer
|
||||
Cell::new(' ', CellAttributes::default()),
|
||||
Cell::new('h', hyperlink_attr.clone()),
|
||||
Cell::new('t', hyperlink_attr.clone()),
|
||||
Cell::new('t', hyperlink_attr.clone()),
|
||||
Cell::new('p', hyperlink_attr.clone()),
|
||||
Cell::new(':', hyperlink_attr.clone()),
|
||||
Cell::new('/', hyperlink_attr.clone()),
|
||||
Cell::new('/', hyperlink_attr.clone()),
|
||||
Cell::new('e', hyperlink_attr.clone()),
|
||||
Cell::new('x', hyperlink_attr.clone()),
|
||||
Cell::new('a', hyperlink_attr.clone()),
|
||||
Cell::new('m', hyperlink_attr.clone()),
|
||||
Cell::new('p', hyperlink_attr.clone()),
|
||||
Cell::new('l', hyperlink_attr.clone()),
|
||||
Cell::new('e', hyperlink_attr.clone()),
|
||||
Cell::new('.', hyperlink_attr.clone()),
|
||||
Cell::new('c', hyperlink_attr.clone()),
|
||||
Cell::new('o', hyperlink_attr.clone()),
|
||||
Cell::new('m', hyperlink_attr.clone()),
|
||||
Cell::new(' ', CellAttributes::default()),
|
||||
Cell::new_grapheme(
|
||||
// man: dark skin tone, red hair ZWJ emoji grapheme
|
||||
"\u{1f468}\u{1f3fe}\u{200d}\u{1f9b0}",
|
||||
CellAttributes::default()
|
||||
),
|
||||
Cell::new(' ', CellAttributes::default()), // double width spacer
|
||||
Cell::new(' ', CellAttributes::default()),
|
||||
Cell::new('h', hyperlink_attr.clone()),
|
||||
Cell::new('t', hyperlink_attr.clone()),
|
||||
Cell::new('t', hyperlink_attr.clone()),
|
||||
Cell::new('p', hyperlink_attr.clone()),
|
||||
Cell::new(':', hyperlink_attr.clone()),
|
||||
Cell::new('/', hyperlink_attr.clone()),
|
||||
Cell::new('/', hyperlink_attr.clone()),
|
||||
Cell::new('e', hyperlink_attr.clone()),
|
||||
Cell::new('x', hyperlink_attr.clone()),
|
||||
Cell::new('a', hyperlink_attr.clone()),
|
||||
Cell::new('m', hyperlink_attr.clone()),
|
||||
Cell::new('p', hyperlink_attr.clone()),
|
||||
Cell::new('l', hyperlink_attr.clone()),
|
||||
Cell::new('e', hyperlink_attr.clone()),
|
||||
Cell::new('.', hyperlink_attr.clone()),
|
||||
Cell::new('c', hyperlink_attr.clone()),
|
||||
Cell::new('o', hyperlink_attr.clone()),
|
||||
Cell::new('m', hyperlink_attr.clone()),
|
||||
]
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user