sniplab 0.3.2

A filesystem-native snippet library and agent-friendly CLI, installed as `snip`
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
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
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
use clap::{Args, Parser, Subcommand, ValueEnum};
use std::path::PathBuf;

use snip::domain::{FolderFilter, SearchField};
use snip::sort::SortMode;

#[derive(Parser, Debug)]
#[command(
    name = "snip",
    version,
    about = "Filesystem-native snippets for humans, scripts, and AI agents"
)]
pub struct Cli {
    /// Library root. Falls back to SNIP_LIBRARY, ancestor discovery, then config.
    #[arg(long, global = true, env = "SNIP_LIBRARY")]
    pub library: Option<PathBuf>,

    /// Structured output mode for commands that return records.
    #[arg(long, global = true, value_enum)]
    pub output: Option<OutputMode>,

    /// Color policy for terminal preview.
    #[arg(long, global = true, value_enum)]
    pub color: Option<ColorMode>,

    #[command(subcommand)]
    pub command: Option<Command>,
}

#[derive(Clone, Copy, Debug, Eq, PartialEq, ValueEnum)]
pub enum OutputMode {
    Human,
    Json,
    Jsonl,
}

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

#[derive(Subcommand, Debug)]
pub enum Command {
    /// Open the interactive terminal browser.
    #[cfg(feature = "tui")]
    Tui,
    /// Inspect and modify ~/.config/snip/config.toml.
    Config(ConfigArgs),
    /// Create a new filesystem snippet library.
    Init(InitArgs),
    /// Show library metadata and counts.
    Info,
    /// List snippets without their full content.
    List(FilterArgs),
    /// Search titles, tags, notes, and fragment content.
    Search(SearchArgs),
    /// Show a complete snippet.
    Show(SelectorArgs),
    /// Print one fragment with no decorations.
    Cat(FragmentSelectorArgs),
    /// Render a snippet for terminal or HTML preview.
    Preview(PreviewArgs),
    /// Print a managed filesystem path.
    Path(PathArgs),
    /// Open a managed path in an external application.
    Open(OpenArgs),
    /// Create a snippet.
    Create(CreateArgs),
    /// Modify a snippet or launch an external editor.
    Edit(EditArgs),
    /// Manage snippet fragments.
    Fragment(FragmentArgs),
    /// Manage physical folder paths.
    Folder(FolderArgs),
    /// Rename or remove tags across snippets.
    Tag(TagArgs),
    /// List, inspect, and switch TUI color themes.
    Theme(ThemeArgs),
    /// Move a snippet to the library trash.
    Delete(DeleteArgs),
    /// List deleted snippets.
    Trash,
    /// Restore a deleted snippet.
    Restore(RestoreArgs),
    /// Permanently remove a trash entry.
    Purge(PurgeArgs),
    /// Validate the library and optionally recover interrupted transactions.
    Doctor(DoctorArgs),
    /// Normalize snippet package directory names.
    Organize(OrganizeArgs),
    /// Import another snippet format.
    Import(ImportArgs),
    /// Run Git operations scoped to this library.
    Git(GitArgs),
    /// Install, inspect, or export the embedded manual pages.
    Man(ManArgs),
    /// Generate shell completion code.
    Completion(CompletionArgs),
}

#[derive(Args, Debug)]
pub struct InitArgs {
    pub path: Option<PathBuf>,
    #[arg(long)]
    pub name: Option<String>,
    /// Initialize a dedicated Git repository after creating the library.
    #[arg(long)]
    pub git: bool,
    /// Skip the interactive setup and use defaults.
    #[arg(long, short = 'y')]
    pub yes: bool,
}

#[derive(Args, Debug)]
pub struct FilterArgs {
    /// Restrict to a folder and its subfolders. Pass "" for Uncategorized.
    #[arg(long)]
    pub folder: Option<String>,
    /// Restrict --folder to that folder alone, excluding its subfolders.
    #[arg(long, requires = "folder")]
    pub no_subfolders: bool,
    #[arg(long)]
    pub tag: Option<String>,
    /// Order of the listing. Pinned snippets always come first.
    #[arg(long, default_value = "modified")]
    pub sort: SortMode,
}

