Skip to content

Commit 6d21bd2

Browse files
committed
Handle ASCII art errors & use logging
Wrap ASCII-art generation in a try/except to avoid crashing when the asset or terminal ops fail (catch FileNotFoundError, RuntimeError, OSError and fall back to no banner). Add a clarifying comment for the Windows ANSI os.system("") workaround. In argument parsing, allow parse_args to accept argv, detect --no-art early, safely build the help description with a fallback default and log a warning on failure. Replace startup prints with logging.info and use the computed description for argparse. These changes make startup more robust and avoid surprising exceptions when ASCII art cannot be produced.
1 parent 7f5b75b commit 6d21bd2

2 files changed

Lines changed: 36 additions & 18 deletions

File tree

dlclivegui/assets/ascii_art.py

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ def enable_windows_ansi_support() -> None:
4747
"""
4848
if os.name == "nt":
4949
# This call toggles the console mode to enable VT processing in many hosts
50-
os.system("")
50+
# Always leave the string empty.
51+
os.system("") # This is a known, safe workaround to enable ANSI support on Windows.
5152

5253

5354
def get_terminal_width(default: int = 80) -> int:
@@ -347,21 +348,24 @@ def build_help_description(
347348
enable_windows_ansi_support()
348349
desc = "DeepLabCut-Live GUI — launch the graphical interface." if desc is None else desc
349350
if static_banner is None:
350-
banner = "\n".join(
351-
generate_ascii_lines(
352-
str(ASCII_IMAGE_PATH),
353-
width=shutil.get_terminal_size((80, 24)).columns - 10,
354-
aspect=0.5,
355-
color=color,
356-
fine=True,
357-
invert=False,
358-
crop_content=True,
359-
crop_bg="white",
360-
alpha_thresh=1,
361-
crop_pad=1,
362-
bg_bgr=(255, 255, 255),
351+
try:
352+
banner = "\n".join(
353+
generate_ascii_lines(
354+
str(ASCII_IMAGE_PATH),
355+
width=shutil.get_terminal_size((80, 24)).columns - 10,
356+
aspect=0.5,
357+
color=color,
358+
fine=True,
359+
invert=False,
360+
crop_content=True,
361+
crop_bg="white",
362+
alpha_thresh=1,
363+
crop_pad=1,
364+
bg_bgr=(255, 255, 255),
365+
)
363366
)
364-
)
367+
except (FileNotFoundError, RuntimeError, OSError):
368+
banner = None
365369
else:
366370
banner = static_banner
367371
if banner and terminal_is_wide_enough(min_width=min_width):

dlclivegui/main.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,22 @@ def _sigint_handler(_signum, _frame) -> None:
5555

5656

5757
def parse_args(argv=None):
58+
if argv is None:
59+
argv = sys.argv[1:]
60+
61+
default_desc = "Welcome to DeepLabCut-Live GUI!"
62+
no_art_flag = "--no-art" in argv
63+
if not no_art_flag:
64+
try:
65+
desc = art.build_help_description()
66+
except Exception as e:
67+
logging.warning(f"Failed to build ASCII art for help description: {e}")
68+
desc = default_desc
69+
else:
70+
desc = default_desc
71+
5872
parser = argparse.ArgumentParser(
59-
description=art.build_help_description(),
73+
description=desc,
6074
formatter_class=argparse.RawDescriptionHelpFormatter,
6175
)
6276
parser.add_argument("--no-art", action="store_true", help="Disable ASCII art in help and when launching.")
@@ -68,9 +82,9 @@ def main() -> None:
6882

6983
# HiDPI pixmaps - always enabled in Qt 6 so no need to set it explicitly
7084
# QApplication.setAttribute(Qt.ApplicationAttribute.AA_UseHighDpiPixmaps, True)
71-
print("Starting DeepLabCut-Live GUI...")
85+
logging.info("Starting DeepLabCut-Live GUI...")
7286
if not args.no_art:
73-
print(art.build_help_description(desc="Welcome to DeepLabCut-Live GUI!"))
87+
logging.info(art.build_help_description(desc="Welcome to DeepLabCut-Live GUI!"))
7488

7589
app = QApplication(sys.argv)
7690
app.setWindowIcon(QIcon(LOGO))

0 commit comments

Comments
 (0)