Move processing segment['mode'] to segment.py

This commit is contained in:
ZyX 2014-08-22 21:34:37 +04:00
parent 974ec17bae
commit 5a45b7d291
2 changed files with 25 additions and 22 deletions

View File

@ -1,8 +1,6 @@
# vim:fileencoding=utf-8:noet # vim:fileencoding=utf-8:noet
from __future__ import absolute_import, unicode_literals, division, print_function from __future__ import absolute_import, unicode_literals, division, print_function
import sys
from powerline.lib.watcher import create_file_watcher from powerline.lib.watcher import create_file_watcher
@ -90,7 +88,7 @@ def get_attr_func(contents_func, key, args):
return lambda pl, shutdown_event: func(pl=pl, shutdown_event=shutdown_event, **args) return lambda pl, shutdown_event: func(pl=pl, shutdown_event=shutdown_event, **args)
def process_segment_lister(pl, segment_info, parsed_segments, side, lister, subsegments, patcher_args): def process_segment_lister(pl, segment_info, parsed_segments, side, mode, lister, subsegments, patcher_args):
for subsegment_info, subsegment_update in lister(pl=pl, segment_info=segment_info, **patcher_args): for subsegment_info, subsegment_update in lister(pl=pl, segment_info=segment_info, **patcher_args):
draw_inner_divider = subsegment_update.pop('draw_inner_divider', False) draw_inner_divider = subsegment_update.pop('draw_inner_divider', False)
old_pslen = len(parsed_segments) old_pslen = len(parsed_segments)
@ -100,7 +98,18 @@ def process_segment_lister(pl, segment_info, parsed_segments, side, lister, subs
subsegment.update(subsegment_update) subsegment.update(subsegment_update)
if subsegment_update['priority_multiplier'] and subsegment['priority']: if subsegment_update['priority_multiplier'] and subsegment['priority']:
subsegment['priority'] *= subsegment_update['priority_multiplier'] subsegment['priority'] *= subsegment_update['priority_multiplier']
process_segment(pl, side, subsegment_info, parsed_segments, subsegment)
subsegment_mode = subsegment_update.get('mode')
if subsegment_mode and (
subsegment_mode in subsegment['exclude_modes']
or (
subsegment['include_modes']
and subsegment_mode not in subsegment['include_modes']
)
):
continue
process_segment(pl, side, subsegment_info, parsed_segments, subsegment, subsegment_mode or mode)
new_pslen = len(parsed_segments) new_pslen = len(parsed_segments)
if new_pslen > old_pslen + 1 and draw_inner_divider is not None: if new_pslen > old_pslen + 1 and draw_inner_divider is not None:
for i in range(old_pslen, new_pslen - 1) if side == 'left' else range(old_pslen + 1, new_pslen): for i in range(old_pslen, new_pslen - 1) if side == 'left' else range(old_pslen + 1, new_pslen):
@ -108,22 +117,27 @@ def process_segment_lister(pl, segment_info, parsed_segments, side, lister, subs
return None return None
def process_segment(pl, side, segment_info, parsed_segments, segment): def process_segment(pl, side, segment_info, parsed_segments, segment, mode):
segment = segment.copy()
segment['mode'] = mode
if segment['type'] in ('function', 'segment_list'): if segment['type'] in ('function', 'segment_list'):
pl.prefix = segment['name'] pl.prefix = segment['name']
try: try:
if segment['type'] == 'function': if segment['type'] == 'function':
contents = segment['contents_func'](pl, segment_info) contents = segment['contents_func'](pl, segment_info)
else: else:
contents = segment['contents_func'](pl, segment_info, parsed_segments, side) contents = segment['contents_func'](pl, segment_info, parsed_segments, side, mode)
except Exception as e: except Exception as e:
pl.exception('Exception while computing segment: {0}', str(e)) pl.exception('Exception while computing segment: {0}', str(e))
return return
if contents is None: if contents is None:
return return
if isinstance(contents, list): if isinstance(contents, list):
segment_base = segment.copy() # Needs copying here, but it was performed at the very start of the
# function
segment_base = segment
if contents: if contents:
draw_divider_position = -1 if side == 'left' else 0 draw_divider_position = -1 if side == 'left' else 0
for key, i, newval in ( for key, i, newval in (
@ -206,8 +220,8 @@ def gen_segment_getter(pl, ext, common_config, theme_configs, default_module, ge
'divider_highlight_group': None, 'divider_highlight_group': None,
'before': None, 'before': None,
'after': None, 'after': None,
'contents_func': lambda pl, segment_info, parsed_segments, side: process_segment_lister( 'contents_func': lambda pl, segment_info, parsed_segments, side, mode: process_segment_lister(
pl, segment_info, parsed_segments, side, pl, segment_info, parsed_segments, side, mode,
patcher_args=args, patcher_args=args,
subsegments=subsegments, subsegments=subsegments,
lister=_contents_func, lister=_contents_func,

View File

@ -107,17 +107,8 @@ class Theme(object):
if mode not in segment['exclude_modes'] and ( if mode not in segment['exclude_modes'] and (
not segment['include_modes'] or mode in segment['include_modes'] not segment['include_modes'] or mode in segment['include_modes']
): ):
process_segment(self.pl, side, segment_info, parsed_segments, segment) process_segment(self.pl, side, segment_info, parsed_segments, segment, mode)
for segment in parsed_segments: for segment in parsed_segments:
segment_mode = segment['mode']
if segment_mode and (
segment_mode in segment['exclude_modes']
or (
segment['include_modes']
and segment_mode not in segment['include_modes']
)
):
continue
segment['contents'] = segment['before'] + u(segment['contents'] if segment['contents'] is not None else '') + segment['after'] segment['contents'] = segment['before'] + u(segment['contents'] if segment['contents'] is not None else '') + segment['after']
# Align segment contents # Align segment contents
if segment['width'] and segment['width'] != 'auto': if segment['width'] and segment['width'] != 'auto':
@ -130,6 +121,4 @@ class Theme(object):
# We need to yield a copy of the segment, or else mode-dependent # We need to yield a copy of the segment, or else mode-dependent
# segment contents can't be cached correctly e.g. when caching # segment contents can't be cached correctly e.g. when caching
# non-current window contents for vim statuslines # non-current window contents for vim statuslines
segment = segment.copy() yield segment.copy()
segment['mode'] = segment_mode or mode
yield segment