topos-bible-cli 0.4.0

Find Bible references in files, like ripgrep
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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
use std::{fs, path::PathBuf};

use crate::{complete, search::WalkOptions};

use clap::{Parser, ValueEnum, ValueHint};
use clap_complete::engine::ArgValueCompleter;
use topos_bible::{
    data::{
        bible_data::{BibleData, BibleDataInput},
        patch::DataPatch,
    },
    error::AnyResult,
    filter::{
        bible_filter::BibleFilter,
        filters::{book::BookFilter, genre::GenreFilter, testament::TestamentFilter},
    },
    matcher::{BibleMatcher, context::BookContext},
    segments::formatter::{BookStyle, FormatOptions},
};

/**
Find Bible references in files, directories, text, or stdin.

- Including a testament limits the search to it: `--nt -g Gospels` is the four Gospels
- Included genres and books add up: `-g Pentateuch -b Revelation` is six books
- Exclusions always win, so a book can be excluded from an included genre
- Passage filters that keep references (`-i`, `--any-overlap`, `-o`, `--exact-overlap`) are joined
  with a logical OR, then `--exclude-overlap` removes references
*/
#[derive(Parser, Debug)]
#[command(
    name = "topos",
    version,
    about = "Find Bible references, like ripgrep",
    verbatim_doc_comment,
    // Options from the config file come first, so the command line overrides them
    args_override_self = true
)]
pub struct Args {
    /// Files or directories to search (respecting .gitignore); defaults to stdin when piped,
    /// otherwise the current directory
    #[arg(value_hint = ValueHint::AnyPath)]
    pub paths: Vec<PathBuf>,

    /// Search this text instead of files
    #[arg(long, conflicts_with = "paths")]
    pub text: Option<String>,

    /// Include books from a testament (old/new)
    #[arg(long = "testament", short = 't', add = ArgValueCompleter::new(complete::testaments))]
    pub testaments: Vec<TestamentFilter>,

    /// Include the New Testament (same as `-t new`)
    #[arg(long)]
    pub nt: bool,

    /// Include the Old Testament (same as `-t old`)
    #[arg(long)]
    pub ot: bool,

    /// Exclude books from a testament
    #[arg(long = "exclude-testament", add = ArgValueCompleter::new(complete::testaments))]
    pub exclude_testaments: Vec<TestamentFilter>,

    /// Include books of a genre (e.g. epistles, gospels)
    #[arg(long = "genre", short = 'g', add = ArgValueCompleter::new(complete::genres))]
    pub genres: Vec<String>,

    /// Exclude books of a genre
    #[arg(long = "exclude-genre", add = ArgValueCompleter::new(complete::genres))]
    pub exclude_genres: Vec<String>,

    /// Include a book (e.g. John)
    #[arg(long = "book", short = 'b', add = ArgValueCompleter::new(complete::books))]
    pub books: Vec<String>,

    /// Exclude a book
    #[arg(long = "exclude-book", add = ArgValueCompleter::new(complete::books))]
    pub exclude_books: Vec<String>,

    /// Only keep references entirely inside this passage (e.g. "John 1" keeps John 1:2-3)
    #[arg(long = "inside", short = 'i', add = ArgValueCompleter::new(complete::passages))]
    pub inside: Vec<String>,

