From 9af71dfb4b8771ad51c447c88a13e8c0617cb981 Mon Sep 17 00:00:00 2001 From: Sassan Haradji Date: Fri, 13 Nov 2015 23:40:00 +0330 Subject: [PATCH] ctrlp statusline --- .../colorschemes/vim/__main__.json | 9 +- powerline/config_files/config.json | 3 +- .../config_files/themes/vim/plugin_ctrlp.json | 41 ++++++ powerline/matchers/vim/plugin/ctrlp.py | 11 ++ powerline/segments/vim/plugin/ctrlp.py | 123 ++++++++++++++++++ .../test_python/test_provided_config_files.py | 5 +- tests/test_vim/tests/ctrlp_plugin.vim | 17 +++ 7 files changed, 205 insertions(+), 4 deletions(-) create mode 100644 powerline/config_files/themes/vim/plugin_ctrlp.json create mode 100644 powerline/matchers/vim/plugin/ctrlp.py create mode 100644 powerline/segments/vim/plugin/ctrlp.py create mode 100755 tests/test_vim/tests/ctrlp_plugin.vim diff --git a/powerline/config_files/colorschemes/vim/__main__.json b/powerline/config_files/colorschemes/vim/__main__.json index 1ce2e7b3..d64b2d6f 100644 --- a/powerline/config_files/colorschemes/vim/__main__.json +++ b/powerline/config_files/colorschemes/vim/__main__.json @@ -45,6 +45,13 @@ "commandt:label": "file_name", "commandt:background": "background", "commandt:finder": "file_name", - "commandt:path": "file_directory" + "commandt:path": "file_directory", + + "ctrlp:label": "file_name", + "ctrlp:background": "background", + "ctrlp:status": "background", + "ctrlp:status_other": "file_name", + "ctrlp:marked": "file_name", + "ctrlp:prog": "file_name" } } diff --git a/powerline/config_files/config.json b/powerline/config_files/config.json index 44918856..29db2037 100644 --- a/powerline/config_files/config.json +++ b/powerline/config_files/config.json @@ -41,7 +41,8 @@ "powerline.matchers.vim.plugin.nerdtree.nerdtree": "plugin_nerdtree", "powerline.matchers.vim.plugin.commandt.commandt": "plugin_commandt", "powerline.matchers.vim.plugin.gundo.gundo": "plugin_gundo", - "powerline.matchers.vim.plugin.gundo.gundo_preview": "plugin_gundo-preview" + "powerline.matchers.vim.plugin.gundo.gundo_preview": "plugin_gundo-preview", + "powerline.matchers.vim.plugin.ctrlp.ctrlp": "plugin_ctrlp" } }, "wm": { diff --git a/powerline/config_files/themes/vim/plugin_ctrlp.json b/powerline/config_files/themes/vim/plugin_ctrlp.json new file mode 100644 index 00000000..5e77bca4 --- /dev/null +++ b/powerline/config_files/themes/vim/plugin_ctrlp.json @@ -0,0 +1,41 @@ +{ + "segments": { + "left": [ + { + "type": "string", + "contents": "ControlP", + "highlight_groups": ["ctrlp:label"] + }, + { + "function": "powerline.segments.vim.plugin.ctrlp.mode_prev", + "draw_soft_divider": false, + "draw_hard_divider": false + }, + { + "function": "powerline.segments.vim.plugin.ctrlp.mode", + "draw_soft_divider": false, + "draw_hard_divider": false + }, + { + "function": "powerline.segments.vim.plugin.ctrlp.mode_next", + "draw_soft_divider": false, + "draw_hard_divider": false + }, + { + "function": "powerline.segments.vim.plugin.ctrlp.marked", + "draw_soft_divider": false, + "draw_hard_divider": false + }, + { + "function": "powerline.segments.vim.plugin.ctrlp.prog" + }, + { + "type": "string", + "highlight_groups": ["ctrlp:background"], + "draw_soft_divider": false, + "draw_hard_divider": false, + "width": "auto" + } + ] + } +} diff --git a/powerline/matchers/vim/plugin/ctrlp.py b/powerline/matchers/vim/plugin/ctrlp.py new file mode 100644 index 00000000..e701b900 --- /dev/null +++ b/powerline/matchers/vim/plugin/ctrlp.py @@ -0,0 +1,11 @@ +# vim:fileencoding=utf-8:noet +from __future__ import (unicode_literals, division, absolute_import, print_function) + +import os + +from powerline.bindings.vim import buffer_name + + +def ctrlp(matcher_info): + name = buffer_name(matcher_info) + return name and os.path.basename(name) == b'ControlP' diff --git a/powerline/segments/vim/plugin/ctrlp.py b/powerline/segments/vim/plugin/ctrlp.py new file mode 100644 index 00000000..95d5bf04 --- /dev/null +++ b/powerline/segments/vim/plugin/ctrlp.py @@ -0,0 +1,123 @@ +# vim:fileencoding=utf-8:noet +from __future__ import (unicode_literals, division, absolute_import, print_function) + +try: + import vim +except ImportError: + vim = object() + +from powerline.bindings.vim import vim_getvar + + +initialized = False + + +def initialize(pl, shutdown_event): + global initialized + if initialized: + return + initialized = True + vim.command( + ''' + function! Ctrlp_status_main(focus, byfname, regex, prev, item, next, marked) + let g:_powerline_ctrlp_status = {\ + 'focus': a:focus,\ + 'byfname': a:byfname,\ + 'regex': a:regex,\ + 'prev': a:prev,\ + 'item': a:item,\ + 'next': a:next,\ + 'marked': a:marked,\ + } + return '' + endfunction + + function! Ctrlp_status_prog(str) + let g:_powerline_ctrlp_status.prog = a:str + return '' + endfunction + + let g:ctrlp_status_func = {\ + 'main': 'Ctrlp_status_main',\ + 'prog': 'Ctrlp_status_prog',\ + } + + call ctrlp#call('s:opts') + call ctrlp#statusline() + ''' + ) + + +def marked(pl): + '''Returns boolean indicating whether anything is marked or not. + + Highlight groups used: ``ctrlp:marked``. + ''' + status = vim_getvar('_powerline_ctrlp_status') + if 'prog' in status or 'marked' not in status or status['marked'] == ' <->': + return None + return [{ + 'highlight_groups': ['ctrlp:marked'], + 'contents': status['marked'].strip() + }] +marked.startup = initialize + + +def mode(pl): + '''Returns current mode. + + Highlight groups used: ``ctrlp:status``. + ''' + status = vim_getvar('_powerline_ctrlp_status') + if 'prog' in status or 'item' not in status: + return None + return [{ + 'highlight_groups': ['ctrlp:status'], + 'contents': ' ' + status['item'] + ' ' + }] +mode.startup = initialize + + +def mode_prev(pl): + '''Returns previous mode. + + Highlight groups used: ``ctrlp:status_other``. + ''' + status = vim_getvar('_powerline_ctrlp_status') + if 'prog' in status or 'prev' not in status: + return None + return [{ + 'highlight_groups': ['ctrlp:status_other'], + 'contents': status['prev'] + ' ' + }] +mode_prev.startup = initialize + + +def mode_next(pl): + '''Returns next mode. + + Highlight groups used: ``ctrlp:status_other``. + ''' + status = vim_getvar('_powerline_ctrlp_status') + if 'prog' in status or 'next' not in status: + return None + return [{ + 'highlight_groups': ['ctrlp:status_other'], + 'contents': ' ' + status['next'] + ' ' + }] +mode_next.startup = initialize + + +def prog(pl): + '''Returns progress status. + + Highlight groups used: ``ctrlp:prog``. + ''' + status = vim_getvar('_powerline_ctrlp_status') + if 'prog' not in status: + return None + return [{ + 'highlight_groups': ['ctrlp:prog'], + 'contents': status['prog'] + }] +prog.startup = initialize diff --git a/tests/test_python/test_provided_config_files.py b/tests/test_python/test_provided_config_files.py index fd8b16e6..ccb9de03 100644 --- a/tests/test_python/test_provided_config_files.py +++ b/tests/test_python/test_provided_config_files.py @@ -54,6 +54,7 @@ class TestVimConfig(TestCase): (('bufname', 'NERD_tree_1'), {}), (('bufname', '__Gundo__'), {}), (('bufname', '__Gundo_Preview__'), {}), + # No CtrlP tests here: requires emulation # No Command-T tests here: requires +ruby or emulation # No tabline here: tablines are tested separately ) @@ -61,8 +62,8 @@ class TestVimConfig(TestCase): local_themes_raw = json.load(f)['ext']['vim']['local_themes'] # Don’t run tests on external/plugin segments local_themes = dict((k, v) for (k, v) in local_themes_raw.items()) - # See end of the buffers definition above for `- 2` - self.assertEqual(len(buffers), len(local_themes) - 2) + # See end of the buffers definition above for `- 3` + self.assertEqual(len(buffers), len(local_themes) - 3) outputs = {} i = 0 diff --git a/tests/test_vim/tests/ctrlp_plugin.vim b/tests/test_vim/tests/ctrlp_plugin.vim new file mode 100755 index 00000000..313160bb --- /dev/null +++ b/tests/test_vim/tests/ctrlp_plugin.vim @@ -0,0 +1,17 @@ +#!/usr/bin/vim -S +set nocompatible +set columns=80 +execute 'source' fnameescape(expand(':p:h:h').'/vim_utils.vim') +call EnablePlugins('ctrlp.vim') +call SourcePowerline() +let g:statusline_values = [] +call PyFile('setup_statusline_catcher') +execute 'CtrlPBuffer' +call RunPython('powerline.render = _powerline_old_render') +let g:expected_statusline = '%#Pl_231_16777215_240_5789784_bold# ControlP %#Pl_231_16777215_240_5789784_NONE# %#Pl_231_16777215_240_5789784_bold#fil %#Pl_231_16777215_236_3158064_NONE# buffers %#Pl_231_16777215_240_5789784_bold# mru %#Pl_231_16777215_236_3158064_NONE#                                                  ' +call CheckMessages() +if index(g:statusline_values, g:expected_statusline) == -1 + call CheckStatuslineValue(get(g:statusline_values, -1, ''), g:expected_statusline) + cquit +endif +qall