mirror of
https://github.com/powerline/powerline.git
synced 2025-07-28 08:14:41 +02:00
Make it possible to return several segment in segment functions
This requires a couple of minor changes to custom segments. The segment `highlight` key has been renamed to `highlight_group`, and segment functions must return a list of segments dicts instead of just a dict. Closes #88.
This commit is contained in:
parent
973ea572d4
commit
424f979136
@ -61,7 +61,7 @@ Common configuration is a subdictionary that is a value of ``common`` key in
|
|||||||
segments with the same background color.
|
segments with the same background color.
|
||||||
|
|
||||||
``paths``
|
``paths``
|
||||||
.. _config-common-paths
|
.. _config-common-paths:
|
||||||
|
|
||||||
Defines additional paths which will be searched for modules when using
|
Defines additional paths which will be searched for modules when using
|
||||||
:ref:`module segment option <config-themes-seg-module>`. Paths defined here
|
:ref:`module segment option <config-themes-seg-module>`. Paths defined here
|
||||||
@ -166,14 +166,14 @@ Themes
|
|||||||
``string``
|
``string``
|
||||||
A static string segment where the contents is defined in the
|
A static string segment where the contents is defined in the
|
||||||
:ref:`contents option <config-themes-seg-contents>`, and the
|
:ref:`contents option <config-themes-seg-contents>`, and the
|
||||||
highlighting group is defined in the :ref:`highlight option
|
highlighting group is defined in the :ref:`highlight_group
|
||||||
<config-themes-seg-highlight>`.
|
option <config-themes-seg-highlight_group>`.
|
||||||
|
|
||||||
``filler``
|
``filler``
|
||||||
If the statusline is rendered with a specific width, remaining
|
If the statusline is rendered with a specific width, remaining
|
||||||
whitespace is distributed among filler segments. The
|
whitespace is distributed among filler segments. The
|
||||||
highlighting group is defined in the :ref:`highlight option
|
highlighting group is defined in the :ref:`highlight_group
|
||||||
<config-themes-seg-highlight>`.
|
option <config-themes-seg-highlight_group>`.
|
||||||
|
|
||||||
``module``
|
``module``
|
||||||
.. _config-themes-seg-module:
|
.. _config-themes-seg-module:
|
||||||
@ -187,8 +187,8 @@ Themes
|
|||||||
|
|
||||||
Function name, only required for function segments.
|
Function name, only required for function segments.
|
||||||
|
|
||||||
``highlight``
|
``highlight_group``
|
||||||
.. _config-themes-seg-highlight:
|
.. _config-themes-seg-highlight_group:
|
||||||
|
|
||||||
Highlighting group for this segment. Consists of a prioritized list
|
Highlighting group for this segment. Consists of a prioritized list
|
||||||
of highlighting groups, where the first highlighting group that is
|
of highlighting groups, where the first highlighting group that is
|
||||||
@ -259,6 +259,6 @@ A segment function must return one of the following values:
|
|||||||
|
|
||||||
* ``None``, which will remove the segment from the prompt/statusline.
|
* ``None``, which will remove the segment from the prompt/statusline.
|
||||||
* A string, which will be the segment contents.
|
* A string, which will be the segment contents.
|
||||||
* A dict consisting of a ``contents`` string, and a ``highlight`` list. This
|
* A list of dicts consisting of a ``contents`` string, and
|
||||||
is useful for providing a particular highlighting group depending on the
|
a ``highlight_group`` list. This is useful for providing a particular
|
||||||
segment contents.
|
highlighting group depending on the segment contents.
|
||||||
|
@ -32,8 +32,7 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"name": "file_vcs_status",
|
"name": "file_vcs_status",
|
||||||
"draw_divider": false,
|
"draw_divider": false
|
||||||
"before": " "
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"name": "modified_indicator",
|
"name": "modified_indicator",
|
||||||
@ -42,7 +41,7 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"type": "filler",
|
"type": "filler",
|
||||||
"highlight": ["background"]
|
"highlight_group": ["background"]
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"right": [
|
"right": [
|
||||||
@ -72,7 +71,7 @@
|
|||||||
{
|
{
|
||||||
"type": "string",
|
"type": "string",
|
||||||
"contents": " ",
|
"contents": " ",
|
||||||
"highlight": ["line_current_symbol", "line_current"]
|
"highlight_group": ["line_current_symbol", "line_current"]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"name": "line_current",
|
"name": "line_current",
|
||||||
|
@ -7,7 +7,7 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"type": "filler",
|
"type": "filler",
|
||||||
"highlight": ["background"]
|
"highlight_group": ["background"]
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"right": [
|
"right": [
|
||||||
@ -21,7 +21,7 @@
|
|||||||
{
|
{
|
||||||
"type": "string",
|
"type": "string",
|
||||||
"contents": "⭡ ",
|
"contents": "⭡ ",
|
||||||
"highlight": ["line_current_symbol", "line_current"]
|
"highlight_group": ["line_current_symbol", "line_current"]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"name": "line_current",
|
"name": "line_current",
|
||||||
|
@ -89,7 +89,7 @@ class Renderer(object):
|
|||||||
next_segment = segments[index + 1] if index < segments_len - 1 else theme.EMPTY_SEGMENT
|
next_segment = segments[index + 1] if index < segments_len - 1 else theme.EMPTY_SEGMENT
|
||||||
compare_segment = next_segment if segment['side'] == 'left' else prev_segment
|
compare_segment = next_segment if segment['side'] == 'left' else prev_segment
|
||||||
segment['rendered_raw'] = u''
|
segment['rendered_raw'] = u''
|
||||||
outer_padding = ' ' if index == 0 or (index == segments_len - 1 and segment['side'] == 'right') else ''
|
outer_padding = ' ' if (index == 0 and segment['side'] == 'left') or (index == segments_len - 1 and segment['side'] == 'right') else ''
|
||||||
divider_type = 'soft' if compare_segment['highlight'][mode]['bg'] == segment['highlight'][mode]['bg'] else 'hard'
|
divider_type = 'soft' if compare_segment['highlight'][mode]['bg'] == segment['highlight'][mode]['bg'] else 'hard'
|
||||||
divider = theme.get_divider(segment['side'], divider_type)
|
divider = theme.get_divider(segment['side'], divider_type)
|
||||||
divider_hl = ''
|
divider_hl = ''
|
||||||
|
@ -34,11 +34,12 @@ class Segment(object):
|
|||||||
except AttributeError:
|
except AttributeError:
|
||||||
raise TypeError('Unknown segment type: {0}'.format(segment_type))
|
raise TypeError('Unknown segment type: {0}'.format(segment_type))
|
||||||
contents, contents_func, key = get_segment_info(segment)
|
contents, contents_func, key = get_segment_info(segment)
|
||||||
highlighting_group = segment.get('highlight', segment.get('name'))
|
highlight_group = segment.get('highlight_group', segment.get('name'))
|
||||||
return {
|
return {
|
||||||
'key': key,
|
'key': key,
|
||||||
'type': segment_type,
|
'type': segment_type,
|
||||||
'highlight': self.colorscheme.get_group_highlighting(highlighting_group),
|
'highlight_group': highlight_group,
|
||||||
|
'highlight': self.colorscheme.get_group_highlighting(highlight_group),
|
||||||
'before': segment.get('before', ''),
|
'before': segment.get('before', ''),
|
||||||
'after': segment.get('after', ''),
|
'after': segment.get('after', ''),
|
||||||
'contents_func': contents_func,
|
'contents_func': contents_func,
|
||||||
|
@ -53,10 +53,10 @@ def hostname():
|
|||||||
def user():
|
def user():
|
||||||
user = os.environ.get('USER')
|
user = os.environ.get('USER')
|
||||||
euid = os.geteuid()
|
euid = os.geteuid()
|
||||||
return {
|
return [{
|
||||||
'contents': user,
|
'contents': user,
|
||||||
'highlight': 'user' if euid != 0 else ['superuser', 'user'],
|
'highlight_group': 'user' if euid != 0 else ['superuser', 'user'],
|
||||||
}
|
}]
|
||||||
|
|
||||||
|
|
||||||
def branch():
|
def branch():
|
||||||
@ -140,20 +140,26 @@ def weather(unit='c', location_query=None):
|
|||||||
return u'{0} {1}°{2}'.format(icon, condition['temp'], unit.upper())
|
return u'{0} {1}°{2}'.format(icon, condition['temp'], unit.upper())
|
||||||
|
|
||||||
|
|
||||||
def system_load(format='{avg[0]:.1f}, {avg[1]:.1f}, {avg[2]:.1f}', threshold_good=1, threshold_bad=2):
|
def system_load(format='{avg:.1f}', threshold_good=1, threshold_bad=2):
|
||||||
from multiprocessing import cpu_count
|
import multiprocessing
|
||||||
averages = os.getloadavg()
|
cpu_count = multiprocessing.cpu_count()
|
||||||
normalized = averages[1] / cpu_count()
|
ret = []
|
||||||
if normalized < threshold_good:
|
for avg in os.getloadavg():
|
||||||
gradient = 'system_load_good'
|
normalized = avg / cpu_count
|
||||||
elif normalized < threshold_bad:
|
if normalized < threshold_good:
|
||||||
gradient = 'system_load_bad'
|
hl = 'system_load_good'
|
||||||
else:
|
elif normalized < threshold_bad:
|
||||||
gradient = 'system_load_ugly'
|
hl = 'system_load_bad'
|
||||||
return {
|
else:
|
||||||
'contents': format.format(avg=averages),
|
hl = 'system_load_ugly'
|
||||||
'highlight': [gradient, 'system_load']
|
ret.append({
|
||||||
}
|
'contents': format.format(avg=avg),
|
||||||
|
'highlight_group': [hl, 'system_load'],
|
||||||
|
'draw_divider': False,
|
||||||
|
})
|
||||||
|
ret[0]['contents'] += ' '
|
||||||
|
ret[1]['contents'] += ' '
|
||||||
|
return ret
|
||||||
|
|
||||||
|
|
||||||
def cpu_load_percent(measure_interval=.5):
|
def cpu_load_percent(measure_interval=.5):
|
||||||
|
@ -90,10 +90,10 @@ def file_name(display_no_file=False, no_file_text='[No file]'):
|
|||||||
if not file_name and not display_no_file:
|
if not file_name and not display_no_file:
|
||||||
return None
|
return None
|
||||||
if not file_name:
|
if not file_name:
|
||||||
return {
|
return [{
|
||||||
'contents': no_file_text,
|
'contents': no_file_text,
|
||||||
'highlight': ['file_name_no_file', 'file_name'],
|
'highlight_group': ['file_name_no_file', 'file_name'],
|
||||||
}
|
}]
|
||||||
return file_name.decode('utf-8')
|
return file_name.decode('utf-8')
|
||||||
|
|
||||||
|
|
||||||
@ -128,10 +128,10 @@ def line_percent(gradient=False):
|
|||||||
percentage = int(line_current * 100 // line_last)
|
percentage = int(line_current * 100 // line_last)
|
||||||
if not gradient:
|
if not gradient:
|
||||||
return percentage
|
return percentage
|
||||||
return {
|
return [{
|
||||||
'contents': percentage,
|
'contents': percentage,
|
||||||
'highlight': ['line_percent_gradient' + str(int(5 * percentage // 100) + 1), 'line_percent'],
|
'highlight_group': ['line_percent_gradient' + str(int(5 * percentage // 100) + 1), 'line_percent'],
|
||||||
}
|
}]
|
||||||
|
|
||||||
|
|
||||||
def line_current():
|
def line_current():
|
||||||
@ -166,8 +166,12 @@ def file_vcs_status():
|
|||||||
if not status:
|
if not status:
|
||||||
return None
|
return None
|
||||||
status = status.strip()
|
status = status.strip()
|
||||||
return {
|
ret = []
|
||||||
'contents': status,
|
for status in status:
|
||||||
'highlight': ['file_vcs_status_' + status, 'file_vcs_status'],
|
ret.append({
|
||||||
}
|
'contents': status,
|
||||||
|
'highlight_group': ['file_vcs_status_' + status, 'file_vcs_status'],
|
||||||
|
})
|
||||||
|
ret[0]['before'] = ' '
|
||||||
|
return ret
|
||||||
return None
|
return None
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
from collections import defaultdict
|
from collections import defaultdict
|
||||||
import copy
|
from copy import copy
|
||||||
|
|
||||||
from .segment import Segment
|
from .segment import Segment
|
||||||
|
|
||||||
@ -38,24 +38,30 @@ class Theme(object):
|
|||||||
and ljust/rjust properties applied.
|
and ljust/rjust properties applied.
|
||||||
'''
|
'''
|
||||||
for side in [side] if side else ['left', 'right']:
|
for side in [side] if side else ['left', 'right']:
|
||||||
|
parsed_segments = []
|
||||||
for segment in self.segments[side]:
|
for segment in self.segments[side]:
|
||||||
if segment['type'] == 'function':
|
if segment['type'] == 'function':
|
||||||
contents = segment['contents_func'](**segment['args'])
|
contents = segment['contents_func'](**segment['args'])
|
||||||
if contents is None:
|
if contents is None:
|
||||||
continue
|
continue
|
||||||
try:
|
if isinstance(contents, list):
|
||||||
segment['highlight'] = self.colorscheme.get_group_highlighting(contents['highlight'])
|
for subsegment in contents:
|
||||||
segment['contents'] = contents['contents']
|
segment_copy = copy(segment)
|
||||||
except TypeError:
|
segment_copy.update(subsegment)
|
||||||
|
parsed_segments.append(segment_copy)
|
||||||
|
else:
|
||||||
segment['contents'] = contents
|
segment['contents'] = contents
|
||||||
|
parsed_segments.append(segment)
|
||||||
elif segment['type'] == 'filler' or (segment['type'] == 'string' and segment['contents'] is not None):
|
elif segment['type'] == 'filler' or (segment['type'] == 'string' and segment['contents'] is not None):
|
||||||
pass
|
parsed_segments.append(segment)
|
||||||
else:
|
else:
|
||||||
continue
|
continue
|
||||||
|
for segment in parsed_segments:
|
||||||
|
segment['highlight'] = self.colorscheme.get_group_highlighting(segment['highlight_group'])
|
||||||
segment['contents'] = (segment['before'] + unicode(segment['contents']) + segment['after'])\
|
segment['contents'] = (segment['before'] + unicode(segment['contents']) + segment['after'])\
|
||||||
.ljust(segment['ljust'])\
|
.ljust(segment['ljust'])\
|
||||||
.rjust(segment['rjust'])
|
.rjust(segment['rjust'])
|
||||||
# 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
|
||||||
yield copy.copy(segment)
|
yield copy(segment)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user