    /// Only keep references that share any verse with this passage, whole chapters included
    /// (e.g. "John 3:16" keeps John 3 and John 3:14-18)
    #[arg(
        long = "any-overlap",
        alias = "overlaps",
        add = ArgValueCompleter::new(complete::passages)
    )]
    pub any_overlap: Vec<String>,

    /// Only keep references that name a verse of this passage: whole chapters don't count
    /// (e.g. "John 3:16" keeps John 3:14-18 and John 2; 3:16, but not John 3)
    #[arg(
        long = "explicit-overlap",
        short = 'o',
        add = ArgValueCompleter::new(complete::passages)
    )]
    pub explicit_overlap: Vec<String>,

    /// Only keep references that are exactly this passage, however they are written (e.g.
    /// "John 3:16-18" keeps Jn 3:16-18 and John 3:16, 17-18, but not John 3:16-17)
    #[arg(long = "exact-overlap", add = ArgValueCompleter::new(complete::passages))]
    pub exact_overlap: Vec<String>,

    /// Drop references that share any verse with this passage
    #[arg(
        long = "exclude-overlap",
        alias = "outside",
        add = ArgValueCompleter::new(complete::passages)
    )]
    pub exclude_overlap: Vec<String>,

    /// Treat the input as being about this book, so references like 3:16 match
    #[arg(long, add = ArgValueCompleter::new(complete::books))]
    pub context_book: Option<String>,

    /// Lines matching this pattern set the book for references after them, like '^#+ {book}$'
    #[arg(long, conflicts_with = "context_book")]
    pub context_heading: Option<String>,

    /// A JSON file with custom books, genres, or chapter and verse counts
    #[arg(long, value_hint = ValueHint::FilePath)]
    pub data: Option<PathBuf>,

    /// A JSON file (like --data, every field optional) whose names and values are added to the
    /// data, keeping the defaults: new abbreviations, books, genres, or chapter counts
    #[arg(long, value_name = "PATH", value_hint = ValueHint::FilePath)]
    pub merge_data: Vec<PathBuf>,

    /// A JSON file (like --data, every field optional) whose values are removed from the data: a
    /// book or genre listed alone is removed entirely, otherwise just the values listed
    #[arg(long, value_name = "PATH", value_hint = ValueHint::FilePath)]
    pub remove_data: Vec<PathBuf>,

    /// Use a named query from ~/.config/topos/queries.toml (its options go where this is)
    #[arg(long, short = 'q', value_name = "NAME", add = ArgValueCompleter::new(complete::queries))]
    pub query: Vec<String>,

    /// List the named queries and exit
    #[arg(long)]
    pub list_queries: bool,

    /// Read default options from this file instead of ~/.config/topos/config.toml
    #[arg(long, value_name = "PATH", conflicts_with = "no_config", value_hint = ValueHint::FilePath)]
    pub config: Option<PathBuf>,

    /// Do not read default options from a config file
    #[arg(long)]
    pub no_config: bool,

    /// Print only the total number of matches across all files (same as `-m total-count`)
    #[arg(long)]
    pub total_count: bool,

    /// How to print results
    #[arg(long, short = 'm', value_enum, default_value_t)]
    pub mode: OutputMode,

    /// How to write each reference's book (also used by --complete); overrides --psg-fmt's
    /// `book` [default: name]
    #[arg(long, short = 'f', value_enum)]
    pub format: Option<ReferenceFormat>,

    /// How to write references (results and completions), as JSON with any of: book,
    /// book_separator, chapter_verse, range, verse_separator, chapter_separator,
    /// omit_first_verse_of_chapter_range, join_adjacent, chapter_in_single_chapter_books. In
    /// config.toml it is a table: psg-fmt = { join_adjacent = true }
    #[arg(long, value_name = "JSON", value_parser = parse_format)]
    pub psg_fmt: Option<FormatOptions>,

    /// Between the book and its chapters [default: " "]
    #[arg(long, value_name = "TEXT")]
    pub fmt_book_separator: Option<String>,

    /// Between a chapter and a verse [default: ":"]
    #[arg(long, value_name = "TEXT")]
    pub fmt_chapter_verse: Option<String>,

    /// Between the ends of a range [default: "-"]
    #[arg(long, value_name = "TEXT")]
    pub fmt_range: Option<String>,

    /// Before another verse in the same chapter [default: ","]
    #[arg(long, value_name = "TEXT")]
    pub fmt_verse_separator: Option<String>,

    /// Before a part in another chapter [default: "; "]
    #[arg(long, value_name = "TEXT")]
    pub fmt_chapter_separator: Option<String>,

    /// Write adjacent verses as a range: 3:16-18 instead of 3:16,17,18 [default: false]
    #[arg(long, value_name = "BOOL", num_args = 0..=1, default_missing_value = "true")]
    pub fmt_join_adjacent: Option<bool>,

    /// Write a range from a chapter's first verse as 1-2:3 instead of 1:1-2:3 [default: false]
    #[arg(long, value_name = "BOOL", num_args = 0..=1, default_missing_value = "true")]
    pub fmt_omit_first_verse_of_chapter_range: Option<bool>,

    /// Write the chapter in single-chapter books: Jude 1:5 instead of Jude 5 [default: true]
    #[arg(long, value_name = "BOOL", num_args = 0..=1, default_missing_value = "true")]
    pub fmt_chapter_in_single_chapter_books: Option<bool>,

    /// Lines of context to show after each match
    #[arg(long, short = 'A', default_value_t = 0)]
    pub after_context: usize,

    /// Lines of context to show before each match
    #[arg(long, short = 'B', default_value_t = 0)]
    pub before_context: usize,

    /// Lines of context to show before and after each match
    #[arg(long, short = 'C')]
    pub context: Option<usize>,

    /// When to use colors
    #[arg(long, value_enum, default_value_t)]
    pub color: ColorChoice,

    /// Print results sorted by path (waits for the whole search)
    #[arg(long)]
    pub sort: bool,

    /// Reuse results for files that have not changed (stored unfiltered, so any filters can use them)
    #[arg(long, overrides_with = "no_cache")]
    pub cache: bool,

    /// Don't use the cache, even if the config turns it on
    #[arg(long, overrides_with = "cache")]
    pub no_cache: bool,

    /// Print the completions for a partly typed reference, one per line, and exit: books for ""
    /// (or nothing), then chapters, verses, and range ends ("John 3:" gives John 3:1, ...).
    /// Uses -f for the book style and the book filters; -m json prints objects
    #[arg(
        long,
        value_name = "TEXT",
        num_args = 0..=1,
        default_missing_value = "",
        add = ArgValueCompleter::new(complete::passages)
    )]
    pub complete: Option<String>,

    /// Print every book, one per line, and exit (the same as `--complete ""`)
    #[arg(long)]
    pub list_books: bool,

    /// Delete the cache (in ~/.cache/topos) and exit
    #[arg(long)]
    pub clear_cache: bool,

    /// Only search files with these extensions when walking directories (e.g. md,txt); files
    /// named on the command line are always searched
    #[arg(long = "ext", value_name = "EXT", value_delimiter = ',')]
    pub extensions: Vec<String>,

    /// Don't search files with these extensions when walking directories (e.g. pdf,epub)
    #[arg(long, value_name = "EXT", value_delimiter = ',')]
    pub exclude_ext: Vec<String>,

    /// Include or exclude paths matching a glob (`!` excludes), like `--glob '*.md' --glob
    /// '!drafts/**'`; can be repeated. Like ripgrep, globs override ignore files and --hidden
    #[arg(long, value_name = "GLOB")]
    pub glob: Vec<String>,

    /// Like --glob, ignoring case (when a path matches both, the --iglob wins)
    #[arg(long, value_name = "GLOB")]
    pub iglob: Vec<String>,

    /// Search hidden files and directories (names starting with `.`)
    #[arg(long, short = '.')]
    pub hidden: bool,

    /// Don't respect ignore files (.gitignore, .ignore, .toposignore, and Git's global and
    /// exclude files)
    #[arg(long)]
    pub no_ignore: bool,

    /// Don't respect Git's ignore files (.gitignore, the global gitignore, .git/info/exclude)
    #[arg(long)]
    pub no_ignore_vcs: bool,

    /// Don't respect ignore files in parent directories
    #[arg(long)]
    pub no_ignore_parent: bool,

    /// Respect .gitignore files outside of Git repositories too
    #[arg(long)]
    pub no_require_git: bool,

    /// Also ignore the paths in this file (gitignore syntax); can be repeated
    #[arg(long, value_name = "PATH", value_hint = ValueHint::FilePath)]
    pub ignore_file: Vec<PathBuf>,

    /// Search more: -u is --no-ignore, -uu adds --hidden, -uuu adds --binary
    #[arg(long, short = 'u', action = clap::ArgAction::Count)]
    pub unrestricted: u8,

    /// Search files that look binary as text (normally they are skipped)
    #[arg(long)]
    pub binary: bool,

    /// Follow symbolic links
    #[arg(long, short = 'L')]
    pub follow: bool,

    /// Descend at most this many directories below the paths given (0 searches only them)
    #[arg(long, short = 'd', value_name = "NUM")]
    pub max_depth: Option<usize>,

    /// Skip files larger than this, like 500K, 10M, or 1G
    #[arg(long, value_name = "SIZE", value_parser = parse_size)]
    pub max_filesize: Option<u64>,

    /// Don't cross into other file systems (like mounted drives)
    #[arg(long)]
    pub one_file_system: bool,

    /// Print the files that would be searched, without searching them
    #[arg(long)]
    pub files: bool,

    /// Print only the paths of files with references
    #[arg(long, short = 'l', conflicts_with = "files_without_match")]
    pub files_with_matches: bool,

    /// Print only the paths of searched files without references
    #[arg(long)]
    pub files_without_match: bool,
}

#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, ValueEnum)]
pub enum OutputMode {
    /// Grouped by file on a terminal, otherwise `path:line:column: reference`
    #[default]
    Auto,
    /// Grouped by file, with optional context lines
    Grouped,
    /// `path:line:column: reference` (for Vim's quickfix list)
    #[value(alias = "qf", alias = "vimgrep")]
    Quickfix,
    /// A Markdown table
    #[value(alias = "t")]
    Table,
    /// One JSON object per match
    #[value(alias = "j")]
    Json,
    /// Matches per file
    #[value(alias = "c")]
    Count,
    /// Matches across all files
    TotalCount,
}

#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, ValueEnum)]
pub enum ReferenceFormat {
    /// `Genesis 1:1`
    #[default]
    Name,
    /// `Gn 1:1`
    #[value(alias = "abbrev")]
    Abbreviation,
    /// `Gen.1.1`
    Osis,
}

#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, ValueEnum)]
pub enum ColorChoice {
    #[default]
    Auto,
    Always,
    Never,
}

/// `--psg-fmt`: format options as JSON, every field optional
fn parse_format(json: &str) -> Result<FormatOptions, String> {
    serde_json::from_str(json).map_err(|e| format!("{e} (see --help for the fields)"))
}

/// `500`, `500K`, `10M`, or `1G` (powers of 1024)
fn parse_size(text: &str) -> Result<u64, String> {
    let text = text.trim();
    let invalid = || format!("`{text}` isn't a size like 500K, 10M, or 1G");
    let split = text
        .find(|c: char| c.is_ascii_alphabetic())
        .unwrap_or(text.len());
    let (number, unit) = text.split_at(split);
    let number: u64 = number.trim().parse().map_err(|_| invalid())?;
    let scale: u64 = match unit.to_ascii_uppercase().as_str() {
        "" | "B" => 1,
        "K" | "KB" => 1 << 10,
        "M" | "MB" => 1 << 20,
        "G" | "GB" => 1 << 30,
        _ => return Err(invalid()),
    };
    Ok(number.saturating_mul(scale))
}

