perga 0.1.5

A terminal Markdown browser with a persistent file tree, tabs, wiki-links and backlinks
Documentation
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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
//! Project-wide content search, built on `grep-searcher`.
//!
//! The search runs on its own thread and streams hits as it finds them, so the
//! first results appear while the rest of the vault is still being read. A new
//! query cancels the one in flight through an atomic flag the sink checks; the
//! thread then unwinds out of `grep`'s own iteration rather than being killed,
//! which is what keeps the thread count flat across a session of typing.

use std::path::{Path, PathBuf};
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;

use grep_matcher::Matcher;
use grep_regex::{RegexMatcher, RegexMatcherBuilder};
use grep_searcher::{sinks::UTF8, BinaryDetection, SearcherBuilder};

use crate::config::schema::{FilesConfig, SearchConfig};
use crate::vault::walker::{self, WalkOptions};

/// How many hits are collected before they are sent on.
const BATCH: usize = 32;

/// One matching line.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Hit {
    /// The file, relative to the vault root.
    pub path: PathBuf,
    /// The one-based line number.
    pub line: u64,
    /// The matching line, with trailing whitespace removed.
    pub text: String,
    /// The byte range of the match within [`Hit::text`].
    pub span: (usize, usize),
}

/// What the searcher reports.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum SearchEvent {
    /// A batch of hits, in the order the files were walked.
    Hits(Vec<Hit>),
    /// The search finished. `truncated` when it stopped at the result cap.
    Finished {
        /// How many hits were reported in total.
        total: usize,
        /// Whether the cap cut the results short.
        truncated: bool,
    },
    /// The pattern could not be compiled. Shown inline, never a panic.
    BadPattern(String),
}

/// A query as the reader wrote it.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Query {
    /// The pattern, with any `/…/` wrapper already removed.
    pub pattern: String,
    /// Whether the pattern is a regular expression.
    pub regex: bool,
}

impl Query {
    /// Read a query the way the prompt accepts it.
    ///
    /// A `/pattern/` wrapper turns on regex for that search alone, which is
    /// the shorthand Section 9.7 asks for; otherwise the configured default
    /// decides.
    pub fn parse(input: &str, config: &SearchConfig) -> Self {
        let trimmed = input.trim();

        if trimmed.len() >= 2 && trimmed.starts_with('/') && trimmed.ends_with('/') {
            return Query {
                pattern: trimmed[1..trimmed.len() - 1].to_string(),
                regex: true,
            };
        }

        Query {
            pattern: trimmed.to_string(),
            regex: config.regex,
        }
    }

    /// Build the matcher for this query.
    pub fn matcher(&self, config: &SearchConfig) -> Result<RegexMatcher, String> {
        RegexMatcherBuilder::new()
            // Smart case is `grep`'s own: lower case matches either, and an
            // upper-case character in the pattern means it.
            .case_smart(config.smart_case)
            .fixed_strings(!self.regex)
            .line_terminator(Some(b'\n'))
            .build(&self.pattern)
            .map_err(|e| format!("{e}"))
    }
}

/// A running search.
///
/// Dropping the handle cancels it, so the searcher for a query the reader has
/// already replaced stops reading files at the next line.
#[derive(Debug)]
pub struct SearchHandle {
    cancelled: Arc<AtomicBool>,
}

impl SearchHandle {
    /// Stop the search at the next line.
    pub fn cancel(&self) {
        self.cancelled.store(true, Ordering::Relaxed);
    }
}

impl Drop for SearchHandle {
    fn drop(&mut self) {
        self.cancel();
    }
}

/// Search `root` on a background thread, reporting to `sink`.
pub fn spawn(
    root: PathBuf,
    query: Query,
    search: SearchConfig,
    files: FilesConfig,
    walk: WalkOptions,
    sink: impl Fn(SearchEvent) + Send + Sync + 'static,
) -> SearchHandle {
    let cancelled = Arc::new(AtomicBool::new(false));
    let flag = Arc::clone(&cancelled);
    let sink = Arc::new(sink);
    let thread_sink = Arc::clone(&sink);

    let spawned = std::thread::Builder::new()
        .name("perga-search".to_string())
        .spawn(move || {
            run(&root, &query, &search, &files, walk, &flag, &*thread_sink);
        });

    if spawned.is_err() {
        sink(SearchEvent::Finished {
            total: 0,
            truncated: false,
        });
    }

    SearchHandle { cancelled }
}

