From 0f2b3fde76ac0d2a423010f8ff83acb9c04ab605 Mon Sep 17 00:00:00 2001 From: Matt Davis Date: Thu, 7 Apr 2022 11:28:15 -0700 Subject: [PATCH] smarter splash size selection, disable flag --- lib/ansible/cli/splash/__init__.py | 69 ++++++++++++++++++++++--- lib/ansible/cli/splash/redhat_40x15.txt | 15 ------ lib/ansible/cli/splash/redhat_80x30.txt | 30 ----------- 3 files changed, 61 insertions(+), 53 deletions(-) delete mode 100644 lib/ansible/cli/splash/redhat_40x15.txt delete mode 100644 lib/ansible/cli/splash/redhat_80x30.txt diff --git a/lib/ansible/cli/splash/__init__.py b/lib/ansible/cli/splash/__init__.py index ad0ae1fb02c..f18a372c687 100644 --- a/lib/ansible/cli/splash/__init__.py +++ b/lib/ansible/cli/splash/__init__.py @@ -1,18 +1,71 @@ +import fcntl +import os import pkgutil +import random import re +from importlib.resources import contents +from itertools import groupby +from struct import pack, unpack +from termios import TIOCGWINSZ -def dump_splash(splash_name='chaosbomb_80x40', color=True): + +class _SplashInfo: + splash_rc_re = re.compile(r'(?P\w+)_(?P\d+)x(?P\d+).txt') + + def __init__(self, splash_name: str): + m = self.splash_rc_re.match(splash_name) + if not m: + raise ValueError(f'invalid splash name {splash_name}') + + self.cols: int = int(m.group('cols')) + self.rows: int = int(m.group('rows')) + self.basename: str = m.group('name') + self.filename = splash_name + + @property + def area(self) -> int: + return self.cols * self.rows + + def fits(self, cols, rows): + return self.cols <= cols and self.rows <= rows + + def __repr__(self): + return f"_SplashInfo('{self.filename}')" + + @classmethod + def is_valid_splash_name(self, splash_name): + return bool(_SplashInfo.splash_rc_re.match(splash_name)) + + +def dump_splash(color=True): try: - b_data = pkgutil.get_data(__package__, f'{splash_name}.txt') + if not os.isatty(1): + return + tty_rows, tty_cols = unpack('HHHH', fcntl.ioctl(1, TIOCGWINSZ, pack('HHHH', 0, 0, 0, 0)))[0:2] except Exception: - # TODO: warn return - # TODO: are we a TTY? - # TODO: select the right size based on TTY caps - # TODO: do we support color - # TODO: filter escape codes if not color + if os.environ.get('ANSIBLE_NO_SPLASH', None): + return + + # adjust for typical version output length + tty_rows -= 12 + + all_splashes = [_SplashInfo(s) for s in sorted(contents(__package__)) if _SplashInfo.is_valid_splash_name(s)] + + available_splashes = [] + # get the largest version of each basename that fits the window + available_splashes = [max(splashes, key=lambda s: s.area) for _, splashes in groupby((s for s in all_splashes if s.fits(tty_cols, tty_rows)), lambda x: x.basename)] + + if not available_splashes: + return + + try: + b_data = pkgutil.get_data(__package__, f'{random.choice(available_splashes).filename}') + except Exception: + # TODO: warn + return if not b_data: return @@ -28,6 +81,6 @@ _ansi_re = None # ANSI escape sequence re lazy compiled on first use def _filter_ansi(raw): global _ansi_re if not _ansi_re: # lazily compile the RE - _ansi_re = re.compile('\033\[([0-9;]*)m') + _ansi_re = re.compile(r'\033\[([0-9;]*)m') return re.sub(_ansi_re, '', raw) diff --git a/lib/ansible/cli/splash/redhat_40x15.txt b/lib/ansible/cli/splash/redhat_40x15.txt deleted file mode 100644 index 433af988677..00000000000 --- a/lib/ansible/cli/splash/redhat_40x15.txt +++ /dev/null @@ -1,15 +0,0 @@ -           .'''''.'''''''..              -          .'''''''''''''''''''.          -          .''''''''''''''''''''.         -         .''''''''''''''''''''''.        -         .''''''''''''''''''''''.        -  ..''''  .''''''''''''''''''''''        -.'''''''    ..'''''''''''''''''''.       -'''''''''       ..''''''''''''''. ..     -.'''''''''..         ..........   .''..  - ''''''''''''..                   '''''. -  .''''''''''''''....          ..''''''' -    .''''''''''''''''''''''''''''''''''' -       .'''''''''''''''''''''''''''''''' -           .'''''''''''''''''''''''''''. -                .'''''''''''''''''''..   diff --git a/lib/ansible/cli/splash/redhat_80x30.txt b/lib/ansible/cli/splash/redhat_80x30.txt deleted file mode 100644 index 57bf0666111..00000000000 --- a/lib/ansible/cli/splash/redhat_80x30.txt +++ /dev/null @@ -1,30 +0,0 @@ -                        .......       ..'''''...                                 -                       .'''''''''....''''''''''''''..                            -                      .''''''''''''''''''''''''''''''''...                       -                     .'''''''''''''''''''''''''''''''''''''..                    -                    ..''''''''''''''''''''''''''''''''''''''''.                  -                    .''''''''''''''''''''''''''''''''''''''''''.                 -                   .'''''''''''''''''''''''''''''''''''''''''''.                 -                  ..''''''''''''''''''''''''''''''''''''''''''''.                -                  .'''''''''''''''''''''''''''''''''''''''''''''.                -                  .''''''''''''''''''''''''''''''''''''''''''''''.               -           ..'..    .''''''''''''''''''''''''''''''''''''''''''''.               -    ...'''''''''     ..'''''''''''''''''''''''''''''''''''''''''''               -  ..''''''''''''        .'''''''''''''''''''''''''''''''''''''''''.              - .''''''''''''''.         ..''''''''''''''''''''''''''''''''''''''.              -.''''''''''''''''.            ..''''''''''''''''''''''''''''''''''.              -.'''''''''''''''''..              ...''''''''''''''''''''''''''''.  '...         -.'''''''''''''''''''..                  ....'''''''''''''''''...    .'''..       - .'''''''''''''''''''''.                              .             .''''''.     -  .'''''''''''''''''''''''..                                        '''''''''.   -   .'''''''''''''''''''''''''...                                   .''''''''''.. -    ..''''''''''''''''''''''''''''...                           ..'''''''''''''. -      ..''''''''''''''''''''''''''''''''......            ....'''''''''''''''''' -        ..'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' -           ..''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' -              ..'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' -                  ..'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''. -                      ..''''''''''''''''''''''''''''''''''''''''''''''''''''''.  -                          ...'''''''''''''''''''''''''''''''''''''''''''''''..   -                                ...''''''''''''''''''''''''''''''''''''''..      -                                       .'..''''''''''''''''''''''''''..