Преглед изворни кода

Merge pull request #74 from dimonomid/wrap_optimize

Optimize output
Matt Wang пре 6 година
родитељ
комит
22793881dc
No account linked to committer's email

+ 2
- 12
cdiff.py Прегледај датотеку

@@ -149,14 +149,6 @@ def strtrim(text, width, wrap_char, pad):
149 149
     if tlen > width:
150 150
         text, _, _ = strsplit(text, width - 1)
151 151
 
152
-        # Old code always added trailing 'reset' sequence, but strsplit is
153
-        # smarter and only adds it when there is something to reset. However,
154
-        # in order not to distract with changed test data, here's workaround
155
-        # which keeps output exactly the same. TODO: remove it; it doesn't add
156
-        # any practical value for the user.
157
-        if not text.endswith(COLORS['reset']):
158
-            text += COLORS['reset']
159
-
160 152
         text += wrap_char
161 153
     elif pad:
162 154
         # The string is short enough, but it might need to be padded.
@@ -549,10 +541,8 @@ class DiffMarker(object):
549 541
                     out.append(COLORS['underline'] + COLORS[base_color])
550 542
                     text = text[2:]
551 543
                 elif text.startswith('\x01'):   # reset
552
-                    # TODO: Append resetting sequence if only there is some
553
-                    # text after that. That is, call out.append(...) if only
554
-                    # len(text) > 1.
555
-                    out.append(COLORS['reset'] + COLORS[base_color])
544
+                    if len(text) > 1:
545
+                        out.append(COLORS['reset'] + COLORS[base_color])
556 546
                     text = text[1:]
557 547
                 else:
558 548
                     # FIXME: utf-8 wchar might break the rule here, e.g.

+ 5
- 5
tests/crlf/out.w70 Прегледај датотеку

@@ -5,11 +5,11 @@
5 5
 @@ -3,7 +3,6 @@
6 6
   3 ## About                                                                 3 ## About
7 7
   4                                                                          4 
8
-  5 Coderev is a toolkit generates static side-by-side html pages for cod>   5 Coderev is a toolkit generates static side-by-side html pages for cod>
8
+  5 Coderev is a toolkit generates static side-by-side html pages for cod>   5 Coderev is a toolkit generates static side-by-side html pages for cod>
9 9
   6 Typical use case is to generate diff pages for local modification in >     
10 10
   7 workspace and send page link to teammates for code review.               6 workspace and send page link to teammates for code review.
11 11
   8                                                                          7 
