Skip to content

Commit a9498d2

Browse files
committed
Fall back to tput when terminal size information is missing
1 parent d699f64 commit a9498d2

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

pre_commit/output.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,29 @@
11
from __future__ import unicode_literals
22

3+
import os
4+
import subprocess
35
import sys
46

57
from backports.shutil_get_terminal_size import get_terminal_size
68

79
from pre_commit import color
810
from pre_commit import five
911

10-
# TODO: smell: import side-effects
11-
# TODO: https://github.com/chrippa/backports.shutil_get_terminal_size/issues/4
12-
COLS = get_terminal_size().columns or 80
12+
13+
def _get_cols_from_tput(): # pragma: no cover (fallback)
14+
if not os.environ.get('TERM'):
15+
return 80
16+
else:
17+
return int(
18+
subprocess.Popen(
19+
('tput', 'cols'), stdout=subprocess.PIPE,
20+
).communicate()[0] or
21+
# Default in the case of no terminal
22+
80
23+
)
24+
25+
26+
COLS = get_terminal_size((0, 0)).columns or _get_cols_from_tput()
1327

1428

1529
def get_hook_message(

0 commit comments

Comments
 (0)