Add status_colors argument to branch segments

Closes #206
This commit is contained in:
ZyX 2013-03-13 14:44:35 +04:00
parent 0a5a09db12
commit e24703dbdd
7 changed files with 40 additions and 7 deletions

View File

@ -5,6 +5,8 @@
"superuser": { "fg": "white", "bg": "brightred", "attr": ["bold"] }, "superuser": { "fg": "white", "bg": "brightred", "attr": ["bold"] },
"virtualenv": { "fg": "white", "bg": "darkcyan" }, "virtualenv": { "fg": "white", "bg": "darkcyan" },
"branch": { "fg": "gray9", "bg": "gray2" }, "branch": { "fg": "gray9", "bg": "gray2" },
"branch_dirty": { "fg": "brightyellow", "bg": "gray2" },
"branch_clean": { "fg": "gray9", "bg": "gray2" },
"cwd": { "fg": "gray9", "bg": "gray4" }, "cwd": { "fg": "gray9", "bg": "gray4" },
"cwd:current_folder": { "fg": "gray10", "bg": "gray4", "attr": ["bold"] }, "cwd:current_folder": { "fg": "gray10", "bg": "gray4", "attr": ["bold"] },
"cwd:divider": { "fg": "gray7", "bg": "gray4" }, "cwd:divider": { "fg": "gray7", "bg": "gray4" },

View File

@ -5,6 +5,8 @@
"superuser": { "fg": "oldlace", "bg": "red", "attr": ["bold"] }, "superuser": { "fg": "oldlace", "bg": "red", "attr": ["bold"] },
"virtualenv": { "fg": "oldlace", "bg": "green" }, "virtualenv": { "fg": "oldlace", "bg": "green" },
"branch": { "fg": "gray61", "bg": "royalblue5" }, "branch": { "fg": "gray61", "bg": "royalblue5" },
"branch_dirty": { "fg": "yellow", "bg": "royalblue5" },
"branch_clean": { "fg": "gray61", "bg": "royalblue5" },
"cwd": { "fg": "lightyellow", "bg": "darkgreencopper" }, "cwd": { "fg": "lightyellow", "bg": "darkgreencopper" },
"cwd:current_folder": { "fg": "oldlace", "bg": "darkgreencopper", "attr": ["bold"] }, "cwd:current_folder": { "fg": "oldlace", "bg": "darkgreencopper", "attr": ["bold"] },
"cwd:divider": { "fg": "gray61", "bg": "darkgreencopper" }, "cwd:divider": { "fg": "gray61", "bg": "darkgreencopper" },

View File

@ -8,6 +8,8 @@
"paste_indicator": { "fg": "white", "bg": "mediumorange", "attr": ["bold"] }, "paste_indicator": { "fg": "white", "bg": "mediumorange", "attr": ["bold"] },
"readonly_indicator": { "fg": "brightestred", "bg": "gray4" }, "readonly_indicator": { "fg": "brightestred", "bg": "gray4" },
"branch": { "fg": "gray9", "bg": "gray4" }, "branch": { "fg": "gray9", "bg": "gray4" },
"branch_dirty": { "fg": "brightyellow", "bg": "gray4" },
"branch_clean": { "fg": "gray9", "bg": "gray4" },
"branch:divider": { "fg": "gray7", "bg": "gray4" }, "branch:divider": { "fg": "gray7", "bg": "gray4" },
"file_directory": { "fg": "gray9", "bg": "gray4" }, "file_directory": { "fg": "gray9", "bg": "gray4" },
"file_name": { "fg": "white", "bg": "gray4", "attr": ["bold"] }, "file_name": { "fg": "white", "bg": "gray4", "attr": ["bold"] },

View File

@ -8,6 +8,8 @@
"paste_indicator": { "fg": "oldlace", "bg": "orange", "attr": ["bold"] }, "paste_indicator": { "fg": "oldlace", "bg": "orange", "attr": ["bold"] },
"readonly_indicator": { "fg": "red", "bg": "darkgreencopper" }, "readonly_indicator": { "fg": "red", "bg": "darkgreencopper" },
"branch": { "fg": "lightyellow", "bg": "darkgreencopper" }, "branch": { "fg": "lightyellow", "bg": "darkgreencopper" },
"branch_dirty": { "fg": "yellow", "bg": "darkgreencopper" },
"branch_clean": { "fg": "lightyellow", "bg": "darkgreencopper" },
"branch:divider": { "fg": "gray61", "bg": "darkgreencopper" }, "branch:divider": { "fg": "gray61", "bg": "darkgreencopper" },
"file_directory": { "fg": "lightyellow", "bg": "darkgreencopper" }, "file_directory": { "fg": "lightyellow", "bg": "darkgreencopper" },
"file_name": { "fg": "oldlace", "bg": "darkgreencopper", "attr": ["bold"] }, "file_name": { "fg": "oldlace", "bg": "darkgreencopper", "attr": ["bold"] },

