mirror of
https://github.com/lotabout/skim.git
synced 2026-09-10 07:16:23 -04:00
[item] change data structure to support indivisual matched characters
This commit is contained in:
parent
c62afe4d94
commit
ccbfc24607
22
src/item.rs
22
src/item.rs
|
|
@ -25,27 +25,33 @@ impl Item {
|
|||
}
|
||||
}
|
||||
|
||||
pub type Score = (usize, usize); // score (matched-len, start pos)
|
||||
pub type Range = (usize, usize); // (start, end), end is excluded
|
||||
pub type Score = i32;
|
||||
|
||||
|
||||
#[derive(PartialEq, Eq)]
|
||||
pub enum MatchedRange {
|
||||
Range(usize, usize),
|
||||
Chars(Vec<usize>),
|
||||
}
|
||||
|
||||
#[derive(Eq)]
|
||||
pub struct MatchedItem {
|
||||
pub index: usize, // index of current item in items
|
||||
pub score: Score,
|
||||
pub matched_range_chars: Range, // range of chars that metched the pattern
|
||||
pub matched_range: Option<MatchedRange>, // range of chars that metched the pattern
|
||||
}
|
||||
|
||||
impl MatchedItem {
|
||||
pub fn new(index: usize) -> Self {
|
||||
MatchedItem {
|
||||
index: index,
|
||||
score: (std::usize::MAX, 0),
|
||||
matched_range_chars: (0, 0),
|
||||
score: -200000,
|
||||
matched_range: None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn set_matched_range(&mut self, range: Range) {
|
||||
self.matched_range_chars = range;
|
||||
pub fn set_matched_range(&mut self, range: MatchedRange) {
|
||||
self.matched_range = Some(range);
|
||||
}
|
||||
|
||||
pub fn set_score(&mut self, score: Score) {
|
||||
|
|
@ -55,7 +61,7 @@ impl MatchedItem {
|
|||
|
||||
impl Ord for MatchedItem {
|
||||
fn cmp(&self, other: &MatchedItem) -> Ordering {
|
||||
other.score.cmp(&self.score)
|
||||
self.score.cmp(&other.score)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@ use std::sync::{Arc, RwLock};
|
|||
|
||||
use std::sync::mpsc::Sender;
|
||||
use event::Event;
|
||||
use item::{Item, MatchedItem};
|
||||
use item::{Item, MatchedItem, MatchedRange};
|
||||
use util::eventbox::EventBox;
|
||||
use score;
|
||||
|
||||
|
|
@ -43,16 +43,17 @@ impl Matcher {
|
|||
}
|
||||
|
||||
fn match_item(&self, index: usize, item: &str) -> Option<MatchedItem> {
|
||||
let matched_result = score::compute_match_length(item, &self.query);
|
||||
//let matched_result = score::compute_match_length(item, &self.query);
|
||||
let matched_result = score::fuzzy_match(item, &self.query);
|
||||
if matched_result == None {
|
||||
return None;
|
||||
}
|
||||
|
||||
let (matched_start, matched_len) = matched_result.unwrap();
|
||||
let (score, matched_range) = matched_result.unwrap();
|
||||
|
||||
let mut item = MatchedItem::new(index);
|
||||
item.set_matched_range((matched_start as usize, (matched_start + matched_len) as usize));
|
||||
item.set_score((matched_len, matched_start));
|
||||
item.set_matched_range(MatchedRange::Chars(matched_range));
|
||||
item.set_score(score);
|
||||
Some(item)
|
||||
}
|
||||
|
||||
|
|
@ -67,7 +68,7 @@ impl Matcher {
|
|||
}
|
||||
|
||||
self.item_pos += 1;
|
||||
if (self.item_pos % 100) == 99 && !self.eb_req.is_empty() {
|
||||
if (self.item_pos % 1000) == 999 && !self.eb_req.is_empty() {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
35
src/model.rs
35
src/model.rs
|
|
@ -3,7 +3,7 @@
|
|||
|
||||
|
||||
use std::sync::{Arc, RwLock};
|
||||
use item::{Item, MatchedItem};
|
||||
use item::{Item, MatchedItem, MatchedRange};
|
||||
use ncurses::*;
|
||||
use std::cmp;
|
||||
use std::cell::RefCell;
|
||||
|
|
@ -130,20 +130,35 @@ impl Model {
|
|||
printw(" ");
|
||||
}
|
||||
|
||||
for (idx, ch) in item.text.chars().enumerate() {
|
||||
if idx == matched.matched_range_chars.0 {
|
||||
attron(A_BOLD());
|
||||
match matched.matched_range {
|
||||
Some(MatchedRange::Chars(ref matched_indics)) => {
|
||||
let mut matched_indics_iter = matched_indics.iter().peekable();
|
||||
for (idx, ch) in item.text.chars().enumerate() {
|
||||
if let Some(&&index) = matched_indics_iter.peek() {
|
||||
if idx == index {
|
||||
attron(A_UNDERLINE());
|
||||
addch(ch as u64);
|
||||
attroff(A_UNDERLINE());
|
||||
let _ = matched_indics_iter.next();
|
||||
} else {
|
||||
addch(ch as u64);
|
||||
}
|
||||
} else {
|
||||
addch(ch as u64);
|
||||
}
|
||||
if idx >= (self.max_x - 3) as usize {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if idx == matched.matched_range_chars.1 {
|
||||
attroff(A_BOLD());
|
||||
Some(MatchedRange::Range(_, _)) => {
|
||||
// pass
|
||||
}
|
||||
addch(ch as u64);
|
||||
if idx >= (self.max_x - 3) as usize {
|
||||
break;
|
||||
None => {
|
||||
// pass
|
||||
}
|
||||
}
|
||||
|
||||
//addstr(&shown_str);
|
||||
}
|
||||
|
||||
pub fn print_items(&self) {
|
||||
|
|
|
|||
Loading…
Reference in a new issue