From 6122077124ddea5427f739909d1d42e4bde22fb0 Mon Sep 17 00:00:00 2001 From: Marty Oehme Date: Fri, 25 Aug 2023 16:26:43 +0200 Subject: [PATCH] papis: Add papis-tag tag-listing command Command takes a query and lists all the tags contained in all documents of the query sorted alphabetically. --- writing/.config/papis/scripts/papis-tags | 39 ++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100755 writing/.config/papis/scripts/papis-tags diff --git a/writing/.config/papis/scripts/papis-tags b/writing/.config/papis/scripts/papis-tags new file mode 100755 index 0000000..4803705 --- /dev/null +++ b/writing/.config/papis/scripts/papis-tags @@ -0,0 +1,39 @@ +#!/usr/bin/env python +# papis-short-help: List all tags occuring in query items +# +# Takes a query and spits out a sorted list of all tags contained therein, +# nothing more. +# Can be very useful for things like picking a tag or two and listing all +# items that contain it: +# $ papis tags "*" | fzf | xargs papis show "tags:{}" + +import argparse +from papis import database +from papis.database.base import Database + +from papis.document import Document + +parser = argparse.ArgumentParser() +parser.add_argument("query", nargs="*", help="the query to search for") +args = parser.parse_args() + + +def main(db: Database, args) -> None: + query = " ".join(args.query) + docs: list[Document] = db.query(query) + + all_tags: set[str] = set() + for doc in docs: + t: list[str] | str = doc.get("tags", "") + tags = ( + t.replace(";", ",").replace(" ", "").split(",") if isinstance(t, str) else t + ) + + for tag in tags: + all_tags.add(tag) + + for tag in sorted(all_tags): + print(tag) + +if __name__ == "__main__": + main(database.get(), args)