Skip to content

Commit

Permalink
Catch exception from inside terminal_width()
Browse files Browse the repository at this point in the history
  • Loading branch information
ymattw committed Nov 3, 2024
1 parent f9d603d commit 3b924b9
Showing 1 changed file with 10 additions and 11 deletions.
21 changes: 10 additions & 11 deletions ydiff.py
Original file line number Diff line number Diff line change
Expand Up @@ -474,14 +474,10 @@ def _fit_with_marker_mix(text, base_color):
# Set up line width
width = self._width
if width <= 0:
# Autodetection of text width according to terminal size
try:
# Each line is like 'nnn TEXT nnn TEXT\n', so width is half of
# [terminal size minus the line number columns and 3 separating
# spaces
width = (terminal_width() - num_width * 2 - 3) // 2
except Exception:
width = 80
# Autodetection of text width according to terminal size. Each
# line is like 'nnn TEXT nnn TEXT\n', so width is half of terminal
# size minus the line number columns and 3 separating spaces
width = (terminal_width() - num_width * 2 - 3) // 2

# Setup lineno and line format
left_num_fmt = colorize('%%(left_num)%ds' % num_width, Color.YELLOW)
Expand Down Expand Up @@ -640,9 +636,12 @@ def terminal_width():
IOError or AttributeError when impossible to detect.
"""
s = struct.pack('HHHH', 0, 0, 0, 0)
x = fcntl.ioctl(1, termios.TIOCGWINSZ, s)
_, width = struct.unpack('HHHH', x)[0:2] # height unused
return width
try:
x = fcntl.ioctl(1, termios.TIOCGWINSZ, s)
_, width = struct.unpack('HHHH', x)[0:2] # height unused
return width
except Exception:
return 80


def trap_interrupts(entry_fn):
Expand Down

0 comments on commit 3b924b9

Please sign in to comment.