/// The body of the search, factored out so the tests can run it synchronously.
pub fn run(
    root: &Path,
    query: &Query,
    search: &SearchConfig,
    files: &FilesConfig,
    walk: WalkOptions,
    cancelled: &AtomicBool,
    sink: &impl Fn(SearchEvent),
) {
    if query.pattern.is_empty() {
        sink(SearchEvent::Finished {
            total: 0,
            truncated: false,
        });
        return;
    }

    let matcher = match query.matcher(search) {
        Ok(matcher) => matcher,
        // An invalid regex is something the reader typed, not a failure:
        // it is reported inline and the previous results stay put.
        Err(e) => {
            sink(SearchEvent::BadPattern(e));
            return;
        }
    };

    let mut searcher = SearcherBuilder::new()
        .line_number(true)
        // A binary file in a notes vault is an attachment; matching inside one
        // produces unreadable lines rather than useful hits.
        .binary_detection(BinaryDetection::quit(0))
        .build();

    let mut batch: Vec<Hit> = Vec::with_capacity(BATCH);
    let mut total = 0usize;
    let mut truncated = false;

    // The same walk the tree uses, so search and tree agree about what is in
    // the vault — including the ignore rules.
    let paths = collect_paths(root, walk, files, search, cancelled);

    'files: for relative in paths {
        if cancelled.load(Ordering::Relaxed) {
            return;
        }

        let absolute = root.join(&relative);
        let mut hits: Vec<Hit> = Vec::new();

        let outcome = searcher.search_path(
            &matcher,
            &absolute,
            UTF8(|line, text| {
                let span = matcher
                    .find(text.as_bytes())
                    .ok()
                    .flatten()
                    .map_or((0, 0), |m| (m.start(), m.end()));

                hits.push(Hit {
                    path: relative.clone(),
                    line,
                    text: text.trim_end().to_string(),
                    span,
                });

                // Returning `false` stops this file; the cap and the
                // cancellation flag are both checked here so a search of a
                // huge file can be abandoned mid-file.
                Ok(!cancelled.load(Ordering::Relaxed) && total + hits.len() < search.max_results)
            }),
        );

        if let Err(e) = outcome {
            tracing::debug!("skipping {} while searching: {e}", absolute.display());
        }

        total += hits.len();
        batch.extend(hits);

        if total >= search.max_results {
            truncated = true;
        }

        if batch.len() >= BATCH || truncated {
            sink(SearchEvent::Hits(std::mem::take(&mut batch)));
            batch.reserve(BATCH);
        }

        if truncated {
            break 'files;
        }
    }

    if cancelled.load(Ordering::Relaxed) {
        return;
    }

    if !batch.is_empty() {
        sink(SearchEvent::Hits(batch));
    }

    sink(SearchEvent::Finished { total, truncated });
}

/// The files the search will read, in walk order.
fn collect_paths(
    root: &Path,
    walk: WalkOptions,
    files: &FilesConfig,
    search: &SearchConfig,
    cancelled: &AtomicBool,
) -> Vec<PathBuf> {
    let paths = std::sync::Mutex::new(Vec::new());

    walker::walk(root, walk, cancelled, &|event| {
        if let walker::WalkEvent::Entries(entries) = event {
            let mut paths = paths.lock().expect("the walk is the only writer");
            paths.extend(
                entries
                    .into_iter()
                    .filter(|entry| !entry.is_dir)
                    .filter(|entry| search.all_files || files.is_markdown(&entry.path))
                    .map(|entry| entry.path),
            );
        }
    });

    paths.into_inner().expect("the walk has finished")
}

#[cfg(test)]
mod tests {
    use super::*;

    use std::sync::Mutex;

    fn vault() -> PathBuf {
        Path::new(env!("CARGO_MANIFEST_DIR")).join("tests/fixtures/vault")
    }

    /// Run a search to completion and return everything it reported.
    fn search(pattern: &str, config: SearchConfig) -> (Vec<Hit>, Option<(usize, bool)>) {
        let hits = Mutex::new(Vec::new());
        let finished = Mutex::new(None);
        let query = Query::parse(pattern, &config);

        run(
            &vault(),
            &query,
            &config,
            &FilesConfig::default(),
            WalkOptions::default(),
            &AtomicBool::new(false),
            &|event| match event {
                SearchEvent::Hits(batch) => hits.lock().unwrap().extend(batch),
                SearchEvent::Finished { total, truncated } => {
                    *finished.lock().unwrap() = Some((total, truncated));
                }
                SearchEvent::BadPattern(e) => panic!("the pattern failed: {e}"),
            },
        );

        (hits.into_inner().unwrap(), finished.into_inner().unwrap())
    }

