initial commit

This commit is contained in:
garcal001
2024-01-23 09:40:16 +01:00
commit b5e363c5bd
10 changed files with 609 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
/target

261
Cargo.lock generated Normal file
View File

@@ -0,0 +1,261 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "autocfg"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa"
[[package]]
name = "bitflags"
version = "1.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
[[package]]
name = "bitflags"
version = "2.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ed570934406eb16438a4e976b1b4500774099c13b8cb96eec99f620f05090ddf"
[[package]]
name = "cfg-if"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "crossterm"
version = "0.27.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f476fe445d41c9e991fd07515a6f463074b782242ccf4a5b7b1d1012e70824df"
dependencies = [
"bitflags 2.4.2",
"crossterm_winapi",
"libc",
"mio",
"parking_lot",
"signal-hook",
"signal-hook-mio",
"winapi",
]
[[package]]
name = "crossterm_winapi"
version = "0.9.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "acdd7c62a3665c7f6830a51635d9ac9b23ed385797f70a83bb8bafe9c572ab2b"
dependencies = [
"winapi",
]
[[package]]
name = "fuzzy-search"
version = "0.1.0"
dependencies = [
"crossterm",
]
[[package]]
name = "libc"
version = "0.2.152"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "13e3bf6590cbc649f4d1a3eefc9d5d6eb746f5200ffb04e5e142700b8faa56e7"
[[package]]
name = "lock_api"
version = "0.4.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3c168f8615b12bc01f9c17e2eb0cc07dcae1940121185446edc3744920e8ef45"
dependencies = [
"autocfg",
"scopeguard",
]
[[package]]
name = "log"
version = "0.4.20"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b5e6163cb8c49088c2c36f57875e58ccd8c87c7427f7fbd50ea6710b2f3f2e8f"
[[package]]
name = "mio"
version = "0.8.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8f3d0b296e374a4e6f3c7b0a1f5a51d748a0d34c85e7dc48fc3fa9a87657fe09"
dependencies = [
"libc",
"log",
"wasi",
"windows-sys",
]
[[package]]
name = "parking_lot"
version = "0.12.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3742b2c103b9f06bc9fff0a37ff4912935851bee6d36f3c02bcc755bcfec228f"
dependencies = [
"lock_api",
"parking_lot_core",
]
[[package]]
name = "parking_lot_core"
version = "0.9.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4c42a9226546d68acdd9c0a280d17ce19bfe27a46bf68784e4066115788d008e"
dependencies = [
"cfg-if",
"libc",
"redox_syscall",
"smallvec",
"windows-targets",
]
[[package]]
name = "redox_syscall"
version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4722d768eff46b75989dd134e5c353f0d6296e5aaa3132e776cbdb56be7731aa"
dependencies = [
"bitflags 1.3.2",
]
[[package]]
name = "scopeguard"
version = "1.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49"
[[package]]
name = "signal-hook"
version = "0.3.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8621587d4798caf8eb44879d42e56b9a93ea5dcd315a6487c357130095b62801"
dependencies = [
"libc",
"signal-hook-registry",
]
[[package]]
name = "signal-hook-mio"
version = "0.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "29ad2e15f37ec9a6cc544097b78a1ec90001e9f71b81338ca39f430adaca99af"
dependencies = [
"libc",
"mio",
"signal-hook",
]
[[package]]
name = "signal-hook-registry"
version = "1.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d8229b473baa5980ac72ef434c4415e70c4b5e71b423043adb4ba059f89c99a1"
dependencies = [
"libc",
]
[[package]]
name = "smallvec"
version = "1.13.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e6ecd384b10a64542d77071bd64bd7b231f4ed5940fba55e98c3de13824cf3d7"
[[package]]
name = "wasi"
version = "0.11.0+wasi-snapshot-preview1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423"
[[package]]
name = "winapi"
version = "0.3.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419"
dependencies = [
"winapi-i686-pc-windows-gnu",
"winapi-x86_64-pc-windows-gnu",
]
[[package]]
name = "winapi-i686-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
[[package]]
name = "winapi-x86_64-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
[[package]]
name = "windows-sys"
version = "0.48.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "677d2418bec65e3338edb076e806bc1ec15693c5d0104683f2efe857f61056a9"
dependencies = [
"windows-targets",
]
[[package]]
name = "windows-targets"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9a2fa6e2155d7247be68c096456083145c183cbbbc2764150dda45a87197940c"
dependencies = [
"windows_aarch64_gnullvm",
"windows_aarch64_msvc",
"windows_i686_gnu",
"windows_i686_msvc",
"windows_x86_64_gnu",
"windows_x86_64_gnullvm",
"windows_x86_64_msvc",
]
[[package]]
name = "windows_aarch64_gnullvm"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2b38e32f0abccf9987a4e3079dfb67dcd799fb61361e53e2882c3cbaf0d905d8"
[[package]]
name = "windows_aarch64_msvc"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dc35310971f3b2dbbf3f0690a219f40e2d9afcf64f9ab7cc1be722937c26b4bc"
[[package]]
name = "windows_i686_gnu"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a75915e7def60c94dcef72200b9a8e58e5091744960da64ec734a6c6e9b3743e"
[[package]]
name = "windows_i686_msvc"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8f55c233f70c4b27f66c523580f78f1004e8b5a8b659e05a4eb49d4166cca406"
[[package]]
name = "windows_x86_64_gnu"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "53d40abd2583d23e4718fddf1ebec84dbff8381c07cae67ff7768bbf19c6718e"
[[package]]
name = "windows_x86_64_gnullvm"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0b7b52767868a23d5bab768e390dc5f5c55825b6d30b86c844ff2dc7414044cc"
[[package]]
name = "windows_x86_64_msvc"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538"

