Skip to content

Commit 7f51cc4

Browse files
committed
Avoid importing shutil for speed
1 parent 9dac80b commit 7f51cc4

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

sphinx/_cli/console_utilities.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import os
44
import re
5-
import shutil
65
import sys
76
from typing import Dict, Pattern
87

@@ -24,7 +23,17 @@ def terminal_safe(s: str) -> str:
2423

2524
def get_terminal_width() -> int:
2625
"""Return the width of the terminal in columns."""
27-
return shutil.get_terminal_size().columns - 1
26+
try:
27+
columns = int(os.environ.get('COLUMNS', 0))
28+
except ValueError:
29+
columns = 0
30+
if columns > 1:
31+
return columns - 1
32+
try:
33+
return os.get_terminal_size(sys.__stdout__.fileno()).columns - 1
34+
except (AttributeError, ValueError, OSError):
35+
# fallback
36+
return 80 - 1
2837

2938

3039
_tw: int = get_terminal_width()

0 commit comments

Comments
 (0)