Skip to content
Myr

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

3 files+37 added−6 removed

modified:src/click/termui.py+23 added-6 removed
5 unmodified lines
6import sys78from ._compat import DEFAULT_COLUMNS9from ._compat import get_winterm_size10from ._compat import is_bytes11from ._compat import isatty12from ._compat import strip_ansi13from ._compat import WIN14from .exceptions import Abort
467 unmodified lines
482 * ``bright_cyan``483 * ``bright_white``484 * ``reset`` (reset the color code only)485485 .. versionadded:: 2.0486487 .. versionadded:: 7.0488 Added support for bright colors.489486 :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 lines
496 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.500501 .. versionchanged:: 8.0502 A non-string ``message`` is converted to a string.503504 .. versionchanged:: 7.0505 Added support for bright colors.506507 .. versionadded:: 2.0508 """509 if not isinstance(text, str):510 text = str(text)511512 bits = []513 if fg:514 try:515 bits.append(f"\033[{_ansi_colors[fg]}m")
41 unmodified lines
557558 All keyword arguments are forwarded to the underlying functions559 depending on which one they go with.560561 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.565566 .. versionchanged:: 8.0567 A non-string ``message`` is converted to a string. Bytes are568 passed through without style applied.569570 .. 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)574575 return echo(message, file=file, nl=nl, err=err, color=color)576577578def edit(
130 unmodified lines
modified:tests/test_termui.py+12 added
1import platform2import time34import pytest5
251 unmodified lines
257 bytes = outstreams[0].getvalue()258 assert bytes == b""259260261@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)265def 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 == expect270271272def test_progressbar_yields_all_items(runner):273 with click.progressbar(range(3)) as progress:274 assert len(list(progress)) == 3275
106 unmodified lines
modified:CHANGES.rst+2 added
37 unmodified lines
38- 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`444546Version 7.1.247-------------
635 unmodified lines