1# vim:fileencoding=utf-8:noet
2from __future__ import (unicode_literals, division, absolute_import, print_function)
3
4from powerline.lib.vcs import guess, tree_status
5from powerline.segments import Segment, with_docstring
6from powerline.theme import requires_segment_info, requires_filesystem_watcher
7
8
9@requires_filesystem_watcher
10@requires_segment_info
11class BranchSegment(Segment):
12	divider_highlight_group = None
13
14	@staticmethod
15	def get_directory(segment_info):
16		return segment_info['getcwd']()
17
18	def __call__(self, pl, segment_info, create_watcher, status_colors=False, ignore_statuses=()):
19		name = self.get_directory(segment_info)
20		if name:
21			repo = guess(path=name, create_watcher=create_watcher)
22			if repo is not None:
23				branch = repo.branch()
24				scol = ['branch']
25				if status_colors:
26					try:
27						status = tree_status(repo, pl)
28					except Exception as e:
29						pl.exception('Failed to compute tree status: {0}', str(e))
30						status = '?'
31					else:
32						status = status and status.strip()
33						if status in ignore_statuses:
34							status = None
35					scol.insert(0, 'branch_dirty' if status else 'branch_clean')
36				return [{
37					'contents': branch,
38					'highlight_groups': scol,
39					'divider_highlight_group': self.divider_highlight_group,
40				}]
41
42
43branch = with_docstring(BranchSegment(),
44'''Return the current VCS branch.
45
46:param bool status_colors:
47	Determines whether repository status will be used to determine highlighting.
48	Default: False.
49:param list ignore_statuses:
50	List of statuses which will not result in repo being marked as dirty. Most
51	useful is setting this option to ``["U"]``: this will ignore repository
52	which has just untracked files (i.e. repository with modified, deleted or
53	removed files will be marked as dirty, while just untracked files will make
54	segment show clean repository). Only applicable if ``status_colors`` option
55	is True.
56
57Highlight groups used: ``branch_clean``, ``branch_dirty``, ``branch``.
58''')
59
60
61@requires_filesystem_watcher
62@requires_segment_info
63class StashSegment(Segment):
64	divider_highlight_group = None
65
66	@staticmethod
67	def get_directory(segment_info):
68		return segment_info['getcwd']()
69
70	def __call__(self, pl, segment_info, create_watcher):
71		name = self.get_directory(segment_info)
72		if name:
73			repo = guess(path=name, create_watcher=create_watcher)
74			if repo is not None:
75				stash = getattr(repo, 'stash', None)
76				if stash:
77					stashes = stash()
78					if stashes:
79						return [{
80							'contents': str(stashes),
81							'highlight_groups': ['stash'],
82							'divider_highlight_group': self.divider_highlight_group
83						}]
84
85stash = with_docstring(StashSegment(),
86'''Return the number of current VCS stash entries, if any.
87
88Highlight groups used: ``stash``.
89''')
90