Sfoglia il codice sorgente

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

Matthew Wang 12 anni fa
parent
commit
59e21fcfd4
2 ha cambiato i file con 29 aggiunte e 9 eliminazioni
  1. 1
    0
      Makefile
  2. 28
    9
      src/cdiff.py

+ 1
- 0
Makefile Vedi File

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

+ 28
- 9
src/cdiff.py Vedi File

1
 #!/usr/bin/env python
1
 #!/usr/bin/env python
2
+# -*- coding: utf-8 -*-
2
 
3
 
3
 import sys
4
 import sys
4
 import os
5
 import os
136
             """str len does not count correctly if left column contains ansi
137
             """str len does not count correctly if left column contains ansi
137
             color code.  Only left side need to set `pad`
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
         # Setup line width and number width
162
         # Setup line width and number width
148
         if width <= 0:
163
         if width <= 0:
154
         num_width = max(len(str(max1)), len(str(max2)))
169
         num_width = max(len(str(max1)), len(str(max2)))
155
         left_num_fmt = colorize('%%(left_num)%ds' % num_width, 'yellow')
170
         left_num_fmt = colorize('%%(left_num)%ds' % num_width, 'yellow')
156
         right_num_fmt = colorize('%%(right_num)%ds' % num_width, 'yellow')
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
         # yield header, old path and new path
175
         # yield header, old path and new path
161
         for line in self._headers:
176
         for line in self._headers:
443
 
458
 
444
     # FIXME: can't use generator for now due to current implementation in parser
459
     # FIXME: can't use generator for now due to current implementation in parser
445
     stream = diff_hdl.readlines()
460
     stream = diff_hdl.readlines()
461
+    # Don't let empty diff pass thru
462
+    if not stream:
463
+        sys.exit(0)
464
+
446
     if diff_hdl is not sys.stdin:
465
     if diff_hdl is not sys.stdin:
447
         diff_hdl.close()
466
         diff_hdl.close()
448
 
467