How to use the yaspin.compat.PY2 function in yaspin

To help you get started, we’ve selected a few yaspin examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github pavdmyt / yaspin / tests / test_in_out.py View on Github external
request.addfinalizer(teardown)

    #
    # Actual test
    #
    sp.hide()

    # ensure that hidden spinner flag was set
    assert sp._hide_spin.is_set()
    out, _ = capsys.readouterr()

    # ensure that text was cleared with the hide method
    assert out[-4:] == "\r\033[K"

    # properly encode text to unicode if running in PY2
    if PY2:
        text = to_unicode(text).encode(ENCODING)

    # ``\n`` is required to flush stdout during
    # the hidden state of the spinner
    sys.stdout.write("{0}\n".format(text))
    out, _ = capsys.readouterr()

    # cleans stdout from _clear_line and \r
    out = out.replace("\r\033[K", "")

    # handle out and text encodings (come out messy in PY2)
    # Under PY2 ``capsys.readouterr`` always produces ``out``
    # of type ``unicode``. Conversion to bytes is required
    # for proper ``out`` and ``text`` comparison.
    if PY2:
        out = out.encode(ENCODING)
github pavdmyt / yaspin / yaspin / core.py View on Github external
def _compose_out(self, frame, mode=None):
        # Ensure Unicode input
        assert isinstance(frame, str)
        assert isinstance(self._text, str)

        frame = frame.encode(ENCODING) if PY2 else frame
        text = self._text.encode(ENCODING) if PY2 else self._text

        # Colors
        if self._color_func is not None:
            frame = self._color_func(frame)

        # Position
        if self._side == "right":
            frame, text = text, frame

        # Mode
        if not mode:
            out = "\r{0} {1}".format(frame, text)
        else:
            out = "{0} {1}\n".format(frame, text)

        # Ensure output is bytes for Py2 and Unicode for Py3