From 4388175cb43f43acd269accbd98977c029482f62 Mon Sep 17 00:00:00 2001 From: Ivan Habunek Date: Wed, 28 Jun 2023 13:56:59 +0200 Subject: [PATCH] Respect color setting --- toot/output.py | 15 +++++++++++---- toot/settings.py | 3 ++- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/toot/output.py b/toot/output.py index 83cd235..186f4d9 100644 --- a/toot/output.py +++ b/toot/output.py @@ -3,6 +3,7 @@ import re import sys import textwrap +from functools import lru_cache from toot.entities import Instance, Notification, Poll, Status from toot.utils import get_text, parse_html from toot.wcstring import wc_wrap @@ -99,6 +100,7 @@ def strip_tags(message): return re.sub(STYLE_TAG_PATTERN, "", message) +@lru_cache(maxsize=None) def use_ansi_color(): """Returns True if ANSI color codes should be used.""" @@ -115,23 +117,28 @@ def use_ansi_color(): if "--no-color" in sys.argv: return False + # Check in settings + from toot.settings import get_setting + color = get_setting("common.color", bool) + if color is not None: + return color + + # Use color by default return True -USE_ANSI_COLOR = use_ansi_color() - QUIET = "--quiet" in sys.argv def print_out(*args, **kwargs): if not QUIET: - args = [colorize(a) if USE_ANSI_COLOR else strip_tags(a) for a in args] + args = [colorize(a) if use_ansi_color() else strip_tags(a) for a in args] print(*args, **kwargs) def print_err(*args, **kwargs): args = [f"{a}" for a in args] - args = [colorize(a) if USE_ANSI_COLOR else strip_tags(a) for a in args] + args = [colorize(a) if use_ansi_color() else strip_tags(a) for a in args] print(*args, file=sys.stderr, **kwargs) diff --git a/toot/settings.py b/toot/settings.py index 3bb0ab2..1f08afe 100644 --- a/toot/settings.py +++ b/toot/settings.py @@ -1,8 +1,8 @@ from functools import lru_cache from os.path import exists, join -from typing import Optional, Type, TypeVar from tomlkit import parse from toot.config import get_config_dir +from typing import Type TOOT_SETTINGS_FILE_NAME = "settings.toml" @@ -43,6 +43,7 @@ def _get_setting(dct, keys, type: Type, default=None): if isinstance(dct, type): return dct else: + # TODO: warn? cast? both? return default key = keys[0]