Display banner and pretty terminal output in GUI mode too

This commit is contained in:
Micah Lee 2021-06-10 12:03:24 -07:00
parent 38ea24393a
commit 429d1e3f08
No known key found for this signature in database
GPG key ID: 403C2657CD994F73
7 changed files with 211 additions and 189 deletions

View file

@ -1,8 +1,5 @@
import os import os
import sys import sys
import colorama
colorama.init(autoreset=True)
# Depending on the filename, decide if we want to run: # Depending on the filename, decide if we want to run:
# dangerzone, dangerzone-cli, or dangerzone-container # dangerzone, dangerzone-cli, or dangerzone-container

View file

@ -1,187 +1,10 @@
import os import os
import shutil import shutil
import click import click
import colorama
from colorama import Fore, Back, Style from colorama import Fore, Back, Style
from .global_common import GlobalCommon from .global_common import GlobalCommon
from .common import Common 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): def print_header(s):
@ -233,7 +56,7 @@ def cli_main(custom_container, safe_pdf_filename, ocr_lang, skip_update, filenam
global_common = GlobalCommon() global_common = GlobalCommon()
common = Common() common = Common()
display_banner(global_common) global_common.display_banner()
# Validate filename # Validate filename
valid = True valid = True

View file

@ -26,6 +26,7 @@ def exec_container(args):
args_str = " ".join(pipes.quote(s) for s in args) args_str = " ".join(pipes.quote(s) for s in args)
print("\u2023 " + args_str) # ‣ print("\u2023 " + args_str) # ‣
sys.stdout.flush()
with subprocess.Popen( with subprocess.Popen(
args, args,

View file

@ -5,7 +5,8 @@ import appdirs
import platform import platform
import subprocess import subprocess
import pipes import pipes
from colorama import Fore, Style import colorama
from colorama import Fore, Back, Style
from .settings import Settings from .settings import Settings
@ -20,6 +21,9 @@ class GlobalCommon(object):
with open(self.get_resource_path("version.txt")) as f: with open(self.get_resource_path("version.txt")) as f:
self.version = f.read().strip() self.version = f.read().strip()
# Initialize terminal colors
colorama.init(autoreset=True)
# App data folder # App data folder
self.appdata_path = appdirs.user_config_dir("dangerzone") self.appdata_path = appdirs.user_config_dir("dangerzone")
@ -196,6 +200,181 @@ class GlobalCommon(object):
# Load settings # Load settings
self.settings = Settings(self) 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): def get_container_name(self):
if self.custom_container: if self.custom_container:
return self.custom_container return self.custom_container

View file

@ -60,6 +60,8 @@ def gui_main(custom_container, filename):
global_common = GlobalCommon() global_common = GlobalCommon()
gui_common = GuiCommon(app, global_common) gui_common = GuiCommon(app, global_common)
global_common.display_banner()
if custom_container: if custom_container:
success, error_message = global_common.container_exists(custom_container) success, error_message = global_common.container_exists(custom_container)
if not success: if not success:

View file

@ -2,7 +2,9 @@ import os
import platform import platform
import subprocess import subprocess
import shlex import shlex
import pipes
from PySide2 import QtCore, QtGui, QtWidgets from PySide2 import QtCore, QtGui, QtWidgets
from colorama import Fore
if platform.system() == "Darwin": if platform.system() == "Darwin":
import CoreServices import CoreServices
@ -47,16 +49,21 @@ class GuiCommon(object):
if self.global_common.settings.get("open_app") in self.pdf_viewers: if self.global_common.settings.get("open_app") in self.pdf_viewers:
if platform.system() == "Darwin": if platform.system() == "Darwin":
# Get the PDF reader bundle command # 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] args = ["open", "-b", bundle_identifier, filename]
# Run # 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) subprocess.run(args)
elif platform.system() == "Linux": elif platform.system() == "Linux":
# Get the PDF reader command # 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 # %f, %F, %u, and %U are filenames or URLS -- so replace with the file to open
for i in range(len(args)): for i in range(len(args)):
if ( if (
@ -68,7 +75,8 @@ class GuiCommon(object):
args[i] = filename args[i] = filename
# Open as a background process # 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) subprocess.Popen(args)
def _find_pdf_viewers(self): def _find_pdf_viewers(self):

View file

@ -1,5 +1,5 @@
from PySide2 import QtCore, QtWidgets, QtGui from PySide2 import QtCore, QtWidgets, QtGui
from colorama import Style from colorama import Style, Fore
class TaskBase(QtCore.QThread): class TaskBase(QtCore.QThread):
@ -18,11 +18,23 @@ class TaskBase(QtCore.QThread):
with self.global_common.exec_dangerzone_container(args) as p: with self.global_common.exec_dangerzone_container(args) as p:
for line in p.stdout: for line in p.stdout:
output += line.decode() 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) self.update_details.emit(output)
stderr = p.stderr.read().decode() 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) self.update_details.emit(output)
if p.returncode == 126 or p.returncode == 127: if p.returncode == 126 or p.returncode == 127: