papis: Add ability to show tag counts to papis-tags

The `papis tags` command is extended with the functionality to not
just display all tags used in current query, sorted alphabetically,
but also to display how often they appear.

Use `papis tags -c` to show a space-separated count of how often
each tag is used next to the tag name, in descending order.
This commit is contained in:
Marty Oehme 2023-10-02 19:16:23 +02:00
parent c4809c78f7
commit 643deaebec
Signed by: Marty
GPG key ID: EDBF2ED917B2EF6A

View file

@ -14,7 +14,10 @@ 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")
parser.add_argument(
"--count", "-c", help="the query to search for", action="store_true"
)
parser.add_argument("query", nargs="*", help="the query to search for", default="*")
args = parser.parse_args()
@ -22,7 +25,7 @@ def main(db: Database, args) -> None:
query = " ".join(args.query)
docs: list[Document] = db.query(query)
all_tags: set[str] = set()
all_tags: dict[str, int] = {}
for doc in docs:
t: list[str] | str = doc.get("tags", "")
tags = (
@ -30,10 +33,25 @@ def main(db: Database, args) -> None:
)
for tag in tags:
all_tags.add(tag)
if tag == '':
continue
all_tags[tag] = all_tags.get(tag, 0) + 1
if args.count:
print_tags_and_counts(all_tags)
else:
print_tags_only(all_tags)
def print_tags_only(all_tags):
for tag in sorted(all_tags):
print(tag)
def print_tags_and_counts(all_tags):
for tag, count in sorted(all_tags.items(), key=lambda d: d[1], reverse=True):
if args.count:
print(tag, count)
else:
print(tag)
if __name__ == "__main__":
main(database.get(), args)