From fb4b4c893255828d0b9d8e6a7d3561a22d474698 Mon Sep 17 00:00:00 2001 From: Araxeus <78568641+Araxeus@users.noreply.github.com> Date: Fri, 13 May 2022 22:06:51 +0300 Subject: [PATCH] don't highlight RTL chars --- Cargo.toml | 4 +++- src/theme.rs | 12 +++++++++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 19f4465f..a409f151 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,7 +18,7 @@ readme = "README.md" [features] default = ["editor", "password"] editor = ["tempfile"] -fuzzy-select = ["fuzzy-matcher", "unicode-segmentation"] +fuzzy-select = ["fuzzy-matcher", "unicode-segmentation", "regex", "lazy_static"] history = [] password = ["zeroize"] completion = [] @@ -30,6 +30,8 @@ tempfile = { version = "3", optional = true } zeroize = { version = "1.1.1", optional = true } fuzzy-matcher = { version = "0.3.7", optional = true } unicode-segmentation = { version = "1.9.0", optional = true } +regex = { version = "1.5.5", optional = true } +lazy_static = { version = "1.4.0", optional = true } [[example]] name = "password" diff --git a/src/theme.rs b/src/theme.rs index fe31c314..57a3c7d5 100644 --- a/src/theme.rs +++ b/src/theme.rs @@ -4,6 +4,8 @@ use std::{fmt, io}; use console::{style, Style, StyledObject, Term}; #[cfg(feature = "fuzzy-select")] use fuzzy_matcher::{skim::SkimMatcherV2, FuzzyMatcher}; +#[cfg(feature = "fuzzy-select")] +use regex::Regex; /// Implements a theme for dialoguer. pub trait Theme { @@ -627,12 +629,20 @@ impl Theme for ColorfulTheme { matcher: &SkimMatcherV2, search_term: &str, ) -> fmt::Result { + //check if char is right to left aligned (arabic/hebrew unicode range) + fn is_rle(ch: char) -> bool { + lazy_static::lazy_static! { + static ref REG: Regex = Regex::new(r"\p{IsArabic}|\p{IsHebrew}").unwrap(); + } + REG.is_match(&ch.to_string()) + } + write!(f, "{} ", if active { ">" } else { " " })?; if highlight_matches { if let Some((_score, indices)) = matcher.fuzzy_indices(text, search_term) { for (idx, c) in text.chars().into_iter().enumerate() { - if indices.contains(&idx) { + if indices.contains(&idx) && !is_rle(c) { write!(f, "{}", self.fuzzy_match_highlight_style.apply_to(c))?; } else { write!(f, "{}", c)?;