Create basic vim segment renderer

This commit also includes a basic proof-of-concept demo for creating vim
statuslines. When creating vim statuslines you basically need to first
create the statusline the same way as the terminal demo, use the vim
renderer and then afterwards loop through the collected highlight groups
in the vim renderer and write those as vim highlight statements.

Vim obviously needs a ton of wrapper code to make everything work
properly (separate modes, callbacks, all that stuff) so the current demo
only shows some basic statusline highlighting.
This commit is contained in:
Kim Silkebækken 2012-11-08 14:05:48 +01:00
parent 20892b14d8
commit b461ab1358
3 changed files with 77 additions and 4 deletions

View File

@ -3,3 +3,4 @@ class SegmentRenderer:
raise NotImplementedError
from lib.renderers.terminal import TerminalSegmentRenderer
from lib.renderers.vim import VimSegmentRenderer

59
lib/renderers/vim.py Normal file
View File

@ -0,0 +1,59 @@
#!/usr/bin/env python
from lib.core import Segment
from lib.renderers import SegmentRenderer
class VimSegmentRenderer(SegmentRenderer):
'''Powerline vim segment renderer.
'''
def __init__(self):
self.hl_groups = {}
def hl(self, fg=None, bg=None, attr=None):
'''Highlight a segment.
If an argument is None, the argument is ignored. If an argument is
False, the argument is reset to the terminal defaults. If an argument
is a valid color or attribute, it's added to the vim highlight group.
'''
hl_group = {
'ctermfg': 'NONE',
'guifg': 'NONE',
'ctermbg': 'NONE',
'guibg': 'NONE',
'attr': ['NONE'],
}
# We don't need to explicitly reset attributes in vim, so skip those calls
if not attr and not bg and not fg:
return ''
if fg is not None and fg is not False:
hl_group['ctermfg'] = fg[0]
hl_group['guifg'] = fg[1]
if bg is not None and bg is not False:
hl_group['ctermbg'] = bg[0]
hl_group['guibg'] = bg[1]
if attr is not None and attr is not False and attr != 0:
hl_group['attr'] = []
if attr & Segment.ATTR_BOLD:
hl_group['attr'].append('bold')
if attr & Segment.ATTR_ITALIC:
hl_group['attr'].append('italic')
if attr & Segment.ATTR_UNDERLINE:
hl_group['attr'].append('underline')
hl_group_name = 'Pl_{ctermfg}_{guifg}_{ctermbg}_{guibg}_{attr}'.format(
ctermfg=hl_group['ctermfg'],
guifg=hl_group['guifg'],
ctermbg=hl_group['ctermbg'],
guibg=hl_group['guibg'],
attr=''.join(attr[0] for attr in hl_group['attr']),
)
self.hl_groups[hl_group_name] = hl_group
return '%#{0}#'.format(hl_group_name)

View File

@ -3,7 +3,7 @@
'''
from lib.core import Segment
from lib.renderers import TerminalSegmentRenderer
from lib.renderers import VimSegmentRenderer
powerline = Segment([
Segment('NORMAL', 22, 148, attr=Segment.ATTR_BOLD),
@ -12,12 +12,12 @@ powerline = Segment([
Segment(' ~/projects/powerline/lib/'),
Segment('core.py ', 231, attr=Segment.ATTR_BOLD),
], 250, 240, separate=False, padding=''),
Segment(),
Segment('%<%='),
Segment([
Segment('unix'),
Segment('utf-8'),
Segment('python'),
Segment(' 83%', 247, 240),
Segment(' 83%%', 247, 240),
Segment([
Segment('', 239),
Segment('23', attr=Segment.ATTR_BOLD),
@ -26,4 +26,17 @@ powerline = Segment([
], 245, side='r'),
], bg=236)
print(powerline.render(TerminalSegmentRenderer()))
renderer = VimSegmentRenderer()
stl = powerline.render(renderer)
for group, hl in renderer.hl_groups.items():
print('hi {group} ctermfg={ctermfg} guifg={guifg} guibg={guibg} ctermbg={ctermbg} cterm={attr} gui={attr}'.format(
group=group,
ctermfg=hl['ctermfg'],
guifg='#{0:06x}'.format(hl['guifg']) if hl['guifg'] != 'NONE' else 'NONE',
ctermbg=hl['ctermbg'],
guibg='#{0:06x}'.format(hl['guibg']) if hl['guibg'] != 'NONE' else 'NONE',
attr=','.join(hl['attr']),
))
print('let &stl = "{0}"'.format(stl))