diff options
Diffstat (limited to 'color.py')
| -rw-r--r-- | color.py | 44 |
1 files changed, 25 insertions, 19 deletions
| @@ -84,29 +84,39 @@ def _Color(fg=None, bg=None, attr=None): | |||
| 84 | 84 | ||
| 85 | DEFAULT = None | 85 | DEFAULT = None |
| 86 | 86 | ||
| 87 | # Placholder value that indicates we need to check if the user is in an | ||
| 88 | # interactive terminal session to determine if we turn on color or not. | ||
| 89 | _CHECK_CONSOLE = object() | ||
| 90 | |||
| 91 | # https://git-scm.com/docs/git-config#Documentation/git-config.txt-colorui | ||
| 92 | _CONFIG_TO_COLOR_SETTING = { | ||
| 93 | "false": False, | ||
| 94 | "never": False, | ||
| 95 | "no": False, | ||
| 96 | "auto": _CHECK_CONSOLE, | ||
| 97 | "true": _CHECK_CONSOLE, | ||
| 98 | "yes": _CHECK_CONSOLE, | ||
| 99 | "always": True, | ||
| 100 | } | ||
| 101 | |||
| 87 | 102 | ||
| 88 | def SetDefaultColoring(state: Optional[str]) -> None: | 103 | def SetDefaultColoring(state: Optional[str]) -> None: |
| 89 | """Set coloring behavior to |state|. | 104 | """Set coloring behavior to |state|. |
| 90 | 105 | ||
| 91 | This is useful for overriding config options via the command line. | 106 | This is useful for overriding config options via the command line. |
| 92 | """ | 107 | """ |
| 93 | if state is None: | ||
| 94 | # Leave it alone -- return quick! | ||
| 95 | return | ||
| 96 | 108 | ||
| 97 | global DEFAULT | 109 | global DEFAULT |
| 98 | state = state.lower() | 110 | |
| 99 | if state in ("auto",): | 111 | if isinstance(state, str): |
| 112 | state = state.lower() | ||
| 113 | if state in _CONFIG_TO_COLOR_SETTING: | ||
| 100 | DEFAULT = state | 114 | DEFAULT = state |
| 101 | elif state in ("always", "yes", "true"): | ||
| 102 | DEFAULT = "always" | ||
| 103 | elif state in ("never", "no", "false"): | ||
| 104 | DEFAULT = "never" | ||
| 105 | 115 | ||
| 106 | 116 | ||
| 107 | class Coloring: | 117 | class Coloring: |
| 108 | def __init__(self, config, section_type): | 118 | def __init__(self, config, section_type): |
| 109 | self._section = "color.%s" % section_type | 119 | self._section = f"color.{section_type}" |
| 110 | self._config = config | 120 | self._config = config |
| 111 | self._out = sys.stdout | 121 | self._out = sys.stdout |
| 112 | 122 | ||
| @@ -115,16 +125,12 @@ class Coloring: | |||
| 115 | on = self._config.GetString(self._section) | 125 | on = self._config.GetString(self._section) |
| 116 | if on is None: | 126 | if on is None: |
| 117 | on = self._config.GetString("color.ui") | 127 | on = self._config.GetString("color.ui") |
| 128 | if isinstance(on, str): | ||
| 129 | on = on.lower() | ||
| 118 | 130 | ||
| 119 | if on == "auto": | 131 | self._on = _CONFIG_TO_COLOR_SETTING.get(on, _CHECK_CONSOLE) |
| 120 | if pager.active or os.isatty(1): | 132 | if self._on is _CHECK_CONSOLE: |
| 121 | self._on = True | 133 | self._on = pager.active or os.isatty(1) |
| 122 | else: | ||
| 123 | self._on = False | ||
| 124 | elif on in ("true", "always"): | ||
| 125 | self._on = True | ||
| 126 | else: | ||
| 127 | self._on = False | ||
| 128 | 134 | ||
| 129 | def redirect(self, out): | 135 | def redirect(self, out): |
| 130 | self._out = out | 136 | self._out = out |
