bibox 0.1.0

Terminal-based bibliography manager with three-panel TUI and AI-agent-friendly Markdown notes
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
use anyhow::Result;
use clap::{Parser, Subcommand};
use std::path::PathBuf;

mod arxiv;
mod bibtex;
mod commands;
mod config;
mod crossref;
mod git;
mod i18n;
mod interactive;
mod models;
mod notes;
mod openlibrary;
mod pdf;
mod storage;
mod tui;
mod unpaywall;
mod url_resolver;

use config::load_config;

#[derive(Parser)]
#[command(
    name = "bibox",
    about = "Terminal-based bibliography manager with three-panel TUI and AI-agent-friendly notes",
    long_about = "bibox is a terminal-based bibliography manager built in Rust.\n\n\
Add papers by PDF, DOI, ISBN, arXiv ID, or URL — metadata is fetched automatically \
from Crossref, arXiv, and OpenLibrary. Manage your library through a three-panel TUI \
(collections | entries | preview) or a scriptable CLI.\n\n\
Notes are stored as Markdown files with section-level read/write via --stdin and --section, \
designed for AI agent workflows.\n\n\
Use `bibox init <path>` to create a portable home directory that can be synced with Git.\n\n\
Run `bibox` with no arguments to launch the TUI. Press ? for keybindings.",
    version
)]
struct Cli {
    #[command(subcommand)]
    command: Option<Commands>,
}

#[derive(Subcommand)]
enum Commands {
    /// Add a new entry from PDF, DOI, ISBN, arXiv ID, URL, or title search.
    ///
    /// Examples:
    ///   bibox add paper.pdf --to ml
    ///   bibox add --doi 10.1145/3290605.3300907
    ///   bibox add --arxiv 2301.12345
    ///   bibox add --isbn 978-0-13-468599-1
    ///   bibox add --url https://arxiv.org/abs/2301.12345
    ///   bibox add --search "attention is all you need"
    Add {
        /// PDF file to add
        file: Option<PathBuf>,

        /// DOI to look up (skips PDF if provided alone)
        #[arg(long)]
        doi: Option<String>,

        /// ISBN to look up via Open Library (skips PDF if provided alone)
        #[arg(long)]
        isbn: Option<String>,

        /// URL to resolve (academic paper page)
        #[arg(long, conflicts_with_all = ["doi", "isbn", "file", "search"])]
        url: Option<String>,

        /// Search Crossref by title and select interactively
        #[arg(long, conflicts_with_all = ["doi", "isbn", "file", "url"])]
        search: Option<String>,

        /// Assign to a collection
        #[arg(long)]
        to: Option<String>,

        /// Override auto-generated citation key
        #[arg(long)]
        key: Option<String>,

        /// Override title
        #[arg(long)]
        title: Option<String>,

        /// Override author(s), semicolon-separated  e.g. "Kim, J; Lee, S"
        #[arg(long)]
        author: Option<String>,

        /// Override publication year
        #[arg(long)]
        year: Option<u32>,

        /// Entry type: article, book, inproceedings, misc
        #[arg(long, value_name = "TYPE")]
        r#type: Option<String>,

        /// Journal name
        #[arg(long)]
        journal: Option<String>,

        /// Publisher name
        #[arg(long)]
        publisher: Option<String>,

        /// Booktitle (for inproceedings)
        #[arg(long)]
        booktitle: Option<String>,
    },

    /// List entries, optionally filtered by collection, type, tag, or year.
    ///
    /// Examples:
    ///   bibox list                  # Show all collections with counts
    ///   bibox list cs               # List entries in 'cs' collection
    ///   bibox list --type article   # Only articles
    ///   bibox list --year 2024      # Only 2024 entries
    List {
        /// Collection name (omit to list all collections)
        collection: Option<String>,

        /// Filter by type: article, book, inproceedings, misc
        #[arg(long, value_name = "TYPE")]
        r#type: Option<String>,

        /// Filter by tag
        #[arg(long)]
        tag: Option<String>,

        /// Filter by year
        #[arg(long)]
        year: Option<u32>,

        /// Maximum number of entries to show
        #[arg(long)]
        limit: Option<usize>,
    },

    /// Search entries by keyword across all fields. Copies citekey to clipboard on Enter.
    ///
    /// Examples:
    ///   bibox search "transformer"
    ///   bibox search "kim" --field author
    ///   bibox search "2024" --collection ml
    Search {
        /// Search query
        query: String,

        /// Restrict search to a collection
        #[arg(long)]
        collection: Option<String>,

        /// Search in a specific field: title, author, journal, doi, tag
        #[arg(long)]
        field: Option<String>,
    },

    /// Show full metadata of an entry (title, authors, DOI, tags, collections, file path).
    ///
    /// Examples:
    ///   bibox show kim2025rust
    Show {
        /// Citation key or entry ID
        key: String,
    },

    /// Edit entry metadata. When --doi is provided, re-fetches from Crossref (preserving existing values).
    ///
    /// Examples:
    ///   bibox edit kim2025rust --title "New Title" --year 2025
    ///   bibox edit kim2025rust --doi 10.1234/new   # re-fetch metadata
    ///   bibox edit kim2025rust --tags-add "ml,nlp"
    Edit {
        /// Citation key or entry ID
        key: String,

        /// New title
        #[arg(long)]
        title: Option<String>,

        /// New author(s), semicolon-separated
        #[arg(long)]
        author: Option<String>,

        /// New year
        #[arg(long)]
        year: Option<u32>,

        /// New DOI
        #[arg(long)]
        doi: Option<String>,

        /// New journal
        #[arg(long)]
        journal: Option<String>,

        /// New publisher
        #[arg(long)]
        publisher: Option<String>,

        /// New booktitle
        #[arg(long)]
        booktitle: Option<String>,

        /// New volume
        #[arg(long)]
        volume: Option<String>,

        /// New number/issue
        #[arg(long)]
        number: Option<String>,

        /// New pages  e.g. "1--10"
        #[arg(long)]
        pages: Option<String>,

        /// Note
        #[arg(long)]
        note: Option<String>,

        /// Tags to add, comma-separated
        #[arg(long)]
        tags_add: Option<String>,

        /// Tags to remove, comma-separated
        #[arg(long)]
        tags_remove: Option<String>,
    },

    /// Delete an entry and its associated PDF file.
    ///
    /// Examples:
    ///   bibox delete kim2025rust
    ///   bibox delete kim2025rust -y   # skip confirmation
    Delete {
        /// Citation key or entry ID
        key: String,

        /// Skip confirmation prompt
        #[arg(short = 'y', long)]
        yes: bool,
    },

    /// Add an entry to one or more collections.
    ///
    /// Examples:
    ///   bibox collect kim2025rust ml systems
    Collect {
        /// Citation key or entry ID
        key: String,

        /// Collection name(s)
        #[arg(required = true)]
        collections: Vec<String>,
    },

    /// Remove an entry from a collection.
    ///
    /// Examples:
    ///   bibox uncollect kim2025rust ml
    Uncollect {
        /// Citation key or entry ID
        key: String,

        /// Collection name to remove from
        collection: String,
    },

    /// Import entries from a BibTeX (.bib) file.
    ///
    /// Examples:
    ///   bibox import refs.bib
    ///   bibox import refs.bib --to ml
    Import {
        /// Path to .bib file
        file: PathBuf,

        /// Assign all imported entries to a collection
        #[arg(long)]
        to: Option<String>,
    },

    /// Export entries as BibTeX, YAML, RIS, or CSV. Optionally include PDF files.
    ///
    /// Examples:
    ///   bibox export                                  # all entries as BibTeX
    ///   bibox export kim2025 dijkstra1968             # specific entries
    ///   bibox export --collection cs --format ris     # collection as RIS
    ///   bibox export --include-pdf                    # BibTeX + PDFs
    ///   bibox export --as-pdf --zip                   # PDFs only, zipped
    ///   bibox export kim2025 --clipboard              # copy to clipboard
    #[command(alias = "out")]
    Export {
        /// Citation keys to export (omit for collection/all)
        keys: Vec<String>,

        /// Export entries from this collection
        #[arg(long)]
        collection: Option<String>,

        /// Filter by entry type
        #[arg(long, value_name = "TYPE")]
        r#type: Option<String>,

        /// Filter by tag
        #[arg(long)]
        tag: Option<String>,

        /// Output file path (default: auto-generated filename)
        #[arg(long, short = 'o')]
        output: Option<PathBuf>,

        /// Copy BibTeX to clipboard instead of writing a file
        #[arg(long)]
        clipboard: bool,

        /// Export PDF files instead of BibTeX
        #[arg(long)]
        as_pdf: bool,

        /// Also export PDF files alongside the bibliography
        #[arg(long)]
        include_pdf: bool,

        /// Compress output into a ZIP archive
        #[arg(long)]
        zip: bool,

        /// Output format: bibtex (default), yaml, ris, csv
        #[arg(long, default_value = "bibtex")]
        format: String,
    },

    /// Open the PDF file for an entry in the system viewer.
    ///
    /// Examples:
    ///   bibox open kim2025rust
    Open {
        /// Citation key or entry ID
        key: String,
    },

    /// Reconcile the bibox directory with the database (detect orphaned PDFs, missing entries).
    Sync,

    /// Initialize a portable bibox home directory. All data (db, pdfs, notes) lives in one folder.
    ///
    /// Examples:
    ///   bibox init ~/bibox              # create home
    ///   bibox init ~/bibox --migrate    # create + copy existing data
    Init {
        /// Path to the home directory (e.g., ~/bibox)
        path: PathBuf,

        /// Migrate existing data from Library to the new home
        #[arg(long)]
        migrate: bool,
    },