impl FilterArgs {
    pub fn folder_filter(&self) -> Option<FolderFilter<'_>> {
        self.folder
            .as_deref()
            .map(|folder| FolderFilter::new(folder, !self.no_subfolders))
    }
}

#[derive(Args, Debug)]
pub struct SearchArgs {
    pub query: String,
    /// Treat QUERY as a regular expression. Case-insensitive; use (?-i) to opt out.
    #[arg(long)]
    pub regex: bool,
    /// Restrict to a folder and its subfolders. Pass "" for Uncategorized.
    #[arg(long)]
    pub folder: Option<String>,
    /// Restrict --folder to that folder alone, excluding its subfolders.
    #[arg(long, requires = "folder")]
    pub no_subfolders: bool,
    #[arg(long)]
    pub tag: Option<String>,
    /// Only search these parts of a snippet. Repeatable; defaults to all.
    #[arg(long = "field")]
    pub fields: Vec<SearchField>,
    /// Lines of surrounding context to include with each match.
    #[arg(long, short = 'C', default_value_t = 0, value_name = "N")]
    pub context: usize,
    /// Keep only the top N results after scoring.
    #[arg(long, short = 'm', value_name = "N")]
    pub limit: Option<usize>,
}

impl SearchArgs {
    pub fn folder_filter(&self) -> Option<FolderFilter<'_>> {
        self.folder
            .as_deref()
            .map(|folder| FolderFilter::new(folder, !self.no_subfolders))
    }
}

#[derive(Args, Debug)]
pub struct SelectorArgs {
    pub selector: String,
}

#[derive(Args, Debug)]
pub struct FragmentSelectorArgs {
    pub selector: String,
    /// 1-based index or fragment UUID prefix.
    #[arg(long)]
    pub fragment: Option<String>,
}

#[derive(Clone, Copy, Debug, ValueEnum)]
pub enum RenderArg {
    Ansi,
    Plain,
    Html,
}

#[derive(Args, Debug)]
pub struct PreviewArgs {
    pub selector: String,
    #[arg(long, value_enum)]
    pub render: Option<RenderArg>,
    /// Send output through the configured pager or $PAGER.
    #[arg(long, conflicts_with = "no_pager")]
    pub pager: bool,
    /// Disable a pager enabled in the config file.
    #[arg(long, conflicts_with = "pager")]
    pub no_pager: bool,
}

#[derive(Args, Debug)]
pub struct PathArgs {
    pub selector: String,
    #[arg(long, conflicts_with_all = ["readme", "fragment"])]
    pub metadata: bool,
    #[arg(long, conflicts_with_all = ["metadata", "fragment"])]
    pub readme: bool,
    #[arg(long, conflicts_with_all = ["metadata", "readme"])]
    pub fragment: Option<String>,
}

/// Same target selection as `snip path`, but hands the resolved path to an app.
/// This is the CLI counterpart of the TUI's `v` key.
#[derive(Args, Debug)]
pub struct OpenArgs {
    pub selector: String,
    #[arg(long, conflicts_with_all = ["readme", "fragment"])]
    pub metadata: bool,
    #[arg(long, conflicts_with_all = ["metadata", "fragment"])]
    pub readme: bool,
    #[arg(long, conflicts_with_all = ["metadata", "readme"])]
    pub fragment: Option<String>,
    /// Command to launch. Defaults to the `vscode_cmd` config key, then `code`.
    #[arg(long)]
    pub app: Option<String>,
}

