From 18f4e7007114505aaa9980a35cf0ca9c0ab145b0 Mon Sep 17 00:00:00 2001 From: Arun Prakash Jana Date: Sat, 1 Oct 2016 20:59:53 +0530 Subject: [PATCH] Open a random index with option -o. --- README.md | 3 ++- buku | 27 +++++++++++++++++++++------ buku.1 | 4 ++-- 3 files changed, 25 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index eaeedbf..fabb530 100644 --- a/README.md +++ b/README.md @@ -178,7 +178,8 @@ Shell completion scripts for Bash, Fish and Zsh can be found in respective subdi delete oldtag, if no newtag -j, --json Json formatted output for -p, -s, -S, --st --noprompt do not show the prompt, run and exit - -o, --open N open bookmark at DB index N in web browser + -o, --open [N] open bookmark at DB index N in web browser + open a random index if N is omitted -z, --debug show debug information and additional logs prompt keys: diff --git a/buku b/buku index d3acee6..0a89ca6 100755 --- a/buku +++ b/buku @@ -746,7 +746,7 @@ class BukuDb: self.cur.execute('SELECT MAX(id) from bookmarks') results = self.cur.fetchall() - # Return if the last index was just deleted + # Return if the last index left in DB was just deleted if len(results) == 1 and results[0][0] is None: return @@ -940,6 +940,20 @@ class BukuDb: Params: index ''' + if index == 0: + self.cur.execute('SELECT MAX(id) from bookmarks') + results = self.cur.fetchall() + + # Return if no entries in DB + if len(results) == 1 and results[0][0] is None: + print("No bookmarks added yet ...") + return + + import random + index = random.randint(1, results[0][0]) + logger.debug('Opening random index ' + str(index)) + + query = 'SELECT URL FROM bookmarks WHERE id = ?' try: for row in self.cur.execute(query, (index,)): @@ -1739,7 +1753,8 @@ if __name__ == '__main__': delete oldtag, if no newtag -j, --json Json formatted output for -p, -s, -S, --st --noprompt do not show the prompt, run and exit --o, --open N open bookmark at DB index N in web browser +-o, --open [N] open bookmark at DB index N in web browser + open a random index if N is omitted -z, --debug show debug information and additional logs''') addarg = power_grp.add_argument addarg('-e', '--export', nargs=1, dest='export', metavar='file', @@ -1758,8 +1773,8 @@ if __name__ == '__main__': help=argparse.SUPPRESS) addarg('--noprompt', dest='noninteractive', action='store_true', help=argparse.SUPPRESS) - addarg('-o', '--open', dest='openurl', type=int, metavar='N', - help=argparse.SUPPRESS) + addarg('-o', '--open', nargs='?', dest='openurl', type=int, const=0, + metavar='N', help=argparse.SUPPRESS) addarg('-z', '--debug', dest='debug', action='store_true', help=argparse.SUPPRESS) @@ -1953,8 +1968,8 @@ if __name__ == '__main__': # Open URL in browser if args.openurl is not None: - if args.openurl < 1: - logger.error('Index must be >= 1') + if args.openurl < 0: + logger.error('Index must be >= 0') bdb.close_quit(1) bdb.browse_by_index(args.openurl) diff --git a/buku.1 b/buku.1 index a12f449..0b606d0 100644 --- a/buku.1 +++ b/buku.1 @@ -142,10 +142,10 @@ Output data formatted as json, works with -p, -s, -S, --st. .BI \--noprompt Do not show the prompt, run and exit. .TP -.BI \-o " " \--open " N" +.BI \-o " " \--open " [N]" Open URL at DB index .I N -in browser. +in browser. Open a random index if N is omitted. .TP .BI \-z " " \--debug Show debug information and additional logs.