Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix RTL chars highlight #4

Merged
merged 1 commit into from May 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion Cargo.toml
Expand Up @@ -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 = []
Expand All @@ -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"
Expand Down
12 changes: 11 additions & 1 deletion src/theme.rs
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)?;
Expand Down