scriv 1.1.1

Fast local CLI note manager with optional password encryption
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
//! CLI entrypoint and command wiring.

use chrono::{DateTime, Utc};
use scriv::{
    ListOptions, Note, active_password, add_note, append_note, clear_notes, collect_tags,
    edit_note, get_note, highlight_match, import_notes, list_notes, load_notes, note_age,
    notes_file_is_encrypted, read_stdin_text, remove_notes, search_notes, set_active_password,
    tag_note, untag_note,
};
use std::collections::BTreeMap;
use std::env;
use std::io::{self, BufRead, Read, Write};

const USAGE_TEMPLATE: &str = "scriv - Fast local note manager

Version: {version}

Usage: scriv <command> [arguments]

Commands:
    add <text>              Add a new note (or pipe text via stdin)
    list [--tag=<tag>] [--sort=id|date|updated] [--limit=N] [--full]
                            List notes, optionally filtered, sorted, and limited
    edit <id> <text>        Edit a note by id (or pipe new text via stdin)
    append <id> <text>      Append text to an existing note
    done [--force] <id> [id2...]
                            Remove one or more notes by id (--force skips missing)
    search <text>           Search notes by text or tag
    view <id>               View full details of a note
    tag <id> <tag1> [...]   Add tags to a note
    untag <id> <tag>        Remove a tag from a note
    tags                    List all tags with note counts
    export                  Print all notes as NDJSON to stdout
    import                  Read NDJSON from stdin and append notes
    clear                   Remove all notes
    lock                    Set or change the notes password
    unlock                  Remove password protection

Options:
    -h, --help     Print help
    -V, --version  Print version
";

/// Return crate version embedded at compile time.
fn app_version() -> &'static str {
    env!("CARGO_PKG_VERSION")
}

/// Print consistent CLI help text.
fn print_usage() {
    println!("{}", USAGE_TEMPLATE.replace("{version}", app_version()));
}

/// Print an error and exit with status 1.
fn fatal(msg: &str) -> ! {
    eprintln!("Error: {}", msg);
    std::process::exit(1);
}

/// Parse a required positive note id.
fn parse_id(s: &str) -> Result<u64, String> {
    let id = s
        .parse::<u64>()
        .map_err(|_| "id must be a positive integer".to_string())?;
    if id == 0 {
        return Err("id must be a positive integer".to_string());
    }
    Ok(id)
}

/// True when stdin is piped rather than interactive.
fn stdin_is_piped() -> bool {
    !std::io::IsTerminal::is_terminal(&io::stdin())
}

/// True when stdout is attached to a terminal.
fn stdout_is_terminal() -> bool {
    std::io::IsTerminal::is_terminal(&io::stdout())
}

/// Prompt for a password without echoing input.
fn prompt_password(msg: &str) -> Result<String, String> {
    eprint!("{}", msg);
    rpassword::read_password().map_err(|e| e.to_string())
}

fn cmd_add(text: String) -> Result<(), String> {
    let note = add_note(&text)?;
    println!("Added [{}] {}", note.id, note.text);
    Ok(())
}

fn cmd_list(opts: ListOptions) -> Result<(), String> {
    let notes = list_notes(&opts)?;
    if notes.is_empty() {
        println!("No notes.");
        return Ok(());
    }

    for note in &notes {
        let mut text = note.text.clone();
        if !opts.full && text.chars().count() > 72 {
            text = text.chars().take(72).collect::<String>() + "...";
        }

        let mut line = format!("[{}] ({}) {}", note.id, note_age(&note.created_at), text);
        if !note.tags.is_empty() {
            line.push_str(&format!(" #{}", note.tags.join(" #")));
        }
        println!("{}", line);
    }

    println!("{} notes.", notes.len());
    Ok(())
}

fn cmd_view(id_str: &str) -> Result<(), String> {
    let id = parse_id(id_str)?;
    let note = get_note(id)?;
    println!("[{}] {}", note.id, note.text);

    if let Ok(created) = DateTime::parse_from_rfc3339(&note.created_at) {
        println!(
            "    Created: {}",
            created.with_timezone(&Utc).format("%Y-%m-%d")
        );
    }
    if !note.updated_at.is_empty()
        && let Ok(updated) = DateTime::parse_from_rfc3339(&note.updated_at)
    {
        println!(
            "    Updated: {}",
            updated.with_timezone(&Utc).format("%Y-%m-%d")
        );
    }
    if !note.tags.is_empty() {
        println!("    Tags: #{}", note.tags.join(" #"));
    }

    Ok(())
}

fn cmd_done(id_strs: &[String], force: bool) -> Result<(), String> {
    let ids = id_strs
        .iter()
        .map(|s| parse_id(s))
        .collect::<Result<Vec<_>, _>>()?;

    let removed = remove_notes(&ids, force)?;
    for note in removed {
        println!("Removed [{}] {}", note.id, note.text);
    }

    Ok(())
}

fn cmd_edit(id_str: &str, text: String) -> Result<(), String> {
    let id = parse_id(id_str)?;
    let note = edit_note(id, &text)?;
    println!("Updated [{}] {}", note.id, note.text);
    Ok(())
}

fn cmd_tag(id_str: &str, tags: &[String]) -> Result<(), String> {
    let id = parse_id(id_str)?;
    let note = tag_note(id, tags)?;
    println!(
        "Tagged [{}] {}: #{}",
        note.id,
        note.text,
        note.tags.join(" #")
    );
    Ok(())
}

fn cmd_untag(id_str: &str, tag: &str) -> Result<(), String> {
    let id = parse_id(id_str)?;
    let note = untag_note(id, tag)?;
    println!("Removed tag #{} from [{}] {}", tag, note.id, note.text);
    Ok(())
}

fn cmd_tags() -> Result<(), String> {
    let notes = load_notes()?;
    let counts = collect_tags(&notes);
    if counts.is_empty() {
        println!("No tags.");
        return Ok(());
    }

    let sorted: BTreeMap<String, usize> = counts.into_iter().collect();
    for (tag, count) in sorted {
        println!("{:<20} {}", tag, count);
    }

    Ok(())
}

fn cmd_append(id_str: &str, text: String) -> Result<(), String> {
    let id = parse_id(id_str)?;
    let note = append_note(id, &text)?;
    println!("Updated [{}] {}", note.id, note.text);
    Ok(())
}

fn cmd_clear(force: bool) -> Result<(), String> {
    let notes = load_notes()?;
    if notes.is_empty() {
        println!("No notes.");
        return Ok(());
    }

    if !force {
        print!("Remove all {} notes? [y/N] ", notes.len());
        io::stdout().flush().map_err(|e| e.to_string())?;
        let mut line = String::new();
        io::stdin()
            .read_line(&mut line)
            .map_err(|e| e.to_string())?;
        if line.trim().to_lowercase() != "y" {
            return Ok(());
        }
    }

    clear_notes()?;
    println!("Cleared.");
    Ok(())
}

fn cmd_search(query: &str) -> Result<(), String> {
    let results = search_notes(query)?;
    if results.is_empty() {
        println!("No matches.");
        return Ok(());
    }

    let color = stdout_is_terminal();
    for note in &results {
        let text = if color {
            highlight_match(&note.text, query)
        } else {
            note.text.clone()
        };
        println!("[{}] {}", note.id, text);
    }
    println!("{} matches.", results.len());
    Ok(())
}

fn cmd_export() -> Result<(), String> {
    let notes = load_notes()?;
    for note in notes {
        println!(
            "{}",
            serde_json::to_string(&note).map_err(|e| e.to_string())?
        );
    }
    Ok(())
}

fn cmd_import<R: Read>(reader: R) -> Result<(), String> {
    let mut incoming = Vec::<Note>::new();
    let br = io::BufReader::new(reader);

    for (idx, line) in br.lines().enumerate() {
        let line = line.map_err(|e| e.to_string())?;
        let trimmed = line.trim();
        if trimmed.is_empty() {
            continue;
        }
        let note: Note = serde_json::from_str(trimmed)
            .map_err(|e| format!("line {}: invalid JSON: {}", idx + 1, e))?;
        if note.text.trim().is_empty() {
            return Err(format!("line {}: note text cannot be empty", idx + 1));
        }
        if note.created_at.is_empty() || DateTime::parse_from_rfc3339(&note.created_at).is_err() {
            return Err(format!("line {}: invalid created_at timestamp", idx + 1));
        }
        if !note.updated_at.is_empty() && DateTime::parse_from_rfc3339(&note.updated_at).is_err() {
            return Err(format!("line {}: invalid updated_at timestamp", idx + 1));
        }
        incoming.push(note);
    }

    if incoming.is_empty() {
        println!("No notes to import.");
        return Ok(());
    }

    let count = incoming.len();
    import_notes(incoming)?;
    println!("Imported {} notes.", count);
    Ok(())
}

fn cmd_lock() -> Result<(), String> {
    let notes = if notes_file_is_encrypted() {
        let current = prompt_password("Current password: ")?;
        set_active_password(current);
        match load_notes() {
            Ok(v) => v,
            Err(e) => {
                set_active_password(String::new());
                return Err(e);
            }
        }
    } else {
        load_notes()?
    };

    let pw = prompt_password("New password: ")?;
    if pw.is_empty() {
        return Err("password cannot be empty".to_string());
    }
    let confirm = prompt_password("Confirm password: ")?;
    if pw != confirm {
        return Err("passwords do not match".to_string());
    }

    set_active_password(pw);
    scriv::save_notes(&notes)?;
    println!("Notes are now password protected.");
    Ok(())
}

