From 0dffbf1bb43cb2f5225c33b7888e5be65f0c9397 Mon Sep 17 00:00:00 2001 From: Nick Sweeting Date: Tue, 24 Sep 2024 19:37:29 -0700 Subject: [PATCH] fix rich autodetection of TTY, USE_COLOR, SHOW_PROGRESS --- archivebox/config.py | 14 ++++++++++++-- archivebox/logging_util.py | 22 +++++++++++++++------- archivebox/misc/logging.py | 9 +++------ archivebox/plugins_pkg/pip/apps.py | 4 ++-- 4 files changed, 32 insertions(+), 17 deletions(-) diff --git a/archivebox/config.py b/archivebox/config.py index c85fca72..5eb6dc8a 100644 --- a/archivebox/config.py +++ b/archivebox/config.py @@ -41,6 +41,7 @@ import importlib.metadata from pydantic_pkgr import SemVer from rich.progress import Progress +from rich.console import Console import django from django.db.backends.sqlite3.base import Database as sqlite3 @@ -55,8 +56,6 @@ from .config_stubs import ( ) from .misc.logging import ( - CONSOLE, - SHOW_PROGRESS, DEFAULT_CLI_COLORS, ANSI, COLOR_DICT, @@ -1289,6 +1288,17 @@ if not CONFIG['CHECK_SSL_VALIDITY']: ########################### Config Validity Checkers ########################### +if not CONFIG.USE_COLOR: + os.environ['NO_COLOR'] = '1' +if not CONFIG.SHOW_PROGRESS: + os.environ['TERM'] = 'dumb' + +# recreate rich console obj based on new config values +CONSOLE = Console() +from .misc import logging +logging.CONSOLE = CONSOLE + + INITIAL_STARTUP_PROGRESS = None INITIAL_STARTUP_PROGRESS_TASK = 0 diff --git a/archivebox/logging_util.py b/archivebox/logging_util.py index b2468d03..d81b291f 100644 --- a/archivebox/logging_util.py +++ b/archivebox/logging_util.py @@ -17,6 +17,9 @@ from typing import Any, Optional, List, Dict, Union, IO, TYPE_CHECKING if TYPE_CHECKING: from .index.schema import Link, ArchiveResult +from rich import print +from rich.panel import Panel + from .system import get_dir_size from .util import enforce_types from .config import ( @@ -231,15 +234,20 @@ def progress_bar(seconds: int, prefix: str='') -> None: def log_cli_command(subcommand: str, subcommand_args: List[str], stdin: Optional[str | IO], pwd: str): - cmd = ' '.join(('archivebox', subcommand, *subcommand_args)) - stderr('{black}[i] [{now}] ArchiveBox v{VERSION}: {cmd}{reset}'.format( + args = ' '.join(subcommand_args) + version_msg = '[dark_magenta]\\[i] [{now}] ArchiveBox v{VERSION}: [/dark_magenta][green4]archivebox [green3]{subcommand}[green2] {args}[/green2]'.format( now=datetime.now(timezone.utc).strftime('%Y-%m-%d %H:%M:%S'), VERSION=VERSION, - cmd=cmd, - **ANSI, - )) - stderr('{black} > {pwd}{reset}'.format(pwd=pwd, **ANSI)) - stderr() + subcommand=subcommand, + args=args, + ) + # stderr() + # stderr('{black} > {pwd}{reset}'.format(pwd=pwd, **ANSI)) + # stderr() + if SHOW_PROGRESS: + print(Panel(version_msg), file=sys.stderr) + else: + print(version_msg, file=sys.stderr) ### Parsing Stage diff --git a/archivebox/misc/logging.py b/archivebox/misc/logging.py index aee43254..5abdfee7 100644 --- a/archivebox/misc/logging.py +++ b/archivebox/misc/logging.py @@ -2,7 +2,6 @@ __package__ = 'archivebox.misc' # TODO: merge/dedupe this file with archivebox/logging_util.py -import os import sys from typing import Optional, Union, Tuple, List from collections import defaultdict @@ -11,12 +10,10 @@ from rich.console import Console from ..config_stubs import ConfigDict -SHOW_PROGRESS = None -if os.environ.get('SHOW_PROGRESS', 'None') in ('True', '1', 'true', 'yes'): - SHOW_PROGRESS = True +# SETUP RICH CONSOLE / TTY detection / COLOR / PROGRESS BARS +CONSOLE = Console() +IS_TTY = CONSOLE.is_interactive -CONSOLE = Console(force_interactive=SHOW_PROGRESS) -SHOW_PROGRESS = CONSOLE.is_interactive if SHOW_PROGRESS is None else SHOW_PROGRESS DEFAULT_CLI_COLORS = benedict( { diff --git a/archivebox/plugins_pkg/pip/apps.py b/archivebox/plugins_pkg/pip/apps.py index f11c8645..c60b86f3 100644 --- a/archivebox/plugins_pkg/pip/apps.py +++ b/archivebox/plugins_pkg/pip/apps.py @@ -147,7 +147,7 @@ class CheckUserIsNotRoot(BaseCheck): hint=f'Run ArchiveBox as a non-root user with a UID greater than 500. (currently running as UID {os.getuid()}).', ) ) - logger.debug('[√] UID is not root') + # logger.debug('[√] UID is not root') return errors @@ -168,7 +168,7 @@ class CheckPipEnvironment(BaseCheck): hint="Make sure the data dir is writable and make sure python3-pip and python3-venv are installed & available on the host.", ) ) - logger.debug("[√] CheckPipEnvironment: data/lib/pip virtualenv is setup properly") + # logger.debug("[√] CheckPipEnvironment: data/lib/pip virtualenv is setup properly") return errors