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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
//! # superwhich
//!
//! Smart `which` alternative that uses [Jaro-Winkler distance](https://en.wikipedia.org/wiki/Jaro%E2%80%93Winkler_distance) to calculate the similarity between the provided pattern and the executable filenames.
//!
//! ## MSRV
//!
//! | Version | Edition | MSRV |
//! | --- | --- | --- |
//! | 3.x.y | 2024 | 1.85 |
//! | 2.x.y | 2021 | 1.80 |
//! | 1.x.y | 2021 | N/A |
//!
//! ## Installation
//!
//! ```bash
//! cargo add superwhich
//! ```
//!
//! Or add this to your `Cargo.toml` file:
//!
//! ```toml
//! [dependencies]
//! superwhich = "3.0.1"
//! ```
//!
//! ## Usage
//!
//! ```rust,no_run
//! use rayon::iter::{ParallelBridge, ParallelIterator};
//! use std::{
//! env::{split_paths, var_os},
//! path::Path,
//! sync::Arc,
//! };
//! use superwhich::{
//! SearchCtx, colored::Color, crossbeam::channel::unbounded, find_files, par_find_files,
//! };
//!
//! // construct the context needed for the search
//! let ctx = Arc::new(
//! SearchCtx::new("GURE".to_string())
//! .threshold(0.7)
//! .color(Color::Red),
//! );
//! let (tx, rx) = unbounded(); // channel to send found paths in
//!
//! // run the search directly with a singular path
//! let path = Path::new("path/to/search");
//! par_find_files(path, &ctx, &tx);
//!
//! // or run the search inside a parallel iterator
//! split_paths(&var_os("PATH").expect("PATH is not set"))
//! .par_bridge()
//! .for_each(|path| {
//! find_files(&path, &ctx, &tx);
//! });
//!
//! // DO NOT run the parallel function inside a parallel iterator, it actually slows down the search considerably
//!
//! // print the found paths (they're String)
//! while let Ok(path) = rx.try_recv() {
//! println!("{}", path);
//! }
//! ```
//!
//! ## Benchmarks
//!
//! ```text
//! Timer precision: 100 ns
//! str fastest │ slowest │ median │ mean │ samples │ iters
//! ├─ fast lowercase 2.554 ms │ 3.497 ms │ 2.666 ms │ 2.74 ms │ 100 │ 100
//! │ 39.14 Mitem/s │ 28.59 Mitem/s │ 37.5 Mitem/s │ 36.49 Mitem/s │ │
//! │ max alloc: │ │ │ │ │
//! │ 1 │ 1 │ 1 │ 1 │ │
//! │ 2.4 MB │ 2.4 MB │ 2.4 MB │ 2.4 MB │ │
//! │ alloc: │ │ │ │ │
//! │ 1 │ 1 │ 1 │ 1 │ │
//! │ 2.4 MB │ 2.4 MB │ 2.4 MB │ 2.4 MB │ │
//! ╰─ std lowercase 4.877 ms │ 7.723 ms │ 5.288 ms │ 5.456 ms │ 100 │ 100
//! 20.5 Mitem/s │ 12.94 Mitem/s │ 18.91 Mitem/s │ 18.32 Mitem/s │ │
//! max alloc: │ │ │ │ │
//! 100001 │ 100001 │ 100001 │ 100001 │ │
//! 4.9 MB │ 4.9 MB │ 4.9 MB │ 4.9 MB │ │
//! alloc: │ │ │ │ │
//! 100001 │ 100001 │ 100001 │ 100001 │ │
//! 4.9 MB │ 4.9 MB │ 4.9 MB │ 4.9 MB │ │
//! ```
//!
//! ```text
//! Timer precision: 100 ns
//! swhich fastest │ slowest │ median │ mean │ samples │ iters
//! ├─ find_files (1000 files) 505.7 µs │ 1.263 ms │ 514.7 µs │ 548.2 µs │ 100 │ 100
//! │ 1.977 Mitem/s │ 791.4 Kitem/s │ 1.942 Mitem/s │ 1.824 Mitem/s │ │
//! │ max alloc: │ │ │ │ │
//! │ 4 │ 4 │ 4 │ 4 │ │
//! │ 289 B │ 289 B │ 289 B │ 289 B │ │
//! │ alloc: │ │ │ │ │
//! │ 1004 │ 1004 │ 1004 │ 1004 │ │
//! │ 12.13 KB │ 12.13 KB │ 12.13 KB │ 12.13 KB │ │
//! │ dealloc: │ │ │ │ │
//! │ 1004 │ 1004 │ 1004 │ 1004 │ │
//! │ 12.18 KB │ 12.18 KB │ 12.18 KB │ 12.18 KB │ │
//! │ grow: │ │ │ │ │
//! │ 1 │ 1 │ 1 │ 1 │ │
//! │ 47 B │ 47 B │ 47 B │ 47 B │ │
//! ╰─ par_find_files (1000 files) 1.007 ms │ 2.26 ms │ 1.266 ms │ 1.321 ms │ 100 │ 100
//! 992.1 Kitem/s │ 442.4 Kitem/s │ 789.6 Kitem/s │ 756.6 Kitem/s │ │
//! max alloc: │ │ │ │ │
//! 4 │ 4 │ 4 │ 5.49 │ │
//! 289 B │ 289 B │ 289 B │ 852 B │ │
//! alloc: │ │ │ │ │
//! 5 │ 5 │ 5 │ 6.56 │ │
//! 258 B │ 258 B │ 258 B │ 828.3 B │ │
//! dealloc: │ │ │ │ │
//! 3 │ 3 │ 3 │ 3.06 │ │
//! 210 B │ 210 B │ 210 B │ 221.2 B │ │
//! grow: │ │ │ │ │
//! 1 │ 1 │ 1 │ 1 │ │
//! 47 B │ 47 B │ 47 B │ 47 B │ │
//! ```
/*
* superwhich: smart which alternative
* Copyright (C) 2024 `DarkCeptor44`
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
pub use colored;
pub use crossbeam;
use ;
use Sender;
use jaro_winkler;
use ;
use Cow;
use ;
/// Search context. This holds the search pattern, threshold needed for the search, and the color for the highlighted text.
/// Must be wrapped in an [Arc] to be shared between threads
/// Trait providing fast lowercase conversion
/// Find files within the specified path that match the pattern in the context
///
/// It just looks at the filename, it doesn't check for file permissions, types or anything
///
/// ## Arguments
///
/// * `path` - Path to search
/// * `ctx` - Search context
/// * `tx` - Channel to send found paths
///
/// ## Examples
///
/// ```rust,no_run
/// use rayon::iter::{ParallelBridge, ParallelIterator};
/// use std::{
/// env::{split_paths, var_os},
/// path::PathBuf,
/// sync::Arc,
/// };
/// use superwhich::{SearchCtx, colored::Color, crossbeam::channel::unbounded, find_files};
///
/// let ctx = Arc::new(
/// SearchCtx::new("GURE".to_string())
/// .threshold(0.7)
/// .color(Color::Red),
/// );
/// let (tx, rx) = unbounded();
///
/// split_paths(&var_os("PATH").expect("PATH is not set"))
/// .par_bridge()
/// .for_each(|path: PathBuf| {
/// find_files(&path, &ctx, &tx);
/// });
///
/// while let Ok(path) = rx.try_recv() {
/// println!("{}", path);
/// }
/// ```
/// Find files within the specified path that match the pattern in the context, in parallel
///
/// It just looks at the filename, it doesn't check for file permissions, types or anything
///
/// ## Arguments
///
/// * `path` - Path to search
/// * `ctx` - Search context
/// * `tx` - Channel to send found paths
///
/// ## Examples
///
/// ```rust,no_run
/// use std::{path::Path, sync::Arc};
/// use superwhich::{SearchCtx, colored::Color, crossbeam::channel::unbounded, par_find_files};
///
/// let ctx = Arc::new(
/// SearchCtx::new("GURE".to_string())
/// .threshold(0.7)
/// .color(Color::Red),
/// );
/// let (tx, rx) = unbounded();
/// let path = Path::new("path/to/search");
///
/// par_find_files(path, &ctx, &tx);
///
/// while let Ok(path) = rx.try_recv() {
/// println!("{}", path);
/// }
/// ```