mirror of
https://github.com/freedomofpress/dangerzone.git
synced 2025-04-29 10:12: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 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
|
||||||
|
|
|
@ -28,7 +28,6 @@ def display_banner(global_common):
|
||||||
│ https://dangerzone.rocks │
|
│ https://dangerzone.rocks │
|
||||||
╰──────────────────────────╯
|
╰──────────────────────────╯
|
||||||
"""
|
"""
|
||||||
colorama.init(autoreset=True)
|
|
||||||
print(Back.BLACK + Fore.YELLOW + Style.DIM + "╭──────────────────────────╮")
|
print(Back.BLACK + Fore.YELLOW + Style.DIM + "╭──────────────────────────╮")
|
||||||
print(
|
print(
|
||||||
Back.BLACK
|
Back.BLACK
|
||||||
|
@ -185,23 +184,38 @@ def display_banner(global_common):
|
||||||
print(Back.BLACK + Fore.YELLOW + Style.DIM + "╰──────────────────────────╯")
|
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):
|
def exec_container(global_common, args):
|
||||||
output = ""
|
output = ""
|
||||||
|
|
||||||
with global_common.exec_dangerzone_container(args) as p:
|
with 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="")
|
|
||||||
|
# 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()
|
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 != 0:
|
||||||
|
click.echo(f"Return code: {p.returncode}")
|
||||||
if p.returncode == 126 or p.returncode == 127:
|
if p.returncode == 126 or p.returncode == 127:
|
||||||
click.echo(f"Authorization failed")
|
click.echo(f"Authorization failed")
|
||||||
elif p.returncode == 0:
|
|
||||||
click.echo(f"Return code: {p.returncode}")
|
|
||||||
|
|
||||||
print("")
|
|
||||||
return p.returncode, output, stderr
|
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
|
# Pull the latest image
|
||||||
if not skip_update:
|
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"])
|
returncode, _, _ = exec_container(global_common, ["pull"])
|
||||||
if returncode != 0:
|
if returncode != 0:
|
||||||
return
|
return
|
||||||
|
|
||||||
# Convert to pixels
|
# Convert to pixels
|
||||||
click.echo("Converting document to pixels")
|
print_header("Converting document to pixels")
|
||||||
returncode, output, _ = exec_container(
|
returncode, output, _ = exec_container(
|
||||||
global_common,
|
global_common,
|
||||||
[
|
[
|
||||||
|
@ -333,7 +347,7 @@ def cli_main(custom_container, safe_pdf_filename, ocr_lang, skip_update, filenam
|
||||||
return
|
return
|
||||||
|
|
||||||
# Convert to PDF
|
# Convert to PDF
|
||||||
click.echo("Converting pixels to safe PDF")
|
print_header("Converting pixels to safe PDF")
|
||||||
|
|
||||||
if ocr_lang:
|
if ocr_lang:
|
||||||
ocr = "1"
|
ocr = "1"
|
||||||
|
@ -364,4 +378,5 @@ def cli_main(custom_container, safe_pdf_filename, ocr_lang, skip_update, filenam
|
||||||
# Save the safe PDF
|
# Save the safe PDF
|
||||||
source_filename = f"{common.safe_dir.name}/safe-output-compressed.pdf"
|
source_filename = f"{common.safe_dir.name}/safe-output-compressed.pdf"
|
||||||
shutil.move(source_filename, common.save_filename)
|
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 subprocess
|
||||||
import sys
|
import sys
|
||||||
import pipes
|
import pipes
|
||||||
import getpass
|
|
||||||
import shutil
|
import shutil
|
||||||
|
|
||||||
# What is the container runtime for this platform?
|
# What is the container runtime for this platform?
|
||||||
|
@ -26,8 +25,7 @@ def exec_container(args):
|
||||||
args = [container_runtime] + args
|
args = [container_runtime] + args
|
||||||
|
|
||||||
args_str = " ".join(pipes.quote(s) for s in args)
|
args_str = " ".join(pipes.quote(s) for s in args)
|
||||||
sys.stdout.write(f"Executing: {args_str}\n\n")
|
print("\u2023 " + args_str) # ‣
|
||||||
sys.stdout.flush()
|
|
||||||
|
|
||||||
with subprocess.Popen(
|
with subprocess.Popen(
|
||||||
args,
|
args,
|
||||||
|
|
|
@ -5,7 +5,7 @@ import appdirs
|
||||||
import platform
|
import platform
|
||||||
import subprocess
|
import subprocess
|
||||||
import pipes
|
import pipes
|
||||||
from PySide2 import QtCore, QtGui, QtWidgets
|
from colorama import Fore, Style
|
||||||
|
|
||||||
from .settings import Settings
|
from .settings import Settings
|
||||||
|
|
||||||
|
@ -263,7 +263,7 @@ class GlobalCommon(object):
|
||||||
|
|
||||||
# Execute dangerzone-container
|
# Execute dangerzone-container
|
||||||
args_str = " ".join(pipes.quote(s) for s in args)
|
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(
|
return subprocess.Popen(
|
||||||
args,
|
args,
|
||||||
startupinfo=self.get_subprocess_startupinfo(),
|
startupinfo=self.get_subprocess_startupinfo(),
|
||||||
|
@ -289,8 +289,9 @@ class GlobalCommon(object):
|
||||||
["ls", "--container-name", container_name]
|
["ls", "--container-name", container_name]
|
||||||
) as p:
|
) as p:
|
||||||
stdout_data, _ = p.communicate()
|
stdout_data, _ = p.communicate()
|
||||||
if stdout_data.startswith(b"Executing: "):
|
lines = stdout_data.split(b"\n")
|
||||||
stdout_data = b"\n".join(stdout_data.split(b"\n")[1:])
|
if b"\u2023 " in lines[0]: # ‣
|
||||||
|
stdout_data = b"\n".join(lines[1:])
|
||||||
|
|
||||||
# The user canceled, or permission denied
|
# The user canceled, or permission denied
|
||||||
if p.returncode == 126 or p.returncode == 127:
|
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 PySide2 import QtCore, QtWidgets, QtGui
|
||||||
from colorama import Style
|
from colorama import Style
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue