-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcommand_update_status_view.py
110 lines (99 loc) · 4.94 KB
/
command_update_status_view.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
from typing import Dict, List
from .core.git_commands import GitStatus, ModificationType
from .core.status_view import get_status_view
import sublime_plugin
import sublime
# command: update_status_view
class UpdateStatusViewCommand(sublime_plugin.TextCommand):
def __init__(self, view: sublime.View) -> None:
super().__init__(view)
self.phantom_set = sublime.PhantomSet(view, "status_view_phantoms")
self.prev_git_statuses: str = ''
def _is_same(self, git_statuses: List[GitStatus]) -> bool:
return self.prev_git_statuses == str(git_statuses)
def run(self, edit: sublime.Edit, git_statuses: List[GitStatus]) -> None:
if self._is_same(git_statuses):
return
self.prev_git_statuses = str(git_statuses)
window = self.view.window()
if not window:
return
active_view = window.active_view()
status_view = get_status_view(window.views())
if status_view is None:
return
files = []
phantoms: List[sublime.Phantom] = []
for git_status in git_statuses:
file_name = git_status['file_name']
if git_status['old_file_name']:
file_name = f"{git_status['old_file_name']} -> {git_status['file_name']}"
files.append(file_name)
new_content = '\n'.join(files)
# update diff view if necessary
if len(git_statuses) < 1:
new_content = "No changes"
# update status view
status_view.set_read_only(False)
status_view.replace(edit, sublime.Region(0, status_view.size()), new_content)
status_view.set_read_only(True)
style = status_view.style()
changed = status_view.style_for_scope("markup.changed").get('foreground')
inserted = status_view.style_for_scope("markup.inserted").get('foreground')
deleted = status_view.style_for_scope("markup.deleted").get('foreground')
untracked = status_view.style_for_scope("markup.untracked").get('foreground')
comment = status_view.style_for_scope("comment").get('foreground')
renamed = style.get('purplish')
background = style.get('background')
for i, git_status in enumerate(git_statuses):
point = status_view.text_point(i, 0)
modification_type = git_status['modification_type']
primary_color = "#ff0000"
if modification_type == 'MM':
primary_color = changed
elif 'A' in modification_type:
primary_color = inserted
elif 'M' in modification_type:
primary_color = changed
elif 'U' in modification_type:
primary_color = changed
elif 'R' in modification_type:
primary_color = renamed
elif 'C' in modification_type:
primary_color = changed
elif '?' in modification_type:
primary_color = untracked
elif 'D' in modification_type:
primary_color = deleted
is_staged = git_status['is_staged']
unstaged_styles = f"color: {primary_color}; border: 1px solid {primary_color};"
staged_styles = f"color: {background}; background-color: {primary_color}; border: 1px solid {primary_color};"
styles = staged_styles if is_staged else unstaged_styles
readable_modification_type = modification_type_to_readable(modification_type)
title = f'{readable_modification_type} and STAGED' if is_staged else f'{readable_modification_type} and UNSTAGED'
phantom = sublime.Phantom(sublime.Region(point), f'''<div title="{title}" style="font-weight: bold; text-align: center; border-radius: 4px; width: 1.2em; padding:0 0.1rem; margin-right: 0.4em; {styles}">
<div>{git_status['modification_type'].strip()}</div>
</div>''', sublime.LAYOUT_INLINE)
phantoms.append(phantom)
help_phantom = sublime.Phantom(sublime.Region(status_view.size(), status_view.size()), f'''<div style="border-top: 1px solid #77777720; color: {comment}">
<div style="margin-top: 0.4rem">a - stage/unstage a file</div>
<div style="margin-top: 0.4rem">d - discard changes to a file</div>
<div style="margin-top: 0.4rem">g - goto a file</div>
</div>''', sublime.LAYOUT_BLOCK)
phantoms.append(help_phantom)
self.phantom_set.update(phantoms)
if active_view:
window.focus_view(active_view)
def modification_type_to_readable(modification_type: ModificationType) -> str:
title_dict: Dict[ModificationType, str] = {
"??": "File is UNTRACKED",
" A": "File is ADDED",
"AM": "File is ADDED",
" M": "File is MODIFIED",
"MM": "File is MODIFIED",
" D": "File is Deleted",
" R": "File is Renamed",
" C": "File is Copied",
"UU": "File has Conflict/s"
}
return title_dict.get(modification_type, modification_type)