From 16c01e8d644d2f5c733f1aa5e0606b4511d95052 Mon Sep 17 00:00:00 2001 From: ZyX Date: Sat, 12 Jul 2014 23:39:45 +0400 Subject: [PATCH] Add support for display option --- docs/source/configuration.rst | 15 +++++++++++---- powerline/lint/__init__.py | 28 +++++++++++++++++++--------- powerline/segment.py | 3 +++ powerline/theme.py | 17 +++++++++-------- tests/test_configuration.py | 10 ++++++++++ 5 files changed, 52 insertions(+), 21 deletions(-) diff --git a/docs/source/configuration.rst b/docs/source/configuration.rst index c3c8c110..23a3022e 100644 --- a/docs/source/configuration.rst +++ b/docs/source/configuration.rst @@ -325,13 +325,14 @@ Themes A dict where keys are segment names or strings ``{module}.{name}``. Used to specify default values for various keys: :ref:`after `, + :ref:`args ` (only for function segments), :ref:`before `, :ref:`contents ` (only for string segments if :ref:`name ` is defined), - :ref:`args ` (only for function segments). When - using :ref:`local themes ` values of these keys are - first searched in the segment description, then in ``segment_data`` key of - a local theme, then in ``segment_data`` key of a :ref:`default theme + :ref:`display ` values of these + keys are first searched in the segment description, then in ``segment_data`` + key of a local theme, then in ``segment_data`` key of a :ref:`default theme `. For the :ref:`default theme ` itself step 2 is obviously avoided. @@ -455,6 +456,12 @@ Themes A list of modes where this segment will be included: The segment is *not* included in any modes, *except* for the modes in this list. + ``display`` + .. _config-themes-seg-display: + + Boolean. If false disables displaying of the segment. + Defaults to ``True``. + Segments ======== diff --git a/powerline/lint/__init__.py b/powerline/lint/__init__.py index 074e00e8..1976e876 100644 --- a/powerline/lint/__init__.py +++ b/powerline/lint/__init__.py @@ -668,17 +668,25 @@ shell_colorscheme_spec = (Spec( ).context_message('Error while loading shell colorscheme')) -generic_keys = set(('exclude_modes', 'include_modes', 'width', 'align', 'name', 'draw_soft_divider', 'draw_hard_divider', 'priority', 'after', 'before')) +generic_keys = set(( + 'exclude_modes', 'include_modes', + 'width', 'align', + 'name', + 'draw_soft_divider', 'draw_hard_divider', + 'priority', + 'after', 'before', + 'display' +)) type_keys = { - 'function': set(('args', 'module', 'draw_inner_divider')), - 'string': set(('contents', 'type', 'highlight_group', 'divider_highlight_group')), - 'filler': set(('type', 'highlight_group', 'divider_highlight_group')), - } + 'function': set(('args', 'module', 'draw_inner_divider')), + 'string': set(('contents', 'type', 'highlight_group', 'divider_highlight_group')), + 'filler': set(('type', 'highlight_group', 'divider_highlight_group')), +} required_keys = { - 'function': set(), - 'string': set(('contents',)), - 'filler': set(), - } + 'function': set(), + 'string': set(('contents',)), + 'filler': set(), +} function_keys = set(('args', 'module')) highlight_keys = set(('highlight_group', 'name')) @@ -1071,6 +1079,7 @@ segments_spec = Spec().optional().list( draw_hard_divider=Spec().type(bool).optional(), draw_soft_divider=Spec().type(bool).optional(), draw_inner_divider=Spec().type(bool).optional(), + display=Spec().type(bool).optional(), module=segment_module_spec(), priority=Spec().type(int, float, type(None)).optional(), after=Spec().type(unicode).optional(), @@ -1101,6 +1110,7 @@ theme_spec = (Spec( Spec( after=Spec().type(unicode).optional(), before=Spec().type(unicode).optional(), + display=Spec().type(bool).optional(), args=args_spec().func(lambda *args, **kwargs: check_args(get_all_possible_segments, *args, **kwargs)), contents=Spec().type(unicode).optional(), ), diff --git a/powerline/segment.py b/powerline/segment.py index d0ab2746..836a5739 100644 --- a/powerline/segment.py +++ b/powerline/segment.py @@ -82,6 +82,9 @@ def gen_segment_getter(pl, ext, common_config, theme_configs, default_module=Non pl.exception('Failed to generate segment from {0!r}: {1}', segment, str(e), prefix='segment_generator') return None + if not get_key(segment, module, 'display', True): + return None + if segment_type == 'function': highlight_group = [module + '.' + segment['name'], segment['name']] else: diff --git a/powerline/theme.py b/powerline/theme.py index 6450fab3..9de033a9 100644 --- a/powerline/theme.py +++ b/powerline/theme.py @@ -49,14 +49,15 @@ class Theme(object): for side in ['left', 'right']: for segment in segdict.get(side, []): segment = get_segment(segment, side) - if not run_once: - if segment['startup']: - try: - segment['startup'](pl, shutdown_event) - except Exception as e: - pl.error('Exception during {0} startup: {1}', segment['name'], str(e)) - continue - self.segments[-1][side].append(segment) + if segment: + if not run_once: + if segment['startup']: + try: + segment['startup'](pl, shutdown_event) + except Exception as e: + pl.error('Exception during {0} startup: {1}', segment['name'], str(e)) + continue + self.segments[-1][side].append(segment) def shutdown(self): for line in self.segments: diff --git a/tests/test_configuration.py b/tests/test_configuration.py index b532db81..faf355b5 100644 --- a/tests/test_configuration.py +++ b/tests/test_configuration.py @@ -171,6 +171,16 @@ class TestLines(TestRender): ], width=10) +class TestSegments(TestRender): + @add_p_arg + def test_display(self, p): + with replace_item(globals(), 'config', deepcopy(config)): + config['themes/test/default']['segments']['left'][0]['display'] = False + config['themes/test/default']['segments']['left'][1]['display'] = True + config['themes/test/default']['segments']['right'][0]['display'] = False + self.assertRenderEqual(p, '{344} g{4-}>>{--}') + + class TestColorschemesHierarchy(TestRender): @add_p_arg def test_group_redirects(self, p):