Skip to content

Commit 1cf1fce

Browse files
committed
update TERMINAL_SIZE globally on SIGWINCH
1 parent 675d45d commit 1cf1fce

File tree

2 files changed

+21
-19
lines changed

2 files changed

+21
-19
lines changed

library/utils/consts.py

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import errno, os, random, re, shutil, string, sys
1+
import errno, os, random, re, shutil, signal, string, sys
22
from datetime import datetime, timezone
33
from pathlib import Path
44
from tempfile import gettempdir
@@ -71,8 +71,6 @@ def random_string() -> str:
7171
)
7272
REGEX_V_REDD_IT = re.compile("https?://v.redd.it/(?:[^/?#&]+)")
7373
APPLICATION_START = now()
74-
TERMINAL_SIZE = shutil.get_terminal_size(fallback=(600, 50))
75-
MOBILE_TERMINAL = TERMINAL_SIZE.columns < 80
7674
TABULATE_STYLE = "simple"
7775
DEFAULT_DIFFLIB_RATIO = 0.73
7876
DEFAULT_MIN_SPLIT = "150s"
@@ -82,6 +80,24 @@ def random_string() -> str:
8280
NOT_WINDOWS = os.name == "posix"
8381
REQUESTS_TIMEOUT = (8, 45)
8482

83+
84+
def get_terminal_size():
85+
return shutil.get_terminal_size(fallback=(600, 50))
86+
87+
88+
TERMINAL_SIZE = get_terminal_size()
89+
MOBILE_TERMINAL = TERMINAL_SIZE.columns < 80
90+
91+
92+
def _on_winch(_signal, _frame):
93+
global TERMINAL_SIZE, MOBILE_TERMINAL
94+
TERMINAL_SIZE = get_terminal_size()
95+
MOBILE_TERMINAL = TERMINAL_SIZE.columns < 80
96+
97+
98+
signal.signal(signal.SIGWINCH, _on_winch)
99+
100+
85101
SQLITE_INT1 = 255
86102
SQLITE_INT2 = 65_535
87103
SQLITE_INT3 = 16_777_215

library/utils/printing.py

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import csv, itertools, math, shutil, signal, sys, textwrap, time
1+
import csv, itertools, math, sys, textwrap, time
22
from collections.abc import Callable
33
from datetime import datetime, timezone
44

@@ -30,25 +30,11 @@ def print_overwrite(*text, **kwargs):
3030

3131

3232
class MultilineOverwriteConsole:
33-
34-
def _term_width(self):
35-
try:
36-
return shutil.get_terminal_size().columns
37-
except Exception:
38-
return consts.TERMINAL_SIZE.columns
39-
40-
def _on_resize(self, *_):
41-
self.width = self._term_width()
42-
4333
def __init__(self, stream=None):
4434
self.stream = stream or sys.stdout
4535
self.overwrite_enabled = self.stream.isatty()
46-
self.width = consts.TERMINAL_SIZE.columns
4736
self.lines = 0
4837

49-
if self.overwrite_enabled:
50-
signal.signal(signal.SIGWINCH, self._on_resize)
51-
5238
def reset(self):
5339
if self.lines and self.overwrite_enabled:
5440
# Move cursor to initial line
@@ -60,7 +46,7 @@ def _wrapped_lines(self, text):
6046
rows = 0
6147
for line in text.splitlines() or [""]:
6248
width = max(0, wcswidth(line))
63-
rows += max(1, (width // max(1, self.width)) + 1)
49+
rows += max(1, (width // max(1, consts.TERMINAL_SIZE.columns)) + 1)
6450
return rows
6551

6652
def print(self, *args, sep=" ", end="\n"):

0 commit comments

Comments
 (0)