A simple CPU rendered GUI IDE experience.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
pub mod generic;
use std::any::TypeId;
use std::borrow::Cow;
use std::cmp::Reverse;
use std::sync::LazyLock;

use itertools::Itertools;

use crate::Freq;
use crate::menu::generic::MenuData;

#[lower::apply(saturating)]
pub fn next<const N: usize>(n: usize, sel: &mut usize, vo: &mut usize) {
    *sel += 1;
    if *sel == n {
        *vo = 0;
        *sel = 0;
    }
    if *sel >= *vo + N {
        *vo += 1;
    }
}
#[lower::apply(saturating)]
pub fn back<const N: usize>(n: usize, sel: &mut usize, vo: &mut usize) {
    if *sel == 0 {
        *vo = n - N;
        *sel = n - 1;
    } else {
        *sel -= 1;
        if *sel < *vo {
            *vo -= 1;
        }
    }
}

#[thread_local]
static mut MATCHER: LazyLock<nucleo::Matcher> =
    LazyLock::new(|| nucleo::Matcher::new(nucleo::Config::DEFAULT));

pub fn score<'a, T: Key<'a>, D: MenuData<Element<'a> = T> + 'static>(
    x: impl Iterator<Item = T>,
    filter: &'_ str,
    freq: Option<&Freq>,
) -> Vec<(u32, T, Vec<u32>)> {
    let p = nucleo::pattern::Pattern::parse(
        filter,
        nucleo::pattern::CaseMatching::Smart,
        nucleo::pattern::Normalization::Smart,
    );
    let mut v = x
        .map(move |y| {
            if let Some(f) = freq
                && filter == ""
                && let Some(f) = f.get(&TypeId::of::<D>())
            {
                return (
                    f.get(&D::hashed(&y).unwrap())
                        .copied()
                        .unwrap_or_default() as u32,
                    y,
                    vec![],
                );
            }
            let mut utf32 = vec![];
            // std::env::args().nth(1).unwrap().as_bytes().fi .fold(0, |acc, x| acc * 10 + x - b'0');
            let hay = y.k();
            let mut indices = vec![];
            let score = p
                .indices(
                    nucleo::Utf32Str::new(&hay, &mut utf32),
                    unsafe { &mut *MATCHER },
                    &mut indices,
                )
                .unwrap_or(0);
            indices.sort_unstable();
            indices.dedup();

            (score, y, indices)
        })
        .collect::<Vec<_>>();
    // std::fs::write(
    //     "com",
    //     v.iter().map(|x| x.1.label.clone() + "\n").collect::<String>(),
    // );
    v.sort_by_key(|x| Reverse(x.0));
    v
}
pub fn score_basic<'a, T: Key<'a>>(
    x: impl Iterator<Item = T>,
    filter: &'_ str,
) -> Vec<(u32, T, Vec<u32>)> {
    let p = nucleo::pattern::Pattern::parse(
        filter,
        nucleo::pattern::CaseMatching::Smart,
        nucleo::pattern::Normalization::Smart,
    );
    let mut v = x
        .map(move |y| {
            let mut utf32 = vec![];
            // std::env::args().nth(1).unwrap().as_bytes().fi .fold(0, |acc, x| acc * 10 + x - b'0');
            let hay = y.k();
            let mut indices = vec![];
            let score = p
                .indices(
                    nucleo::Utf32Str::new(&hay, &mut utf32),
                    unsafe { &mut *MATCHER },
                    &mut indices,
                )
                .unwrap_or(0);
            indices.sort_unstable();
            indices.dedup();

            (score, y, indices)
        })
        .collect::<Vec<_>>();
    // std::fs::write(
    //     "com",
    //     v.iter().map(|x| x.1.label.clone() + "\n").collect::<String>(),
    // );
    v.sort_by_key(|x| Reverse(x.0));
    v
}

pub fn filter<'a, T: Key<'a>>(
    i: impl Iterator<Item = T>,
    filter: &'_ str,
) -> impl Iterator<Item = T> {
    i.filter(move |y| {
        filter.is_empty()
            || y.k().chars().any(|x| filter.chars().contains(&x))
        // .collect::<HashSet<_>>()
        // .intersection(&filter.chars().collect())
        // .count()
        // > 0
    })
}

pub trait Key<'a> {
    fn key(&self) -> impl Into<Cow<'a, str>>;
    fn k(&self) -> Cow<'a, str> {
        self.key().into()
    }
}
pub fn charc(c: &str) -> usize {
    c.chars().count()
}

impl<'a> crate::menu::Key<'a> for &'a str {
    fn key(&self) -> impl Into<std::borrow::Cow<'a, str>> {
        *self
    }
}