From 38ea24393ab41968de168b6040c6b1d941f4e319 Mon Sep 17 00:00:00 2001 From: Micah Lee Date: Thu, 10 Jun 2021 11:39:26 -0700 Subject: [PATCH] Beautiful CLI colors and formatting --- dangerzone/__init__.py | 3 +++ dangerzone/cli.py | 37 ++++++++++++++++++++++++++----------- dangerzone/container.py | 4 +--- dangerzone/global_common.py | 9 +++++---- dangerzone/gui/tasks.py | 5 ----- 5 files changed, 35 insertions(+), 23 deletions(-) diff --git a/dangerzone/__init__.py b/dangerzone/__init__.py index d89750d..2426cb6 100644 --- a/dangerzone/__init__.py +++ b/dangerzone/__init__.py @@ -1,5 +1,8 @@ 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 cf297cf..796e7f3 100644 --- a/dangerzone/cli.py +++ b/dangerzone/cli.py @@ -28,7 +28,6 @@ def display_banner(global_common): │ https://dangerzone.rocks │ ╰──────────────────────────╯ """ - colorama.init(autoreset=True) print(Back.BLACK + Fore.YELLOW + Style.DIM + "╭──────────────────────────╮") print( Back.BLACK @@ -185,23 +184,38 @@ def display_banner(global_common): print(Back.BLACK + Fore.YELLOW + Style.DIM + "╰──────────────────────────╯") +def print_header(s): + click.echo("") + click.echo(Style.BRIGHT + Fore.LIGHTWHITE_EX + s) + + def exec_container(global_common, args): output = "" with global_common.exec_dangerzone_container(args) as p: for line in p.stdout: output += line.decode() - print(line.decode(), end="") + + # Hack to add colors to the command executing + if line.startswith(b"\xe2\x80\xa3 "): + print( + Fore.WHITE + "\u2023 " + Fore.LIGHTCYAN_EX + line.decode()[2:], + end="", + ) + else: + print(" " + line.decode(), end="") stderr = p.stderr.read().decode() - print(stderr) + if len(stderr) > 0: + print("") + for line in stderr.strip().split("\n"): + print(" " + Style.DIM + line) - if p.returncode == 126 or p.returncode == 127: - click.echo(f"Authorization failed") - elif p.returncode == 0: + if p.returncode != 0: click.echo(f"Return code: {p.returncode}") + if p.returncode == 126 or p.returncode == 127: + click.echo(f"Authorization failed") - print("") return p.returncode, output, stderr @@ -302,13 +316,13 @@ def cli_main(custom_container, safe_pdf_filename, ocr_lang, skip_update, filenam # Pull the latest image if not skip_update: - click.echo("Pulling container image (this might take a few minutes)") + print_header("Pulling container image (this might take a few minutes)") returncode, _, _ = exec_container(global_common, ["pull"]) if returncode != 0: return # Convert to pixels - click.echo("Converting document to pixels") + print_header("Converting document to pixels") returncode, output, _ = exec_container( global_common, [ @@ -333,7 +347,7 @@ def cli_main(custom_container, safe_pdf_filename, ocr_lang, skip_update, filenam return # Convert to PDF - click.echo("Converting pixels to safe PDF") + print_header("Converting pixels to safe PDF") if ocr_lang: ocr = "1" @@ -364,4 +378,5 @@ def cli_main(custom_container, safe_pdf_filename, ocr_lang, skip_update, filenam # Save the safe PDF source_filename = f"{common.safe_dir.name}/safe-output-compressed.pdf" shutil.move(source_filename, common.save_filename) - print(f"Success! {common.save_filename}") + print_header("Safe PDF created successfully") + click.echo(common.save_filename) diff --git a/dangerzone/container.py b/dangerzone/container.py index 3a745fd..ae9cbdb 100644 --- a/dangerzone/container.py +++ b/dangerzone/container.py @@ -3,7 +3,6 @@ import platform import subprocess import sys import pipes -import getpass import shutil # What is the container runtime for this platform? @@ -26,8 +25,7 @@ def exec_container(args): args = [container_runtime] + args args_str = " ".join(pipes.quote(s) for s in args) - sys.stdout.write(f"Executing: {args_str}\n\n") - sys.stdout.flush() + print("\u2023 " + args_str) # ‣ with subprocess.Popen( args, diff --git a/dangerzone/global_common.py b/dangerzone/global_common.py index 47a21e2..628cfe6 100644 --- a/dangerzone/global_common.py +++ b/dangerzone/global_common.py @@ -5,7 +5,7 @@ import appdirs import platform import subprocess import pipes -from PySide2 import QtCore, QtGui, QtWidgets +from colorama import Fore, Style from .settings import Settings @@ -263,7 +263,7 @@ class GlobalCommon(object): # Execute dangerzone-container args_str = " ".join(pipes.quote(s) for s in args) - print(f"Executing: {args_str}") + print(Fore.YELLOW + "\u2023 " + Fore.CYAN + args_str) # ‣ return subprocess.Popen( args, startupinfo=self.get_subprocess_startupinfo(), @@ -289,8 +289,9 @@ class GlobalCommon(object): ["ls", "--container-name", container_name] ) as p: stdout_data, _ = p.communicate() - if stdout_data.startswith(b"Executing: "): - stdout_data = b"\n".join(stdout_data.split(b"\n")[1:]) + lines = stdout_data.split(b"\n") + if b"\u2023 " in lines[0]: # ‣ + stdout_data = b"\n".join(lines[1:]) # The user canceled, or permission denied if p.returncode == 126 or p.returncode == 127: diff --git a/dangerzone/gui/tasks.py b/dangerzone/gui/tasks.py index 4ccb2cb..9ff5448 100644 --- a/dangerzone/gui/tasks.py +++ b/dangerzone/gui/tasks.py @@ -1,8 +1,3 @@ -import subprocess -import time -import os -import pipes -import platform from PySide2 import QtCore, QtWidgets, QtGui from colorama import Style