Update vim and terminal examples
The examples have been moved into their own directory. The vim example now uses pyeval() and vim.bindeval() and loads the Python file through an import (so it gets compiled) for speed improvements.
This commit is contained in:
parent
cbf8d16ed8
commit
1d1021992f
|
@ -2,6 +2,10 @@
|
|||
'''Powerline terminal prompt example.
|
||||
'''
|
||||
|
||||
import os
|
||||
import sys
|
||||
sys.path.append(os.path.join(os.path.dirname(__file__), '../..'))
|
||||
|
||||
from lib.core import Powerline, Segment
|
||||
from lib.renderers import TerminalSegmentRenderer
|
||||
|
|
@ -0,0 +1,113 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
|
||||
import vim
|
||||
import os
|
||||
import re
|
||||
|
||||
from lib.core import Powerline, Segment
|
||||
from lib.renderers import VimSegmentRenderer
|
||||
|
||||
modes = {
|
||||
'n': 'NORMAL',
|
||||
'no': 'N·OPER',
|
||||
'v': 'VISUAL',
|
||||
'V': 'V·LINE',
|
||||
'': 'V·BLCK',
|
||||
's': 'SELECT',
|
||||
'S': 'S·LINE',
|
||||
'': 'S·BLCK',
|
||||
'i': 'INSERT',
|
||||
'R': 'REPLACE',
|
||||
'Rv': 'V·RPLCE',
|
||||
'c': 'COMMND',
|
||||
'cv': 'VIM EX',
|
||||
'ce': 'EX',
|
||||
'r': 'PROMPT',
|
||||
'rm': 'MORE',
|
||||
'r?': 'CONFIRM',
|
||||
'!': 'SHELL',
|
||||
}
|
||||
|
||||
|
||||
def statusline():
|
||||
winwidth = int(vim.bindeval('winwidth(0)'))
|
||||
|
||||
# Prepare segment contents
|
||||
mode = modes[vim.bindeval('mode()')]
|
||||
|
||||
branch = vim.bindeval('fugitive#head(5)')
|
||||
if branch:
|
||||
branch = '⭠ ' + branch
|
||||
|
||||
line_current = int(vim.bindeval('line(".")'))
|
||||
line_end = int(vim.bindeval('line("$")'))
|
||||
line_percent = int(float(line_current) / float(line_end) * 100)
|
||||
|
||||
# Fun gradient colored percent segment
|
||||
line_percent_gradient = [160, 166, 172, 178, 184, 190]
|
||||
line_percent_color = line_percent_gradient[int((len(line_percent_gradient) - 1) * line_percent / 100)]
|
||||
|
||||
col_current = vim.bindeval('col(".")')
|
||||
|
||||
filepath = os.path.split(vim.bindeval('expand("%:~:.")'))
|
||||
filename_color = 231
|
||||
if filepath[0]:
|
||||
filepath[0] += os.sep
|
||||
|
||||
if not filepath[1]:
|
||||
filepath = ('', '[No Name]')
|
||||
filename_color = 250
|
||||
|
||||
readonly = vim.bindeval('&ro ? "⭤ " : ""')
|
||||
modified = vim.bindeval('&mod ? " +" : ""')
|
||||
|
||||
currenttag = vim.bindeval('tagbar#currenttag("%s", "")')
|
||||
|
||||
# The Syntastic segment is center aligned (filler segment on each side) to show off how the filler segments work
|
||||
# Not necessarily how it's going to look in the final theme
|
||||
vim.command('let g:syntastic_stl_format = "⮃ %E{ ERRORS (%e) ⭡ %fe }%W{ WARNINGS (%w) ⭡ %fw } ⮁"')
|
||||
syntastic = vim.bindeval('SyntasticStatuslineFlag()')
|
||||
|
||||
powerline = Powerline([
|
||||
Segment(mode, 22, 148, attr=Segment.ATTR_BOLD),
|
||||
Segment(vim.bindeval('&paste ? "PASTE" : ""'), 231, 166, attr=Segment.ATTR_BOLD),
|
||||
Segment(branch, 250, 240, priority=10),
|
||||
Segment(readonly, 196, 240, draw_divider=False),
|
||||
Segment(filepath[0], 250, 240, draw_divider=False, priority=5),
|
||||
Segment(filepath[1], filename_color, 240, attr=Segment.ATTR_BOLD, draw_divider=not len(modified)),
|
||||
Segment(modified, 220, 240, attr=Segment.ATTR_BOLD),
|
||||
Segment(currenttag, 246, 236, draw_divider=False, priority=100),
|
||||
Segment(filler=True, fg=236, bg=236),
|
||||
Segment(syntastic, 214, 236, attr=Segment.ATTR_BOLD, draw_divider=False, priority=100),
|
||||
Segment(filler=True, fg=236, bg=236),
|
||||
Segment(vim.bindeval('&ff'), 247, 236, side='r', priority=50),
|
||||
Segment(vim.bindeval('&fenc'), 247, 236, side='r', priority=50),
|
||||
Segment(vim.bindeval('&ft'), 247, 236, side='r', priority=50),
|
||||
Segment(str(line_percent).rjust(3) + '%', line_percent_color, 240, side='r', priority=30),
|
||||
Segment('⭡ ', 239, 252, side='r'),
|
||||
Segment(str(line_current).rjust(3), 235, 252, attr=Segment.ATTR_BOLD, side='r', draw_divider=False),
|
||||
Segment(':' + str(col_current).ljust(2), 244, 252, side='r', priority=30, draw_divider=False),
|
||||
])
|
||||
|
||||
renderer = VimSegmentRenderer()
|
||||
stl = powerline.render(renderer, winwidth)
|
||||
|
||||
# Escape percent chars in the statusline, but only if they aren't part of any stl escape sequence
|
||||
stl = re.sub('(\w+)\%(?![-{()<=#*%])', '\\1%%', stl)
|
||||
|
||||
# Create highlighting groups
|
||||
for group, hl in renderer.hl_groups.items():
|
||||
if int(vim.bindeval('hlexists("{0}")'.format(group))):
|
||||
# Only create hl group if it doesn't already exist
|
||||
continue
|
||||
|
||||
vim.command('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']),
|
||||
))
|
||||
|
||||
return stl
|
|
@ -0,0 +1,21 @@
|
|||
" Powerline vim example
|
||||
" Run with :source %
|
||||
|
||||
python import sys, vim, os
|
||||
python sys.path.append(vim.eval('expand("<sfile>:h:h:h")'))
|
||||
python from examples.vim.powerline import statusline
|
||||
|
||||
if exists('*pyeval')
|
||||
let s:pyeval=function('pyeval')
|
||||
else
|
||||
python import json
|
||||
function s:pyeval(e)
|
||||
python vim.command('return ' + json.dumps(eval(vim.eval('a:e'))))
|
||||
endfunction
|
||||
endif
|
||||
|
||||
function! DynStl()
|
||||
return s:pyeval('statusline()')
|
||||
endfunction
|
||||
|
||||
set stl=%!DynStl()
|
|
@ -1,89 +0,0 @@
|
|||
" Powerline vim example
|
||||
" Run with :source %
|
||||
|
||||
set stl=%!DynStl()
|
||||
|
||||
function! DynStl()
|
||||
python <<EOF
|
||||
|
||||
import vim
|
||||
import os
|
||||
import re
|
||||
import sys
|
||||
sys.path.append('.')
|
||||
|
||||
from lib.core import Powerline, Segment
|
||||
from lib.renderers import VimSegmentRenderer
|
||||
|
||||
winwidth = int(vim.eval('winwidth(0)'))
|
||||
|
||||
# Prepare segment contents
|
||||
mode = {
|
||||
'n': 'NORMAL',
|
||||
'v': 'VISUAL',
|
||||
'V': 'V·LINE',
|
||||
'': 'V·BLCK',
|
||||
's': 'SELECT',
|
||||
'S': 'SELECT',
|
||||
'': 'SELECT',
|
||||
'i': 'INSERT',
|
||||
'R': 'REPLACE',
|
||||
'Rv': 'REPLACE',
|
||||
}[vim.eval('mode()')]
|
||||
|
||||
branch = vim.eval('fugitive#head(5)')
|
||||
|
||||
line_current = int(vim.eval('line(".")'))
|
||||
line_end = int(vim.eval('line("$")'))
|
||||
line_percent = int(float(line_current) / float(line_end) * 100)
|
||||
|
||||
# Fun gradient colored percent segment
|
||||
line_percent_gradient = [160, 166, 172, 178, 184, 190]
|
||||
line_percent_color = line_percent_gradient[int((len(line_percent_gradient) - 1) * line_percent / 100)]
|
||||
|
||||
col_current = vim.eval('col(".")')
|
||||
|
||||
filepath = os.path.split(vim.eval('expand("%:~:.")'))
|
||||
if filepath[0]:
|
||||
filepath[0] += os.sep
|
||||
|
||||
powerline = Powerline([
|
||||
Segment(mode, 22, 148, attr=Segment.ATTR_BOLD),
|
||||
Segment('⭠ ' + branch, 250, 240, priority=10),
|
||||
Segment(filepath[0], 250, 240, draw_divider=False, priority=5),
|
||||
Segment(filepath[1], 231, 240, attr=Segment.ATTR_BOLD),
|
||||
Segment(filler=True, fg=236, bg=236),
|
||||
Segment(vim.eval('&ff'), 247, 236, side='r', priority=50),
|
||||
Segment(vim.eval('&fenc'), 247, 236, side='r', priority=50),
|
||||
Segment(vim.eval('&ft'), 247, 236, side='r', priority=50),
|
||||
Segment(str(line_percent).rjust(3) + '%', line_percent_color, 240, side='r', priority=30),
|
||||
Segment('⭡ ', 239, 252, side='r'),
|
||||
Segment(str(line_current).rjust(3), 235, 252, attr=Segment.ATTR_BOLD, side='r', draw_divider=False),
|
||||
Segment(':' + str(col_current).ljust(2), 244, 252, side='r', priority=30, draw_divider=False),
|
||||
])
|
||||
|
||||
renderer = VimSegmentRenderer()
|
||||
stl = powerline.render(renderer, winwidth)
|
||||
|
||||
# Escape percent chars in the statusline, but only if they aren't part of any stl escape sequence
|
||||
stl = re.sub('(\w+)\%(?![-{()<=#*%])', '\\1%%', stl)
|
||||
|
||||
# Create highlighting groups
|
||||
for group, hl in renderer.hl_groups.items():
|
||||
if int(vim.eval('hlexists("{0}")'.format(group))):
|
||||
# Only create hl group if it doesn't already exist
|
||||
continue
|
||||
|
||||
vim.command('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']),
|
||||
))
|
||||
|
||||
vim.command('return "{0}"'.format(stl))
|
||||
|
||||
EOF
|
||||
endfunction
|
Loading…
Reference in New Issue