View File

@ -41,11 +41,24 @@ def user():
}] }]
def branch(): def branch(status_colors=True):
'''Return the current VCS branch.''' '''Return the current VCS branch.@
:param bool status_colors:
determines whether repository status will be used to determine highlighting. Default: True.
Highlight groups used: ``branch_clean``, ``branch_dirty``, ``branch``.
'''
repo = guess(path=os.path.abspath(os.getcwd())) repo = guess(path=os.path.abspath(os.getcwd()))
if repo: if repo:
return repo.branch() branch = repo.branch()
if status_colors:
return [{
'contents': branch,
'highlight_group': ['branch_dirty' if repo.status().strip() else 'branch_clean', 'branch'],
}]
else:
return branch
return None return None

View File

@ -306,15 +306,21 @@ def modified_buffers(text='+ ', join_str=','):
@requires_segment_info @requires_segment_info
@memoize(2, cache_key=bufnr, cache_reg_func=purgeall_on_shell) @memoize(2, cache_key=bufnr, cache_reg_func=purgeall_on_shell)
def branch(segment_info): def branch(segment_info, status_colors=True):
'''Return the current working branch. '''Return the current working branch.
:param bool status_colors:
determines whether repository status will be used to determine highlighting. Default: True.
Highlight groups used: ``branch_clean``, ``branch_dirty``, ``branch``.
Divider highlight group used: ``branch:divider``. Divider highlight group used: ``branch:divider``.
''' '''
repo = guess(path=os.path.abspath(segment_info['buffer'].name or os.getcwd())) repo = guess(path=os.path.abspath(segment_info['buffer'].name or os.getcwd()))
if repo: if repo:
return [{ return [{
'contents': repo.branch(), 'contents': repo.branch(),
'highlight_group': (['branch_dirty' if repo.status().strip() else 'branch_clean'] if status_colors else []) + ['branch'],
'divider_highlight_group': 'branch:divider', 'divider_highlight_group': 'branch:divider',
}] }]
return None return None
@ -351,5 +357,5 @@ def repository_status(segment_info):
'''Return the status for the current repo.''' '''Return the status for the current repo.'''
repo = guess(path=os.path.abspath(segment_info['buffer'].name or os.getcwd())) repo = guess(path=os.path.abspath(segment_info['buffer'].name or os.getcwd()))
if repo: if repo:
return repo.status() return repo.status().strip() or None
return None return None

View File

@ -46,8 +46,14 @@ class TestCommon(TestCase):
self.assertEqual(common.user(), [{'contents': 'def', 'highlight_group': ['superuser', 'user']}]) self.assertEqual(common.user(), [{'contents': 'def', 'highlight_group': ['superuser', 'user']}])
def test_branch(self): def test_branch(self):
with replace_module_attr(common, 'guess', lambda path: Args(branch=lambda: os.path.basename(path))): with replace_module_attr(common, 'guess', lambda path: Args(branch=lambda: os.path.basename(path), status=lambda: ' ')):
self.assertEqual(common.branch(), 'tests') self.assertEqual(common.branch(status_colors=False), 'tests')
self.assertEqual(common.branch(status_colors=True),
[{'contents': 'tests', 'highlight_group': ['branch_clean', 'branch']}])
with replace_module_attr(common, 'guess', lambda path: Args(branch=lambda: os.path.basename(path), status=lambda: 'D ')):
self.assertEqual(common.branch(status_colors=False), 'tests')
self.assertEqual(common.branch(),
[{'contents': 'tests', 'highlight_group': ['branch_dirty', 'branch']}])
with replace_module_attr(common, 'guess', lambda path: None): with replace_module_attr(common, 'guess', lambda path: None):
self.assertEqual(common.branch(), None) self.assertEqual(common.branch(), None)