Remove get_segment_info and passing segment_info through globals

This commit is contained in:
ZyX 2013-03-24 04:09:39 +04:00
parent 49b54353a1
commit b1f9edab4c
5 changed files with 16 additions and 29 deletions

View File

@ -57,7 +57,6 @@ class Powerline(object):
theme_kwargs = { theme_kwargs = {
'ext': ext, 'ext': ext,
'common_config': common_config, 'common_config': common_config,
'segment_info': self.get_segment_info(),
'run_once': run_once, 'run_once': run_once,
} }
local_themes = self.get_local_themes(ext_config.get('local_themes')) local_themes = self.get_local_themes(ext_config.get('local_themes'))
@ -143,14 +142,3 @@ class Powerline(object):
``__init__`` arguments, refer to its documentation. ``__init__`` arguments, refer to its documentation.
''' '''
return None return None
@staticmethod
def get_segment_info():
'''Get information for segments that require ``segment_info`` argument.
To be overridden in subclasses.
:return:
anything accepted by segments as ``segment_info`` argument. Depends
on the segments used, refer to Powerline subclass documentation.
'''
return None

View File

@ -61,10 +61,7 @@ class Renderer(object):
reached. reached.
''' '''
theme = self.get_theme(matcher_info) theme = self.get_theme(matcher_info)
segments = theme.get_segments(side) segments = theme.get_segments(side, segment_info)
if segment_info:
theme.segment_info.update(segment_info)
# Handle excluded/included segments for the current mode # Handle excluded/included segments for the current mode
segments = [self.get_highlighting(segment, mode) for segment in segments segments = [self.get_highlighting(segment, mode) for segment in segments

View File

@ -24,7 +24,7 @@ def requires_segment_info(func):
class Theme(object): class Theme(object):
def __init__(self, ext, theme_config, common_config, top_theme_config=None, segment_info=None, run_once=False): def __init__(self, ext, theme_config, common_config, top_theme_config=None, run_once=False):
self.dividers = theme_config.get('dividers', common_config['dividers']) self.dividers = theme_config.get('dividers', common_config['dividers'])
self.spaces = theme_config.get('spaces', common_config['spaces']) self.spaces = theme_config.get('spaces', common_config['spaces'])
self.segments = { self.segments = {
@ -35,7 +35,6 @@ class Theme(object):
'contents': None, 'contents': None,
'highlight': {'fg': False, 'bg': False, 'attr': 0} 'highlight': {'fg': False, 'bg': False, 'attr': 0}
} }
self.segment_info = segment_info
theme_configs = [theme_config] theme_configs = [theme_config]
if top_theme_config: if top_theme_config:
theme_configs.append(top_theme_config) theme_configs.append(top_theme_config)
@ -62,7 +61,7 @@ class Theme(object):
def get_spaces(self): def get_spaces(self):
return self.spaces return self.spaces
def get_segments(self, side=None): def get_segments(self, side=None, segment_info=None):
'''Return all segments. '''Return all segments.
Function segments are called, and all segments get their before/after Function segments are called, and all segments get their before/after
@ -74,7 +73,7 @@ class Theme(object):
if segment['type'] == 'function': if segment['type'] == 'function':
if (hasattr(segment['contents_func'], 'powerline_requires_segment_info') if (hasattr(segment['contents_func'], 'powerline_requires_segment_info')
and segment['contents_func'].powerline_requires_segment_info): and segment['contents_func'].powerline_requires_segment_info):
contents = segment['contents_func'](segment_info=self.segment_info, **segment['args']) contents = segment['contents_func'](segment_info=segment_info, **segment['args'])
else: else:
contents = segment['contents_func'](**segment['args']) contents = segment['contents_func'](**segment['args'])
if contents is None: if contents is None:

View File

@ -2,6 +2,7 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
'''Powerline prompt and statusline script.''' '''Powerline prompt and statusline script.'''
import sys import sys
import os
try: try:
from powerline.shell import ShellPowerline, get_argparser from powerline.shell import ShellPowerline, get_argparser
@ -13,7 +14,7 @@ except ImportError:
if __name__ == '__main__': if __name__ == '__main__':
args = get_argparser(description=__doc__).parse_args() args = get_argparser(description=__doc__).parse_args()
powerline = ShellPowerline(args, run_once=True) powerline = ShellPowerline(args, run_once=True)
rendered = powerline.renderer.render(width=args.width, side=args.side) rendered = powerline.renderer.render(width=args.width, side=args.side, segment_info=args)
try: try:
sys.stdout.write(rendered) sys.stdout.write(rendered)
except UnicodeEncodeError: except UnicodeEncodeError:

View File

@ -76,18 +76,20 @@ class TestConfig(TestCase):
def test_zsh(self): def test_zsh(self):
from powerline.shell import ShellPowerline from powerline.shell import ShellPowerline
powerline = ShellPowerline(Args(last_pipe_status=[1, 0], ext=['shell'], renderer_module='zsh_prompt'), run_once=False) args = Args(last_pipe_status=[1, 0], ext=['shell'], renderer_module='zsh_prompt')
powerline.renderer.render() powerline = ShellPowerline(args, run_once=False)
powerline = ShellPowerline(Args(last_pipe_status=[1, 0], ext=['shell'], renderer_module='zsh_prompt'), run_once=False) powerline.renderer.render(segment_info=args)
powerline.renderer.render() powerline = ShellPowerline(args, run_once=False)
powerline.renderer.render(segment_info=args)
shutdown(powerline) shutdown(powerline)
def test_bash(self): def test_bash(self):
from powerline.shell import ShellPowerline from powerline.shell import ShellPowerline
powerline = ShellPowerline(Args(last_exit_code=1, ext=['shell'], renderer_module='bash_prompt', config=[('ext', {'shell': {'theme': 'default_leftonly'}})]), run_once=False) args = Args(last_exit_code=1, ext=['shell'], renderer_module='bash_prompt', config=[('ext', {'shell': {'theme': 'default_leftonly'}})])
powerline.renderer.render() powerline = ShellPowerline(args, run_once=False)
powerline = ShellPowerline(Args(last_exit_code=1, ext=['shell'], renderer_module='bash_prompt', config=[('ext', {'shell': {'theme': 'default_leftonly'}})]), run_once=False) powerline.renderer.render(segment_info=args)
powerline.renderer.render() powerline = ShellPowerline(args, run_once=False)
powerline.renderer.render(segment_info=args)
shutdown(powerline) shutdown(powerline)
def test_ipython(self): def test_ipython(self):