    #[test]
    fn a_literal_query_finds_its_lines() {
        let (hits, finished) = search("Bearer tokens", SearchConfig::default());

        assert_eq!(finished, Some((hits.len(), false)));
        assert!(hits
            .iter()
            .any(|hit| hit.path == Path::new("docs/api/auth.md")));

        let hit = &hits[0];
        assert!(hit.text.contains("Bearer tokens"));
        assert_eq!(&hit.text[hit.span.0..hit.span.1], "Bearer tokens");
        assert!(hit.line > 0);
    }

    #[test]
    fn a_literal_query_is_not_read_as_a_pattern() {
        // `.` and `*` would match everything if this were a regex.
        let (hits, _) = search("a.*b", SearchConfig::default());
        assert!(hits.is_empty(), "{hits:?}");
    }

    #[test]
    fn a_slash_wrapped_query_turns_on_regex_for_that_search() {
        let query = Query::parse("/tok[e]n/", &SearchConfig::default());
        assert!(query.regex);
        assert_eq!(query.pattern, "tok[e]n");

        let (hits, _) = search("/Bearer +tokens/", SearchConfig::default());
        assert!(!hits.is_empty());
    }

    #[test]
    fn smart_case_matches_either_case_until_the_query_says_otherwise() {
        let (lower, _) = search("bearer", SearchConfig::default());
        let (upper, _) = search("Bearer", SearchConfig::default());
        assert!(!lower.is_empty());
        assert_eq!(lower.len(), upper.len());

        // A pattern that only exists in lower case is not found in upper.
        let (missing, _) = search("BEARER", SearchConfig::default());
        assert!(missing.is_empty());
    }

    #[test]
    fn an_invalid_regex_is_reported_rather_than_panicking() {
        let reported = Mutex::new(None);
        let config = SearchConfig {
            regex: true,
            ..SearchConfig::default()
        };

        run(
            &vault(),
            &Query::parse("[unclosed", &config),
            &config,
            &FilesConfig::default(),
            WalkOptions::default(),
            &AtomicBool::new(false),
            &|event| {
                if let SearchEvent::BadPattern(e) = event {
                    *reported.lock().unwrap() = Some(e);
                }
            },
        );

        assert!(reported.into_inner().unwrap().is_some());
    }

    #[test]
    fn the_result_cap_truncates_rather_than_running_on() {
        let config = SearchConfig {
            max_results: 3,
            ..SearchConfig::default()
        };
        let (hits, finished) = search("e", config);

        assert!(hits.len() >= 3);
        assert_eq!(finished.map(|(_, truncated)| truncated), Some(true));
    }

    #[test]
    fn non_markdown_files_are_searched_only_when_asked_for() {
        // The fixture's `.gitignore` holds the word `notes`.
        let (markdown_only, _) = search("notes/", SearchConfig::default());
        assert!(!markdown_only
            .iter()
            .any(|hit| hit.path == Path::new(".gitignore")));

        let config = SearchConfig {
            all_files: true,
            ..SearchConfig::default()
        };
        let (everything, _) = search("notes/", config);
        assert!(everything
            .iter()
            .any(|hit| hit.path == Path::new(".gitignore")));
    }

    #[test]
    fn a_cancelled_search_stops_and_does_not_report_finishing() {
        let cancelled = AtomicBool::new(false);
        let finished = Mutex::new(false);
        let config = SearchConfig::default();

        run(
            &vault(),
            &Query::parse("e", &config),
            &config,
            &FilesConfig::default(),
            WalkOptions::default(),
            &cancelled,
            &|event| match event {
                SearchEvent::Hits(_) => cancelled.store(true, Ordering::Relaxed),
                SearchEvent::Finished { .. } => *finished.lock().unwrap() = true,
                SearchEvent::BadPattern(_) => {}
            },
        );

        assert!(!finished.into_inner().unwrap());
    }

    #[test]
    fn an_empty_query_finishes_without_reading_anything() {
        let (hits, finished) = search("   ", SearchConfig::default());
        assert!(hits.is_empty());
        assert_eq!(finished, Some((0, false)));
    }
}