12
-  9 See [joyus.org/pub/coderev-demo](http://joyus.org/pub/coderev-demo) f>   8 See [joyus.org/pub/coderev-demo](http://joyus.org/pub/coderev-demo) f>
12
+  9 See [joyus.org/pub/coderev-demo](http://joyus.org/pub/coderev-demo) f>   8 See [joyus.org/pub/coderev-demo](http://joyus.org/pub/coderev-demo) f>
13 13
 @@ -18,9 +17,10 @@
14 14
  18                                                                         17 
15 15
  19 ## Usage of coderev.sh                                                  18 ## Usage of coderev.sh
@@ -18,12 +18,12 @@
18 18
  22                                                                         21 
19 19
  23     Usage:                                                              22     Usage:
20 20
                                                                             23 
21
- 24         coderev.sh [-r revision] [-w width] [-o outdir] [-y] [-d name>  24         coderev.sh [-r revision] [-w width] [-o outdir] [-y] [-d name>
21
+ 24         coderev.sh [-r revision] [-w width] [-o outdir] [-y] [-d name>  24         coderev.sh [-r revision] [-w width] [-o outdir] [-y] [-d name>
22 22
  25                 [-F comment-file | -m 'comment...'] [file...]           25                 [-F comment-file | -m 'comment...'] [file...]
23 23
  26                                                                         26 
24 24
 @@ -145,3 +145,5 @@
25
-145                             specify column number where lines are bro> 145                             specify column number where lines are bro>
26
-146                             wrapped for sdiff, default is no line wra> 146                             wrapped for sdiff, default is no line wra>
25
+145                             specify column number where lines are bro> 145                             specify column number where lines are bro>
26
+146                             wrapped for sdiff, default is no line wra> 146                             wrapped for sdiff, default is no line wra>
27 27
 147     -y, --yes             do not prompt for overwriting                147     -y, --yes             do not prompt for overwriting
28 28
                                                                            148 
29 29
                                                                            149 # EOF

+ 1
- 1
tests/diff-ru-bin/out.side-by-side Прегледај датотеку

@@ -14,5 +14,5 @@
14 14
 --- a/foo.txt	2013-02-23 19:55:03.000000000 +0800
15 15
 +++ b/foo.txt	2013-02-23 19:55:10.000000000 +0800
16 16
 @@ -1,2 +1,2 @@
17
-1 Hello                                                                            1 hella
17
+1 Hello                                                                            1 hella
18 18
 2 world                                                                            2 world

+ 1
- 1
tests/diff-ru-bin/out.w70 Прегледај датотеку

@@ -14,5 +14,5 @@
14 14
 --- a/foo.txt	2013-02-23 19:55:03.000000000 +0800
15 15
 +++ b/foo.txt	2013-02-23 19:55:10.000000000 +0800
16 16
 @@ -1,2 +1,2 @@
17
-1 Hello                                                                  1 hella
17
+1 Hello                                                                  1 hella
18 18
 2 world                                                                  2 world

+ 2
- 2
tests/diff-ru/out.side-by-side Прегледај датотеку

@@ -15,7 +15,7 @@
15 15
 --- a/common/foo.txt	2013-02-22 20:28:32.000000000 +0800
16 16
 +++ b/common/foo.txt	2013-02-22 20:30:18.000000000 +0800
17 17
 @@ -1 +1 @@
18
-1 Hello                                                                            1 world
18
+1 Hello                                                                            1 world
19 19
 Only in b: date.txt
20 20
 Only in a: time.txt
21
-
21
+

+ 2
- 2
tests/diff-ru/out.w70 Прегледај датотеку

@@ -15,7 +15,7 @@
15 15
 --- a/common/foo.txt	2013-02-22 20:28:32.000000000 +0800
16 16
 +++ b/common/foo.txt	2013-02-22 20:30:18.000000000 +0800
17 17
 @@ -1 +1 @@
18
-1 Hello                                                                  1 world
18
+1 Hello                                                                  1 world
19 19
 Only in b: date.txt
20 20
 Only in a: time.txt
21
-
21
+

+ 2
- 2
tests/git-bin/out.side-by-side Прегледај датотеку

@@ -16,11 +16,11 @@
16 16
 --- a/foo.html
17 17
 +++ b/foo.html
18 18
 @@ -1,4 +1,4 @@
19
-1 <!doctype html>                                                                  1 <!DOCTYPE html>
19
+1 <!doctype html>                                                                  1 <!DOCTYPE html>
20 20
 2 <html>                                                                           2 <html>
21 21
 3                                                                                  3 
22 22
 4 <head>                                                                           4 <head>
23 23
 diff --git a/foo.pdf b/foo.pdf
24 24
 index 0e90017..3c3b90d 100644
25 25
 Binary files a/foo.pdf and b/foo.pdf differ
26
-
26
+

+ 3
- 3
tests/git-bin/out.w70 Прегледај датотеку

@@ -5,7 +5,7 @@
5 5
 @@ -13,3 +13,5 @@ INPUT=${1:-foo.html}
6 6
 13 OUTPUT=${INPUT%.html}.pdf                                              13 OUTPUT=${INPUT%.html}.pdf
7 7
 14                                                                        14 
8
-15 wkhtmltopdf --page-size A4 $INPUT $OUTPUT  # very very long comments > 15 wkhtmltopdf --page-size A4 $INPUT $OUTPUT  # very very long comments >
8
+15 wkhtmltopdf --page-size A4 $INPUT $OUTPUT  # very very long comments > 15 wkhtmltopdf --page-size A4 $INPUT $OUTPUT  # very very long comments >
9 9
                                                                           16 
10 10
                                                                           17 # EOF
11 11
 diff --git a/example.pdf b/example.pdf
@@ -16,11 +16,11 @@
16 16
 --- a/foo.html
17 17
 +++ b/foo.html
18 18
 @@ -1,4 +1,4 @@
19
-1 <!doctype html>                                                        1 <!DOCTYPE html>
19
+1 <!doctype html>                                                        1 <!DOCTYPE html>
20 20
 2 <html>                                                                 2 <html>
21 21
 3                                                                        3 
22 22
 4 <head>                                                                 4 <head>
23 23
 diff --git a/foo.pdf b/foo.pdf
24 24
 index 0e90017..3c3b90d 100644
25 25
 Binary files a/foo.pdf and b/foo.pdf differ
26
-
26
+

+ 1
- 1
tests/git-log/out.w70 Прегледај датотеку

@@ -34,4 +34,4 @@
34 34
                                                                           17 
35 35
 17     """                                                                18     """
36 36
 18     # Set up option parser                                             19     # Set up option parser
37
-19     parser = optparse.OptionParser(usage="modernize [options] file|di> 20     parser = optparse.OptionParser(usage="modernize [options] file|di>
37
+19     parser = optparse.OptionParser(usage="modernize [options] file|di> 20     parser = optparse.OptionParser(usage="modernize [options] file|di>

+ 2
- 2
tests/git/out.side-by-side Прегледај датотеку

@@ -12,7 +12,7 @@
12 12
 128                     yield self._markup_common(' ' + old[1])                      128                     yield self._markup_common(' ' + old[1])
13 13
 129                                                                                  129 
14 14
 130     def markup_side_by_side(self, width):                                        130     def markup_side_by_side(self, width):
15
-131         """width of 0 means infinite width, None means auto detect. Returns a    131         """Returns a generator"""
15
+131         """width of 0 means infinite width, None means auto detect. Returns a    131         """Returns a generator"""
16 16
 132         generator     
17 17
 133         """     
18 18
 134         def _normalize(line):                                                    132         def _normalize(line):
@@ -22,7 +22,7 @@
22 22
 147                 return markup                                                    145                 return markup
23 23
 148                                                                                  146 
24 24
 149         # Setup line width and number width                                      147         # Setup line width and number width
25
-150         if not width: width = 80                                                 148         if width <= 0:
25
+150         if not width: width = 80                                                 148         if width <= 0:
26 26
                                                                                      149             width = 80
27 27
 151         (start, offset) = self._hunks[-1].get_old_addr()                         150         (start, offset) = self._hunks[-1].get_old_addr()
28 28
 152         max1 = start + offset - 1                                                151         max1 = start + offset - 1

+ 2
- 2
tests/git/out.w70 Прегледај датотеку

@@ -12,7 +12,7 @@
12 12
 128                     yield self._markup_common(' ' + old[1])            128                     yield self._markup_common(' ' + old[1])
13 13
 129                                                                        129 
14 14
 130     def markup_side_by_side(self, width):                              130     def markup_side_by_side(self, width):
15
-131         """width of 0 means infinite width, None means auto detect. R> 131         """Returns a generator"""
15
+131         """width of 0 means infinite width, None means auto detect. R> 131         """Returns a generator"""
16 16
 132         generator     
17 17
 133         """     
18 18
 134         def _normalize(line):                                          132         def _normalize(line):
@@ -22,7 +22,7 @@
22 22
 147                 return markup                                          145                 return markup
23 23
 148                                                                        146 
24 24
 149         # Setup line width and number width                            147         # Setup line width and number width
25
-150         if not width: width = 80                                       148         if width <= 0:
25
+150         if not width: width = 80                                       148         if width <= 0:
26 26
                                                                            149             width = 80
27 27
 151         (start, offset) = self._hunks[-1].get_old_addr()               150         (start, offset) = self._hunks[-1].get_old_addr()
28 28
 152         max1 = start + offset - 1                                      151         max1 = start + offset - 1

+ 3
- 1
tests/regression.sh Прегледај датотеку

@@ -33,7 +33,7 @@ function cmp_output()
33 33
     local cdiff_opt=${3:-""}
34 34
     local cmd
35 35
 
36
-    cmd=$(printf "%-8s $CDIFF %-18s <  %-30s " $PYTHON "$cdiff_opt" "$input")
36
+    cmd=$(printf "%-8s $CDIFF %-24s <  %-30s " $PYTHON "$cdiff_opt" "$input")
37 37
     printf "$cmd"
38 38
     if eval $cmd 2>/dev/null | diff -ubq $expected_out - >& /dev/null; then
39 39
         pass
@@ -56,9 +56,11 @@ function main()
56 56
         cmp_output $d/in.diff $d/out.normal "-c always" || ((e++))
57 57
         cmp_output $d/in.diff $d/out.side-by-side "-c always -s" || ((e++))
58 58
         cmp_output $d/in.diff $d/out.w70 "-c always -s -w70" || ((e++))
59
+        cmp_output $d/in.diff $d/out.w70.wrap "-c always -s -w70 --wrap" || ((e++))
59 60
         cmp_output $d/in.diff $d/in.diff "-c auto" || ((e++))
60 61
         cmp_output $d/in.diff $d/in.diff "-c auto -s" || ((e++))
61 62
         cmp_output $d/in.diff $d/in.diff "-c auto -s -w70" || ((e++))
63
+        cmp_output $d/in.diff $d/in.diff "-c auto -s -w70 --wrap" || ((e++))
62 64
         (( total += 6 ))
63 65
     done
64 66
 

+ 5
- 5
tests/svn/out.w70 Прегледај датотеку

@@ -5,11 +5,11 @@
5 5
 @@ -3,7 +3,6 @@
6 6
   3 ## About                                                                 3 ## About
7 7
   4                                                                          4 
8
-  5 Coderev is a toolkit generates static side-by-side html pages for cod>   5 Coderev is a toolkit generates static side-by-side html pages for cod>
8
+  5 Coderev is a toolkit generates static side-by-side html pages for cod>   5 Coderev is a toolkit generates static side-by-side html pages for cod>
9 9
   6 Typical use case is to generate diff pages for local modification in >     
10 10
   7 workspace and send page link to teammates for code review.               6 workspace and send page link to teammates for code review.
11 11
   8                                                                          7 
12
-  9 See [joyus.org/pub/coderev-demo](http://joyus.org/pub/coderev-demo) f>   8 See [joyus.org/pub/coderev-demo](http://joyus.org/pub/coderev-demo) f>
12
+  9 See [joyus.org/pub/coderev-demo](http://joyus.org/pub/coderev-demo) f>   8 See [joyus.org/pub/coderev-demo](http://joyus.org/pub/coderev-demo) f>
13 13
 @@ -18,9 +17,10 @@
14 14
  18                                                                         17 
15 15
  19 ## Usage of coderev.sh                                                  18 ## Usage of coderev.sh
@@ -18,12 +18,12 @@
18 18
  22                                                                         21 
19 19
  23     Usage:                                                              22     Usage:
20 20
                                                                             23 
21
- 24         coderev.sh [-r revision] [-w width] [-o outdir] [-y] [-d name>  24         coderev.sh [-r revision] [-w width] [-o outdir] [-y] [-d name>
21
+ 24         coderev.sh [-r revision] [-w width] [-o outdir] [-y] [-d name>  24         coderev.sh [-r revision] [-w width] [-o outdir] [-y] [-d name>
22 22
  25                 [-F comment-file | -m 'comment...'] [file...]           25                 [-F comment-file | -m 'comment...'] [file...]
23 23
  26                                                                         26 
24 24
 @@ -145,3 +145,5 @@
25
-145                             specify column number where lines are bro> 145                             specify column number where lines are bro>
26
-146                             wrapped for sdiff, default is no line wra> 146                             wrapped for sdiff, default is no line wra>
25
+145                             specify column number where lines are bro> 145                             specify column number where lines are bro>
26
+146                             wrapped for sdiff, default is no line wra> 146                             wrapped for sdiff, default is no line wra>
27 27
 147     -y, --yes             do not prompt for overwriting                147     -y, --yes             do not prompt for overwriting
28 28
                                                                            148 
29 29
                                                                            149 # EOF

+ 6
- 7
tests/test_cdiff.py Прегледај датотеку

@@ -231,7 +231,7 @@ class DiffMarkupTest(unittest.TestCase):
231 231
             '\x1b[33m1\x1b[0m '
232 232
             '\x1b[31m\x1b[7m\x1b[31mh\x1b[0m\x1b[31mhello\x1b[0m  '
233 233
             '\x1b[0m\x1b[33m1\x1b[0m '
234
-            '\x1b[32mhello\x1b[7m\x1b[32mo\x1b[0m\x1b[32m\x1b[0m\n')
234
+            '\x1b[32mhello\x1b[7m\x1b[32mo\x1b[0m\n')
235 235
         self.assertEqual(
236 236
             out[6],
237 237
             '\x1b[33m '
@@ -261,7 +261,7 @@ class DiffMarkupTest(unittest.TestCase):
261 261
             '\x1b[33m5\x1b[0m '
262 262
             '\x1b[31m\x1b[7m\x1b[31m      \x1b[0m\x1b[1;35m>\x1b[0m '
263 263
             '\x1b[0m\x1b[33m5\x1b[0m '
264
-            '\x1b[32m\x1b[7m\x1b[32m spaced\x1b[0m\x1b[32m\x1b[0m\n')
264
+            '\x1b[32m\x1b[7m\x1b[32m spaced\x1b[0m\n')
265 265
 
266 266
     # This test is not valid anymore
267 267
     def __test_markup_side_by_side_neg_width(self):
@@ -325,11 +325,10 @@ class DiffMarkupTest(unittest.TestCase):
325 325
         self.assertEqual(out[4], '\x1b[1;34m@@ -1,5 +1,5 @@\n\x1b[0m')
326 326
         self.assertEqual(
327 327
             out[5],
328
-
329 328
             '\x1b[33m1\x1b[0m '
330 329
             '\x1b[31m\x1b[7m\x1b[31mh\x1b[0m\x1b[31mhello\x1b[0m '
331 330
             '\x1b[0m\x1b[33m1\x1b[0m '
332
-            '\x1b[32mhello\x1b[7m\x1b[32mo\x1b[0m\x1b[32m\x1b[0m\n')
331
+            '\x1b[32mhello\x1b[7m\x1b[32mo\x1b[0m\n')
333 332
         self.assertEqual(
334 333
             out[6],
335 334
             '\x1b[33m \x1b[0m        '
@@ -432,7 +431,7 @@ class DiffMarkupTest(unittest.TestCase):
432 431
             '\x1b[33m1\x1b[0m '
433 432
             '\x1b[31m\x1b[7m\x1b[31mh\x1b[0m\x1b[31mhello\x1b[0m   '
434 433
             '\x1b[0m\x1b[33m1\x1b[0m '
435
-            '\x1b[32mhello\x1b[7m\x1b[32mo\x1b[0m\x1b[32m\x1b[0m\n')
434
+            '\x1b[32mhello\x1b[7m\x1b[32mo\x1b[0m\n')
436 435
         self.assertEqual(
437 436
             out[6],
438 437
             '\x1b[33m '
@@ -460,9 +459,9 @@ class DiffMarkupTest(unittest.TestCase):
460 459
         self.assertEqual(
461 460
             out[10],
462 461
             '\x1b[33m5\x1b[0m '
463
-            '\x1b[31m\x1b[7m\x1b[31m  tabbed\x1b[0m\x1b[31m\x1b[0m '
462
+            '\x1b[31m\x1b[7m\x1b[31m  tabbed\x1b[0m '
464 463
             '\x1b[0m\x1b[33m5\x1b[0m '
465
-            '\x1b[32m\x1b[7m\x1b[32m spaced\x1b[0m\x1b[32m\x1b[0m\n')
464
+            '\x1b[32m\x1b[7m\x1b[32m spaced\x1b[0m\n')
466 465
 
467 466
 
468 467
 class UnifiedDiffTest(unittest.TestCase):