Skip to content

Commit 740d7fc

Browse files
committed
Add colortheme feature #27
1 parent 2698548 commit 740d7fc

File tree

5 files changed

+129
-52
lines changed

5 files changed

+129
-52
lines changed

README.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ commits.
5353
5454
$ gitcheck.py -v
5555
56-
.. figure:: http://bruno.adele.im/static/gitcheck_verbose.png
56+
.. figure:: http://bruno.adele.im/static/gitcheck_verbose_v2.png
5757
:alt: Gitcheck detailed report
5858

5959
Gitcheck detailed report

gitcheck/gitcheck.py

Lines changed: 58 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -20,22 +20,38 @@
2020

2121
import json
2222

23+
from colored import fg, bg, attr
2324

2425
# Global vars
2526
argopts = {}
2627

27-
28-
# Class for terminal Color
29-
class tcolor:
30-
DEFAULT = "\033[0m"
31-
BOLD = "\033[1m"
32-
RED = "\033[0;1;31;40m"
33-
GREEN = "\033[0;1;32;40m"
34-
BLUE = "\033[0;1;36;40m"
35-
ORANGE = "\033[0;1;33;40m"
36-
MAGENTA = "\033[0;1;35;40m"
37-
RESET = "\033[2J\033[H"
38-
BELL = "\a"
28+
#Load custom parameters from ~/mygitcheck.py
29+
configfile = expanduser('~/mygitcheck.py')
30+
if os.path.exists(configfile):
31+
sys.path.append(expanduser('~'))
32+
import mygitcheck as userconf
33+
34+
# Try to load colorthemme
35+
if hasattr(userconf, 'colortheme'):
36+
colortheme = userconf.colortheme
37+
else:
38+
# Default theme
39+
defaultcolor = attr('reset') + fg('white')
40+
colortheme = {
41+
'default': defaultcolor,
42+
'prjchanged': attr('reset') + attr('bold') + fg('deep_pink_1a'),
43+
'prjremote': attr('reverse') + fg('light_cyan'),
44+
'prjname': attr('reset') + fg('chartreuse_1'),
45+
'reponame': attr('reset') + fg('light_goldenrod_2b'),
46+
'branchname': defaultcolor,
47+
'fileupdated': attr('reset') + fg('light_goldenrod_2b'),
48+
'remoteto': attr('reset') + fg('deep_sky_blue_3b'),
49+
'committo': attr('reset') + fg('violet'),
50+
'commitinfo': attr('reset') + fg('deep_sky_blue_3b'),
51+
'commitstate': attr('reset') + fg('deep_pink_1a'),
52+
'bell': "\a",
53+
'reset': "\033[2J\033[H"
54+
}
3955

4056

