|
3 | 3 | use std::{process::Command, thread}; |
4 | 4 |
|
5 | 5 | use arboard::Clipboard; |
| 6 | +use ignore::{DirEntry, WalkBuilder}; |
6 | 7 | use objc2_app_kit::NSWorkspace; |
7 | 8 | use objc2_foundation::NSURL; |
8 | 9 |
|
9 | | -use crate::{calculator::Expr, clipboard::ClipBoardContentType, config::Config}; |
| 10 | +use crate::{ |
| 11 | + app::{ |
| 12 | + ToApp, |
| 13 | + apps::{App, AppCommand}, |
| 14 | + }, |
| 15 | + calculator::Expr, |
| 16 | + clipboard::ClipBoardContentType, |
| 17 | + config::Config, |
| 18 | +}; |
10 | 19 |
|
11 | 20 | /// The different functions that rustcast can perform |
12 | 21 | #[derive(Debug, Clone, PartialEq)] |
@@ -106,3 +115,54 @@ impl Function { |
106 | 115 | } |
107 | 116 | } |
108 | 117 | } |
| 118 | + |
| 119 | +pub fn search(home: &str, name: &str) -> Vec<App> { |
| 120 | + let mut builder = WalkBuilder::new(home); |
| 121 | + builder.follow_links(false); |
| 122 | + builder.threads(10); |
| 123 | + |
| 124 | + let name_clone = name.to_string(); |
| 125 | + builder |
| 126 | + .build() |
| 127 | + .filter_map(move |x| { |
| 128 | + if let Ok(ent) = x { |
| 129 | + let name = ent.file_name().to_string_lossy(); |
| 130 | + if name.contains(&name_clone) && !name.starts_with(".") { |
| 131 | + Some(ent.to_app()) |
| 132 | + } else { |
| 133 | + None |
| 134 | + } |
| 135 | + } else { |
| 136 | + None |
| 137 | + } |
| 138 | + }) |
| 139 | + .take(400) |
| 140 | + .collect() |
| 141 | +} |
| 142 | + |
| 143 | +pub fn search_for_file(name: &str) -> Vec<App> { |
| 144 | + let home = std::env::var("HOME").unwrap_or_else(|_| "/".into()); |
| 145 | + |
| 146 | + search(&home, name) |
| 147 | +} |
| 148 | + |
| 149 | +impl ToApp for DirEntry { |
| 150 | + fn to_app(&self) -> App { |
| 151 | + let path = "~".to_string() |
| 152 | + + self |
| 153 | + .path() |
| 154 | + .to_str() |
| 155 | + .unwrap_or("") |
| 156 | + .to_string() |
| 157 | + .strip_prefix(&std::env::var("HOME").unwrap_or("".to_string())) |
| 158 | + .unwrap_or(""); |
| 159 | + App { |
| 160 | + ranking: 0, |
| 161 | + open_command: AppCommand::Function(Function::OpenApp(path.clone())), |
| 162 | + desc: path, |
| 163 | + icons: None, |
| 164 | + display_name: self.file_name().to_str().unwrap_or("").to_string(), |
| 165 | + search_name: "".to_string(), |
| 166 | + } |
| 167 | + } |
| 168 | +} |
0 commit comments