added implicit autocompletion for open and edit cmds
This commit is contained in:
parent
bb45eee96d
commit
dd0475db46
@ -3,7 +3,7 @@ from .. import repo
|
|||||||
from ..configs import config
|
from ..configs import config
|
||||||
from ..uis import get_ui
|
from ..uis import get_ui
|
||||||
from ..endecoder import EnDecoder
|
from ..endecoder import EnDecoder
|
||||||
|
from ..utils import resolve_citekey
|
||||||
|
|
||||||
def parser(subparsers):
|
def parser(subparsers):
|
||||||
parser = subparsers.add_parser('edit',
|
parser = subparsers.add_parser('edit',
|
||||||
@ -19,14 +19,10 @@ def command(args):
|
|||||||
|
|
||||||
ui = get_ui()
|
ui = get_ui()
|
||||||
meta = args.meta
|
meta = args.meta
|
||||||
citekey = args.citekey
|
|
||||||
|
|
||||||
rp = repo.Repository(config())
|
rp = repo.Repository(config())
|
||||||
try:
|
citekey = resolve_citekey(rp, args.citekey, ui=ui, exit_on_fail=True)
|
||||||
paper = rp.pull_paper(citekey)
|
paper = rp.pull_paper(citekey)
|
||||||
except repo.InvalidReference as v:
|
|
||||||
ui.error(v)
|
|
||||||
ui.exit(1)
|
|
||||||
|
|
||||||
coder = EnDecoder()
|
coder = EnDecoder()
|
||||||
if meta:
|
if meta:
|
||||||
|
@ -5,7 +5,7 @@ from ..configs import config
|
|||||||
from ..uis import get_ui
|
from ..uis import get_ui
|
||||||
from .. import color
|
from .. import color
|
||||||
from ..content import system_path
|
from ..content import system_path
|
||||||
|
from ..utils import resolve_citekey
|
||||||
|
|
||||||
def parser(subparsers):
|
def parser(subparsers):
|
||||||
parser = subparsers.add_parser('open',
|
parser = subparsers.add_parser('open',
|
||||||
@ -21,10 +21,11 @@ def command(args):
|
|||||||
|
|
||||||
ui = get_ui()
|
ui = get_ui()
|
||||||
with_command = args.with_command
|
with_command = args.with_command
|
||||||
citekey = args.citekey
|
|
||||||
|
|
||||||
rp = repo.Repository(config())
|
rp = repo.Repository(config())
|
||||||
|
citekey = resolve_citekey(rp, args.citekey, ui=ui, exit_on_fail=True)
|
||||||
paper = rp.pull_paper(citekey)
|
paper = rp.pull_paper(citekey)
|
||||||
|
|
||||||
if with_command is None:
|
if with_command is None:
|
||||||
with_command = config().open_cmd
|
with_command = config().open_cmd
|
||||||
|
|
||||||
|
@ -50,6 +50,11 @@ class Repository(object):
|
|||||||
for key in self.citekeys:
|
for key in self.citekeys:
|
||||||
yield self.pull_paper(key)
|
yield self.pull_paper(key)
|
||||||
|
|
||||||
|
def citekeys_from_prefix(self, prefix):
|
||||||
|
"""Return all citekey beginning with prefix."""
|
||||||
|
return tuple(citekey for citekey in self.citekeys
|
||||||
|
if citekey.startswith(prefix))
|
||||||
|
|
||||||
def pull_paper(self, citekey):
|
def pull_paper(self, citekey):
|
||||||
"""Load a paper by its citekey from disk, if necessary."""
|
"""Load a paper by its citekey from disk, if necessary."""
|
||||||
if citekey in self:
|
if citekey in self:
|
||||||
|
@ -89,7 +89,7 @@ class UI:
|
|||||||
if default is not None:
|
if default is not None:
|
||||||
return default
|
return default
|
||||||
else:
|
else:
|
||||||
try:
|
try: # FIXME options handling !!!
|
||||||
return option_chars.index(answer.lower())
|
return option_chars.index(answer.lower())
|
||||||
except ValueError:
|
except ValueError:
|
||||||
pass
|
pass
|
||||||
|
24
pubs/utils.py
Normal file
24
pubs/utils.py
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
# Function here may belong somewhere else. In the mean time...
|
||||||
|
|
||||||
|
def resolve_citekey(repo, citekey, ui=None, exit_on_fail=True):
|
||||||
|
"""Check that a citekey exists, or autocompletes it if not ambiguous."""
|
||||||
|
# FIXME. Make me optionally non ui interactive/exiting
|
||||||
|
citekeys = repo.citekeys_from_prefix(citekey)
|
||||||
|
if len(citekeys) == 0:
|
||||||
|
if ui is not None:
|
||||||
|
ui.error("No citekey named or beginning with '{}".format(citekey))
|
||||||
|
if exit_on_fail:
|
||||||
|
ui.exit()
|
||||||
|
elif len(citekeys) == 1:
|
||||||
|
if citekeys[0] != citekey:
|
||||||
|
if ui is not None:
|
||||||
|
ui.print_("Provided citekey '{}' has been autocompleted into '{}'".format(citekey, citekeys[0]))
|
||||||
|
citekey = citekeys[0]
|
||||||
|
elif citekey not in citekeys:
|
||||||
|
if ui is not None:
|
||||||
|
ui.error("Be more specific. Provided citekey '{}' matches multiples citekeys: {}".format(citekey, ', '.join(citekeys)))
|
||||||
|
if exit_on_fail:
|
||||||
|
ui.exit()
|
||||||
|
return citekey
|
||||||
|
|
||||||
|
|
Loading…
x
Reference in New Issue
Block a user