4157
class html:
@@ -105,11 +121,11 @@ def checkRepository(rep, branch):
105121
actionNeeded = actionNeeded or (count > 0)
106122
if count > 0:
107123
topush += " %s%s%s[%sTo Push:%s%s]" % (
108-
tcolor.ORANGE,
124+
colortheme['reponame'],
109125
r,
110-
tcolor.DEFAULT,
111-
tcolor.BLUE,
112-
tcolor.DEFAULT,
126+
colortheme['default'],
127+
colortheme['remoteto'],
128+
colortheme['default'],
113129
count
114130
)
115131
html.topush += '<b style="color:black">%s</b>[<b style="color:blue">To Push:</b><b style="color:black">%s</b>]' % (
@@ -123,11 +139,11 @@ def checkRepository(rep, branch):
123139
actionNeeded = actionNeeded or (count > 0)
124140
if count > 0:
125141
topull += " %s%s%s[%sTo Pull:%s%s]" % (
126-
tcolor.ORANGE,
142+
colortheme['reponame'],
127143
r,
128-
tcolor.DEFAULT,
129-
tcolor.BLUE,
130-
tcolor.DEFAULT,
144+
colortheme['default'],
145+
colortheme['remoteto'],
146+
colortheme['default'],
131147
count
132148
)
133149
html.topull += '<b style="color:black">%s</b>[<b style="color:blue">To Pull:</b><b style="color:black">%s</b>]' % (
@@ -153,24 +169,22 @@ def checkRepository(rep, branch):
153169
repname = rep
154170

155171
if ischange:
156-
color = tcolor.BOLD + tcolor.RED
172+
prjname = "%s%s%s" % (colortheme['prjchanged'], repname, colortheme['default'])
157173
html.prjname = '<b style="color:red">%s</b>' % (repname)
158174
elif not hasremotes:
159-
color = tcolor.BOLD + tcolor.MAGENTA
175+
prjname = "%s%s%s" % (colortheme['prjremote'], repname, colortheme['default'])
160176
html.prjname = '<b style="color:magenta">%s</b>' % (repname)
161177
else:
162-
color = tcolor.DEFAULT + tcolor.GREEN
178+
prjname = "%s%s%s" % (colortheme['prjname'], repname, colortheme['default'])
163179
html.prjname = '<b style="color:green">%s</b>' % (repname)
164180

165181
# Print result
166-
prjname = "%s%s%s" % (color, repname, tcolor.DEFAULT)
167-
168182
if len(changes) > 0:
169-
strlocal = "%sLocal%s[" % (tcolor.ORANGE, tcolor.DEFAULT)
183+
strlocal = "%sLocal%s[" % (colortheme['reponame'], colortheme['default'])
170184
lenFilesChnaged = len(getLocalFilesChange(rep))
171185
strlocal += "%sTo Commit:%s%s" % (
172-
tcolor.BLUE,
173-
tcolor.DEFAULT,
186+
colortheme['remoteto'],
187+
colortheme['default'],
174188
lenFilesChnaged
175189
)
176190
html.strlocal = '<b style="color:orange"> Local</b><b style="color:black">['
@@ -187,7 +201,8 @@ def checkRepository(rep, branch):
187201
html.msg += "<li>%s/%s %s %s %s</li>\n" % (html.prjname, branch, html.strlocal, html.topush, html.topull)
188202

189203
else:
190-
print("%(prjname)s/%(branch)s %(strlocal)s%(topush)s%(topull)s" % locals())
204+
cbranch = "%s%s" % (colortheme['branchname'], branch)
205+
print("%(prjname)s/%(cbranch)s %(strlocal)s%(topush)s%(topull)s" % locals())
191206

192207
if argopts.get('verbose', False):
193208
if ischange > 0:
@@ -197,11 +212,11 @@ def checkRepository(rep, branch):
197212
html.msg += '<ul><li><b>Local</b></li></ul>\n<ul>\n'
198213
for c in changes:
199214
filename = " |--%s%s%s %s%s" % (
200-
tcolor.MAGENTA,
215+
colortheme['commitstate'],
201216
c[0],
202-
tcolor.ORANGE,
217+
colortheme['fileupdated'],
203218
c[1],
204-
tcolor.DEFAULT)
219+
colortheme['default'])
205220
html.msg += '<li> <b style="color:orange">[To Commit] </b>%s</li>\n' % c[1]
206221
if not argopts.get('email', False): print(filename)
207222
html.msg += '</ul>\n'
@@ -215,11 +230,11 @@ def checkRepository(rep, branch):
215230
if not argopts.get('email', False): print(rname)
216231
for commit in commits:
217232
pcommit = " |--%s[To Push]%s %s%s%s" % (
218-
tcolor.MAGENTA,
219-
tcolor.DEFAULT,
220-
tcolor.BLUE,
233+
colortheme['committo'],
234+
colortheme['default'],
235+
colortheme['commitinfo'],
221236
commit,
222-
tcolor.DEFAULT)
237+
colortheme['default'])
223238
html.msg += '<li><b style="color:blue">[To Push] </b>%s</li>\n' % commit
224239
if not argopts.get('email', False): print(pcommit)
225240
html.msg += '</ul>\n'
@@ -234,11 +249,11 @@ def checkRepository(rep, branch):
234249
if not argopts.get('email', False): print(rname)
235250
for commit in commits:
236251
pcommit = " |--%s[To Pull]%s %s%s%s" % (
237-
tcolor.MAGENTA,
238-
tcolor.DEFAULT,
239-
tcolor.BLUE,
252+
colortheme['committo'],
253+
colortheme['default'],
254+
colortheme['commitinfo'],
240255
commit,
241-
tcolor.DEFAULT)
256+
colortheme['default'])
242257
html.msg += '<li><b style="color:blue">[To Pull] </b>%s</li>\n' % commit
243258
if not argopts.get('email', False): print(pcommit)
244259
html.msg += '</ul>\n'
@@ -347,7 +362,7 @@ def gitcheck():
347362
updateRemote(r)
348363

349364
if argopts.get('watchInterval', 0) > 0:
350-
print(tcolor.RESET)
365+
print(colortheme['reset'])
351366