impl Args {
    /// How directories are walked, with -u applied
    pub fn walk_options(&self) -> Result<WalkOptions, String> {
        let u = self.unrestricted;
        let mut overrides = None;
        if !self.glob.is_empty() || !self.iglob.is_empty() {
            let root = std::env::current_dir().map_err(|e| e.to_string())?;
            let mut builder = ignore::overrides::OverrideBuilder::new(root);
            for glob in &self.glob {
                builder
                    .add(glob)
                    .map_err(|e| format!("--glob {glob}: {e}"))?;
            }
            builder.case_insensitive(true).map_err(|e| e.to_string())?;
            for glob in &self.iglob {
                builder
                    .add(glob)
                    .map_err(|e| format!("--iglob {glob}: {e}"))?;
            }
            overrides = Some(builder.build().map_err(|e| e.to_string())?);
        }
        let lower = |list: &[String]| -> Vec<String> {
            list.iter()
                .map(|ext| ext.trim().trim_start_matches('.').to_ascii_lowercase())
                .filter(|ext| !ext.is_empty())
                .collect()
        };
        Ok(WalkOptions {
            hidden: self.hidden || u >= 2,
            no_ignore: self.no_ignore || u >= 1,
            no_ignore_vcs: self.no_ignore_vcs,
            no_ignore_parent: self.no_ignore_parent,
            no_require_git: self.no_require_git,
            ignore_files: self.ignore_file.clone(),
            follow: self.follow,
            max_depth: self.max_depth,
            max_filesize: self.max_filesize,
            one_file_system: self.one_file_system,
            overrides,
            extensions: lower(&self.extensions),
            exclude_extensions: lower(&self.exclude_ext),
        })
    }

    /// Whether files that look binary are searched (`--binary` or `-uuu`)
    pub fn search_binary(&self) -> bool {
        self.binary || self.unrestricted >= 3
    }

    /// Every option that changes which references are found before filtering (for the cache,
    /// which stores unfiltered results, so filters are left out)
    pub fn fingerprint(&self) -> String {
        let contents = |paths: &[PathBuf]| -> Vec<Option<String>> {
            paths
                .iter()
                .map(|path| fs::read_to_string(path).ok())
                .collect()
        };
        let data = contents(self.data.as_slice());
        let merge = contents(&self.merge_data);
        let remove = contents(&self.remove_data);
        // Binary files are cached as having no references unless they are searched
        let binary = self.search_binary();
        format!(
            "{:?}",
            (
                &self.context_book,
                &self.context_heading,
                data,
                merge,
                remove,
                binary
            )
        )
    }

    pub fn context_lines(&self) -> (usize, usize) {
        match self.context {
            Some(lines) => (
                lines.max(self.before_context),
                lines.max(self.after_context),
            ),
            None => (self.before_context, self.after_context),
        }
    }