9
Cargo.toml Normal file
View File

@@ -0,0 +1,9 @@
[package]
name = "fuzzy-search"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
crossterm = "0.27.0"

32
src/fuzzy_search/fuzzy.rs Normal file
View File

@@ -0,0 +1,32 @@
#[inline]
pub fn fzs(pattern: &str, list: &mut [&str]) {
list.sort_by_key(|x| distance(pattern, x));
}
fn distance(pattern: &str, text: &str) -> usize {
let mut matrix = vec![vec![0_usize; pattern.len() + 1]; text.len() + 1];
for i in 0..(pattern.len().max(text.len()) + 1) {
if i <= text.len() {
matrix[text.len() - i][pattern.len()] = i;
}
if i <= pattern.len() {
matrix[text.len()][pattern.len() - i] = i;
}
}
for i in (0..text.len()).rev() {
for j in (0..pattern.len()).rev() {
matrix[i][j] = if text.as_bytes()[i] == pattern.as_bytes()[j] {
matrix[i + 1][j + 1]
} else {
matrix[i + 1][j + 1]
.min(matrix[i][j + 1])
.min(matrix[i + 1][j])
+ 1
}
}
}
matrix[0][0]
}

33
src/fuzzy_search/mod.rs Normal file
View File

@@ -0,0 +1,33 @@
use std::{
// io::stdout,
sync::{Arc, RwLock},
thread::{self, JoinHandle}, // time::Duration,
};
use self::fuzzy::fzs;
pub mod fuzzy;
pub fn test(
input: Arc<RwLock<(Vec<char>, String)>>,
result: Arc<RwLock<Vec<String>>>,
) -> JoinHandle<()> {
let list = [
"pear",
"tree",
"platypus",
"building",
"test",
"lime",
"stationary",
];
let mut list2 = list;
thread::spawn(move || {
let pattern = String::from_iter(input.read().unwrap().0.clone());
fzs(pattern.as_str(), &mut list2);
*result.write().unwrap() = list2.map(|x| x.to_string()).to_vec();
})
}

2
src/lib.rs Normal file
View File

@@ -0,0 +1,2 @@
pub mod fuzzy_search;
pub mod ui;

23
src/main.rs Normal file
View File

@@ -0,0 +1,23 @@
use fuzzy_search::ui::renderer;
fn main() {
// let list = [
// "pear",
// "tree",
// "platypus",
// "building",
// "test",
// "lime",
// "stationary",
// ];
// let mut result = list;
// fzs("aaa", &mut result);
// println!("{:?}", result);
renderer::start();
renderer::run();
renderer::stop();
}

126
src/ui/input.rs Normal file
View File