#[derive(Args, Debug)]
pub struct CreateArgs {
    #[arg(long)]
    pub title: String,
    #[arg(long)]
    pub folder: Option<String>,
    #[arg(long = "tag")]
    pub tags: Vec<String>,
    #[arg(long)]
    pub language: Option<String>,
    #[arg(long, default_value = "Fragment")]
    pub fragment_title: String,
    /// Initial fragment content, given inline.
    #[arg(long, conflicts_with = "content_file")]
    pub content: Option<String>,
    /// Read initial fragment content from a UTF-8 file; use - for stdin.
    #[arg(long)]
    pub content_file: Option<String>,
    /// Fragment note (Markdown), given inline.
    #[arg(long, conflicts_with = "note_file")]
    pub note: Option<String>,
    #[arg(long)]
    pub note_file: Option<String>,
    /// Snippet README (Markdown), given inline.
    #[arg(long, conflicts_with = "readme_file")]
    pub readme: Option<String>,
    #[arg(long)]
    pub readme_file: Option<String>,
    #[arg(long)]
    pub pin: bool,
    #[arg(long)]
    pub lock: bool,
}

#[derive(Args, Debug, Clone)]
pub struct OptimisticArgs {
    #[arg(long)]
    pub if_hash: Option<String>,
    #[arg(long)]
    pub force: bool,
}

#[derive(Args, Debug)]
pub struct EditArgs {
    pub selector: String,
    #[arg(long)]
    pub title: Option<String>,
    #[arg(long)]
    pub folder: Option<String>,
    #[arg(long = "tag", conflicts_with = "clear_tags")]
    pub tags: Vec<String>,
    #[arg(long, conflicts_with = "tags")]
    pub clear_tags: bool,
    #[arg(long, conflicts_with = "unpin")]
    pub pin: bool,
    #[arg(long, conflicts_with = "pin")]
    pub unpin: bool,
    #[arg(long, conflicts_with = "unlock")]
    pub lock: bool,
    #[arg(long, conflicts_with = "lock")]
    pub unlock: bool,
    /// Target fragment for structured changes or external editing.
    #[arg(long)]
    pub fragment: Option<String>,
    #[arg(long)]
    pub fragment_title: Option<String>,
    #[arg(long)]
    pub language: Option<String>,
    /// Replacement fragment content, given inline.
    #[arg(long, conflicts_with = "content_file")]
    pub content: Option<String>,
    #[arg(long)]
    pub content_file: Option<String>,
    /// Replacement fragment note (Markdown), given inline.
    #[arg(long, conflicts_with_all = ["note_file", "clear_note"])]
    pub note: Option<String>,
    #[arg(long, conflicts_with = "clear_note")]
    pub note_file: Option<String>,
    #[arg(long, conflicts_with_all = ["note_file", "note"])]
    pub clear_note: bool,
    /// Replacement snippet README (Markdown), given inline.
    #[arg(long, conflicts_with_all = ["readme_file", "clear_readme"])]
    pub readme: Option<String>,
    #[arg(long, conflicts_with = "clear_readme")]
    pub readme_file: Option<String>,
    #[arg(long, conflicts_with_all = ["readme_file", "readme"])]
    pub clear_readme: bool,
    /// Edit snippet.toml in the external editor when no structured change is given.
    #[arg(long, conflicts_with = "readme_editor")]
    pub metadata_editor: bool,
    /// Edit README.md in the external editor when no structured change is given.
    #[arg(long, conflicts_with = "metadata_editor")]
    pub readme_editor: bool,
    /// Edit the selected fragment note instead of its content.
    #[arg(long)]
    pub note_editor: bool,
    #[command(flatten)]
    pub optimistic: OptimisticArgs,
}

#[derive(Args, Debug)]
pub struct FragmentArgs {
    #[command(subcommand)]
    pub command: FragmentCommand,
}

#[derive(Subcommand, Debug)]
pub enum FragmentCommand {
    Add(FragmentAddArgs),
    Edit(FragmentEditArgs),
    Remove(FragmentRemoveArgs),
    Reorder(FragmentReorderArgs),
}

#[derive(Args, Debug)]
pub struct FragmentAddArgs {
    pub selector: String,
    #[arg(long)]
    pub title: String,
    #[arg(long)]
    pub language: Option<String>,
    /// Fragment content, given inline.
    #[arg(long, conflicts_with = "content_file")]
    pub content: Option<String>,
    #[arg(long)]
    pub content_file: Option<String>,
    /// Fragment note (Markdown), given inline.
    #[arg(long, conflicts_with = "note_file")]
    pub note: Option<String>,
    #[arg(long)]
    pub note_file: Option<String>,
    #[command(flatten)]
    pub optimistic: OptimisticArgs,
}

