Debian Codegraph @

hypothesis-python/src/hypothesis/utils/terminal.py

raw
1 # This file is part of Hypothesis, which may be found at
2 # https://github.com/HypothesisWorks/hypothesis/
3 #
4 # Copyright the Hypothesis Authors.
5 # Individual contributors are listed in AUTHORS.rst and the git log.
6 #
7 # This Source Code Form is subject to the terms of the Mozilla Public License,
8 # v. 2.0. If a copy of the MPL was not distributed with this file, You can
9 # obtain one at https://mozilla.org/MPL/2.0/.
10
11 import os
12 from typing import Literal
13
14
15 def guess_background_color() -> Literal["light", "dark", "unknown"]:
16 """Returns one of "dark", "light", or "unknown".
17
18 This is basically just guessing, but better than always guessing "dark"!
19 See also https://stackoverflow.com/questions/2507337/ and
20 https://unix.stackexchange.com/questions/245378/
21 """
22 django_colors = os.getenv("DJANGO_COLORS", "")
23 for theme in ("light", "dark"):
24 if theme in django_colors.split(";"):
25 return theme
26 # Guessing based on the $COLORFGBG environment variable
27 try:
28 fg, *_, bg = os.getenv("COLORFGBG", "").split(";")
29 except Exception:
30 pass
31 else:
32 # 0=black, 7=light-grey, 15=white ; we don't interpret other colors
33 if fg in ("7", "15") and bg == "0":
34 return "dark"
35 elif fg == "0" and bg in ("7", "15"):
36 return "light"
37 # TODO: Guessing based on the xterm control sequence
38 return "unknown"