fn cmd_unlock() -> Result<(), String> {
    if !notes_file_is_encrypted() {
        println!("Notes are not password protected.");
        return Ok(());
    }

    let pw = prompt_password("Password: ")?;
    set_active_password(pw);
    let notes = match load_notes() {
        Ok(v) => v,
        Err(e) => {
            set_active_password(String::new());
            return Err(e);
        }
    };
    set_active_password(String::new());
    scriv::save_notes(&notes)?;
    println!("Password protection removed.");
    Ok(())
}

/// Parse args, dispatch commands, and normalize user-facing errors.
fn main() {
    let args: Vec<String> = env::args().collect();
    if args.len() < 2 {
        print_usage();
        std::process::exit(1);
    }

    let cmd = args[1].clone();
    let no_prompt = [
        "lock",
        "unlock",
        "-h",
        "--help",
        "help",
        "-V",
        "--version",
        "version",
    ]
    .contains(&cmd.as_str());

    if notes_file_is_encrypted() && !no_prompt {
        match prompt_password("Password: ") {
            Ok(pw) => set_active_password(pw),
            Err(e) => fatal(&format!("cannot read password: {}", e)),
        }
    }

    let result = match cmd.as_str() {
        "add" => {
            if stdin_is_piped() {
                cmd_add(read_stdin_text(io::stdin()).unwrap_or_else(|e| fatal(&e)))
            } else {
                if args.len() < 3 {
                    fatal("usage: scriv add <text>");
                }
                cmd_add(args[2..].join(" "))
            }
        }
        "list" => {
            let mut opts = ListOptions::default();
            for arg in &args[2..] {
                if let Some(value) = arg.strip_prefix("--tag=") {
                    opts.tag = value.to_string();
                } else if let Some(value) = arg.strip_prefix("--sort=") {
                    opts.sort = value.to_string();
                } else if let Some(value) = arg.strip_prefix("--limit=") {
                    let parsed = value.parse::<usize>();
                    match parsed {
                        Ok(v) if v >= 1 => opts.limit = v,
                        _ => fatal("--limit must be a positive integer"),
                    }
                } else if arg == "--full" {
                    opts.full = true;
                } else {
                    fatal(&format!("unknown flag: {}", arg));
                }
            }
            cmd_list(opts)
        }
        "edit" => {
            if args.len() < 3 {
                fatal("usage: scriv edit <id> <text>");
            }

            if stdin_is_piped() {
                cmd_edit(
                    &args[2],
                    read_stdin_text(io::stdin()).unwrap_or_else(|e| fatal(&e)),
                )
            } else {
                if args.len() < 4 {
                    fatal("usage: scriv edit <id> <text>");
                }
                cmd_edit(&args[2], args[3..].join(" "))
            }
        }
        "done" => {
            let mut force = false;
            let mut id_args = Vec::new();
            for arg in &args[2..] {
                if arg == "--force" {
                    force = true;
                } else {
                    id_args.push(arg.clone());
                }
            }
            if id_args.is_empty() {
                fatal("usage: scriv done [--force] <id> [id2...]");
            }
            cmd_done(&id_args, force)
        }
        "search" => {
            if args.len() < 3 {
                fatal("usage: scriv search <query>");
            }
            cmd_search(&args[2..].join(" "))
        }
        "view" => {
            if args.len() < 3 {
                fatal("usage: scriv view <id>");
            }
            cmd_view(&args[2])
        }
        "tag" => {
            if args.len() < 4 {
                fatal("usage: scriv tag <id> <tag1> [tag2...]");
            }
            cmd_tag(&args[2], &args[3..])
        }
        "untag" => {
            if args.len() < 4 {
                fatal("usage: scriv untag <id> <tag>");
            }
            cmd_untag(&args[2], &args[3])
        }
        "tags" => cmd_tags(),
        "append" => {
            if args.len() < 4 {
                fatal("usage: scriv append <id> <text>");
            }
            cmd_append(&args[2], args[3..].join(" "))
        }
        "export" => cmd_export(),
        "import" => cmd_import(io::stdin()),
        "clear" => {
            let force = args.get(2).map(|v| v == "--force").unwrap_or(false);
            cmd_clear(force)
        }
        "lock" => cmd_lock(),
        "unlock" => cmd_unlock(),
        "-h" | "--help" | "help" => {
            print_usage();
            Ok(())
        }
        "-V" | "--version" | "version" => {
            println!("scriv {}", app_version());
            Ok(())
        }
        _ => fatal(&format!(
            "unknown command: {}\nRun 'scriv --help' for usage.",
            cmd
        )),
    };

    if let Err(err) = result {
        if !active_password().is_empty() {
            set_active_password(String::new());
        }
        eprintln!("Error: {}", err);
        std::process::exit(1);
    }
}