Thanks to visit codestin.com
Credit goes to github.com

Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 22 additions & 24 deletions src/history/history.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ pub struct Command {
pub selected: bool,
pub dir: Option<String>,
pub features: Features,
pub match_bounds: Vec<(usize, usize)>,
pub match_indices: Vec<usize>,
}

#[derive(Debug, Clone, Serialize)]
Expand Down Expand Up @@ -303,7 +303,7 @@ impl History {
.get(1)
.unwrap_or_else(|err| panic!("McFly error: cmd to be readable ({err})"));

let bounds = Self::calc_match_bounds(&text, &cmd, fuzzy);
let bounds = Self::calc_match_indices(&text, &cmd, fuzzy);

Ok(Command {
id: row.get(0).unwrap_or_else(|err| {
Expand Down Expand Up @@ -331,7 +331,7 @@ impl History {
rank: row.get(8).unwrap_or_else(|err| {
panic!("McFly error: rank to be readable ({err})")
}),
match_bounds: bounds,
match_indices: bounds,
features: Features {
age_factor: row.get(9).unwrap_or_else(|err| {
panic!("McFly error: age_factor to be readable ({err})")
Expand Down Expand Up @@ -409,10 +409,11 @@ impl History {
// the likelihood of the weight flipping the outcome for
// the originally lower-ranked result.

let a_start = a.match_bounds[0].0;
let b_start = b.match_bounds[0].0;
let a_len = a.match_bounds[0].1 - a_start;
let b_len = b.match_bounds[0].1 - b_start;
let a_start = *a.match_indices.first().unwrap_or(&0);
let b_start = *b.match_indices.first().unwrap_or(&0);

let a_len = a.match_indices.last().map(|i| i + 1).unwrap_or(0) - a_start;
let b_len = b.match_indices.last().map(|i| i + 1).unwrap_or(0) - b_start;

let a_mod =
1.0 - (a_start + a_len) as f64 / (a_start + b_start + a_len + b_len) as f64;
Expand All @@ -436,7 +437,8 @@ impl History {
cmd.chars().any(|c| c.is_uppercase())
}

fn calc_match_bounds(text: &str, cmd: &str, fuzzy: i16) -> Vec<(usize, usize)> {
/// Calculate the indices of the matches in the text.
fn calc_match_indices(text: &str, cmd: &str, fuzzy: i16) -> Vec<usize> {
let (text, cmd) = if Self::is_case_sensitive(cmd) {
(text.to_string(), cmd.to_string())
} else {
Expand All @@ -446,28 +448,24 @@ impl History {
match fuzzy {
0 => text
.match_indices(&cmd)
.map(|(index, _)| (index, index + cmd.len()))
.collect::<Vec<_>>(),
.flat_map(|(index, _)| index..index + cmd.len())
.collect(),
_ => {
let mut search_iter = cmd.chars().peekable();
let mut matches = text
.match_indices(|c| {
let next = search_iter.peek();

if next.is_some() && next.unwrap() == &c {
let _advance = search_iter.next();

return true;
}
text.match_indices(|c| {
let next = search_iter.peek();

false
})
.map(|m| m.0);
if next.is_some() && next.unwrap() == &c {
let _advance = search_iter.next();

let start = matches.next().unwrap_or(0);
let end = matches.last().unwrap_or(start) + 1;
return true;
}

vec![(start, end)]
false
})
.map(|m| m.0)
.collect()
}
}
}
Expand Down
34 changes: 13 additions & 21 deletions src/interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -272,12 +272,7 @@ impl<'a> Interface<'a> {
SetBackgroundColor(bg),
SetForegroundColor(fg),
Print(Interface::truncate_for_display(
command,
&self.input.command,
width,
highlight,
fg,
self.debug
command, width, highlight, fg, self.debug
))
)
.unwrap();
Expand Down Expand Up @@ -991,13 +986,11 @@ impl<'a> Interface<'a> {

fn truncate_for_display(
command: &Command,
search: &str,
width: u16,
highlight_color: Color,
base_color: Color,
debug: bool,
) -> String {
let mut prev: usize = 0;
let debug_space = if debug { 90 } else { 0 };
let max_grapheme_length = if width > debug_space {
width - debug_space - 9
Expand All @@ -1006,23 +999,22 @@ impl<'a> Interface<'a> {
};
let mut out = FixedLengthGraphemeString::empty(max_grapheme_length);

if !search.is_empty() {
for (start, end) in &command.match_bounds {
if prev != *start {
out.push_grapheme_str(&command.cmd[prev..*start]);
}
let mut match_indices = command.match_indices.iter().peekable();

execute!(out, SetForegroundColor(highlight_color)).unwrap();
out.push_grapheme_str(&command.cmd[*start..*end]);
execute!(out, SetForegroundColor(base_color)).unwrap();
prev = *end;
for (i, c) in command.cmd.char_indices() {
match match_indices.peek() {
Some(&&j) if i == j => {
let _ = match_indices.next();
execute!(out, SetForegroundColor(highlight_color)).unwrap();
out.push_grapheme_str(c);
}
_ => {
execute!(out, SetForegroundColor(base_color)).unwrap();
out.push_grapheme_str(c);
}
}
}

if prev != command.cmd.len() {
out.push_grapheme_str(&command.cmd[prev..]);
}

if debug {
out.max_grapheme_length += debug_space;
out.push_grapheme_str(" ");
Expand Down
Loading