Browse Source

better _fit_width(); FIXME: out[-1] might broken, see diff of this commit

Matthew Wang 12 years ago
parent
commit
59e21fcfd4
2 changed files with 29 additions and 9 deletions
  1. 1
    0
      Makefile
  2. 28
    9
      src/cdiff.py

+ 1
- 0
Makefile View File

@@ -5,6 +5,7 @@
5 5
 dogfood:
6 6
 	git diff | src/cdiff.py
7 7
 	git diff | src/cdiff.py -s
8
+	git diff | src/cdiff.py -s -w 60
8 9
 	git diff | src/cdiff.py -s -w 100
9 10
 
10 11
 test: single-udiff multi-udiff

+ 28
- 9
src/cdiff.py View File

@@ -1,4 +1,5 @@
1 1
 #!/usr/bin/env python
2
+# -*- coding: utf-8 -*-
2 3
 
3 4
 import sys
4 5
 import os
@@ -136,13 +137,27 @@ class Diff(object):
136 137
             """str len does not count correctly if left column contains ansi
137 138
             color code.  Only left side need to set `pad`
138 139
             """
139
-            line = re.sub(r'\x1b\[(1;)?\d{1,2}m', '', markup)
140
-            if pad and len(line) < width:
141
-                pad_len = width - len(line)
142
-                return '%s%*s' % (markup, pad_len, '')
143
-            else:
144
-                # TODO
145
-                return markup
140
+            out = []
141
+            count = 0
142
+            while markup and count < width:
143
+                patt = re.compile('^(\x1b\[(1;)?\d{1,2}m)(.*)')
144
+                if patt.match(markup):
145
+                    out.append(patt.sub(r'\1', markup, count=1))
146
+                    markup = patt.sub(r'\3', markup, count=1)
147
+                else:
148
+                    out.append(markup[0])
149
+                    markup = markup[1:]
150
+                    count += 1
151
+
152
+            if count == width and patt.sub('', markup):
153
+                # stripped: output fulfil and still have ascii in markup
154
+                #out[-1] = ansi_code('reset') + colorize('☞', 'lightmagenta')
155
+                out[-1] = ansi_code('reset') + colorize('↵', 'lightmagenta')
156
+            elif count < width and pad:
157
+                pad_len = width - count
158
+                out.append('%*s' % (pad_len, ''))
159
+
160
+            return ''.join(out)
146 161
 
147 162
         # Setup line width and number width
148 163
         if width <= 0:
@@ -154,8 +169,8 @@ class Diff(object):
154 169
         num_width = max(len(str(max1)), len(str(max2)))
155 170
         left_num_fmt = colorize('%%(left_num)%ds' % num_width, 'yellow')
156 171
         right_num_fmt = colorize('%%(right_num)%ds' % num_width, 'yellow')
157
-        line_fmt = left_num_fmt + ' %(left)s ' + right_num_fmt + \
158
-                ' %(right)s\n'
172
+        line_fmt = left_num_fmt + ' %(left)s ' + ansi_code('reset') + \
173
+                right_num_fmt + ' %(right)s\n'
159 174
 
160 175
         # yield header, old path and new path
161 176
         for line in self._headers:
@@ -443,6 +458,10 @@ if __name__ == '__main__':
443 458
 
444 459
     # FIXME: can't use generator for now due to current implementation in parser
445 460
     stream = diff_hdl.readlines()
461
+    # Don't let empty diff pass thru
462
+    if not stream:
463
+        sys.exit(0)
464
+
446 465
     if diff_hdl is not sys.stdin:
447 466
         diff_hdl.close()
448 467