#[derive(Args, Debug)]
pub struct FragmentEditArgs {
    pub selector: String,
    pub fragment: String,
    #[arg(long)]
    pub title: Option<String>,
    #[arg(long)]
    pub language: Option<String>,
    /// Replacement fragment content, given inline.
    #[arg(long, conflicts_with = "content_file")]
    pub content: Option<String>,
    #[arg(long)]
    pub content_file: Option<String>,
    /// Replacement fragment note (Markdown), given inline.
    #[arg(long, conflicts_with_all = ["note_file", "clear_note"])]
    pub note: Option<String>,
    #[arg(long, conflicts_with = "clear_note")]
    pub note_file: Option<String>,
    #[arg(long, conflicts_with_all = ["note_file", "note"])]
    pub clear_note: bool,
    #[command(flatten)]
    pub optimistic: OptimisticArgs,
}

#[derive(Args, Debug)]
pub struct FragmentRemoveArgs {
    pub selector: String,
    pub fragment: String,
    #[command(flatten)]
    pub optimistic: OptimisticArgs,
}

#[derive(Args, Debug)]
pub struct FragmentReorderArgs {
    pub selector: String,
    pub fragment: String,
    #[arg(long)]
    pub position: usize,
    #[command(flatten)]
    pub optimistic: OptimisticArgs,
}

#[derive(Args, Debug)]
pub struct FolderArgs {
    #[command(subcommand)]
    pub command: FolderCommand,
}

#[derive(Subcommand, Debug)]
pub enum FolderCommand {
    List,
    Create { folder: String },
    Rename { folder: String, new_name: String },
    Move { folder: String, target: String },
    Delete { folder: String },
}

#[derive(Args, Debug)]
pub struct TagArgs {
    #[command(subcommand)]
    pub command: TagCommand,
}

#[derive(Subcommand, Debug)]
pub enum TagCommand {
    List,
    Rename { tag: String, new_name: String },
    Delete { tag: String },
}

#[derive(Args, Debug)]
pub struct DeleteArgs {
    pub selector: String,
    #[command(flatten)]
    pub optimistic: OptimisticArgs,
}

#[derive(Args, Debug)]
pub struct RestoreArgs {
    pub selector: String,
    #[arg(long)]
    pub folder: Option<String>,
}

#[derive(Args, Debug)]
pub struct PurgeArgs {
    pub selector: String,
    /// Confirm permanent deletion without an interactive prompt.
    #[arg(long)]
    pub yes: bool,
}

#[derive(Args, Debug)]
pub struct DoctorArgs {
    #[arg(long)]
    pub repair: bool,
}

#[derive(Args, Debug)]
pub struct OrganizeArgs {
    #[arg(long)]
    pub dry_run: bool,
}

#[derive(Args, Debug)]
pub struct ImportArgs {
    #[command(subcommand)]
    pub command: ImportCommand,
}

#[derive(Subcommand, Debug)]
pub enum ImportCommand {
    Snippetslab {
        source: PathBuf,
        #[arg(long)]
        into: PathBuf,
        #[arg(long)]
        dry_run: bool,
    },
}

#[derive(Args, Debug)]
pub struct GitArgs {
    #[command(subcommand)]
    pub command: GitCommand,
}

#[derive(Subcommand, Debug)]
pub enum GitCommand {
    /// Show read-only backup status for this library.
    Status,
    /// Make this library a Git repository when it is not one already.
    Init,
    /// Commit all library changes without pushing.
    Commit {
        /// Override the generated backup message.
        #[arg(short, long)]
        message: Option<String>,
    },
    /// Commit all library changes and push when an upstream is configured.
    Backup,
    /// Push commits to the configured upstream without committing.
    Push,
    /// Fetch and prune remote-tracking refs without touching the worktree.
    Fetch,
}

#[derive(Args, Debug)]
pub struct ManArgs {
    #[command(subcommand)]
    pub command: ManCommand,
}

