Special-case None priority in place of -1

This extends priorities in both directions, uses slightly faster `is None`
check, makes it consistent with `interval` special-casing also to `None` and
makes lint able to use one simple `.type()` check in place of `.either()` one.
This commit is contained in:
ZyX 2013-04-09 08:18:37 +04:00
parent 49618fc4e3
commit a8eb0a2471
4 changed files with 8 additions and 9 deletions

View File

@ -377,14 +377,13 @@ Themes
can be aligned with the ``align`` property.
``priority``
Optional segment priority. Segments with priority ``-1`` (the
default priority) will always be included, regardless of the width
of the prompt/statusline.
Optional segment priority. Segments with priority ``None`` (the default
priority, represented by ``null`` in json) will always be included,
regardless of the width of the prompt/statusline.
If the priority is ``0`` or more, the segment may be removed if the
If the priority is any number, the segment may be removed if the
prompt/statusline width is too small for all the segments to be
rendered. A lower number means that the segment has a higher
priority.
rendered. A lower number means that the segment has a higher priority.
Segments are removed according to their priority, with low priority
segments being removed first.

View File

@ -818,7 +818,7 @@ segments_spec = Spec().optional().list(
draw_soft_divider=Spec().type(bool).optional(),
draw_inner_divider=Spec().type(bool).optional(),
module=segment_module_spec(),
priority=Spec().either(Spec().cmp('eq', -1), Spec().cmp('ge', 0.0)).optional(),
priority=Spec().type(int, float, type(None)).optional(),
after=Spec().type(unicode).optional(),
before=Spec().type(unicode).optional(),
width=Spec().either(Spec().unsigned(), Spec().cmp('eq', 'auto')).optional(),

View File

@ -195,7 +195,7 @@ class Renderer(object):
return construct_returned_value(''.join([segment['_rendered_hl'] for segment in segments]) + self.hlstyle(), segments, output_raw)
# Create an ordered list of segments that can be dropped
segments_priority = [segment for segment in sorted(segments, key=lambda segment: segment['priority'], reverse=True) if segment['priority'] > 0]
segments_priority = [segment for segment in sorted(segments, key=lambda segment: segment['priority'], reverse=True) if segment['priority'] is not None]
while sum([segment['_len'] for segment in segments]) > width and len(segments_priority):
segments.remove(segments_priority[0])
segments_priority.pop(0)

View File

@ -73,7 +73,7 @@ def gen_segment_getter(ext, path, theme_configs, default_module=None):
'contents_func': contents_func,
'contents': contents,
'args': get_key(segment, module, 'args', {}) if segment_type == 'function' else {},
'priority': segment.get('priority', -1),
'priority': segment.get('priority', None),
'draw_hard_divider': segment.get('draw_hard_divider', True),
'draw_soft_divider': segment.get('draw_soft_divider', True),
'draw_inner_divider': segment.get('draw_inner_divider', False),