Skip to content

Commit 493dee1

Browse files
authored
Merge pull request #188 from unsecretised/file-searching
Add file searching using ignore
2 parents a8f1fc2 + 11e5ac1 commit 493dee1

File tree

7 files changed

+122
-6
lines changed

7 files changed

+122
-6
lines changed

Cargo.lock

Lines changed: 40 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ emojis = "0.8.0"
1515
global-hotkey = "0.7.0"
1616
iced = { version = "0.14.0", features = ["image", "tokio"] }
1717
icns = "0.3.1"
18+
ignore = "0.4.25"
1819
image = "0.25.9"
1920
libc = "0.2.180"
2021
log = "0.4.29"

src/app.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ pub const RUSTCAST_DESC_NAME: &str = "Utility";
2525
#[derive(Debug, Clone, PartialEq)]
2626
pub enum Page {
2727
Main,
28+
FileSearch,
2829
ClipboardHistory,
2930
EmojiSearch,
3031
}

src/app/apps.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,14 @@ impl App {
132132
display_name: "Clipboard History".to_string(),
133133
search_name: "clipboard".to_string(),
134134
},
135+
App {
136+
ranking: 0,
137+
open_command: AppCommand::Message(Message::SwitchToPage(Page::FileSearch)),
138+
desc: RUSTCAST_DESC_NAME.to_string(),
139+
icons: icons.clone(),
140+
display_name: "Search for a file".to_string(),
141+
search_name: "file search".to_string(),
142+
},
135143
App {
136144
ranking: 0,
137145
open_command: AppCommand::Message(Message::ReloadConfig),

src/app/tile/elm.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,9 +137,8 @@ pub fn view(tile: &Tile, wid: window::Id) -> Element<'_, Message> {
137137
};
138138

139139
let results_count = match &tile.page {
140-
Page::Main => tile.results.len(),
140+
Page::Main | Page::EmojiSearch | Page::FileSearch => tile.results.len(),
141141
Page::ClipboardHistory => tile.clipboard_content.len(),
142-
Page::EmojiSearch => tile.results.len(),
143142
};
144143

145144
let height = if tile.page == Page::ClipboardHistory {

src/app/tile/update.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ use crate::app::tile::AppIndex;
2525
use crate::app::{Message, Page, tile::Tile};
2626
use crate::calculator::Expr;
2727
use crate::commands::Function;
28+
use crate::commands::search_for_file;
2829
use crate::config::Config;
2930
use crate::unit_conversion;
3031
use crate::utils::is_valid_url;
@@ -156,7 +157,7 @@ pub fn handle_update(tile: &mut Tile, message: Message) -> Task<Message> {
156157
};
157158

158159
let quantity = match tile.page {
159-
Page::Main => 66.5,
160+
Page::Main | Page::FileSearch => 66.5,
160161
Page::ClipboardHistory => 50.,
161162
Page::EmojiSearch => 5.,
162163
};
@@ -436,7 +437,9 @@ pub fn handle_update(tile: &mut Tile, message: Message) -> Task<Message> {
436437
};
437438
}
438439

439-
if tile.query_lc.is_empty() {
440+
if tile.query_lc.is_empty()
441+
|| (tile.query_lc.chars().count() < 2 && tile.page == Page::FileSearch)
442+
{
440443
tile.results = Vec::new();
441444
return zero_item_resize_task(id);
442445
};
@@ -505,7 +508,11 @@ pub fn handle_update(tile: &mut Tile, message: Message) -> Task<Message> {
505508
}
506509
}
507510

508-
tile.handle_search_query_changed();
511+
if tile.page != Page::FileSearch {
512+
tile.handle_search_query_changed();
513+
} else {
514+
tile.results = search_for_file(&tile.query_lc);
515+
}
509516

510517
if !tile.results.is_empty() {
511518
tile.results.par_sort_by_key(|x| -x.ranking);

src/commands.rs

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,19 @@
33
use std::{process::Command, thread};
44

55
use arboard::Clipboard;
6+
use ignore::{DirEntry, WalkBuilder};
67
use objc2_app_kit::NSWorkspace;
78
use objc2_foundation::NSURL;
89

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+
};
1019

1120
/// The different functions that rustcast can perform
1221
#[derive(Debug, Clone, PartialEq)]
@@ -106,3 +115,54 @@ impl Function {
106115
}
107116
}
108117
}
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

Comments
 (0)