    /// How references are written: the defaults, then --psg-fmt, then each --fmt-* option, then -f
    /// (OSIS references are written by [`Passage::to_osis`] instead)
    pub fn format_options(&self) -> FormatOptions {
        let mut options = self.psg_fmt.clone().unwrap_or_default();
        let text = |value: &Option<String>, field: &mut String| {
            if let Some(value) = value {
                *field = value.clone();
            }
        };
        text(&self.fmt_book_separator, &mut options.book_separator);
        text(&self.fmt_chapter_verse, &mut options.chapter_verse);
        text(&self.fmt_range, &mut options.range);
        text(&self.fmt_verse_separator, &mut options.verse_separator);
        text(&self.fmt_chapter_separator, &mut options.chapter_separator);
        if let Some(join) = self.fmt_join_adjacent {
            options.join_adjacent = join;
        }
        if let Some(omit) = self.fmt_omit_first_verse_of_chapter_range {
            options.omit_first_verse_of_chapter_range = omit;
        }
        if let Some(chapter) = self.fmt_chapter_in_single_chapter_books {
            options.chapter_in_single_chapter_books = chapter;
        }
        if let Some(format) = self.format {
            options.book = match format {
                ReferenceFormat::Name => BookStyle::Name,
                ReferenceFormat::Abbreviation => BookStyle::Abbreviation,
                ReferenceFormat::Osis => BookStyle::Osis,
            };
        }
        options
    }

    /// `--data` (or the defaults), then each `--merge-data`, then each `--remove-data`
    fn data(&self) -> AnyResult<BibleData> {
        fn read<T: serde::de::DeserializeOwned>(path: &PathBuf) -> AnyResult<T> {
            let text = fs::read_to_string(path).map_err(|e| format!("{}: {e}", path.display()))?;
            Ok(serde_json::from_str(&text).map_err(|e| format!("{}: {e}", path.display()))?)
        }
        if self.data.is_none() && self.merge_data.is_empty() && self.remove_data.is_empty() {
            return Ok(BibleData::default());
        }
        let mut input = match &self.data {
            Some(path) => read::<BibleDataInput>(path)?,
            None => BibleDataInput::defaults(),
        };
        for path in &self.merge_data {
            input
                .merge(read::<DataPatch>(path)?)
                .map_err(|e| format!("{}: {e}", path.display()))?;
        }
        for path in &self.remove_data {
            input
                .remove(read::<DataPatch>(path)?)
                .map_err(|e| format!("{}: {e}", path.display()))?;
        }
        Ok(BibleData::new(input)?)
    }

    pub fn matcher(&self) -> AnyResult<BibleMatcher> {
        let data = self.data()?;
        let mut filter = BibleFilter::new(data);
        filter.include_many(self.testaments.iter().copied())?;
        if self.nt {
            filter.include(TestamentFilter::New)?;
        }
        if self.ot {
            filter.include(TestamentFilter::Old)?;
        }
        filter.include_many(self.genres.iter().map(GenreFilter::new))?;
        filter.include_many(self.books.iter().map(BookFilter::new))?;
        filter.exclude_many(self.exclude_testaments.iter().copied())?;
        filter.exclude_many(self.exclude_genres.iter().map(GenreFilter::new))?;
        filter.exclude_many(self.exclude_books.iter().map(BookFilter::new))?;
        for passage in &self.inside {
            filter.filter_inside(passage)?;
        }
        for passage in &self.any_overlap {
            filter.filter_any_overlap(passage)?;
        }
        for passage in &self.explicit_overlap {
            filter.filter_explicit_overlap(passage)?;
        }
        for passage in &self.exact_overlap {
            filter.filter_exact_overlap(passage)?;
        }
        for passage in &self.exclude_overlap {
            filter.filter_exclude_overlap(passage)?;
        }

        if let Some(reason) = filter.contradiction() {
            eprintln!("topos: warning: {reason}, so nothing can match");
        }
        let matcher = filter.create_matcher();
        let books = matcher.data().books();
        let context = match (&self.context_book, &self.context_heading) {
            (Some(book), _) => Some(BookContext::Book(
                books
                    .search(book)
                    .ok_or_else(|| format!("unknown book {book:?}"))?,
            )),
            (None, Some(pattern)) => Some(BookContext::headings(books, pattern)?),
            (None, None) => None,
        };
        Ok(match context {
            Some(context) => matcher.with_context(context),
            None => matcher,
        })
    }
}