Skip to content

Commit d305b0a

Browse files
authored
Merge pull request #129 from cs50/rongxin-patch-1
Fix terminal size detection when output is piped
2 parents a074d66 + 8fc3e3a commit d305b0a

2 files changed

Lines changed: 3 additions & 32 deletions

File tree

‎setup.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,6 @@
4040
]
4141
},
4242
url="https://github.com/cs50/style50",
43-
version="2.10.4",
43+
version="2.11.0",
4444
include_package_data=True,
4545
)

‎style50/_api.py‎

Lines changed: 2 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
11
from abc import ABCMeta, abstractmethod
22
import errno
33
import difflib
4-
import fcntl
54
import fnmatch
65
import html
76
import itertools
87
import json
98
import os
109
import re
11-
import struct
10+
import shutil
1211
import subprocess
1312
import sys
1413
import tempfile
15-
from termios import TIOCGWINSZ
1614

1715
import icdiff
1816
import magic
@@ -22,34 +20,7 @@
2220

2321
__all__ = ["Style50", "StyleCheck", "Error"]
2422

25-
26-
27-
def get_terminal_size(fallback=(80, 24)):
28-
"""
29-
Return tuple containing columns and rows of controlling terminal, trying harder
30-
than shutil.get_terminal_size to find a tty before returning fallback.
31-
32-
Theoretically, stdout, stderr, and stdin could all be different ttys that could
33-
cause us to get the wrong measurements (instead of using the fallback) but the much more
34-
common case is that IO is piped.
35-
"""
36-
for stream in [sys.__stdout__, sys.__stderr__, sys.__stdin__]:
37-
try:
38-
# Make WINSIZE call to terminal
39-
data = fcntl.ioctl(stream.fileno(), TIOCGWINSZ, b"\x00\x00\00\x00")
40-
except OSError:
41-
pass
42-
else:
43-
# Unpack two shorts from ioctl call
44-
lines, columns = struct.unpack("hh", data)
45-
break
46-
else:
47-
columns, lines = fallback
48-
49-
return columns, lines
50-
51-
52-
COLUMNS, LINES = get_terminal_size()
23+
COLUMNS, LINES = shutil.get_terminal_size(fallback=(80, 24))
5324

5425

5526
class Style50:

0 commit comments

Comments
 (0)