#[derive(Subcommand, Debug)]
pub enum ManCommand {
    /// Print the resolved man page installation directory.
    Path {
        /// Resolve the directory below PREFIX/share instead of the user data directory.
        #[arg(long)]
        prefix: Option<PathBuf>,
    },
    /// Install all manual pages.
    Install {
        /// Install below PREFIX/share instead of the user data directory.
        #[arg(long)]
        prefix: Option<PathBuf>,
        /// Replace pages not recorded in snip's installation manifest.
        #[arg(long)]
        force: bool,
    },
    /// Remove unmodified pages recorded by snip.
    Uninstall {
        /// Uninstall below PREFIX/share instead of the user data directory.
        #[arg(long)]
        prefix: Option<PathBuf>,
    },
    /// Show an embedded manual page with the system man viewer.
    Show {
        /// Page stem, such as snip or snip-create.
        page: Option<String>,
    },
    /// Export all embedded manual pages to a directory.
    Generate {
        /// Destination directory.
        directory: PathBuf,
    },
}

#[derive(Clone, Copy, Debug, ValueEnum)]
pub enum CompletionShell {
    Bash,
    Elvish,
    Fish,
    Powershell,
    Zsh,
}

#[derive(Args, Debug)]
pub struct CompletionArgs {
    #[arg(value_enum)]
    pub shell: CompletionShell,
}

#[derive(Args, Debug)]
pub struct ConfigArgs {
    #[command(subcommand)]
    pub command: ConfigCommand,
}

#[derive(Args, Debug)]
pub struct ThemeArgs {
    #[command(subcommand)]
    pub command: ThemeCommand,
}

#[derive(Subcommand, Debug)]
pub enum ThemeCommand {
    /// List available themes.
    List {
        /// Restrict to one appearance.
        #[arg(long, value_enum)]
        appearance: Option<AppearanceArg>,
    },
    /// Print one theme's resolved colors.
    Show { name: String },
    /// Validate a theme's contrast and role distinctness.
    Check { name: String },
    /// Print the directory user themes are read from.
    Path,
    /// Write a theme to the user theme directory so it can be edited.
    Export {
        name: String,
        /// Name to save it under. Defaults to "<name>-custom".
        #[arg(long)]
        r#as: Option<String>,
        #[arg(long)]
        force: bool,
    },
    /// Select a theme for its appearance slot and save it to the config.
    Use {
        name: String,
        /// Write to this slot instead of the theme's own appearance.
        #[arg(long, value_enum)]
        appearance: Option<AppearanceArg>,
    },
}

#[derive(Clone, Copy, Debug, Eq, PartialEq, ValueEnum)]
#[value(rename_all = "lowercase")]
pub enum AppearanceArg {
    Light,
    Dark,
}

#[derive(Subcommand, Debug)]
pub enum ConfigCommand {
    /// Print the resolved config file path.
    Path,
    /// Print the current config (defaults to an empty schema when absent).
    Show,
    /// Create a config file, optionally binding a default library.
    Init {
        #[arg(long)]
        library: Option<PathBuf>,
        #[arg(long)]
        force: bool,
    },
    /// Set one supported config key.
    Set {
        #[arg(value_enum)]
        key: ConfigKey,
        value: String,
    },
    /// Remove one supported config key and restore its built-in default.
    Unset {
        #[arg(value_enum)]
        key: ConfigKey,
    },
}

#[derive(Clone, Copy, Debug, ValueEnum)]
#[value(rename_all = "kebab-case")]
pub enum ConfigKey {
    DefaultLibrary,
    Output,
    Color,
    PreviewRender,
    PreviewPager,
    Editor,
    Pager,
    DefaultLanguage,
    DefaultFolder,
    DefaultTags,
    TuiTheme,
    TuiLightTheme,
    TuiDarkTheme,
    TuiSort,
    TuiDensity,
    TuiLineNumbers,
    GitAutoCommitInterval,
    GitAutoPush,
    GitBackupOnQuit,
}

#[cfg(test)]
mod tests {
    use super::Cli;
    use clap::CommandFactory;

    #[test]
    fn clap_command_tree_is_valid() {
        Cli::command().debug_assert();
    }
}