From 429d1e3f08183f2a5f19f172361bdf057563f86c Mon Sep 17 00:00:00 2001 From: Micah Lee Date: Thu, 10 Jun 2021 12:03:24 -0700 Subject: [PATCH] Display banner and pretty terminal output in GUI mode too --- dangerzone/__init__.py | 3 - dangerzone/cli.py | 179 +---------------------------------- dangerzone/container.py | 1 + dangerzone/global_common.py | 181 +++++++++++++++++++++++++++++++++++- dangerzone/gui/__init__.py | 2 + dangerzone/gui/common.py | 16 +++- dangerzone/gui/tasks.py | 18 +++- 7 files changed, 211 insertions(+), 189 deletions(-) diff --git a/dangerzone/__init__.py b/dangerzone/__init__.py index 2426cb6..d89750d 100644 --- a/dangerzone/__init__.py +++ b/dangerzone/__init__.py @@ -1,8 +1,5 @@ import os import sys -import colorama - -colorama.init(autoreset=True) # Depending on the filename, decide if we want to run: # dangerzone, dangerzone-cli, or dangerzone-container diff --git a/dangerzone/cli.py b/dangerzone/cli.py index 796e7f3..f94ef14 100644 --- a/dangerzone/cli.py +++ b/dangerzone/cli.py @@ -1,187 +1,10 @@ import os import shutil import click -import colorama from colorama import Fore, Back, Style from .global_common import GlobalCommon from .common import Common -from dangerzone import global_common - - -def display_banner(global_common): - """ - 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(global_common.version) - 1) // 2 - right_spaces = left_spaces - if left_spaces + len(global_common.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{global_common.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 print_header(s): @@ -233,7 +56,7 @@ def cli_main(custom_container, safe_pdf_filename, ocr_lang, skip_update, filenam global_common = GlobalCommon() common = Common() - display_banner(global_common) + global_common.display_banner() # Validate filename valid = True diff --git a/dangerzone/container.py b/dangerzone/container.py index ae9cbdb..a0e1a5a 100644 --- a/dangerzone/container.py +++ b/dangerzone/container.py @@ -26,6 +26,7 @@ def exec_container(args): args_str = " ".join(pipes.quote(s) for s in args) print("\u2023 " + args_str) # ‣ + sys.stdout.flush() with subprocess.Popen( args, diff --git a/dangerzone/global_common.py b/dangerzone/global_common.py index 628cfe6..e19680d 100644 --- a/dangerzone/global_common.py +++ b/dangerzone/global_common.py @@ -5,7 +5,8 @@ import appdirs import platform import subprocess import pipes -from colorama import Fore, Style +import colorama +from colorama import Fore, Back, Style from .settings import Settings @@ -20,6 +21,9 @@ class GlobalCommon(object): with open(self.get_resource_path("version.txt")) as f: self.version = f.read().strip() + # Initialize terminal colors + colorama.init(autoreset=True) + # App data folder self.appdata_path = appdirs.user_config_dir("dangerzone") @@ -196,6 +200,181 @@ class GlobalCommon(object): # Load settings self.settings = Settings(self) + def display_banner(self): + """ + 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_name(self): if self.custom_container: return self.custom_container diff --git a/dangerzone/gui/__init__.py b/dangerzone/gui/__init__.py index 65ccddd..2e79083 100644 --- a/dangerzone/gui/__init__.py +++ b/dangerzone/gui/__init__.py @@ -60,6 +60,8 @@ def gui_main(custom_container, filename): global_common = GlobalCommon() gui_common = GuiCommon(app, global_common) + global_common.display_banner() + if custom_container: success, error_message = global_common.container_exists(custom_container) if not success: diff --git a/dangerzone/gui/common.py b/dangerzone/gui/common.py index 982121e..28b614f 100644 --- a/dangerzone/gui/common.py +++ b/dangerzone/gui/common.py @@ -2,7 +2,9 @@ import os import platform import subprocess import shlex +import pipes from PySide2 import QtCore, QtGui, QtWidgets +from colorama import Fore if platform.system() == "Darwin": import CoreServices @@ -47,16 +49,21 @@ class GuiCommon(object): if self.global_common.settings.get("open_app") in self.pdf_viewers: if platform.system() == "Darwin": # Get the PDF reader bundle command - bundle_identifier = self.pdf_viewers[self.global_common.settings.get("open_app")] + bundle_identifier = self.pdf_viewers[ + self.global_common.settings.get("open_app") + ] args = ["open", "-b", bundle_identifier, filename] # Run - print(f"Executing: {' '.join(args)}") + args_str = " ".join(pipes.quote(s) for s in args) + print(Fore.YELLOW + "\u2023 " + Fore.CYAN + args_str) # ‣ subprocess.run(args) elif platform.system() == "Linux": # Get the PDF reader command - args = shlex.split(self.pdf_viewers[self.global_common.settings.get("open_app")]) + args = shlex.split( + self.pdf_viewers[self.global_common.settings.get("open_app")] + ) # %f, %F, %u, and %U are filenames or URLS -- so replace with the file to open for i in range(len(args)): if ( @@ -68,7 +75,8 @@ class GuiCommon(object): args[i] = filename # Open as a background process - print(f"Executing: {' '.join(args)}") + args_str = " ".join(pipes.quote(s) for s in args) + print(Fore.YELLOW + "\u2023 " + Fore.CYAN + args_str) # ‣ subprocess.Popen(args) def _find_pdf_viewers(self): diff --git a/dangerzone/gui/tasks.py b/dangerzone/gui/tasks.py index 9ff5448..5b97f34 100644 --- a/dangerzone/gui/tasks.py +++ b/dangerzone/gui/tasks.py @@ -1,5 +1,5 @@ from PySide2 import QtCore, QtWidgets, QtGui -from colorama import Style +from colorama import Style, Fore class TaskBase(QtCore.QThread): @@ -18,11 +18,23 @@ class TaskBase(QtCore.QThread): with self.global_common.exec_dangerzone_container(args) as p: for line in p.stdout: output += line.decode() - print(line.decode(), end="") + + if line.startswith(b"\xe2\x80\xa3 "): + print( + Fore.WHITE + "\u2023 " + Fore.LIGHTCYAN_EX + line.decode()[2:], + end="", + ) + else: + print(" " + line.decode(), end="") + self.update_details.emit(output) stderr = p.stderr.read().decode() - print(Style.DIM + stderr + Style.RESET_ALL) + if len(stderr) > 0: + print("") + for line in stderr.strip().split("\n"): + print(" " + Style.DIM + line) + self.update_details.emit(output) if p.returncode == 126 or p.returncode == 127: