Merge pull request #1146 from segevfiner/style-non-text
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Handle non text in style/secho by converting to text
authored by David Lord · committed by GitHub · parents 66172ef, 353dd89 · Browse files · 3 files·+37 added−6 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+37 added−6 removed
- tests:+12 added−0 removed
- other:+25 added−6 removed
Provenance
- Commit
77cb257- Tree
e57d41b- Author
David Lord <davidism@gmail.com>- Committer
GitHub <noreply@github.com>- Parent 1
66172ef- Parent 2
353dd89
3 files changed+37 added−6 removed
modified:src/click/termui.py+23 added-6 removed
5 unmodified lines6 import sys7 8 from ._compat import DEFAULT_COLUMNS9 from ._compat import get_winterm_size10+from ._compat import is_bytes11 from ._compat import isatty12 from ._compat import strip_ansi13 from ._compat import WIN14 from .exceptions import Abort467 unmodified lines482 * ``bright_cyan``483 * ``bright_white``484 * ``reset`` (reset the color code only)485 485- .. versionadded:: 2.0486-487- .. versionadded:: 7.0488- Added support for bright colors.489-486 :param text: the string to style with ansi codes.487 :param fg: if provided this will become the foreground color.488 :param bg: if provided this will become the background color.489 :param bold: if provided this will enable or disable bold mode.6 unmodified lines496 other way round).497 :param reset: by default a reset-all code is added at the end of the498 string which means that styles do not carry over. This499 can be disabled to compose styles.500+501+ .. versionchanged:: 8.0502+ A non-string ``message`` is converted to a string.503+504+ .. versionchanged:: 7.0505+ Added support for bright colors.506+507+ .. versionadded:: 2.0508 """509+ if not isinstance(text, str):510+ text = str(text)511+512 bits = []513 if fg:514 try:515 bits.append(f"\033[{_ansi_colors[fg]}m")41 unmodified lines557 558 All keyword arguments are forwarded to the underlying functions559 depending on which one they go with.560 561+ Non-string types will be converted to :class:`str`. However,562+ :class:`bytes` are passed directly to :meth:`echo` without applying563+ style. If you want to style bytes that represent text, call564+ :meth:`bytes.decode` first.565+566+ .. versionchanged:: 8.0567+ A non-string ``message`` is converted to a string. Bytes are568+ passed through without style applied.569+570 .. versionadded:: 2.0571 """556- if message is not None:572+ if message is not None and not is_bytes(message):573 message = style(message, **styles)574+575 return echo(message, file=file, nl=nl, err=err, color=color)576 577 578 def edit(130 unmodified linesmodified:tests/test_termui.py+12 added
1+import platform2 import time3 4 import pytest5 251 unmodified lines257 bytes = outstreams[0].getvalue()258 assert bytes == b""259 260 261+@pytest.mark.skipif(platform.system() == "Windows", reason="No style on Windows.")262+@pytest.mark.parametrize(263+ ("value", "expect"), [(123, b"\x1b[45m123\x1b[0m"), (b"test", b"test")]264+)265+def test_secho_non_text(runner, value, expect):266+ with runner.isolation() as (out, _):267+ click.secho(value, nl=False, color=True, bg="magenta")268+ result = out.getvalue()269+ assert result == expect270+271+272 def test_progressbar_yields_all_items(runner):273 with click.progressbar(range(3)) as progress:274 assert len(list(progress)) == 3275 106 unmodified linesmodified:CHANGES.rst+2 added
37 unmodified lines38 - Include required parent arguments in help synopsis of subcommands.39 :issue:`1475`40 - Help for boolean flags with ``show_default=True`` shows the flag41 name instead of ``True`` or ``False``. :issue:`1538`42+- Non-string objects passed to ``style()`` and ``secho()`` will be43+ converted to string. :pr:`1146`44 45 46 Version 7.1.247 -------------635 unmodified lines