Assume highlight group is always an iterable

According to the documentation and linter `'highlight_group': 'string'` was not
correct even at the current stage, but it worked. This commit fixes this
situation.
This commit is contained in:
ZyX 2014-09-04 21:21:46 +04:00
parent adc08d0cd8
commit 667cd4bce5
5 changed files with 20 additions and 22 deletions

View File

@ -32,14 +32,6 @@ def pick_gradient_value(grad_list, gradient_level):
return grad_list[int(round(gradient_level * (len(grad_list) - 1) / 100))] return grad_list[int(round(gradient_level * (len(grad_list) - 1) / 100))]
def hl_iter(value):
if type(value) is list:
for v in value:
yield v
else:
yield value
class Colorscheme(object): class Colorscheme(object):
def __init__(self, colorscheme_config, colors_config): def __init__(self, colorscheme_config, colors_config):
'''Initialize a colorscheme.''' '''Initialize a colorscheme.'''
@ -105,12 +97,12 @@ class Colorscheme(object):
def get_highlighting(self, groups, mode, gradient_level=None): def get_highlighting(self, groups, mode, gradient_level=None):
trans = self.translations.get(mode, {}) trans = self.translations.get(mode, {})
for group in hl_iter(groups): for group in groups:
group_props = self.get_group_props(mode, trans, group) group_props = self.get_group_props(mode, trans, group)
if group_props: if group_props:
break break
else: else:
raise KeyError('Highlighting groups not found in colorscheme: ' + ', '.join(hl_iter(groups))) raise KeyError('Highlighting groups not found in colorscheme: ' + ', '.join(groups))
if gradient_level is None: if gradient_level is None:
pick_color = self.colors.__getitem__ pick_color = self.colors.__getitem__

View File

@ -142,7 +142,7 @@ def set_segment_highlighting(pl, colorscheme, segment):
) )
if segment['divider_highlight_group']: if segment['divider_highlight_group']:
segment['divider_highlight'] = colorscheme.get_highlighting( segment['divider_highlight'] = colorscheme.get_highlighting(
segment['divider_highlight_group'], (segment['divider_highlight_group'],),
segment['mode'] segment['mode']
) )
else: else:

View File

@ -750,7 +750,7 @@ def user(pl, segment_info=None, hide_user=None):
euid = _geteuid() euid = _geteuid()
return [{ return [{
'contents': username, 'contents': username,
'highlight_group': 'user' if euid != 0 else ['superuser', 'user'], 'highlight_group': ['user'] if euid != 0 else ['superuser', 'user'],
}] }]
if 'psutil' not in globals(): if 'psutil' not in globals():
user = requires_segment_info(user) user = requires_segment_info(user)
@ -966,7 +966,7 @@ class EmailIMAPSegment(KwThreadedSegment):
elif type(unread_count) != int or not max_msgs: elif type(unread_count) != int or not max_msgs:
return [{ return [{
'contents': str(unread_count), 'contents': str(unread_count),
'highlight_group': 'email_alert', 'highlight_group': ['email_alert'],
}] }]
else: else:
return [{ return [{

View File

@ -29,7 +29,7 @@ def last_status(pl, segment_info):
''' '''
if not segment_info['args'].last_exit_code: if not segment_info['args'].last_exit_code:
return None return None
return [{'contents': str(segment_info['args'].last_exit_code), 'highlight_group': 'exit_fail'}] return [{'contents': str(segment_info['args'].last_exit_code), 'highlight_group': ['exit_fail']}]
@requires_segment_info @requires_segment_info
@ -40,8 +40,14 @@ def last_pipe_status(pl, segment_info):
''' '''
last_pipe_status = segment_info['args'].last_pipe_status last_pipe_status = segment_info['args'].last_pipe_status
if any(last_pipe_status): if any(last_pipe_status):
return [{'contents': str(status), 'highlight_group': 'exit_fail' if status else 'exit_success', 'draw_inner_divider': True} return [
for status in last_pipe_status] {
'contents': str(status),
'highlight_group': ['exit_fail' if status else 'exit_success'],
'draw_inner_divider': True
}
for status in last_pipe_status
]
else: else:
return None return None

View File

@ -30,7 +30,7 @@ class TestShell(TestCase):
pl = Pl() pl = Pl()
segment_info = {'args': Args(last_exit_code=10)} segment_info = {'args': Args(last_exit_code=10)}
self.assertEqual(shell.last_status(pl=pl, segment_info=segment_info), [ self.assertEqual(shell.last_status(pl=pl, segment_info=segment_info), [
{'contents': '10', 'highlight_group': 'exit_fail'} {'contents': '10', 'highlight_group': ['exit_fail']}
]) ])
segment_info['args'].last_exit_code = 0 segment_info['args'].last_exit_code = 0
self.assertEqual(shell.last_status(pl=pl, segment_info=segment_info), None) self.assertEqual(shell.last_status(pl=pl, segment_info=segment_info), None)
@ -45,9 +45,9 @@ class TestShell(TestCase):
self.assertEqual(shell.last_pipe_status(pl=pl, segment_info=segment_info), None) self.assertEqual(shell.last_pipe_status(pl=pl, segment_info=segment_info), None)
segment_info['args'].last_pipe_status = [0, 2, 0] segment_info['args'].last_pipe_status = [0, 2, 0]
self.assertEqual(shell.last_pipe_status(pl=pl, segment_info=segment_info), [ self.assertEqual(shell.last_pipe_status(pl=pl, segment_info=segment_info), [
{'contents': '0', 'highlight_group': 'exit_success', 'draw_inner_divider': True}, {'contents': '0', 'highlight_group': ['exit_success'], 'draw_inner_divider': True},
{'contents': '2', 'highlight_group': 'exit_fail', 'draw_inner_divider': True}, {'contents': '2', 'highlight_group': ['exit_fail'], 'draw_inner_divider': True},
{'contents': '0', 'highlight_group': 'exit_success', 'draw_inner_divider': True} {'contents': '0', 'highlight_group': ['exit_success'], 'draw_inner_divider': True}
]) ])
def test_jobnum(self): def test_jobnum(self):
@ -335,10 +335,10 @@ class TestCommon(TestCase):
with replace_attr(common, 'psutil', new_psutil): with replace_attr(common, 'psutil', new_psutil):
with replace_attr(common, '_geteuid', lambda: 5): with replace_attr(common, '_geteuid', lambda: 5):
self.assertEqual(common.user(pl=pl, segment_info=segment_info), [ self.assertEqual(common.user(pl=pl, segment_info=segment_info), [
{'contents': 'def', 'highlight_group': 'user'} {'contents': 'def', 'highlight_group': ['user']}
]) ])
self.assertEqual(common.user(pl=pl, segment_info=segment_info, hide_user='abc'), [ self.assertEqual(common.user(pl=pl, segment_info=segment_info, hide_user='abc'), [
{'contents': 'def', 'highlight_group': 'user'} {'contents': 'def', 'highlight_group': ['user']}
]) ])
self.assertEqual(common.user(pl=pl, segment_info=segment_info, hide_user='def'), None) self.assertEqual(common.user(pl=pl, segment_info=segment_info, hide_user='def'), None)
with replace_attr(common, '_geteuid', lambda: 0): with replace_attr(common, '_geteuid', lambda: 0):