ソースを参照

side-by-side prototype, todo: fix output alignment

Matthew Wang 11 年 前
コミット
1a82253711
共有2 個のファイルを変更した50 個の追加14 個の削除を含む
  1. 1
    0
      Makefile
  2. 49
    14
      src/cdiff.py

+ 1
- 0
Makefile ファイルの表示

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

+ 49
- 14
src/cdiff.py ファイルの表示

@@ -97,27 +97,63 @@ class Diff(object):
97 97
 
98 98
         for hunk in self._hunks:
99 99
             out.append(self._markup_hunk_header(hunk.get_header()))
100
-            save_line = ''
101
-            for from_info, to_info, changed in hunk.mdiff():
100
+            for old, new, changed in hunk.mdiff():
102 101
                 if changed:
103
-                    if not from_info[0]:
104
-                        line = to_info[1].strip('\x00\x01')
102
+                    if not old[0]:
103
+                        # The '+' char after \x00 is kept
104
+                        line = new[1].strip('\x00\x01')
105 105
                         out.append(self._markup_new(line))
106
-                    elif not to_info[0]:
107
-                        line = from_info[1].strip('\x00\x01')
106
+                    elif not new[0]:
107
+                        # The '-' char after \x00 is kept
108
+                        line = old[1].strip('\x00\x01')
108 109
                         out.append(self._markup_old(line))
109 110
                     else:
110 111
                         out.append(self._markup_old('-') +
111
-                            self._markup_old_mix(from_info[1]))
112
+                            self._markup_old_mix(old[1]))
112 113
                         out.append(self._markup_new('+') +
113
-                            self._markup_new_mix(to_info[1]))
114
+                            self._markup_new_mix(new[1]))
114 115
                 else:
115
-                    out.append(self._markup_common(' ' + from_info[1]))
116
+                    out.append(self._markup_common(' ' + old[1]))
116 117
         return ''.join(out)
117 118
 
118 119
     def markup_side_by_side(self, show_number, width):
119
-        """Do not really need to parse the hunks..."""
120
-        return 'TODO: show_number=%s, width=%d' % (show_number, width)
120
+        """width of 0 means infinite width, None means auto detect"""
121
+        def _normalize(line):
122
+            return line.replace('\t', ' ' * 8).replace('\n', '')
123
+
124
+        width = 80
125
+        line_fmt = '%%-%ds | %%-%ds\n' % (width, width)
126
+        out = []
127
+
128
+        for line in self._headers:
129
+            out.append(self._markup_header(line))
130
+
131
+        out.append(self._markup_old_path(self._old_path))
132
+        out.append(self._markup_new_path(self._new_path))
133
+
134
+        for hunk in self._hunks:
135
+            out.append(self._markup_hunk_header(hunk.get_header()))
136
+            for old, new, changed in hunk.mdiff():
137
+                left = _normalize(old[1])
138
+                right = _normalize(new[1])
139
+                if changed:
140
+                    if not old[0]:
141
+                        right = right.lstrip('\x00+').rstrip('\x01')
142
+                        out.append(line_fmt % (' ', self._markup_new(right)))
143
+                    elif not new[0]:
144
+                        left = left.lstrip('\x00-').rstrip('\x01')
145
+                        out.append(line_fmt % (self._markup_old(left), ' '))
146
+                    else:
147
+                        out.append(line_fmt % (
148
+                            self._markup_old_mix(left),
149
+                            self._markup_new_mix(right)
150
+                            ))
151
+                else:
152
+                    out.append(line_fmt % (
153
+                        self._markup_common(left),
154
+                        self._markup_common(right)
155
+                        ))
156
+        return ''.join(out)
121 157
 
122 158
     def _markup_header(self, line):
123 159
         return colorize(line, 'cyan')
@@ -320,7 +356,6 @@ class DiffMarkup(object):
320 356
         return out
321 357
 
322 358
     def _markup_side_by_side(self, show_number, width):
323
-        """width of 0 or negative means auto detect terminal width"""
324 359
         out = []
325 360
         for diff in self._diffs:
326 361
             out.append(diff.markup_side_by_side(show_number, width))
@@ -342,11 +377,11 @@ if __name__ == '__main__':
342 377
             help=('show in side-by-side mode'))
343 378
     parser.add_option('-n', '--number', action='store_true',
344 379
             help='show line number')
345
-    parser.add_option('-w', '--width', type='int', default=0,
380
+    parser.add_option('-w', '--width', type='int', default=None,
346 381
             help='set line width (side-by-side mode only)')
347 382
     opts, args = parser.parse_args()
348 383
 
349
-    if opts.width < 0:
384
+    if opts.width and opts.width < 0:
350 385
         opts.width = 0
351 386
 
352 387
     if len(args) >= 1: