implicit colorization conditional

This simplifies many points in the code where the "color" option had to be
checked by centralizing these checks.
This commit is contained in:
Adrian Sampson 2012-12-30 17:18:14 -08:00
parent 9b4c7f77f4
commit 0fdda2fe0b
3 changed files with 52 additions and 61 deletions

View file

@ -176,10 +176,9 @@ def input_options(options, require=False, prompt=None, fallback_prompt=None,
show_letter = found_letter.upper()
is_default = False
# Possibly colorize the letter shortcut.
if config['color'].get(bool):
color = 'turquoise' if is_default else 'blue'
show_letter = colorize(color, show_letter)
# Colorize the letter shortcut.
show_letter = colorize('turquoise' if is_default else 'blue',
show_letter)
# Insert the highlighted letter back into the word.
capitalized.append(
@ -205,8 +204,7 @@ def input_options(options, require=False, prompt=None, fallback_prompt=None,
if numrange:
if isinstance(default, int):
default_name = str(default)
if color:
default_name = colorize('turquoise', default_name)
default_name = colorize('turquoise', default_name)
tmpl = '# selection (default %s)'
prompt_parts.append(tmpl % default_name)
prompt_part_lengths.append(len(tmpl % str(default)))
@ -339,7 +337,7 @@ DARK_COLORS = ["black", "darkred", "darkgreen", "brown", "darkblue",
LIGHT_COLORS = ["darkgray", "red", "green", "yellow", "blue",
"fuchsia", "turquoise", "white"]
RESET_COLOR = COLOR_ESCAPE + "39;49;00m"
def colorize(color, text):
def _colorize(color, text):
"""Returns a string that prints the given text in the given color
in a terminal that is ANSI color-aware. The color must be something
in DARK_COLORS or LIGHT_COLORS.
@ -352,7 +350,16 @@ def colorize(color, text):
raise ValueError('no such color %s', color)
return escape + text + RESET_COLOR
def colordiff(a, b, highlight='red'):
def colorize(color, text):
"""Colorize text if colored output is enabled. (Like _colorize but
conditional.)
"""
if config['color']:
return _colorize(color, text)
else:
return text
def _colordiff(a, b, highlight='red'):
"""Given two values, return the same pair of strings except with
their differences highlighted in the specified color. Strings are
highlighted intelligently to show differences; other values are
@ -391,6 +398,15 @@ def colordiff(a, b, highlight='red'):
return u''.join(a_out), u''.join(b_out)
def colordiff(a, b, highlight='red'):
"""Colorize differences between two values if color is enabled.
(Like _colordiff but conditional.)
"""
if config['color']:
return _colordiff(a, b, highlight)
else:
return unicode(a), unicode(b)
def get_path_formats():
"""Get the configuration's path formats as a list of query/template
pairs.

View file

@ -79,10 +79,7 @@ def _showdiff(field, oldval, newval):
return
if newval != oldval:
if config['color'].get(bool):
oldval, newval = ui.colordiff(oldval, newval)
else:
oldval, newval = unicode(oldval), unicode(newval)
oldval, newval = ui.colordiff(oldval, newval)
print_(u' %s: %s -> %s' % (field, oldval, newval))
@ -113,13 +110,12 @@ def dist_string(dist):
The string is colorized if color is True.
"""
out = '%.1f%%' % ((1 - dist) * 100)
if config['color'].get(bool):
if dist <= autotag.STRONG_REC_THRESH:
out = ui.colorize('green', out)
elif dist <= autotag.MEDIUM_REC_THRESH:
out = ui.colorize('yellow', out)
else:
out = ui.colorize('red', out)
if dist <= autotag.STRONG_REC_THRESH:
out = ui.colorize('green', out)
elif dist <= autotag.MEDIUM_REC_THRESH:
out = ui.colorize('yellow', out)
else:
out = ui.colorize('red', out)
return out
def show_change(cur_artist, cur_album, match):
@ -135,17 +131,12 @@ def show_change(cur_artist, cur_album, match):
else:
album_description = u' (unknown album)'
out = album_description
# Add a suffix if this is a partial match.
if partial:
warning = PARTIAL_MATCH_MESSAGE
else:
warning = None
if config['color'].get(bool) and warning:
warning = ui.colorize('yellow', warning)
out += u' ' + ui.colorize('yellow', PARTIAL_MATCH_MESSAGE)
out = album_description
if warning:
out += u' ' + warning
print_(out)
def format_index(track_info):
@ -171,9 +162,8 @@ def show_change(cur_artist, cur_album, match):
# Hide artists for VA releases.
artist_l, artist_r = u'', u''
if config['color'].get(bool):
artist_l, artist_r = ui.colordiff(artist_l, artist_r)
album_l, album_r = ui.colordiff(album_l, album_r)
artist_l, artist_r = ui.colordiff(artist_l, artist_r)
album_l, album_r = ui.colordiff(album_l, album_r)
print_("Correcting tags from:")
show_album(artist_l, album_l)
@ -182,10 +172,7 @@ def show_change(cur_artist, cur_album, match):
else:
message = u"Tagging: %s - %s" % (match.info.artist, match.info.album)
if match.extra_items or match.extra_tracks:
warning = PARTIAL_MATCH_MESSAGE
if config['color'].get(bool):
warning = ui.colorize('yellow', PARTIAL_MATCH_MESSAGE)
message += u' ' + warning
message += u' ' + ui.colorize('yellow', PARTIAL_MATCH_MESSAGE)
print_(message)
# Distance/similarity.
@ -203,17 +190,15 @@ def show_change(cur_artist, cur_album, match):
cur_title = item.title
new_title = track_info.title
if item.length and track_info.length:
cur_length = ui.human_seconds_short(item.length)
new_length = ui.human_seconds_short(track_info.length)
if config['color'].get(bool):
cur_length = ui.colorize('red', cur_length)
new_length = ui.colorize('red', new_length)
cur_length = ui.colorize('red',
ui.human_seconds_short(item.length))
new_length = ui.colorize('red',
ui.human_seconds_short(track_info.length))
# Possibly colorize changes.
if config['color'].get(bool):
cur_title, new_title = ui.colordiff(cur_title, new_title)
cur_track = ui.colorize('red', cur_track)
new_track = ui.colorize('red', new_track)
# Colorize changes.
cur_title, new_title = ui.colordiff(cur_title, new_title)
cur_track = ui.colorize('red', cur_track)
new_track = ui.colorize('red', new_track)
# Show filename (non-colorized) when title is not set.
if not item.title.strip():
@ -242,13 +227,11 @@ def show_change(cur_artist, cur_album, match):
for track_info in match.extra_tracks:
line = u' * Missing track: {0} ({1})'.format(track_info.title,
format_index(track_info))
if config['color'].get(bool):
line = ui.colorize('yellow', line)
line = ui.colorize('yellow', line)
print_(line)
for item in match.extra_items:
line = u' * Unmatched track: {0} ({1})'.format(item.title, item.track)
if config['color'].get(bool):
line = ui.colorize('yellow', line)
line = ui.colorize('yellow', line)
print_(line)
def show_item_change(item, match):
@ -259,9 +242,8 @@ def show_item_change(item, match):
cur_title, new_title = item.title, match.info.title
if cur_artist != new_artist or cur_title != new_title:
if config['color'].get():
cur_artist, new_artist = ui.colordiff(cur_artist, new_artist)
cur_title, new_title = ui.colordiff(cur_title, new_title)
cur_artist, new_artist = ui.colordiff(cur_artist, new_artist)
cur_title, new_title = ui.colordiff(cur_title, new_title)
print_("Correcting track tags from:")
print_(" %s - %s" % (cur_artist, cur_title))
@ -378,8 +360,7 @@ def choose_candidate(candidates, singleton, rec, cur_artist=None,
# Point out the partial matches.
if match.extra_items or match.extra_tracks:
warning = PARTIAL_MATCH_MESSAGE
if config['color'].get(bool):
warning = ui.colorize('yellow', warning)
warning = ui.colorize('yellow', warning)
line += u' %s' % warning
print_(line)

View file

@ -342,10 +342,7 @@ def automigrate():
migrate_state()
if config_fn:
heading = u'MIGRATED CONFIGURATION'
if beets.config['color']:
heading = ui.colorize('fuchsia', heading)
ui.print_(heading)
ui.print_(ui.colorize('fuchsia', u'MIGRATED CONFIGURATION'))
ui.print_(CONFIG_MIGRATED_MESSAGE.format(
newconfig=util.displayable_path(config_fn))
@ -355,10 +352,7 @@ def automigrate():
newdb=util.displayable_path(db_fn)
))
prompt = u'Press ENTER to continue:'
if beets.config['color']:
prompt = ui.colorize('fuchsia', prompt)
ui.input_(prompt)
ui.input_(ui.colorize('fuchsia', u'Press ENTER to continue:'))
ui.print_()