Respect color setting

This commit is contained in:
Ivan Habunek 2023-06-28 13:56:59 +02:00
parent 85260ed99d
commit 4388175cb4
No known key found for this signature in database
GPG Key ID: CDBD63C43A30BB95
2 changed files with 13 additions and 5 deletions

View File

@ -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"<red>{a}</red>" 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)

View File

@ -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]