Skip to content

Commit d81a205

Browse files
committed
Refactor console_utilities
- Use `re.Pattern` - Avoid importing `shutil` for speed
1 parent 9f14f76 commit d81a205

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

sphinx/_cli/console_utilities.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@
22

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

98
try:
109
# check if colorama is installed to support color on Windows
@@ -13,7 +12,7 @@
1312
colorama = None
1413

1514

16-
_ansi_re: Pattern = re.compile('\x1b\\[(\\d\\d;){0,2}\\d\\dm')
15+
_ansi_re: 're.Pattern[str]' = re.compile('\x1b\\[(\\d\\d;){0,2}\\d\\dm')
1716
codes: Dict[str, str] = {}
1817

1918

@@ -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)