@@ -0,0 +1,126 @@
use std::{
// io::stdout,
sync::{Arc, RwLock},
thread,
};
use crossterm::{
self,
event::{read, KeyEvent},
};
pub fn start_get_input(
width: usize,
buffer: Arc<RwLock<(Vec<char>, String)>>,
cont_arc: Arc<RwLock<bool>>,
cursor_arc: Arc<RwLock<usize>>,
) {
thread::spawn(move || get_input(width, buffer, cont_arc, cursor_arc));
}
fn get_input(
width: usize,
buffer: Arc<RwLock<(Vec<char>, String)>>,
cont_arc: Arc<RwLock<bool>>,
cursor_arc: Arc<RwLock<usize>>,
) {
let mut input: Vec<char> = Vec::new();
let mut cursor: usize = 0;
let mut cont = true;
let mut window_offset: usize = 0;
while cont {
if let crossterm::event::Event::Key(ke) = read().unwrap() {
handle_ke(
ke,
&mut input,
&mut cursor,
&mut cont,
&mut window_offset,
width,
);
let text = String::from_iter(
&input.clone()[window_offset..(window_offset + width - 4).min(input.len())],
);
let spaces = String::from_iter(vec![' '; (width - 4) - text.chars().count()]);
buffer.write().unwrap().0 = input.clone();
buffer.write().unwrap().1 = format!("{}{}", text, spaces);
*cont_arc.write().unwrap() = cont;
*cursor_arc.write().unwrap() = cursor - window_offset;
}
}
}
fn handle_ke(
ke: KeyEvent,
input: &mut Vec<char>,
cursor: &mut usize,
cont: &mut bool,
window_offset: &mut usize,
width: usize,
) {
match ke.code {
crossterm::event::KeyCode::Esc => {
*cont = false;
}
crossterm::event::KeyCode::Backspace => {
if *cursor > 0 {
input.remove(*cursor - 1);
if *cursor > 0 {
if *cursor == *window_offset && *cursor > 0 {
*window_offset -= 1;
}
*cursor -= 1;
}
}
}
crossterm::event::KeyCode::Left => {
if *cursor > 0 {
if *cursor == *window_offset {
*window_offset -= 1;
}
*cursor -= 1;
}
}
crossterm::event::KeyCode::Right => {
if *cursor < input.len() {
if *cursor == *window_offset + (width - 4) {
*window_offset += 1;
}
*cursor += 1;
}
}
// crossterm::event::KeyCode::Up => todo!(),
// crossterm::event::KeyCode::Down => todo!(),
crossterm::event::KeyCode::Home => {
*cursor = 0;
*window_offset = 0;
}
crossterm::event::KeyCode::End => {
*cursor = input.len();
if *cursor > *window_offset + (width - 4) {
*window_offset = input.len() - (width - 4);
}
}
crossterm::event::KeyCode::Delete => {
if *cursor < input.len() {
input.remove(*cursor);
}
}
crossterm::event::KeyCode::Char(c) => {
input.insert(*cursor, c);
*cursor += 1;
if *cursor > width - 5 {
*window_offset += 1;
}
}
// crossterm::event::KeyCode::CapsLock => todo!(),
// crossterm::event::KeyCode::Modifier(_) => todo!(),
_ => {}
}
}

2
src/ui/mod.rs Normal file
View File

@@ -0,0 +1,2 @@
pub mod input;
pub mod renderer;

120
src/ui/renderer.rs Normal file
View File

@@ -0,0 +1,120 @@
use std::{
io::stdout,
sync::{Arc, RwLock},
thread::sleep,
time::Duration,
};
use crossterm::{
cursor::{MoveTo, Show},
execute, queue,
style::Print,
terminal,
terminal::{Clear, ClearType, EnterAlternateScreen, LeaveAlternateScreen},
};
use crate::fuzzy_search::test;
use super::input::start_get_input;
pub fn start() -> (usize, usize) {
let size = terminal::size().unwrap();
let width = size.0 as usize;
let height = size.1 as usize;
terminal::enable_raw_mode().unwrap();
queue!(
stdout(),
Clear(ClearType::All),
EnterAlternateScreen,
MoveTo(0, 0),
)
.unwrap();
print_frame(width, height);
execute!(stdout(), MoveTo(2, 1)).unwrap();
(width, height)
}
pub fn run() {
let size = terminal::size().unwrap();
let input: Arc<RwLock<(Vec<char>, String)>> =
Arc::new(RwLock::new((Vec::new(), String::new())));
let cont: Arc<RwLock<bool>> = Arc::new(RwLock::new(true));
let cursor: Arc<RwLock<usize>> = Arc::new(RwLock::new(0));
let results: Arc<RwLock<Vec<String>>> = Arc::new(RwLock::new(Vec::new()));
start_get_input(size.0 as usize, input.clone(), cont.clone(), cursor.clone());
let mut search_thread = test(input.clone(), results.clone());
let mut last_input: Vec<char> = Vec::new();
while cont.read().unwrap().to_owned() {
if input.read().unwrap().clone().0 != last_input && search_thread.is_finished() {
search_thread = test(input.clone(), results.clone());
}
for (i, res) in results.read().unwrap().iter().enumerate() {
queue!(stdout(), MoveTo(2, 3 + i as u16), Print(res)).unwrap();
}
queue!(
stdout(),
MoveTo(2, 1),
Print(input.read().unwrap().clone().1),
MoveTo(cursor.read().unwrap().to_owned() as u16 + 2, 1)
)
.unwrap();
execute!(stdout()).unwrap();
sleep(Duration::from_millis(1));
last_input = input.read().unwrap().0.to_owned();
}
}
pub fn stop() {
terminal::disable_raw_mode().unwrap();
execute!(stdout(), Show, LeaveAlternateScreen,).unwrap();
}
fn print_frame(width: usize, height: usize) {
let spaces = String::from_iter(vec![' '; width - 2]);
let lines = String::from_iter(vec!['─'; width - 2]);
queue!(
stdout(),
MoveTo(0, 0),
Print(""),
Print(lines.clone()),
Print(""),
MoveTo(0, 1),
Print(""),
Print(spaces.clone()),
Print(""),
MoveTo(0, 2),
Print(""),
Print(lines.clone()),
Print("")
)
.unwrap();
for i in 0..(height - 4) {
queue!(
stdout(),
MoveTo(0, i as u16 + 3),
Print(""),
Print(spaces.clone()),
Print("")
)
.unwrap();
}
queue!(stdout(), Print(""), Print(lines.clone()), Print("")).unwrap();
execute!(stdout()).unwrap();
}