mirror of
https://github.com/freedomofpress/dangerzone.git
synced 2025-04-28 18:02:38 +02:00
Beautiful CLI colors and formatting
This commit is contained in:
parent
46c73329a5
commit
38ea24393a
5 changed files with 35 additions and 23 deletions
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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:
|
||||
|
|
|
@ -1,8 +1,3 @@
|
|||
import subprocess
|
||||
import time
|
||||
import os
|
||||
import pipes
|
||||
import platform
|
||||
from PySide2 import QtCore, QtWidgets, QtGui
|
||||
from colorama import Style
|
||||
|
||||
|
|
Loading…
Reference in a new issue