diff --git a/powerline/config_files/colorschemes/shell/default.json b/powerline/config_files/colorschemes/shell/default.json index 0320d807..639c1f9b 100644 --- a/powerline/config_files/colorschemes/shell/default.json +++ b/powerline/config_files/colorschemes/shell/default.json @@ -5,6 +5,8 @@ "superuser": { "fg": "white", "bg": "brightred", "attr": ["bold"] }, "virtualenv": { "fg": "white", "bg": "darkcyan" }, "branch": { "fg": "gray9", "bg": "gray2" }, + "branch_dirty": { "fg": "brightyellow", "bg": "gray2" }, + "branch_clean": { "fg": "gray9", "bg": "gray2" }, "cwd": { "fg": "gray9", "bg": "gray4" }, "cwd:current_folder": { "fg": "gray10", "bg": "gray4", "attr": ["bold"] }, "cwd:divider": { "fg": "gray7", "bg": "gray4" }, diff --git a/powerline/config_files/colorschemes/shell/solarized.json b/powerline/config_files/colorschemes/shell/solarized.json index d847cef6..5bf86723 100644 --- a/powerline/config_files/colorschemes/shell/solarized.json +++ b/powerline/config_files/colorschemes/shell/solarized.json @@ -5,6 +5,8 @@ "superuser": { "fg": "oldlace", "bg": "red", "attr": ["bold"] }, "virtualenv": { "fg": "oldlace", "bg": "green" }, "branch": { "fg": "gray61", "bg": "royalblue5" }, + "branch_dirty": { "fg": "yellow", "bg": "royalblue5" }, + "branch_clean": { "fg": "gray61", "bg": "royalblue5" }, "cwd": { "fg": "lightyellow", "bg": "darkgreencopper" }, "cwd:current_folder": { "fg": "oldlace", "bg": "darkgreencopper", "attr": ["bold"] }, "cwd:divider": { "fg": "gray61", "bg": "darkgreencopper" }, diff --git a/powerline/config_files/colorschemes/vim/default.json b/powerline/config_files/colorschemes/vim/default.json index 14a3e780..ec158793 100644 --- a/powerline/config_files/colorschemes/vim/default.json +++ b/powerline/config_files/colorschemes/vim/default.json @@ -8,6 +8,8 @@ "paste_indicator": { "fg": "white", "bg": "mediumorange", "attr": ["bold"] }, "readonly_indicator": { "fg": "brightestred", "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" }, "file_directory": { "fg": "gray9", "bg": "gray4" }, "file_name": { "fg": "white", "bg": "gray4", "attr": ["bold"] }, diff --git a/powerline/config_files/colorschemes/vim/solarized.json b/powerline/config_files/colorschemes/vim/solarized.json index 0a441994..57f4bfb1 100644 --- a/powerline/config_files/colorschemes/vim/solarized.json +++ b/powerline/config_files/colorschemes/vim/solarized.json @@ -8,6 +8,8 @@ "paste_indicator": { "fg": "oldlace", "bg": "orange", "attr": ["bold"] }, "readonly_indicator": { "fg": "red", "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" }, "file_directory": { "fg": "lightyellow", "bg": "darkgreencopper" }, "file_name": { "fg": "oldlace", "bg": "darkgreencopper", "attr": ["bold"] }, diff --git a/powerline/segments/common.py b/powerline/segments/common.py index df931ca8..106df1dd 100644 --- a/powerline/segments/common.py +++ b/powerline/segments/common.py @@ -41,11 +41,24 @@ def user(): }] -def branch(): - '''Return the current VCS branch.''' +def branch(status_colors=True): + '''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())) 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 diff --git a/powerline/segments/vim.py b/powerline/segments/vim.py index c70eb3a1..54dbad92 100644 --- a/powerline/segments/vim.py +++ b/powerline/segments/vim.py @@ -306,15 +306,21 @@ def modified_buffers(text='+ ', join_str=','): @requires_segment_info @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. + :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``. ''' repo = guess(path=os.path.abspath(segment_info['buffer'].name or os.getcwd())) if repo: return [{ 'contents': repo.branch(), + 'highlight_group': (['branch_dirty' if repo.status().strip() else 'branch_clean'] if status_colors else []) + ['branch'], 'divider_highlight_group': 'branch:divider', }] return None @@ -351,5 +357,5 @@ def repository_status(segment_info): '''Return the status for the current repo.''' repo = guess(path=os.path.abspath(segment_info['buffer'].name or os.getcwd())) if repo: - return repo.status() + return repo.status().strip() or None return None diff --git a/tests/test_segments.py b/tests/test_segments.py index d41c145e..5211fe01 100644 --- a/tests/test_segments.py +++ b/tests/test_segments.py @@ -46,8 +46,14 @@ class TestCommon(TestCase): self.assertEqual(common.user(), [{'contents': 'def', 'highlight_group': ['superuser', 'user']}]) def test_branch(self): - with replace_module_attr(common, 'guess', lambda path: Args(branch=lambda: os.path.basename(path))): - self.assertEqual(common.branch(), 'tests') + with replace_module_attr(common, 'guess', lambda path: Args(branch=lambda: os.path.basename(path), status=lambda: ' ')): + 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): self.assertEqual(common.branch(), None)