Add setup function

This commit is contained in:
ZyX 2013-04-15 08:31:02 +04:00 committed by ZyX
parent 3c1ec1959f
commit ca13bc53e4
8 changed files with 80 additions and 56 deletions

View File

@ -382,6 +382,7 @@ class Powerline(object):
self.prev_common_config = None
self.prev_ext_config = None
self.pl = None
self.setup_args = None
self.imported_modules = set()
def create_renderer(self, load_main=False, load_colors=False, load_colorscheme=False, load_theme=False):
@ -728,6 +729,13 @@ class Powerline(object):
pass
yield FailedUnicode(safe_unicode(e))
def setup(self, *args, **kwargs):
'''Setup the environment to use powerline.
To be overridden by subclasses, this one only saves args and kwargs.
'''
self.setup_args = (args, kwargs)
def shutdown(self, set_event=True):
'''Shut down all background threads.

View File

@ -16,8 +16,8 @@ class IpythonInfo(object):
class PowerlinePromptManager(PromptManager):
def __init__(self, prompt_powerline, non_prompt_powerline, shell):
self.prompt_powerline = prompt_powerline
self.non_prompt_powerline = non_prompt_powerline
prompt_powerline.setup('prompt_powerline', self)
non_prompt_powerline.setup('non_prompt_powerline', self)
self.powerline_segment_info = IpythonInfo(shell)
self.shell = shell

View File

@ -19,8 +19,8 @@ class IpythonInfo(object):
class PowerlinePrompt(BasePrompt):
def __init__(self, powerline, other_powerline, powerline_last_in, old_prompt):
self.powerline = powerline
self.other_powerline = other_powerline
powerline.setup('powerline', self)
other_powerline.setup('other_powerline', self)
self.powerline_last_in = powerline_last_in
self.powerline_segment_info = IpythonInfo(old_prompt.cache)
self.cache = old_prompt.cache

View File

@ -49,7 +49,7 @@ if !s:has_python
endif
unlet s:has_python
let s:import_cmd = 'from powerline.vim import setup as powerline_setup'
let s:import_cmd = 'from powerline.vim import VimPowerline'
try
let s:pystr = "try:\n"
let s:pystr .= " ".s:import_cmd."\n"
@ -120,8 +120,8 @@ endtry
let s:can_replace_pyeval = !exists('g:powerline_pyeval')
execute s:pycmd 'import vim'
execute s:pycmd 'powerline_setup(pyeval=vim.eval("s:pyeval"), pycmd=vim.eval("s:pycmd"), can_replace_pyeval=int(vim.eval("s:can_replace_pyeval")))'
execute s:pycmd 'del powerline_setup'
execute s:pycmd 'VimPowerline().setup(pyeval=vim.eval("s:pyeval"), pycmd=vim.eval("s:pycmd"), can_replace_pyeval=int(vim.eval("s:can_replace_pyeval")))'
execute s:pycmd 'del VimPowerline'
unlet s:can_replace_pyeval
unlet s:pycmd

View File

@ -104,6 +104,7 @@ class Prompt(object):
def __init__(self, powerline, side, theme, savedpsvar=None, savedps=None, above=False):
self.powerline = powerline
powerline.setup(self)
self.side = side
self.above = above
self.savedpsvar = savedpsvar

View File

@ -55,3 +55,7 @@ class IpythonPowerline(Powerline):
if name in self.theme_overrides:
mergedicts(r, self.theme_overrides[name])
return r
def setup(self, attr, obj):
setattr(obj, attr, self)
super(IpythonPowerline, self).setup(attr, obj)

View File

@ -43,6 +43,10 @@ class ShellPowerline(Powerline):
for key, val in local_themes.items()
))
def setup(self, obj):
obj.powerline = self
super(ShellPowerline, self).setup(obj)
def get_argparser(parser=None, *args, **kwargs):
if not parser:

View File

@ -110,6 +110,59 @@ class VimPowerline(Powerline):
except KeyError:
return super(VimPowerline, self).get_config_paths()
def setup(self, pyeval=None, pycmd=None, can_replace_pyeval=True):
super(VimPowerline, self).setup()
import __main__
if not pyeval:
pyeval = 'pyeval' if sys.version_info < (3,) else 'py3eval'
can_replace_pyeval = True
if not pycmd:
pycmd = get_default_pycmd()
set_pycmd(pycmd)
# pyeval() and vim.bindeval were both introduced in one patch
if not hasattr(vim, 'bindeval') and can_replace_pyeval:
vim.command(('''
function! PowerlinePyeval(e)
{pycmd} powerline.do_pyeval()
endfunction
''').format(pycmd=pycmd))
pyeval = 'PowerlinePyeval'
self.pyeval = pyeval
self.window_statusline = '%!' + pyeval + '(\'powerline.statusline({0})\')'
self.update_renderer()
__main__.powerline = self
if (
bool(int(vim.eval("has('gui_running') && argc() == 0")))
and not vim.current.buffer.name
and len(vim.windows) == 1
):
# Hack to show startup screen. Problems in GUI:
# - Defining local value of &statusline option while computing global
# value purges startup screen.
# - Defining highlight group while computing statusline purges startup
# screen.
# This hack removes the “while computing statusline” part: both things
# are defined, but they are defined right now.
#
# The above condition disables this hack if no GUI is running, Vim did
# not open any files and there is only one window. Without GUI
# everything works, in other cases startup screen is not shown.
self.new_window()
# Cannot have this in one line due to weird newline handling (in :execute
# context newline is considered part of the command in just the same cases
# when bar is considered part of the command (unless defining function
# inside :execute)). vim.command is :execute equivalent regarding this case.
vim.command('augroup Powerline')
vim.command(' autocmd! ColorScheme * :{pycmd} powerline.reset_highlight()'.format(pycmd=pycmd))
vim.command(' autocmd! VimLeavePre * :{pycmd} powerline.shutdown()'.format(pycmd=pycmd))
vim.command('augroup END')
@staticmethod
def get_segment_info():
return {}
@ -211,52 +264,6 @@ def get_default_pycmd():
return 'python' if sys.version_info < (3,) else 'python3'
def setup(pyeval=None, pycmd=None, can_replace_pyeval=True):
import __main__
if not pyeval:
pyeval = 'pyeval' if sys.version_info < (3,) else 'py3eval'
can_replace_pyeval = True
if not pycmd:
pycmd = get_default_pycmd()
set_pycmd(pycmd)
# pyeval() and vim.bindeval were both introduced in one patch
if not hasattr(vim, 'bindeval') and can_replace_pyeval:
vim.command(('''
function! PowerlinePyeval(e)
{pycmd} powerline.do_pyeval()
endfunction
''').format(pycmd=pycmd))
pyeval = 'PowerlinePyeval'
powerline = VimPowerline(pyeval)
powerline.update_renderer()
__main__.powerline = powerline
if (
bool(int(vim.eval("has('gui_running') && argc() == 0")))
and not vim.current.buffer.name
and len(vim.windows) == 1
):
# Hack to show startup screen. Problems in GUI:
# - Defining local value of &statusline option while computing global
# value purges startup screen.
# - Defining highlight group while computing statusline purges startup
# screen.
# This hack removes the “while computing statusline” part: both things
# are defined, but they are defined right now.
#
# The above condition disables this hack if no GUI is running, Vim did
# not open any files and there is only one window. Without GUI
# everything works, in other cases startup screen is not shown.
powerline.new_window()
# Cannot have this in one line due to weird newline handling (in :execute
# context newline is considered part of the command in just the same cases
# when bar is considered part of the command (unless defining function
# inside :execute)). vim.command is :execute equivalent regarding this case.
vim.command('augroup Powerline')
vim.command(' autocmd! ColorScheme * :{pycmd} powerline.reset_highlight()'.format(pycmd=pycmd))
vim.command(' autocmd! VimLeavePre * :{pycmd} powerline.shutdown()'.format(pycmd=pycmd))
vim.command('augroup END')
def setup(*args, **kwargs):
powerline = VimPowerline()
return powerline.setup(*args, **kwargs)