From f319ddc33b870e0997e2d8a4646c51e81251147e Mon Sep 17 00:00:00 2001 From: ZyX Date: Sat, 23 Feb 2013 15:04:15 +0400 Subject: [PATCH] Ignore highlight definitions for function segments Also removed divider_highlight_group from configuration: it is actually used only in function segments. Fixes #215 --- docs/source/configuration.rst | 3 ++- .../config_files/themes/shell/default.json | 3 +-- .../themes/shell/default_leftonly.json | 3 +-- .../config_files/themes/tmux/default.json | 22 ++++++++----------- .../config_files/themes/vim/default.json | 12 ++++------ powerline/config_files/themes/wm/default.json | 12 +++++----- powerline/lib/__init__.py | 18 +++++++++++++++ powerline/renderer.py | 2 +- powerline/segment.py | 5 ++--- powerline/segments/common.py | 17 +++++++++++--- powerline/segments/vim.py | 14 +++++++++--- 11 files changed, 69 insertions(+), 42 deletions(-) diff --git a/docs/source/configuration.rst b/docs/source/configuration.rst index 98111267..5bc8ba93 100644 --- a/docs/source/configuration.rst +++ b/docs/source/configuration.rst @@ -90,7 +90,6 @@ segments that you may want to customize right away: { "name": "weather", "priority": 50, - "divider_highlight_group": "background:divider" "args": { "unit": "f", "location_query": "oslo, norway" @@ -289,6 +288,8 @@ Themes of highlighting groups, where the first highlighting group that is available in the colorscheme is used. + Ignored for segments that have ``function`` type. + ``before`` .. _config-themes-seg-before: diff --git a/powerline/config_files/themes/shell/default.json b/powerline/config_files/themes/shell/default.json index a2fb4c53..fe34dd89 100644 --- a/powerline/config_files/themes/shell/default.json +++ b/powerline/config_files/themes/shell/default.json @@ -29,8 +29,7 @@ "name": "cwd", "args": { "dir_limit_depth": 3 - }, - "divider_highlight_group": "cwd:divider" + } } ], "right": [ diff --git a/powerline/config_files/themes/shell/default_leftonly.json b/powerline/config_files/themes/shell/default_leftonly.json index 46e8c392..85570107 100644 --- a/powerline/config_files/themes/shell/default_leftonly.json +++ b/powerline/config_files/themes/shell/default_leftonly.json @@ -32,8 +32,7 @@ "name": "cwd", "args": { "dir_limit_depth": 3 - }, - "divider_highlight_group": "cwd:divider" + } }, { "name": "last_status", diff --git a/powerline/config_files/themes/tmux/default.json b/powerline/config_files/themes/tmux/default.json index 841a75f7..d4f12d43 100644 --- a/powerline/config_files/themes/tmux/default.json +++ b/powerline/config_files/themes/tmux/default.json @@ -22,37 +22,33 @@ "right": [ { "name": "uptime", - "priority": 50, - "divider_highlight_group": "background:divider" + "priority": 50 }, { "name": "external_ip", - "priority": 50, - "divider_highlight_group": "background:divider" + "priority": 50 }, { "name": "network_load", - "priority": 50, - "divider_highlight_group": "background:divider" + "priority": 50 }, { "name": "system_load", - "priority": 50, - "divider_highlight_group": "background:divider" + "priority": 50 }, { "name": "weather", - "priority": 50, - "divider_highlight_group": "background:divider" + "priority": 50 }, { "name": "date" }, { "name": "date", - "args": {"format": "%H:%M"}, - "highlight_group": ["time", "date"], - "divider_highlight_group": "time:divider" + "args": { + "format": "%H:%M", + "istime": true + } }, { "name": "email_imap_alert", diff --git a/powerline/config_files/themes/vim/default.json b/powerline/config_files/themes/vim/default.json index 543d717e..3d64018c 100644 --- a/powerline/config_files/themes/vim/default.json +++ b/powerline/config_files/themes/vim/default.json @@ -28,8 +28,7 @@ { "name": "branch", "exclude_modes": ["nc"], - "priority": 60, - "divider_highlight_group": "branch:divider" + "priority": 60 }, { "name": "readonly_indicator", @@ -66,20 +65,17 @@ "name": "file_format", "draw_divider": false, "exclude_modes": ["nc"], - "priority": 50, - "divider_highlight_group": "background:divider" + "priority": 50 }, { "name": "file_encoding", "exclude_modes": ["nc"], - "priority": 50, - "divider_highlight_group": "background:divider" + "priority": 50 }, { "name": "file_type", "exclude_modes": ["nc"], - "priority": 50, - "divider_highlight_group": "background:divider" + "priority": 50 }, { "name": "line_percent", diff --git a/powerline/config_files/themes/wm/default.json b/powerline/config_files/themes/wm/default.json index dd834975..c1cee4b7 100644 --- a/powerline/config_files/themes/wm/default.json +++ b/powerline/config_files/themes/wm/default.json @@ -4,18 +4,18 @@ "right": [ { "name": "weather", - "priority": 50, - "divider_highlight_group": "background:divider" + "priority": 50 }, { "name": "date" }, { "name": "date", - "args": {"format": "%H:%M"}, - "before": "⌚ ", - "highlight_group": ["time", "date"], - "divider_highlight_group": "time:divider" + "args": { + "format": "%H:%M", + "istime": true + }, + "before": "⌚ " }, { "name": "email_imap_alert", diff --git a/powerline/lib/__init__.py b/powerline/lib/__init__.py index fbdacfca..8cdf9d2d 100644 --- a/powerline/lib/__init__.py +++ b/powerline/lib/__init__.py @@ -1,3 +1,5 @@ +from functools import wraps + from powerline.lib.memoize import memoize # NOQA from powerline.lib.humanize_bytes import humanize_bytes # NOQA from powerline.lib.url import urllib_read, urllib_urlencode # NOQA @@ -16,3 +18,19 @@ def mergedicts(d1, d2): mergedicts(d1[k], d2[k]) else: d1[k] = d2[k] + + +def add_divider_highlight_group(highlight_group): + def dec(func): + @wraps(func) + def f(**kwargs): + r = func(**kwargs) + if r: + return [{ + 'contents': r, + 'divider_highlight_group': highlight_group, + }] + else: + return None + return f + return dec diff --git a/powerline/renderer.py b/powerline/renderer.py index 9fdb7015..e9238204 100644 --- a/powerline/renderer.py +++ b/powerline/renderer.py @@ -30,7 +30,7 @@ class Renderer(object): def get_highlighting(self, segment, mode): segment['highlight'] = self.colorscheme.get_highlighting(segment['highlight_group'], mode, segment.get('gradient_level')) if segment['divider_highlight_group']: - segment['divider_highlight'] = self.colorscheme.get_highlighting(segment['divider_highlight_group'], mode) + segment['divider_highlight'] = self.colorscheme.get_highlighting([segment['divider_highlight_group']], mode) else: segment['divider_highlight'] = None return segment diff --git a/powerline/segment.py b/powerline/segment.py index 0efd3949..bb7eacca 100644 --- a/powerline/segment.py +++ b/powerline/segment.py @@ -66,12 +66,11 @@ def gen_segment_getter(ext, path, theme_configs, default_module=None): except KeyError: raise TypeError('Unknown segment type: {0}'.format(segment_type)) contents, contents_func, module = get_segment_info(data, segment) - highlight_group = segment.get('highlight_group', segment.get('name')) - divider_highlight_group = segment.get('divider_highlight_group') + highlight_group = segment_type != 'function' and segment.get('highlight_group') or segment.get('name') return { 'type': segment_type, 'highlight_group': scalar_to_list(highlight_group), - 'divider_highlight_group': scalar_to_list(divider_highlight_group) if divider_highlight_group else None, + 'divider_highlight_group': None, 'before': get_key(segment, module, 'before', ''), 'after': get_key(segment, module, 'after', ''), 'contents_func': contents_func, diff --git a/powerline/segments/common.py b/powerline/segments/common.py index 6f458d01..9b82e97a 100644 --- a/powerline/segments/common.py +++ b/powerline/segments/common.py @@ -3,7 +3,8 @@ import os import sys -from powerline.lib import memoize, urllib_read, urllib_urlencode +from powerline.lib import memoize, urllib_read, urllib_urlencode, add_divider_highlight_group +from functools import wraps def hostname(only_if_ssh=False): @@ -80,19 +81,24 @@ def cwd(dir_shorten_len=None, dir_limit_depth=None): continue ret.append({ 'contents': part, + 'divider_highlight_group': 'cwd:divider', }) ret[-1]['highlight_group'] = ['cwd:current_folder', 'cwd'] return ret -def date(format='%Y-%m-%d'): +def date(format='%Y-%m-%d', istime=False): '''Return the current date. :param str format: strftime-style date format string ''' from datetime import datetime - return datetime.now().strftime(format) + return [{ + 'contents': datetime.now().strftime(format), + 'highlight_group': (['time'] if istime else []) + ['date'], + 'divider_highlight_group': 'time:divider' if istime else None, + }] def fuzzy_time(): @@ -148,6 +154,7 @@ def fuzzy_time(): @memoize(600) +@add_divider_highlight_group('background:divider') def external_ip(query_url='http://ipv4.icanhazip.com/'): '''Return external IP address. @@ -163,6 +170,7 @@ def external_ip(query_url='http://ipv4.icanhazip.com/'): return urllib_read(query_url).strip() +@add_divider_highlight_group('background:divider') def uptime(format='{days:02d}d {hours:02d}h {minutes:02d}m'): '''Return system uptime. @@ -265,6 +273,7 @@ weather_conditions_icons = { @memoize(1800) +@add_divider_highlight_group('background:divider') def weather(unit='c', location_query=None, icons=None): '''Return weather from Yahoo! Weather. @@ -360,6 +369,7 @@ def system_load(format='{avg:.1f}', threshold_good=1, threshold_bad=2): 'contents': format.format(avg=avg), 'highlight_group': [hl, 'system_load'], 'draw_divider': False, + 'divider_highlight_group': 'background:divider', }) ret[0]['draw_divider'] = True ret[0]['contents'] += ' ' @@ -383,6 +393,7 @@ def cpu_load_percent(measure_interval=.5): return u'{0}%'.format(cpu_percent) +@add_divider_highlight_group('background:divider') def network_load(interface='eth0', measure_interval=1, suffix='B/s', binary_prefix=False): '''Return the network load. diff --git a/powerline/segments/vim.py b/powerline/segments/vim.py index 91f80e25..3e6ba129 100644 --- a/powerline/segments/vim.py +++ b/powerline/segments/vim.py @@ -10,7 +10,7 @@ except ImportError: from powerline.bindings.vim import vim_get_func, getbufvar from powerline.theme import requires_segment_info -from powerline.lib import memoize, humanize_bytes +from powerline.lib import memoize, humanize_bytes, add_divider_highlight_group from powerline.lib.vcs import guess from collections import defaultdict @@ -201,6 +201,7 @@ def file_size(segment_info, suffix='B', binary_prefix=False): @requires_segment_info +@add_divider_highlight_group('background:divider') def file_format(segment_info): '''Return file format (i.e. line ending type). @@ -210,6 +211,7 @@ def file_format(segment_info): @requires_segment_info +@add_divider_highlight_group('background:divider') def file_encoding(segment_info): '''Return file encoding/character set. @@ -219,6 +221,7 @@ def file_encoding(segment_info): @requires_segment_info +@add_divider_highlight_group('background:divider') def file_type(segment_info): '''Return file type. @@ -262,7 +265,9 @@ def col_current(segment_info): @window_cached def virtcol_current(): '''Return current visual column with concealed characters ingored''' - return vim_funcs['virtcol']('.') + return [{'contents': vim_funcs['virtcol']('.'), + 'highlight_group': ['virtcol_current', 'col_current'], + }] def modified_buffers(text=u'+', join_str=','): @@ -286,7 +291,10 @@ def branch(segment_info): '''Return the current working branch.''' repo = guess(path=os.path.abspath(segment_info['buffer'].name or os.getcwd())) if repo: - return repo.branch() + return [{ + 'contents': repo.branch(), + 'divider_highlight_group': 'branch:divider', + }] return None