From c6a0ec7b0d17c8e1f9264a794203628728106d4a Mon Sep 17 00:00:00 2001 From: Arun Prakash Jana Date: Sun, 20 Nov 2016 19:01:02 +0530 Subject: [PATCH] Show usage count of tags in listing. --- README.md | 2 +- buku.1 | 4 ++-- buku.py | 23 ++++++++++++++--------- 3 files changed, 17 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 55161a5..06a5c4f 100644 --- a/README.md +++ b/README.md @@ -236,7 +236,7 @@ Shell completion scripts for Bash, Fish and Zsh can be found in respective subdi - --sall : match all the keywords in URL, title or tags. - --deep : match **substrings** (`match` matches `rematched`) in URL, title and tags. - --sreg : match a regular expression (ignores --deep). - - --stag : search bookmarks by a tag, or show all tags alphabetically (if no arguments). + - --stag : search bookmarks by a tag, or show all tags alphabetically with usage count (if no arguments). - Search results are indexed serially. This index is different from actual database index of a bookmark record which is shown in bold within `[]` after the URL. - **Encryption** is optional and manual. AES256 algorithm is used. To use encryption, the database file should be unlocked (-k) before using buku and locked (-l) afterwards. Between these 2 operations, the database file lies unencrypted on the disk, and NOT in memory. Also, note that the database file is *unencrypted on creation*. - **Proxy** support: environment variable *https_proxy*, if defined, is used to tunnel data for both http and https connections. The supported format is: diff --git a/buku.1 b/buku.1 index 2e3c4cd..56e9dd6 100644 --- a/buku.1 +++ b/buku.1 @@ -53,7 +53,7 @@ Bookmarks with immutable titles are listed with bold '(L)' after the URL. - --sall : match all the keywords in URL, title or tags. - --deep : match \fBsubstrings\fR (`match` matches `rematched`) in URL, title and tags. - --sreg : match a regular expression (ignores --deep). - - --stag : search bookmarks by a tag, or show all tags alphabetically (if no arguments). + - --stag : search bookmarks by a tag, or show all tags alphabetically with usage count (if no arguments). - Search results are indexed serially. This index is different from actual database index of a bookmark record which is shown in bold within '[]' after the URL. .PP \fIEncryption\fR is optional and manual. AES256 algorithm is used. To use encryption, the database file should be unlocked (-k) before using buku and locked (-l) afterwards. Between these 2 operations, the database file lies unencrypted on the disk, and NOT in memory. Also, note that the database file is \fBunencrypted on creation\fR. @@ -113,7 +113,7 @@ Scan for a regular expression match. Search modifier to match substrings. Works with --sany, --sall. .TP .BI \--stag " [...]" -Search bookmarks by a tag. List all tags alphabetically, if no arguments. +Search bookmarks by a tag. List all tags alphabetically, if no arguments. The usage count (number of bookmarks having the tag) is shown within first brackets. .SH ENCRYPTION OPTIONS .TP .BI \-l " " \--lock " [N]" diff --git a/buku.py b/buku.py index 2783ea9..d1b35f5 100755 --- a/buku.py +++ b/buku.py @@ -1078,26 +1078,31 @@ class BukuDb: '''Get list of tags in DB :return: list of unique tags sorted alphabetically + :return: a dictionary of {tag:usage_count} ''' tags = [] unique_tags = [] - query = 'SELECT DISTINCT tags FROM bookmarks ORDER BY tags' - for row in self.cur.execute(query): + dic = {} + qry = 'SELECT DISTINCT tags, COUNT(tags) FROM bookmarks GROUP BY tags' + for row in self.cur.execute(qry): tagset = row[0].strip(DELIM).split(DELIM) for tag in tagset: if tag not in tags: + dic[tag] = row[1] tags += (tag,) + else: + dic[tag] += row[1] if len(tags) == 0: return tags if tags[0] == '': - unique_tags = sorted(tags[1:], key=str.lower) + unique_tags = sorted(tags[1:]) else: - unique_tags = sorted(tags, key=str.lower) + unique_tags = sorted(tags) - return unique_tags + return unique_tags, dic def replace_tag(self, orig, new=None): '''Replace orig tags with new tags in DB for all records. @@ -1642,7 +1647,7 @@ def taglist_subprompt(obj): :return: new command string ''' - unique_tags = obj.get_all_tags() + unique_tags, dic = obj.get_all_tags() msg = '\x1b[7mbuku (? for help)\x1b[0m ' new_results = True @@ -1654,7 +1659,7 @@ def taglist_subprompt(obj): else: count = 1 for tag in unique_tags: - print('%6d. %s' % (count, tag)) + print('%6d. %s (%d)' % (count, tag, dic[tag])) count += 1 try: @@ -2401,10 +2406,10 @@ def main(): if len(args.stag) > 0: search_results = bdb.search_by_tag(' '.join(args.stag)) else: - unique_tags = bdb.get_all_tags() + unique_tags, dic = bdb.get_all_tags() count = 1 for tag in unique_tags: - print('%6d. %s' % (count, tag)) + print('%6d. %s (%d)' % (count, tag, dic[tag])) count += 1 if search_results: