|
@@ -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
|
|
@@ -504,6 +500,19 @@ class DiffMarker(object):
|
504
|
500
|
max1 = max2 = 0
|
505
|
501
|
num_width = max(len(str(max1)), len(str(max2)))
|
506
|
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
|
|
515
|
+
|
507
|
516
|
# Setup lineno and line format
|
508
|
517
|
left_num_fmt = colorize('%%(left_num)%ds' % num_width, 'yellow')
|
509
|
518
|
right_num_fmt = colorize('%%(right_num)%ds' % num_width, 'yellow')
|
|
@@ -665,6 +674,21 @@ def decode(line):
|
665
|
674
|
return '*** cdiff: undecodable bytes ***\n'
|
666
|
675
|
|
667
|
676
|
|
|
677
|
+def terminal_size():
|
|
678
|
+ """Returns terminal size. Taken from https://gist.github.com/marsam/7268750
|
|
679
|
+ but removed win32 support which depends on 3rd party extension.
|
|
680
|
+ """
|
|
681
|
+ width, height = None, None
|
|
682
|
+ try:
|
|
683
|
+ import struct, fcntl, termios
|
|
684
|
+ s = struct.pack('HHHH', 0, 0, 0, 0)
|
|
685
|
+ x = fcntl.ioctl(1, termios.TIOCGWINSZ, s)
|
|
686
|
+ height, width = struct.unpack('HHHH', x)[0:2]
|
|
687
|
+ except (IOError, AttributeError):
|
|
688
|
+ pass
|
|
689
|
+ return width, height
|
|
690
|
+
|
|
691
|
+
|
668
|
692
|
def main():
|
669
|
693
|
signal.signal(signal.SIGPIPE, signal.SIG_DFL)
|
670
|
694
|
signal.signal(signal.SIGINT, signal.SIG_DFL)
|
|
@@ -701,7 +725,8 @@ def main():
|
701
|
725
|
help='enable side-by-side mode')
|
702
|
726
|
parser.add_option(
|
703
|
727
|
'-w', '--width', type='int', default=80, metavar='N',
|
704
|
|
- help='set text width for side-by-side mode, default is 80')
|
|
728
|
+ help='set text width for side-by-side mode, 0 for auto detection, '
|
|
729
|
+ 'default is 80')
|
705
|
730
|
parser.add_option(
|
706
|
731
|
'-l', '--log', action='store_true',
|
707
|
732
|
help='show log with changes from revision control')
|