    /// Read, write, or edit per-entry Markdown notes. Supports section-level updates for AI agents.
    ///
    /// Examples:
    ///   bibox note kim2025rust                          # open in $EDITOR
    ///   bibox note kim2025rust --show                   # print to stdout
    ///   bibox note kim2025rust --path                   # print file path
    ///   bibox note kim2025rust --template ai-summary    # init from template
    ///   echo "text" | bibox note kim2025rust --stdin --section "Summary"  # write section
    ///   bibox note kim2025rust --from notes.md --section "Results"        # write from file
    Note {
        /// Citation key or entry ID
        key: String,

        /// Read content from stdin (non-interactive)
        #[arg(long)]
        stdin: bool,

        /// Read content from a file
        #[arg(long, conflicts_with = "stdin")]
        from: Option<PathBuf>,

        /// Target a specific ## section (requires --stdin or --from)
        #[arg(long)]
        section: Option<String>,

        /// Initialize note from a template
        #[arg(long)]
        template: Option<String>,

        /// Print note content to stdout
        #[arg(long, conflicts_with_all = ["stdin", "from", "template"])]
        show: bool,

        /// Print note file path to stdout
        #[arg(long, conflicts_with_all = ["stdin", "from", "template", "show"])]
        path: bool,

        /// Allow --template to overwrite existing note
        #[arg(long)]
        force: bool,
    },

    /// Bulk-update fields across multiple entries
    Modify {
        /// Field=value pairs to set, e.g. year=2024 journal="Nature"
        #[arg(required = true)]
        assignments: Vec<String>,

        /// Filter: collection:<name>, tag:<tag>, type:<type>, year:<year>
        #[arg(long, short = 'f')]
        filter: Option<String>,

        /// Apply to all entries (required if no --filter)
        #[arg(long)]
        all: bool,

        /// Skip confirmation
        #[arg(short = 'y', long)]
        yes: bool,
    },

    /// Review entries interactively one by one
    Review {
        /// Filter by collection
        #[arg(long, short = 'c')]
        collection: Option<String>,

        /// Filter: same syntax as modify (collection:<name>, tag:<tag>, type:<type>, year:<year>)
        #[arg(long, short = 'f')]
        filter: Option<String>,

        /// Only show entries without the "reviewed" tag
        #[arg(long)]
        unreviewed: bool,
    },
}

#[tokio::main]
async fn main() -> Result<()> {
    let cli = Cli::parse();
    let config = load_config()?;

    match cli.command {
        None => {
            tui::run_tui(&config)?;
        }

        Some(Commands::Add {
            file,
            doi,
            isbn,
            url,
            search,
            to,
            key,
            title,
            author,
            year,
            r#type,
            journal,
            publisher,
            booktitle,
        }) => {
            commands::cmd_add(
                file, to, doi, isbn, url, search, key, title, author, year, r#type, journal,
                publisher, booktitle, &config,
            )
            .await?;
        }

        Some(Commands::List {
            collection,
            r#type,
            tag,
            year,
            limit,
        }) => {
            commands::cmd_list(collection, r#type, tag, year, limit, &config)?;
        }

        Some(Commands::Search {
            query,
            collection,
            field,
        }) => {
            commands::cmd_search(query, collection, field, &config)?;
        }

        Some(Commands::Show { key }) => {
            commands::cmd_show(key, &config)?;
        }

        Some(Commands::Edit {
            key,
            title,
            author,
            year,
            doi,
            journal,
            publisher,
            booktitle,
            volume,
            number,
            pages,
            note,
            tags_add,
            tags_remove,
        }) => {
            commands::cmd_edit(
                key,
                title,
                author,
                year,
                doi,
                journal,
                publisher,
                booktitle,
                volume,
                number,
                pages,
                note,
                tags_add,
                tags_remove,
                &config,
            ).await?;
        }

        Some(Commands::Delete { key, yes }) => {
            commands::cmd_delete(key, yes, &config)?;
        }

        Some(Commands::Collect { key, collections }) => {
            commands::cmd_collect(key, collections, &config)?;
        }

        Some(Commands::Uncollect { key, collection }) => {
            commands::cmd_uncollect(key, collection, &config)?;
        }

        Some(Commands::Import { file, to }) => {
            commands::cmd_import(file, to, &config)?;
        }

        Some(Commands::Export {
            keys,
            collection,
            r#type,
            tag,
            output,
            clipboard,
            as_pdf,
            include_pdf,
            zip,
            format,
        }) => {
            commands::cmd_export(
                keys, collection, output, clipboard, r#type, tag, as_pdf, include_pdf, zip, format, &config,
            )?;
        }

        Some(Commands::Open { key }) => {
            commands::cmd_open(key, &config)?;
        }

        Some(Commands::Sync) => {
            commands::cmd_sync(&config)?;
        }

        Some(Commands::Init { path, migrate }) => {
            commands::cmd_init(path, migrate, &config)?;
        }

        Some(Commands::Note { key, stdin, from, section, template, show, path, force }) => {
            commands::cmd_note(key, stdin, from, section, template, show, path, force, &config)?;
        }

        Some(Commands::Modify {
            assignments,
            filter,
            all,
            yes,
        }) => {
            commands::cmd_modify(assignments, filter, all, yes, &config)?;
        }

        Some(Commands::Review {
            collection,
            filter,
            unreviewed,
        }) => {
            commands::cmd_review(collection, filter, unreviewed, &config)?;
        }
    }

    Ok(())
}