diff --git a/dangerzone/cli.py b/dangerzone/cli.py index c4ed60b..29cae8d 100644 --- a/dangerzone/cli.py +++ b/dangerzone/cli.py @@ -5,11 +5,12 @@ import sys from typing import Optional import click -from colorama import Fore, Style +from colorama import Back, Fore, Style from .common import Common from .container import convert from .global_common import GlobalCommon +from .util import get_version def print_header(s: str) -> None: @@ -28,7 +29,7 @@ def cli_main( global_common = GlobalCommon() common = Common() - global_common.display_banner() + display_banner() # Validate filename valid = True @@ -127,3 +128,179 @@ def setup_logging() -> None: logging.basicConfig(level=logging.DEBUG, format=fmt) else: logging.basicConfig(level=logging.ERROR, format=fmt) + + +def display_banner() -> None: + """ + Raw ASCII art example: + ╭──────────────────────────╮ + │ ▄██▄ │ + │ ██████ │ + │ ███▀▀▀██ │ + │ ███ ████ │ + │ ███ ██████ │ + │ ███ ▀▀▀▀████ │ + │ ███████ ▄██████ │ + │ ███████ ▄█████████ │ + │ ████████████████████ │ + │ ▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀ │ + │ │ + │ Dangerzone v0.1.5 │ + │ https://dangerzone.rocks │ + ╰──────────────────────────╯ + """ + + print(Back.BLACK + Fore.YELLOW + Style.DIM + "╭──────────────────────────╮") + print( + Back.BLACK + + Fore.YELLOW + + Style.DIM + + "│" + + Fore.LIGHTYELLOW_EX + + Style.NORMAL + + " ▄██▄ " + + Fore.YELLOW + + Style.DIM + + "│" + ) + print( + Back.BLACK + + Fore.YELLOW + + Style.DIM + + "│" + + Fore.LIGHTYELLOW_EX + + Style.NORMAL + + " ██████ " + + Fore.YELLOW + + Style.DIM + + "│" + ) + print( + Back.BLACK + + Fore.YELLOW + + Style.DIM + + "│" + + Fore.LIGHTYELLOW_EX + + Style.NORMAL + + " ███▀▀▀██ " + + Fore.YELLOW + + Style.DIM + + "│" + ) + print( + Back.BLACK + + Fore.YELLOW + + Style.DIM + + "│" + + Fore.LIGHTYELLOW_EX + + Style.NORMAL + + " ███ ████ " + + Fore.YELLOW + + Style.DIM + + "│" + ) + print( + Back.BLACK + + Fore.YELLOW + + Style.DIM + + "│" + + Fore.LIGHTYELLOW_EX + + Style.NORMAL + + " ███ ██████ " + + Fore.YELLOW + + Style.DIM + + "│" + ) + print( + Back.BLACK + + Fore.YELLOW + + Style.DIM + + "│" + + Fore.LIGHTYELLOW_EX + + Style.NORMAL + + " ███ ▀▀▀▀████ " + + Fore.YELLOW + + Style.DIM + + "│" + ) + print( + Back.BLACK + + Fore.YELLOW + + Style.DIM + + "│" + + Fore.LIGHTYELLOW_EX + + Style.NORMAL + + " ███████ ▄██████ " + + Fore.YELLOW + + Style.DIM + + "│" + ) + print( + Back.BLACK + + Fore.YELLOW + + Style.DIM + + "│" + + Fore.LIGHTYELLOW_EX + + Style.NORMAL + + " ███████ ▄█████████ " + + Fore.YELLOW + + Style.DIM + + "│" + ) + print( + Back.BLACK + + Fore.YELLOW + + Style.DIM + + "│" + + Fore.LIGHTYELLOW_EX + + Style.NORMAL + + " ████████████████████ " + + Fore.YELLOW + + Style.DIM + + "│" + ) + print( + Back.BLACK + + Fore.YELLOW + + Style.DIM + + "│" + + Fore.LIGHTYELLOW_EX + + Style.NORMAL + + " ▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀ " + + Fore.YELLOW + + Style.DIM + + "│" + ) + print(Back.BLACK + Fore.YELLOW + Style.DIM + "│ │") + left_spaces = (15 - len(get_version()) - 1) // 2 + right_spaces = left_spaces + if left_spaces + len(get_version()) + 1 + right_spaces < 15: + right_spaces += 1 + print( + Back.BLACK + + Fore.YELLOW + + Style.DIM + + "│" + + Style.RESET_ALL + + Back.BLACK + + Fore.LIGHTWHITE_EX + + Style.BRIGHT + + f"{' '*left_spaces}Dangerzone v{get_version()}{' '*right_spaces}" + + Fore.YELLOW + + Style.DIM + + "│" + ) + print( + Back.BLACK + + Fore.YELLOW + + Style.DIM + + "│" + + Style.RESET_ALL + + Back.BLACK + + Fore.LIGHTWHITE_EX + + " https://dangerzone.rocks " + + Fore.YELLOW + + Style.DIM + + "│" + ) + print(Back.BLACK + Fore.YELLOW + Style.DIM + "╰──────────────────────────╯") diff --git a/dangerzone/global_common.py b/dangerzone/global_common.py index 3dbe254..a9fe958 100644 --- a/dangerzone/global_common.py +++ b/dangerzone/global_common.py @@ -10,7 +10,6 @@ from typing import Optional import appdirs import colorama -from colorama import Back, Fore, Style from .container import convert from .settings import Settings @@ -25,15 +24,6 @@ class GlobalCommon(object): """ def __init__(self) -> None: - # Version - try: - with open(get_resource_path("version.txt")) as f: - self.version = f.read().strip() - except FileNotFoundError: - # In dev mode, in Windows, get_resource_path doesn't work properly for the container, but luckily - # it doesn't need to know the version - self.version = "unknown" - # Initialize terminal colors colorama.init(autoreset=True) @@ -210,181 +200,6 @@ class GlobalCommon(object): # Load settings self.settings = Settings(self) - def display_banner(self) -> None: - """ - Raw ASCII art example: - ╭──────────────────────────╮ - │ ▄██▄ │ - │ ██████ │ - │ ███▀▀▀██ │ - │ ███ ████ │ - │ ███ ██████ │ - │ ███ ▀▀▀▀████ │ - │ ███████ ▄██████ │ - │ ███████ ▄█████████ │ - │ ████████████████████ │ - │ ▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀ │ - │ │ - │ Dangerzone v0.1.5 │ - │ https://dangerzone.rocks │ - ╰──────────────────────────╯ - """ - - print(Back.BLACK + Fore.YELLOW + Style.DIM + "╭──────────────────────────╮") - print( - Back.BLACK - + Fore.YELLOW - + Style.DIM - + "│" - + Fore.LIGHTYELLOW_EX - + Style.NORMAL - + " ▄██▄ " - + Fore.YELLOW - + Style.DIM - + "│" - ) - print( - Back.BLACK - + Fore.YELLOW - + Style.DIM - + "│" - + Fore.LIGHTYELLOW_EX - + Style.NORMAL - + " ██████ " - + Fore.YELLOW - + Style.DIM - + "│" - ) - print( - Back.BLACK - + Fore.YELLOW - + Style.DIM - + "│" - + Fore.LIGHTYELLOW_EX - + Style.NORMAL - + " ███▀▀▀██ " - + Fore.YELLOW - + Style.DIM - + "│" - ) - print( - Back.BLACK - + Fore.YELLOW - + Style.DIM - + "│" - + Fore.LIGHTYELLOW_EX - + Style.NORMAL - + " ███ ████ " - + Fore.YELLOW - + Style.DIM - + "│" - ) - print( - Back.BLACK - + Fore.YELLOW - + Style.DIM - + "│" - + Fore.LIGHTYELLOW_EX - + Style.NORMAL - + " ███ ██████ " - + Fore.YELLOW - + Style.DIM - + "│" - ) - print( - Back.BLACK - + Fore.YELLOW - + Style.DIM - + "│" - + Fore.LIGHTYELLOW_EX - + Style.NORMAL - + " ███ ▀▀▀▀████ " - + Fore.YELLOW - + Style.DIM - + "│" - ) - print( - Back.BLACK - + Fore.YELLOW - + Style.DIM - + "│" - + Fore.LIGHTYELLOW_EX - + Style.NORMAL - + " ███████ ▄██████ " - + Fore.YELLOW - + Style.DIM - + "│" - ) - print( - Back.BLACK - + Fore.YELLOW - + Style.DIM - + "│" - + Fore.LIGHTYELLOW_EX - + Style.NORMAL - + " ███████ ▄█████████ " - + Fore.YELLOW - + Style.DIM - + "│" - ) - print( - Back.BLACK - + Fore.YELLOW - + Style.DIM - + "│" - + Fore.LIGHTYELLOW_EX - + Style.NORMAL - + " ████████████████████ " - + Fore.YELLOW - + Style.DIM - + "│" - ) - print( - Back.BLACK - + Fore.YELLOW - + Style.DIM - + "│" - + Fore.LIGHTYELLOW_EX - + Style.NORMAL - + " ▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀ " - + Fore.YELLOW - + Style.DIM - + "│" - ) - print(Back.BLACK + Fore.YELLOW + Style.DIM + "│ │") - left_spaces = (15 - len(self.version) - 1) // 2 - right_spaces = left_spaces - if left_spaces + len(self.version) + 1 + right_spaces < 15: - right_spaces += 1 - print( - Back.BLACK - + Fore.YELLOW - + Style.DIM - + "│" - + Style.RESET_ALL - + Back.BLACK - + Fore.LIGHTWHITE_EX - + Style.BRIGHT - + f"{' '*left_spaces}Dangerzone v{self.version}{' '*right_spaces}" - + Fore.YELLOW - + Style.DIM - + "│" - ) - print( - Back.BLACK - + Fore.YELLOW - + Style.DIM - + "│" - + Style.RESET_ALL - + Back.BLACK - + Fore.LIGHTWHITE_EX - + " https://dangerzone.rocks " - + Fore.YELLOW - + Style.DIM - + "│" - ) - print(Back.BLACK + Fore.YELLOW + Style.DIM + "╰──────────────────────────╯") - def get_container_runtime(self) -> str: if platform.system() == "Linux": runtime_name = "podman" diff --git a/dangerzone/util.py b/dangerzone/util.py index 776d9fe..612a3f3 100644 --- a/dangerzone/util.py +++ b/dangerzone/util.py @@ -23,3 +23,14 @@ def get_resource_path(filename: str) -> str: raise NotImplementedError(f"Unsupported system {platform.system()}") resource_path = prefix.joinpath(filename) return str(resource_path) + + +def get_version() -> str: + try: + with open(get_resource_path("version.txt")) as f: + version = f.read().strip() + except FileNotFoundError: + # In dev mode, in Windows, get_resource_path doesn't work properly for the container, but luckily + # it doesn't need to know the version + version = "unknown" + return version diff --git a/tests/test_cli.py b/tests/test_cli.py index 3177b3e..f39a98d 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -6,8 +6,9 @@ import tempfile import pytest from click.testing import CliRunner, Result +from strip_ansi import strip_ansi # type: ignore -from dangerzone.cli import cli_main +from dangerzone.cli import cli_main, display_banner from . import TestBase, for_each_doc @@ -39,6 +40,17 @@ class TestCliBasic(TestCli): result = self.run_cli("--help") assert result.exit_code == 0 + def test_display_banner(self, capfd): + display_banner() # call the test subject + (out, err) = capfd.readouterr() + plain_lines = [strip_ansi(line) for line in out.splitlines()] + assert "╭──────────────────────────╮" in plain_lines, "missing top border" + assert "╰──────────────────────────╯" in plain_lines, "missing bottom border" + + banner_width = len(plain_lines[0]) + for line in plain_lines: + assert len(line) == banner_width, "banner has inconsistent width" + class TestCliConversion(TestCliBasic): def test_invalid_lang(self): diff --git a/tests/test_global_common.py b/tests/test_global_common.py index 37d4ee3..bf28433 100644 --- a/tests/test_global_common.py +++ b/tests/test_global_common.py @@ -3,7 +3,6 @@ import platform from pathlib import Path import pytest -from strip_ansi import strip_ansi # type: ignore import dangerzone.global_common @@ -18,14 +17,3 @@ class TestGlobalCommon: def test_get_subprocess_startupinfo(self, global_common): startupinfo = global_common.get_subprocess_startupinfo() self.assertIsInstance(startupinfo, subprocess.STARTUPINFO) - - def test_display_banner(self, global_common, capfd): - global_common.display_banner() # call the test subject - (out, err) = capfd.readouterr() - plain_lines = [strip_ansi(line) for line in out.splitlines()] - assert "╭──────────────────────────╮" in plain_lines, "missing top border" - assert "╰──────────────────────────╯" in plain_lines, "missing bottom border" - - banner_width = len(plain_lines[0]) - for line in plain_lines: - assert len(line) == banner_width, "banner has inconsistent width"