1717import re
1818import sys
1919
20- from clint .textui import colored , indent
21- from clint .textui import puts as clint_puts
20+ DISABLE_COLOR = False
2221
2322from gitless import core
2423
3332
3433
3534def puts (s = '' , newline = True , stream = sys .stdout .write ):
36- assert not IS_PY2 or (
37- isinstance (s , unicode ) or isinstance (s , colored .ColoredString ))
35+ assert not IS_PY2 or isinstance (s , unicode )
3836
3937 if IS_PY2 :
4038 s = s .encode (ENCODING , errors = 'ignore' )
41- clint_puts (s , newline = newline , stream = stream )
39+ if newline :
40+ s = s + '\n '
41+ stream (s )
42+
43+
44+ # Colored strings
45+ RED = '\033 [31m'
46+ RED_BOLD = '\033 [1;31m'
47+ GREEN = '\033 [32m'
48+ GREEN_BOLD = '\033 [1;32m'
49+ YELLOW = '\033 [33m'
50+ BLUE = '\033 [34m'
51+ MAGENTA = '\033 [35m'
52+ CYAN = '\033 [36m'
53+ CLEAR = '\033 [0m'
54+
55+ def _color (color_code , text ):
56+ return '{0}{1}{2}' .format (color_code , text , CLEAR ) if should_color () else text
57+
58+ def should_color ():
59+ # We only output colored lines if the coloring is enabled and we are not being
60+ # piped or redirected
61+ return not DISABLE_COLOR and sys .stdout .isatty ()
62+
63+ def red (text ):
64+ return _color (RED , text )
65+
66+ def green (text ):
67+ return _color (GREEN , text )
68+
69+ def yellow (text ):
70+ return _color (YELLOW , text )
71+
72+ def blue (text ):
73+ return _color (BLUE , text )
74+
75+ def magenta (text ):
76+ return _color (MAGENTA , text )
77+
78+ def cyan (text ):
79+ return _color (CYAN , text )
4280
4381
4482# Stdout
4583
4684
4785def ok (text ):
48- puts (colored . green ('✔ {0}' .format (text )))
86+ puts (green ('✔ {0}' .format (text )))
4987
5088
5189def warn (text ):
52- puts (colored . yellow ('! {0}' .format (text )))
90+ puts (yellow ('! {0}' .format (text )))
5391
5492
5593def msg (text , stream = sys .stdout .write ):
5694 puts (text , stream = stream )
5795
5896
5997def exp (text , stream = sys .stdout .write ):
60- with indent (2 ):
61- puts ('➜ {0}' .format (text ), stream = stream )
98+ puts (' ➜ {0}' .format (text ), stream = stream )
6299
63100
64101def item (i , opt_text = '' , stream = sys .stdout .write ):
65- with indent (4 ):
66- puts ('{0}{1}' .format (i , opt_text ), stream = stream )
102+ puts (' {0}{1}' .format (i , opt_text ), stream = stream )
67103
68104
69105def blank (stream = sys .stdout .write ):
@@ -77,7 +113,7 @@ def sep(stream=sys.stdout.write):
77113# Err
78114
79115def err (text ):
80- puts (colored . red ('✘ {0}' .format (text )), stream = sys .stderr .write )
116+ puts (red ('✘ {0}' .format (text )), stream = sys .stderr .write )
81117
82118
83119def err_msg (text ):
@@ -137,7 +173,7 @@ def commit_str(ci):
137173
138174def commit (ci , compact = False , stream = sys .stdout .write , line_additions = 0 , line_deletions = 0 ):
139175 merge_commit = len (ci .parent_ids ) > 1
140- color = colored . magenta if merge_commit else colored . yellow
176+ color = magenta if merge_commit else yellow
141177 if compact :
142178 title = ci .message .splitlines ()[0 ]
143179 puts ('{0} {1}' .format (color (str (ci .id )[:7 ]), title ), stream = stream )
@@ -154,11 +190,10 @@ def commit(ci, compact=False, stream=sys.stdout.write, line_additions=0, line_de
154190 puts (color ('Date: {0:%c %z}' .format (ci_author_dt )), stream = stream )
155191 put_s = lambda num : '' if num == 1 else 's'
156192 puts (color ('Stats: {0} line{1} added, {2} line{3} removed'
157- .format (line_additions , put_s (line_additions ),
193+ .format (line_additions , put_s (line_additions ),
158194 line_deletions , put_s (line_deletions ))), stream = stream )
159195 puts (stream = stream )
160- with indent (4 ):
161- puts (ci .message , stream = stream )
196+ puts (' {0}' .format (ci .message ), stream = stream )
162197
163198# Op Callbacks
164199
@@ -203,7 +238,7 @@ def diff(patch, stream=sys.stdout.write):
203238 new_fp = patch .delta .new_file .path
204239 puts ('Diff of file "{0}"' .format (old_fp ), stream = stream )
205240 if old_fp != new_fp :
206- puts (colored . cyan (' (renamed to {0})' .format (new_fp )), stream = stream )
241+ puts (cyan (' (renamed to {0})' .format (new_fp )), stream = stream )
207242 puts (stream = stream )
208243
209244 if patch .delta .is_binary :
@@ -242,7 +277,7 @@ def diff_totals(total_additions, total_deletions, stream=sys.stdout.write):
242277
243278
244279def _hunk (hunk , stream = sys .stdout .write ):
245- puts (colored . cyan ('@@ -{0},{1} +{2},{3} @@' .format (
280+ puts (cyan ('@@ -{0},{1} +{2},{3} @@' .format (
246281 hunk .old_start , hunk .old_lines , hunk .new_start , hunk .new_lines )),
247282 stream = stream )
248283 padding = _padding (hunk )
@@ -305,21 +340,18 @@ def _format_line(diff_line, padding, bold_delim=None):
305340 Returns:
306341 a padded and colored version of the diff line with line numbers
307342 """
308- # Color constants
309- # We only output colored lines if the coloring is enabled and we are not being
310- # piped or redirected
311- if colored .DISABLE_COLOR or not sys .stdout .isatty ():
312- GREEN = ''
313- GREEN_BOLD = ''
314- RED = ''
315- RED_BOLD = ''
316- CLEAR = ''
343+ if should_color ():
344+ green = GREEN
345+ green_bold = GREEN_BOLD
346+ red = RED
347+ red_bold = RED_BOLD
348+ clear = CLEAR
317349 else :
318- GREEN = '\033 [32m '
319- GREEN_BOLD = '\033 [1;32m '
320- RED = '\033 [31m '
321- RED_BOLD = '\033 [1;31m '
322- CLEAR = '\033 [0m '
350+ green = ''
351+ green_bold = ''
352+ red = ''
353+ red_bold = ''
354+ clear = ''
323355
324356 formatted = ''
325357 st = diff_line .origin
@@ -331,25 +363,25 @@ def _format_line(diff_line, padding, bold_delim=None):
331363 formatted = (
332364 str (old_lineno ).ljust (padding ) + str (new_lineno ).ljust (padding ) + line )
333365 elif st == '+' :
334- formatted = ' ' * padding + GREEN + str (new_lineno ).ljust (padding )
366+ formatted = ' ' * padding + green + str (new_lineno ).ljust (padding )
335367 if not bold_delim :
336368 formatted += line
337369 else :
338370 bold_start , bold_end = bold_delim
339371 formatted += (
340- line [:bold_start ] + GREEN_BOLD + line [bold_start :bold_end ] + CLEAR +
341- GREEN + line [bold_end :])
372+ line [:bold_start ] + green_bold + line [bold_start :bold_end ] + clear +
373+ green + line [bold_end :])
342374 elif st == '-' :
343- formatted = RED + str (old_lineno ).ljust (padding ) + ' ' * padding
375+ formatted = red + str (old_lineno ).ljust (padding ) + ' ' * padding
344376 if not bold_delim :
345377 formatted += line
346378 else :
347379 bold_start , bold_end = bold_delim
348380 formatted += (
349- line [:bold_start ] + RED_BOLD + line [bold_start :bold_end ] + CLEAR +
350- RED + line [bold_end :])
381+ line [:bold_start ] + red_bold + line [bold_start :bold_end ] + clear +
382+ red + line [bold_end :])
351383
352- return formatted + CLEAR
384+ return formatted + clear
353385
354386
355387def _highlight (line1 , line2 ):
0 commit comments