Colorize is now to be called with the abstract color_name instead of the color.

E.g., `colorize('text_success', 'hello world')`

To ensure compatibility with 3rd party plugins, a valid color ('red') can still be passed,
but it will be logged.
This commit is contained in:
Tom Jaspers 2015-01-29 14:33:57 +01:00
parent ea687baebd
commit f483012183
4 changed files with 32 additions and 33 deletions

View file

@ -191,9 +191,7 @@ def input_options(options, require=False, prompt=None, fallback_prompt=None,
is_default = False
# Colorize the letter shortcut.
show_letter = colorize(COLORS['action_default']
if is_default
else COLORS['action'],
show_letter = colorize('action_default' if is_default else 'action',
show_letter)
# Insert the highlighted letter back into the word.
@ -220,7 +218,7 @@ def input_options(options, require=False, prompt=None, fallback_prompt=None,
if numrange:
if isinstance(default, int):
default_name = str(default)
default_name = colorize(COLORS['action_default'], default_name)
default_name = colorize('action_default', default_name)
tmpl = '# selection (default %s)'
prompt_parts.append(tmpl % default_name)
prompt_part_lengths.append(len(tmpl % str(default)))
@ -381,18 +379,24 @@ def _colorize(color, text):
return escape + text + RESET_COLOR
def colorize(color, text):
def colorize(color_name, text):
"""Colorize text if colored output is enabled. (Like _colorize but
conditional.)
"""
if config['ui']['color']:
# In case a 3rd party plugin is still passing the actual color ('red')
# instead of the abstract color name ('text_error')
color = COLORS.get(color_name)
if not color:
log.debug(u'Invalid color_name: {0}', color_name)
color = color_name
return _colorize(color, text)
else:
return text
def _colordiff(a, b, highlight=COLORS['text_highlight'],
minor_highlight=COLORS['text_highlight_minor']):
def _colordiff(a, b, highlight='text_highlight',
minor_highlight='text_highlight_minor'):
"""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
@ -442,7 +446,7 @@ def _colordiff(a, b, highlight=COLORS['text_highlight'],
return u''.join(a_out), u''.join(b_out)
def colordiff(a, b, highlight=COLORS['text_highlight']):
def colordiff(a, b, highlight='text_highlight'):
"""Colorize differences between two values if color is enabled.
(Like _colordiff but conditional.)
"""
@ -556,8 +560,8 @@ def _field_diff(field, old, new):
if isinstance(oldval, basestring):
oldstr, newstr = colordiff(oldval, newstr)
else:
oldstr = colorize(COLORS['text_error'], oldstr)
newstr = colorize(COLORS['text_error'], newstr)
oldstr = colorize('text_error', oldstr)
newstr = colorize('text_error', newstr)
return u'{0} -> {1}'.format(oldstr, newstr)
@ -593,7 +597,7 @@ def show_model_changes(new, old=None, fields=None, always=False):
changes.append(u' {0}: {1}'.format(
field,
colorize(COLORS['text_highlight'], new.formatted()[field])
colorize('text_highlight', new.formatted()[field])
))
# Print changes.

View file

@ -175,11 +175,11 @@ def dist_string(dist):
"""
out = '%.1f%%' % ((1 - dist) * 100)
if dist <= config['match']['strong_rec_thresh'].as_number():
out = ui.colorize(ui.COLORS['text_success'], out)
out = ui.colorize('text_success', out)
elif dist <= config['match']['medium_rec_thresh'].as_number():
out = ui.colorize(ui.COLORS['text_warning'], out)
out = ui.colorize('text_warning', out)
else:
out = ui.colorize(ui.COLORS['text_error'], out)
out = ui.colorize('text_error', out)
return out
@ -196,8 +196,7 @@ def penalty_string(distance, limit=None):
if penalties:
if limit and len(penalties) > limit:
penalties = penalties[:limit] + ['...']
return ui.colorize(ui.COLORS['text_warning'],
'(%s)' % ', '.join(penalties))
return ui.colorize('text_warning', '(%s)' % ', '.join(penalties))
def show_change(cur_artist, cur_album, match):
@ -270,8 +269,7 @@ def show_change(cur_artist, cur_album, match):
# Disambiguation.
disambig = disambig_string(match.info)
if disambig:
info.append(ui.colorize(ui.COLORS['text_highlight_minor'],
'(%s)' % disambig))
info.append(ui.colorize('text_highlight_minor', '(%s)' % disambig))
print_(' '.join(info))
# Tracks.
@ -316,9 +314,9 @@ def show_change(cur_artist, cur_album, match):
cur_track, new_track = format_index(item), format_index(track_info)
if cur_track != new_track:
if item.track in (track_info.index, track_info.medium_index):
color = ui.COLORS['text_highlight_minor']
color = 'text_highlight_minor'
else:
color = ui.COLORS['text_highlight']
color = 'text_highlight'
templ = ui.colorize(color, u' (#{0})')
lhs += templ.format(cur_track)
rhs += templ.format(new_track)
@ -330,7 +328,7 @@ def show_change(cur_artist, cur_album, match):
config['ui']['length_diff_thresh'].as_number():
cur_length = ui.human_seconds_short(item.length)
new_length = ui.human_seconds_short(track_info.length)
templ = ui.colorize(ui.COLORS['text_highlight'], u' ({0})')
templ = ui.colorize('text_highlight', u' ({0})')
lhs += templ.format(cur_length)
rhs += templ.format(new_length)
lhs_width += len(cur_length) + 3
@ -365,14 +363,14 @@ def show_change(cur_artist, cur_album, match):
line = ' ! %s (#%s)' % (track_info.title, format_index(track_info))
if track_info.length:
line += ' (%s)' % ui.human_seconds_short(track_info.length)
print_(ui.colorize(ui.COLORS['text_warning'], line))
print_(ui.colorize('text_warning', line))
if match.extra_items:
print_('Unmatched tracks:')
for item in match.extra_items:
line = ' ! %s (#%s)' % (item.title, format_index(item))
if item.length:
line += ' (%s)' % ui.human_seconds_short(item.length)
print_(ui.colorize(ui.COLORS['text_warning'], line))
print_(ui.colorize('text_warning', line))
def show_item_change(item, match):
@ -409,8 +407,7 @@ def show_item_change(item, match):
# Disambiguation.
disambig = disambig_string(match.info)
if disambig:
info.append(ui.colorize(ui.COLORS['text_highlight_minor'],
'(%s)' % disambig))
info.append(ui.colorize('text_highlight_minor', '(%s)' % disambig))
print_(' '.join(info))
@ -569,7 +566,7 @@ def choose_candidate(candidates, singleton, rec, cur_artist=None,
# Disambiguation
disambig = disambig_string(match.info)
if disambig:
line.append(ui.colorize(ui.COLORS['text_highlight_minor'],
line.append(ui.colorize('text_highlight_minor',
'(%s)' % disambig))
print_(' '.join(line))
@ -1004,7 +1001,7 @@ def update_items(lib, query, album, move, pretend):
# Item deleted?
if not os.path.exists(syspath(item.path)):
ui.print_obj(item, lib)
ui.print_(ui.colorize(ui.COLORS['text_error'], u' deleted'))
ui.print_(ui.colorize('text_error', u' deleted'))
if not pretend:
item.remove(True)
affected_albums.add(item.album_id)

View file

@ -444,11 +444,9 @@ class FetchArtPlugin(plugins.BeetsPlugin):
if path:
album.set_art(path, False)
album.store()
message = ui.colorize(ui.COLORS['text_success'],
'found album art')
message = ui.colorize('text_success', 'found album art')
else:
message = ui.colorize(ui.COLORS['text_error'],
'no art found')
message = ui.colorize('text_error', 'no art found')
self._log.info(u'{0.albumartist} - {0.album}: {1}', album, message)

View file

@ -73,14 +73,14 @@ def play_music(lib, opts, args, log):
item_type += 's' if len(selection) > 1 else ''
if not selection:
ui.print_(ui.colorize(ui.COLORS['text_warning'],
ui.print_(ui.colorize('text_warning',
'No {0} to play.'.format(item_type)))
return
# Warn user before playing any huge playlists.
if len(selection) > 100:
ui.print_(ui.colorize(
ui.COLORS['text_warning'],
'text_warning',
'You are about to queue {0} {1}.'.format(len(selection), item_type)
))