|
| 1 | +"""Command-line interface for the Markdown note manager.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import argparse |
| 6 | +import sys |
| 7 | +from collections.abc import Sequence |
| 8 | +from pathlib import Path |
| 9 | + |
| 10 | +from .models import Note |
| 11 | +from .store import NoteStore |
| 12 | + |
| 13 | +DEFAULT_NOTES_DIR = Path("notes") |
| 14 | +DEFAULT_DB_PATH = Path("notes.db") |
| 15 | + |
| 16 | + |
| 17 | +def _parse_tags(raw: str | None) -> list[str]: |
| 18 | + if not raw: |
| 19 | + return [] |
| 20 | + return [tag.strip() for tag in raw.split(",") if tag.strip()] |
| 21 | + |
| 22 | + |
| 23 | +def _read_body(args: argparse.Namespace) -> str: |
| 24 | + if args.body_file: |
| 25 | + return Path(args.body_file).read_text(encoding="utf-8") |
| 26 | + if args.body is not None: |
| 27 | + return args.body |
| 28 | + return sys.stdin.read() |
| 29 | + |
| 30 | + |
| 31 | +def _print_note(note: Note) -> None: |
| 32 | + tags = ", ".join(note.tags) if note.tags else "-" |
| 33 | + print(f"# {note.title}") |
| 34 | + print(f"tags: {tags}") |
| 35 | + print(f"created_at: {note.created_at.isoformat()}") |
| 36 | + print() |
| 37 | + print(note.body) |
| 38 | + |
| 39 | + |
| 40 | +def _print_note_summary(note: Note) -> None: |
| 41 | + tags = ", ".join(note.tags) if note.tags else "-" |
| 42 | + print(f"{note.title}\t[{tags}]\t{note.created_at.isoformat()}") |
| 43 | + |
| 44 | + |
| 45 | +def build_parser() -> argparse.ArgumentParser: |
| 46 | + parser = argparse.ArgumentParser( |
| 47 | + prog="notes", |
| 48 | + description="A command-line Markdown note manager.", |
| 49 | + ) |
| 50 | + parser.add_argument( |
| 51 | + "--notes-dir", |
| 52 | + default=DEFAULT_NOTES_DIR, |
| 53 | + type=Path, |
| 54 | + help=f"Directory to store Markdown note files in (default: {DEFAULT_NOTES_DIR})", |
| 55 | + ) |
| 56 | + parser.add_argument( |
| 57 | + "--db", |
| 58 | + default=DEFAULT_DB_PATH, |
| 59 | + type=Path, |
| 60 | + help=f"Path to the SQLite index database (default: {DEFAULT_DB_PATH})", |
| 61 | + ) |
| 62 | + |
| 63 | + subparsers = parser.add_subparsers(dest="command", required=True) |
| 64 | + |
| 65 | + add_parser = subparsers.add_parser("add", help="Add a new note") |
| 66 | + add_parser.add_argument("title", help="Title of the note") |
| 67 | + add_parser.add_argument( |
| 68 | + "--tags", default="", help="Comma-separated list of tags" |
| 69 | + ) |
| 70 | + body_group = add_parser.add_mutually_exclusive_group() |
| 71 | + body_group.add_argument("--body", help="Body text of the note") |
| 72 | + body_group.add_argument( |
| 73 | + "--body-file", help="Path to a file containing the note body" |
| 74 | + ) |
| 75 | + |
| 76 | + search_parser = subparsers.add_parser( |
| 77 | + "search", help="Search notes by title or body content" |
| 78 | + ) |
| 79 | + search_parser.add_argument("query", help="Text to search for") |
| 80 | + |
| 81 | + list_tag_parser = subparsers.add_parser( |
| 82 | + "list-tag", help="List notes that have a given tag" |
| 83 | + ) |
| 84 | + list_tag_parser.add_argument("tag", help="Tag to filter by") |
| 85 | + |
| 86 | + get_parser = subparsers.add_parser( |
| 87 | + "get", help="Retrieve a note by its exact title" |
| 88 | + ) |
| 89 | + get_parser.add_argument("title", help="Title of the note") |
| 90 | + |
| 91 | + subparsers.add_parser("list", help="List all notes") |
| 92 | + |
| 93 | + subparsers.add_parser( |
| 94 | + "reindex", |
| 95 | + help="Rebuild the SQLite search index from the Markdown files on disk", |
| 96 | + ) |
| 97 | + |
| 98 | + return parser |
| 99 | + |
| 100 | + |
| 101 | +def main(argv: Sequence[str] | None = None) -> int: |
| 102 | + parser = build_parser() |
| 103 | + args = parser.parse_args(argv) |
| 104 | + |
| 105 | + with NoteStore(args.notes_dir, args.db) as store: |
| 106 | + if args.command == "add": |
| 107 | + note = Note( |
| 108 | + title=args.title, |
| 109 | + body=_read_body(args), |
| 110 | + tags=_parse_tags(args.tags), |
| 111 | + ) |
| 112 | + store.add_note(note) |
| 113 | + print(f"Added note '{note.title}'") |
| 114 | + return 0 |
| 115 | + |
| 116 | + if args.command == "search": |
| 117 | + results = store.search_notes(args.query) |
| 118 | + if not results: |
| 119 | + print("No notes found.") |
| 120 | + return 0 |
| 121 | + for note in results: |
| 122 | + _print_note_summary(note) |
| 123 | + return 0 |
| 124 | + |
| 125 | + if args.command == "list-tag": |
| 126 | + results = store.find_notes_by_tag(args.tag) |
| 127 | + if not results: |
| 128 | + print(f"No notes found with tag '{args.tag}'.") |
| 129 | + return 0 |
| 130 | + for note in results: |
| 131 | + _print_note_summary(note) |
| 132 | + return 0 |
| 133 | + |
| 134 | + if args.command == "get": |
| 135 | + note = store.find_note_by_title(args.title) |
| 136 | + if note is None: |
| 137 | + print( |
| 138 | + f"No note found with title '{args.title}'.", |
| 139 | + file=sys.stderr, |
| 140 | + ) |
| 141 | + return 1 |
| 142 | + _print_note(note) |
| 143 | + return 0 |
| 144 | + |
| 145 | + if args.command == "list": |
| 146 | + results = store.find_all() |
| 147 | + if not results: |
| 148 | + print("No notes found.") |
| 149 | + return 0 |
| 150 | + for note in results: |
| 151 | + _print_note_summary(note) |
| 152 | + return 0 |
| 153 | + |
| 154 | + if args.command == "reindex": |
| 155 | + count = store.reindex() |
| 156 | + print(f"Reindexed {count} note(s) from '{args.notes_dir}'.") |
| 157 | + return 0 |
| 158 | + |
| 159 | + parser.error(f"Unknown command: {args.command}") |
| 160 | + return 2 |
| 161 | + |
| 162 | + |
| 163 | +if __name__ == "__main__": |
| 164 | + raise SystemExit(main()) |
0 commit comments