parent
81b51cb7a4
commit
42569f7f23
@ -0,0 +1,23 @@
|
||||
+ requires config file (default repo, open command, ...)
|
||||
- printing should include templating engine and several templates for bib types and output
|
||||
* chose existing engine
|
||||
- tests...
|
||||
- import command for interactive import with auto bib
|
||||
* basic title and author search in pdf
|
||||
* online services (scholar, etc.)
|
||||
- add command does not require pdf -> add from bib
|
||||
|
||||
About strings:
|
||||
--------------
|
||||
- pybtex seems to store entries as utf-8 (TODO: check)
|
||||
- so assumption is made that everything is utf-8
|
||||
- conversions are performed at print time
|
||||
|
||||
Config values:
|
||||
--------------
|
||||
[papers]
|
||||
open-cmd = open
|
||||
edit-cmd = edit
|
||||
import-copy = True
|
||||
import-move = False
|
||||
terminal-encoding = from locale or utf8
|
@ -0,0 +1,58 @@
|
||||
# This file contains functions taken from the user interface of the beet
|
||||
# tool (http://beets.radbox.org).
|
||||
#
|
||||
# Copyright 2013, Adrian Sampson.
|
||||
#
|
||||
# Permission is hereby granted, free of charge, to any person obtaining
|
||||
# a copy of this software and associated documentation files (the
|
||||
# "Software"), to deal in the Software without restriction, including
|
||||
# without limitation the rights to use, copy, modify, merge, publish,
|
||||
# distribute, sublicense, and/or sell copies of the Software, and to
|
||||
# permit persons to whom the Software is furnished to do so, subject to
|
||||
# the following conditions:
|
||||
#
|
||||
# The above copyright notice and this permission notice shall be
|
||||
# included in all copies or substantial portions of the Software.
|
||||
|
||||
|
||||
import locale
|
||||
import sys
|
||||
from ConfigParser import NoOptionError
|
||||
|
||||
|
||||
class UserError(Exception):
|
||||
"""UI exception. Commands should throw this in order to display
|
||||
nonrecoverable errors to the user.
|
||||
"""
|
||||
pass
|
||||
|
||||
|
||||
def _encoding(config):
|
||||
"""Tries to guess the encoding used by the terminal."""
|
||||
# Configured override?
|
||||
try:
|
||||
return config.get('papers', 'terminal-encoding')
|
||||
except NoOptionError:
|
||||
# Determine from locale settings.
|
||||
try:
|
||||
return locale.getdefaultlocale()[1] or 'utf8'
|
||||
except ValueError:
|
||||
# Invalid locale environment variable setting. To avoid
|
||||
# failing entirely for no good reason, assume UTF-8.
|
||||
return 'utf8'
|
||||
|
||||
|
||||
def input_():
|
||||
"""Get input and decodes the result to a Unicode string.
|
||||
Raises a UserError if stdin is not available. The prompt is sent to
|
||||
stdout rather than stderr. A printed between the prompt and the
|
||||
input cursor.
|
||||
"""
|
||||
# raw_input incorrectly sends prompts to stderr, not stdout, so we
|
||||
# use print() explicitly to display prompts.
|
||||
# http://bugs.python.org/issue1927
|
||||
try:
|
||||
resp = raw_input()
|
||||
except EOFError:
|
||||
raise UserError('stdin stream ended while input required')
|
||||
return resp.decode(sys.stdin.encoding or 'utf8', 'ignore')
|
@ -0,0 +1,65 @@
|
||||
from beets_ui import _encoding, input_
|
||||
|
||||
from color import colored
|
||||
|
||||
|
||||
class UI:
|
||||
"""UI class. Stores configuration parameters and system information.
|
||||
"""
|
||||
|
||||
def __init__(self, config):
|
||||
self.encoding = _encoding(config)
|
||||
self.color = config.getboolean('papers', 'color')
|
||||
|
||||
def colored(self, s, *args, **kwargs):
|
||||
if self.color:
|
||||
return colored(s, *args, **kwargs)
|
||||
else:
|
||||
return s
|
||||
|
||||
def print_(self, *strings):
|
||||
"""Like print, but rather than raising an error when a character
|
||||
is not in the terminal's encoding's character set, just silently
|
||||
replaces it.
|
||||
"""
|
||||
txt = [s.encode(self.encoding, 'replace')
|
||||
if isinstance(s, unicode) else s
|
||||
for s in strings]
|
||||
print(' '.join(txt))
|
||||
|
||||
def input_choice(self, options, option_chars, default=None, question=''):
|
||||
"""Ask the user to chose between a set of options. The iser is asked
|
||||
to input a char corresponding to the option he choses.
|
||||
|
||||
:param options: list of strings
|
||||
list of options
|
||||
:param option_chars: list of chars
|
||||
chars used to identify options, should be lowercase and not
|
||||
contain duplicates
|
||||
:param default: int
|
||||
default if no option is accepted, if None answer is required
|
||||
:param question: string
|
||||
:returns: int
|
||||
the index of the chosen option
|
||||
"""
|
||||
displayed_chars = [s.upper() if i == default else s
|
||||
for i, s in enumerate(option_chars)]
|
||||
option_str = ', '.join(["[%s]%s" % (self.colored(c, 'cyan'), o)
|
||||
for c, o in zip(displayed_chars, options)])
|
||||
self.print_(question, option_str)
|
||||
while True:
|
||||
answer = input_()
|
||||
if answer is None or answer == '':
|
||||
if default is not None:
|
||||
return default
|
||||
else:
|
||||
try:
|
||||
return option_chars.index(answer.lower())
|
||||
except ValueError:
|
||||
pass
|
||||
self.print_('Incorrect option.', option_str)
|
||||
|
||||
def input_yn(self, question='', default='y'):
|
||||
d = 0 if default in (True, 'y', 'yes') else 1
|
||||
return (True, False)[self.input_choice(['yes', 'no'], ['y', 'n'],
|
||||
default=d, question=question)]
|
Loading…
Reference in new issue