Skip to content

Commit ce62370

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

File tree

3 files changed

+38
-30
lines changed

3 files changed

+38
-30
lines changed

library/playback/torrents_info.py

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -883,23 +883,29 @@ def gen_row(t):
883883
continue
884884

885885
for file in torrent_files(t):
886+
if invalid_state:
887+
break
888+
886889
for base_path in base_paths:
887890
file_path = Path(base_path) / file.name
888891
if file_path.exists() and not file_path.is_dir():
889-
stat = file_path.stat()
890-
if stat.st_blocks and stat.st_blocks > 12 and file.progress == 0.0:
891-
log.warning(
892-
"Skipping the rest of torrent because invalid state likely. Recheck first. %s", t.name
893-
)
894-
break # invalid state likely
895-
896-
if 0.0 < file.progress < args.delete_incomplete:
897-
print(f"Deleting incomplete file: {file_path}")
892+
if file.progress == 0.0:
893+
stat = file_path.stat()
894+
if stat.st_blocks and stat.st_blocks > 12:
895+
log.error(
896+
"Skipping the rest of torrent because invalid state likely. Recheck first. %s",
897+
t.name,
898+
)
899+
invalid_state = True
900+
break
901+
902+
if file.progress < args.delete_incomplete:
903+
log.warning("Deleting incomplete file: %s", file_path)
898904
shell_utils.trash(args, str(file_path), detach=False)
899905
elif file.progress == 1.0:
900-
print(f"Keeping complete file: {file_path}")
906+
log.info("Keeping complete file: %s", file_path)
901907
else:
902-
print(f"Keeping {strings.percent(1 - file.progress)} incomplete file: {file_path}")
908+
log.warning("Keeping %s incomplete file: %s", strings.percent(1 - file.progress), file_path)
903909
break # Stop after deleting first valid path
904910

905911
alt_move_syntax = any(

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)