Marco Ricci commited on 2025-06-09 22:42:10
Zeige 3 geänderte Dateien mit 25 Einfügungen und 28 Löschungen.
There are still too many open questions concerning the conventions for overriding the automatic detection of color and text styling output. Settling on any interpretation of these conventions now and formalizing that as code will only come back to bite us later.
... | ... |
@@ -887,12 +887,23 @@ def color_forcing_callback( |
887 | 887 |
param: click.Parameter, |
888 | 888 |
value: Any, # noqa: ANN401 |
889 | 889 |
) -> None: |
890 |
- """Force the `click` context to honor `NO_COLOR` and `FORCE_COLOR`.""" |
|
890 |
+ """Disable automatic color (and text highlighting). |
|
891 |
+ |
|
892 |
+ Ideally, we would default to color and text styling if outputting to |
|
893 |
+ a TTY, or monochrome/unstyled otherwise. We would also support the |
|
894 |
+ `NO_COLOR` and `FORCE_COLOR` environment variables to override this |
|
895 |
+ auto-detection, and perhaps the `TTY_COMPATIBLE` variable too. |
|
896 |
+ |
|
897 |
+ Alas, this is not sensible to support at the moment, because the |
|
898 |
+ conventions are still in flux. And settling on a specific |
|
899 |
+ interpretation of the conventions would likely prove very difficult |
|
900 |
+ to change later on in a backward-compatible way. We thus opt for |
|
901 |
+ a conservative approach and use device-indepedendent text output |
|
902 |
+ without any color or text styling whatsoever. |
|
903 |
+ |
|
904 |
+ """ |
|
891 | 905 |
del param, value |
892 |
- if os.environ.get('NO_COLOR'): |
|
893 | 906 |
ctx.color = False |
894 |
- if os.environ.get('FORCE_COLOR'): |
|
895 |
- ctx.color = True |
|
896 | 907 |
|
897 | 908 |
|
898 | 909 |
def validate_occurrence_constraint( |
... | ... |
@@ -1299,11 +1299,6 @@ class Parametrize(types.SimpleNamespace): |
1299 | 1299 |
['--export-as=sh'], |
1300 | 1300 |
], |
1301 | 1301 |
) |
1302 |
- FORCE_COLOR = pytest.mark.parametrize( |
|
1303 |
- 'force_color', |
|
1304 |
- [False, True], |
|
1305 |
- ids=['noforce', 'force'], |
|
1306 |
- ) |
|
1307 | 1302 |
INCOMPLETE = pytest.mark.parametrize('incomplete', ['', 'partial']) |
1308 | 1303 |
ISATTY = pytest.mark.parametrize( |
1309 | 1304 |
'isatty', |
... | ... |
@@ -1459,11 +1454,6 @@ class Parametrize(types.SimpleNamespace): |
1459 | 1454 |
MODERN_EDITOR_INTERFACE = pytest.mark.parametrize( |
1460 | 1455 |
'modern_editor_interface', [False, True], ids=['legacy', 'modern'] |
1461 | 1456 |
) |
1462 |
- NO_COLOR = pytest.mark.parametrize( |
|
1463 |
- 'no_color', |
|
1464 |
- [False, True], |
|
1465 |
- ids=['yescolor', 'nocolor'], |
|
1466 |
- ) |
|
1467 | 1457 |
NOTES_PLACEMENT = pytest.mark.parametrize( |
1468 | 1458 |
['notes_placement', 'placement_args'], |
1469 | 1459 |
[ |
... | ... |
@@ -1824,22 +1814,22 @@ class TestAllCLI: |
1824 | 1814 |
) |
1825 | 1815 |
assert result.clean_exit(empty_stderr=True), 'expected clean exit' |
1826 | 1816 |
|
1827 |
- @Parametrize.NO_COLOR |
|
1828 |
- @Parametrize.FORCE_COLOR |
|
1829 | 1817 |
@Parametrize.ISATTY |
1830 | 1818 |
@Parametrize.COLORFUL_COMMAND_INPUT |
1831 |
- def test_201_no_color_force_color( |
|
1819 |
+ def test_201_automatic_color_mode( |
|
1832 | 1820 |
self, |
1833 |
- no_color: bool, |
|
1834 |
- force_color: bool, |
|
1835 | 1821 |
isatty: bool, |
1836 | 1822 |
command_line: list[str], |
1837 | 1823 |
input: str | None, |
1838 | 1824 |
) -> None: |
1839 |
- """Respect the `NO_COLOR` and `FORCE_COLOR` environment variables.""" |
|
1840 |
- # Force color on if force_color. Otherwise force color off if |
|
1841 |
- # no_color. Otherwise set color if and only if we have a TTY. |
|
1842 |
- color = force_color or not no_color if isatty else force_color |
|
1825 |
+ """Auto-detect if color should be used. |
|
1826 |
+ |
|
1827 |
+ (The answer currently is always no. See the |
|
1828 |
+ [`conventional-configurable-text-styling` wishlist |
|
1829 |
+ entry](../wishlist/conventional-configurable-text-styling.md).) |
|
1830 |
+ |
|
1831 |
+ """ |
|
1832 |
+ color = False |
|
1843 | 1833 |
runner = tests.CliRunner(mix_stderr=False) |
1844 | 1834 |
# TODO(the-13th-letter): Rewrite using parenthesized |
1845 | 1835 |
# with-statements. |
... | ... |
@@ -1852,10 +1842,6 @@ class TestAllCLI: |
1852 | 1842 |
runner=runner, |
1853 | 1843 |
) |
1854 | 1844 |
) |
1855 |
- if no_color: |
|
1856 |
- monkeypatch.setenv('NO_COLOR', 'yes') |
|
1857 |
- if force_color: |
|
1858 |
- monkeypatch.setenv('FORCE_COLOR', 'yes') |
|
1859 | 1845 |
result = runner.invoke( |
1860 | 1846 |
cli.derivepassphrase, |
1861 | 1847 |
command_line, |
1862 | 1848 |