Commit 8333153a authored by adam j hartz's avatar adam j hartz

bookmark: several usability improvements

parent c6f0fd48
......@@ -7,12 +7,15 @@ import json
PLUGIN_SETTINGS.data = {}
# ALIASES
def _add_bookmark(args, stdin=None):
dir = __tako_env__['PWD']
if len(args) == 2:
dir = args[1]
elif len(args) > 2:
return None, 'Too many arguments\n', 1
return None, _ADD_USAGE, 1
else:
args.append('_anonymous')
PLUGIN_SETTINGS.data[args[0]] = dir
......@@ -20,18 +23,35 @@ def _add_bookmark(args, stdin=None):
def _follow_bookmark(args, stdin=None):
if len(args) > 1:
return None, 'Error: too many arguments\n', 1
return None, _FOLLOW_USAGE, 1
if len(args) == 0:
args.append('_anonymous')
if args[0] not in PLUGIN_SETTINGS.data:
return None, 'Error: unknown bookmark %r\n' % args[0], 1
if args[0] == '_anonymous':
emsg = 'No anonymous bookmark set.\n'
else:
emsg = 'Error: unknown bookmark %r\n' % args[0]
return None, emsg, 1
else:
$TAKO_SETTINGS.aliases['cd']([PLUGIN_SETTINGS.data[args[0]]], stdin=None)
def _forget_bookmark(args, stdin=None):
if len(args) > 1:
return None, _FORGET_USAGE, 1
if len(args) == 0:
args.append('_anonymous')
if args[0] not in PLUGIN_SETTINGS.data:
if args[0] == '_anonymous':
emsg = 'No anonymous bookmark set.\n'
else:
emsg = 'Error: unknown bookmark %r\n' % args[0]
return None, emsg, 1
else:
del PLUGIN_SETTINGS.data[args[0]]
def _write_bookmarks(args, stdin=None):
if len(args) > 1:
return None, 'Error: too many arguments\n', 1
return None, _WRITE_USAGE, 1
if len(args) == 1:
fname = args[1]
else:
......@@ -44,7 +64,7 @@ def _write_bookmarks(args, stdin=None):
def _load_bookmarks(args, stdin=None):
if len(args) > 1:
return None, 'Error: too many arguments\n', 1
return None, _LOAD_USAGE, 1
if len(args) == 1:
fname = args[1]
else:
......@@ -55,9 +75,9 @@ def _load_bookmarks(args, stdin=None):
def _list_bookmarks(args, stdin=None):
if len(args) > 0:
return None, 'Error: too many arguments\n', 1
return None, _LIST_USAGE, 1
leftside_len = max(len(i) for i in PLUGIN_SETTINGS.data) + 2
leftside_len = max(len(i) for i in PLUGIN_SETTINGS.data) + 3
out = ''
for k, v in sorted(PLUGIN_SETTINGS.data.items()):
out += '%s \033[0;36m%s\033[0m %s\n' % (k, '.' * (leftside_len-len(k)), v)
......@@ -66,11 +86,15 @@ def _list_bookmarks(args, stdin=None):
PLUGIN_SETTINGS.aliases['bmark'] = _add_bookmark
PLUGIN_SETTINGS.aliases['bgo'] = _follow_bookmark
PLUGIN_SETTINGS.aliases['bdrop'] = _forget_bookmark
PLUGIN_SETTINGS.aliases['bwrite'] = _write_bookmarks
PLUGIN_SETTINGS.aliases['bload'] = _load_bookmarks
PLUGIN_SETTINGS.aliases['blist'] = _list_bookmarks
# TAB COMPLETION
def complete_bookmark_commands(prefix, line, start, end, ctx):
"""Completion for the bookmark plugin"""
args = line.split(' ')
......@@ -83,10 +107,55 @@ def complete_bookmark_commands(prefix, line, start, end, ctx):
return all_bmarks
if curix == 2:
return __tako_completers__['path'](prefix, line, start, end, ctx)
elif args[0] == 'bgo':
elif args[0] in {'bgo', 'bdrop'}:
if curix == 1:
return all_bmarks
from takoshell.completers._aliases import _add_one_completer
_add_one_completer('bookmark_plugin', complete_bookmark_commands, '<completer')
# HELP
_ADD_USAGE = '''Add a bookmark to the in-memory store
Usage:
bmark [NAME] [DIR]
If no name is specified, store the directory in an anonymous bookmark.
If no directory is specified, use the current working directory.
'''
_FOLLOW_USAGE = '''Follow a bookmark
Usage:
bgo [NAME]
If no name is specified, try to move to the anonymous bookmark.
'''
_FORGET_USAGE = '''Delete a bookmark
Usage:
bdrop [NAME]
If no name is specified, try to remove the anonymous bookmark.
'''
_WRITE_USAGE = '''Serialize current bookmarks to disk
Usage:
bwrite [FILE]
If no file is specified, a default location in ~/.local/share/tako/plugins/bookmark is used.
'''
_LOAD_USAGE = '''Load bookmarks from disk
Usage:
bload [FILE]
If no file is specified, a default location in ~/.local/share/tako/plugins/bookmark is used.
'''
_LIST_USAGE = '''List current in-memory bookmarks
Usage:
blist
'''
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment