Skip to content

Commit 6763f32

Browse files
committed
Move item scoring logic into its own module
1 parent 5d05bec commit 6763f32

6 files changed

Lines changed: 58 additions & 56 deletions

File tree

src/apps/mod.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ pub mod mac_apps;
1414
use crate::constants;
1515
use crate::module::{Module, ModuleMessage};
1616
use crate::serworse;
17+
use crate::sorting;
1718
use crate::util;
1819
use crate::widglets;
1920

@@ -206,12 +207,10 @@ impl AppModule {
206207
let start = std::time::Instant::now();
207208
// Cached_key seems to be much faster which is interesting since text_value is
208209
// always changing
209-
let input = &input.to_lowercase();
210+
// let input = &input.to_lowercase();
210211
self.app_list.sort_by_cached_key(|app| {
211-
let mut score = util::longest_common_substr(&app.name.to_lowercase(), input);
212-
if app.name.to_lowercase().starts_with(input) {
213-
score += 2;
214-
}
212+
let mut score = sorting::score_element(&input, &app.name);
213+
215214
if let Some(raw_freq) = self.app_frequencies.get(&app.name) {
216215
// Preview: https://www.desmos.com/calculator/vyac5ua1as
217216
score += (*raw_freq as f32).ln().mul(0.75).max(0.0).floor() as i32;

src/drun/mod.rs

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::io::Write;
33

44
use crate::{
55
module::{Module, ModuleMessage},
6-
util, widglets,
6+
sorting, widglets,
77
};
88

99
pub struct Drun {
@@ -40,16 +40,8 @@ impl Module for Drun {
4040
fn update(&mut self, msg: ModuleMessage) -> iced::Task<ModuleMessage> {
4141
match msg {
4242
ModuleMessage::TextChanged(input) => {
43-
self.text_input = input.clone();
44-
let input = &input.to_lowercase();
45-
self.options.sort_by_cached_key(|opt| {
46-
// Not a fan of this duplicated logic from app/mod.rs
47-
let mut score = util::longest_common_substr(opt, input);
48-
if opt.to_lowercase().starts_with(input) {
49-
score += 2;
50-
}
51-
-score
52-
});
43+
self.options
44+
.sort_by_cached_key(|opt| -sorting::score_element(&input, opt));
5345
Task::none()
5446
}
5547
ModuleMessage::ActivatedIndex(i) => {

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@ pub mod widglets;
99

1010
mod constants;
1111
mod message;
12+
mod sorting;
1213
mod util;

src/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ use drun::Drun;
2121
mod constants;
2222
mod module;
2323
mod serworse;
24+
mod sorting;
2425
mod util;
2526
mod widglets;
2627
use module::{Module, ModuleMessage};

src/sorting.rs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/// Returns positive score, required to negate for use
2+
pub fn score_element(user_input: &str, element_text: &str) -> i32 {
3+
let input = user_input.to_lowercase();
4+
let mut score = longest_common_substr(element_text, &input);
5+
if element_text.to_lowercase().starts_with(&input) {
6+
score += 2;
7+
}
8+
score
9+
}
10+
11+
// https://www.geeksforgeeks.org/dsa/longest-common-substring-dp-29/
12+
fn longest_common_substr(s1: &str, s2: &str) -> i32 {
13+
// Stringslice?
14+
15+
let s1: Vec<char> = s1.chars().collect();
16+
let s2: Vec<char> = s2.chars().collect();
17+
18+
let m = s1.len();
19+
let n = s2.len();
20+
21+
let mut prev = vec![0; n + 1];
22+
23+
let mut res: i32 = 0;
24+
25+
for i in 1..m + 1 {
26+
let mut curr = vec![0; n + 1];
27+
for j in 1..n + 1 {
28+
if s1[i - 1] == s2[j - 1] {
29+
curr[j] = prev[j - 1] + 1;
30+
res = res.max(curr[j]);
31+
} else {
32+
curr[j] = 0;
33+
}
34+
}
35+
prev = curr;
36+
}
37+
38+
res
39+
}
40+
41+
#[test]
42+
fn longest_common_substr_correct() {
43+
assert_eq!(longest_common_substr("hello world", "world"), 5);
44+
assert_eq!(
45+
longest_common_substr("geeksforgeeks", "ggeegeeksquizpractice"),
46+
5
47+
);
48+
assert_eq!(longest_common_substr("", ""), 0);
49+
}

src/util.rs

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -3,46 +3,6 @@ use std::ffi::OsStr;
33
use std::io;
44
use std::process;
55

6-
// https://www.geeksforgeeks.org/dsa/longest-common-substring-dp-29/
7-
pub fn longest_common_substr(s1: &str, s2: &str) -> i32 {
8-
// Stringslice?
9-
10-
let s1: Vec<char> = s1.chars().collect();
11-
let s2: Vec<char> = s2.chars().collect();
12-
13-
let m = s1.len();
14-
let n = s2.len();
15-
16-
let mut prev = vec![0; n + 1];
17-
18-
let mut res: i32 = 0;
19-
20-
for i in 1..m + 1 {
21-
let mut curr = vec![0; n + 1];
22-
for j in 1..n + 1 {
23-
if s1[i - 1] == s2[j - 1] {
24-
curr[j] = prev[j - 1] + 1;
25-
res = res.max(curr[j]);
26-
} else {
27-
curr[j] = 0;
28-
}
29-
}
30-
prev = curr;
31-
}
32-
33-
res
34-
}
35-
36-
#[test]
37-
fn longest_common_substr_correct() {
38-
assert_eq!(longest_common_substr("hello world", "world"), 5);
39-
assert_eq!(
40-
longest_common_substr("geeksforgeeks", "ggeegeeksquizpractice"),
41-
5
42-
);
43-
assert_eq!(longest_common_substr("", ""), 0);
44-
}
45-
466
#[cfg(unix)]
477
pub fn execute_command_detached<S, I>(
488
cmd: S,

0 commit comments

Comments
 (0)