352367
showDebug("Processing repositories... please wait.")
353368
for r in repo:
@@ -362,7 +377,7 @@ def gitcheck():
362377
html.msg += "</ul>\n<p>Report created on %s</p>\n" % html.timestamp
363378

364379
if actionNeeded and argopts.get('bellOnActionNeeded', False):
365-
print(tcolor.BELL)
380+
print(colortheme['bell'])
366381

367382

368383
def sendReport(content):
@@ -423,13 +438,13 @@ def initEmailConfig():
423438
json.dump(config, fp=open(filename, 'w'), indent=4)
424439
print('Please, modify config file located here : %s' % filename)
425440

441+
426442
def readDefaultConfig():
427443
filename = expanduser('~/.gitcheck')
428444
if os.path.exists(filename):
429445
pass
430446

431447

432-
433448
def usage():
434449
print("Usage: %s [OPTIONS]" % (sys.argv[0]))
435450
print("Check multiple git repository in one pass")

mygitcheck.py.sample

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
from colored import fg, bg, attr
2+
from colored import colored as cobj
3+
4+
# In developpement mode, for use
5+
# python ~/mygitcheck.py
6+
# python ~/mygitcheck.py | grep magenta
7+
8+
defaultcolor = attr('reset') + fg('white')
9+
colortheme = {
10+
'default': defaultcolor,
11+
'prjchanged': attr('reset') + attr('bold') + fg('deep_pink_1a'),
12+
'prjremote': attr('reverse') + fg('light_cyan'),
13+
'prjname': attr('reset') + fg('chartreuse_1'),
14+
'reponame': attr('reset') + fg('light_goldenrod_2b'),
15+
'branchname': defaultcolor,
16+
'fileupdated': attr('reset') + fg('light_goldenrod_2b'),
17+
'remoteto': attr('reset') + fg('deep_sky_blue_3b'),
18+
'committo': attr('reset') + fg('violet'),
19+
'commitinfo': attr('reset') + fg('deep_sky_blue_3b'),
20+
'commitstate': attr('reset') + fg('deep_pink_1a'),
21+
'bell': "\a",
22+
'reset': "\033[2J\033[H"
23+
}
24+
25+
26+
def searchKeyByValue(search):
27+
"""Search keyname by value"""
28+
c = cobj(0)
29+
for key, value in c.paint.iteritems():
30+
if value == str(search):
31+
return key
32+
33+
def searchMaxColorName():
34+
"""Search Max length colorname"""
35+
c = cobj(0)
36+
maxi = 0
37+
for key, value in c.paint.iteritems():
38+
lencolor = len(str(key))
39+
maxi = max(lencolor, maxi)
40+
41+
print "Lencolor: %s" % maxi
42+
43+
if __name__ == "__main__":
44+
#searchMaxColorName()
45+
for idx in range(0, 255):
46+
print "%s%19s %s%s%s%s" % (
47+
attr('reset') + fg(idx),
48+
searchKeyByValue(idx),
49+
'Normal',
50+
attr('reset') + fg(idx) + attr('bold') + 'Bold',
51+
attr('reset') + fg(idx) + attr('underlined') + 'Underline',
52+
attr('reset') + fg(idx) + attr('reverse') + 'Reverse',
53+
)

requirements/base.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
colored==1.2.1

tests.py

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -65,14 +65,22 @@ def test_searchRepositories(self):
6565
def test_gitcheck(self):
6666
os.chdir(GITROOT)
6767

68-
gitcheck.tcolor.DEFAULT = ""
69-
gitcheck.tcolor.BOLD = ""
70-
gitcheck.tcolor.RED = ""
71-
gitcheck.tcolor.GREEN = ""
72-
gitcheck.tcolor.BLUE = ""
73-
gitcheck.tcolor.ORANGE = ""
74-
gitcheck.tcolor.MAGENTA = ""
75-
gitcheck.tcolor.RESET = ""
68+
defaulttheme = ""
69+
gitcheck.colortheme = {
70+
'default': defaulttheme,
71+
'prjchanged': defaulttheme,
72+
'prjremote': defaulttheme,
73+
'prjname': defaulttheme,
74+
'reponame': defaulttheme,
75+
'branchname': defaulttheme,
76+
'fileupdated': defaulttheme,
77+
'remoteto': defaulttheme,
78+
'committo': defaulttheme,
79+
'commitinfo': defaulttheme,
80+
'commitstate': defaulttheme,
81+
'bell': defaulttheme,
82+
'reset': defaulttheme,
83+
}
7684

7785
gitcheck.gitcheck()
7886

0 commit comments

Comments
 (0)