Browse Source

Auto setup line width once num_width is known

Matthew Wang 10 years ago
parent
commit
01af713f55
1 changed files with 13 additions and 14 deletions
  1. 13
    14
      cdiff.py

+ 13
- 14
cdiff.py View File

@@ -490,10 +490,6 @@ class DiffMarker(object):
490 490
 
491 491
             return ''.join(out)
492 492
 
493
-        # Set up line width
494
-        if width <= 0:
495
-            width = 80
496
-
497 493
         # Set up number width, note last hunk might be empty
498 494
         try:
499 495
             (start, offset) = diff._hunks[-1]._old_addr
@@ -503,7 +499,19 @@ class DiffMarker(object):
503 499
         except IndexError:
504 500
             max1 = max2 = 0
505 501
         num_width = max(len(str(max1)), len(str(max2)))
506
-        width -= num_width
502
+
503
+        # Set up line width
504
+        if width <= 0:
505
+            # Autodetection of text width according to terminal size
506
+            try:
507
+                # Each line is like "nnn TEXT nnn TEXT\n", so width is half of
508
+                # [terminal size minus the line number columns and 3 separating
509
+                # spaces
510
+                #
511
+                width = (terminal_size()[0] - num_width * 2 - 3) / 2
512
+            except Exception:
513
+                # If terminal detection failed, set back to default
514
+                width = 80
507 515
 
508 516
         # Setup lineno and line format
509 517
         left_num_fmt = colorize('%%(left_num)%ds' % num_width, 'yellow')
@@ -734,15 +742,6 @@ def main():
734 742
 
735 743
     opts, args = parser.parse_args()
736 744
 
737
-    if opts.width == 0 and opts.side_by_side:
738
-        # Autodetection of text width according to terminal size
739
-        try:
740
-            # width = half the terminal size minus the 3 minimum padding chars
741
-            opts.width = (terminal_size()[0] - 3) / 2
742
-        except Exception:
743
-            # If terminal detection failed, set back to default
744
-            opts.width = 80
745
-
746 745
     if opts.log:
747 746
         diff_hdl = revision_control_log(args)
748 747
         if not diff_hdl: