manrajgrover / halo

💫 Beautiful spinners for terminal, IPython and Jupyter
MIT License
2.86k stars 148 forks source link

Bug: UnicodeEncodeError when redirect stdout to a file #110

Closed alarreine closed 5 years ago

alarreine commented 5 years ago

Description

I'm trying to use Halo in a CLI that can be called from a terminal directly or from a CI, when I try to redirect the stdout to a file I have this exception.

Example:

def my_cli_task:
    spinner = halo.Halo(enabled=True, spinner='dots')
    spinner.start(text="Starting")
    ...

    ...
    spinner.succeed('Complete')
    spinner.stop()

It works perfectly in the terminal but when I try to redirect the stdout to a file, it raise an exception. e.g.:

$ ./my_example_cli_task.py > cli.log

System settings

Error

 Traceback (most recent call last):
  File "test.py", line 7, in <module>
    spin.start(text="Starting")
  File "/home/agustin/git/halo/halo/halo.py", line 423, in start
    self._render_frame()
  File "/home/agustin/git/halo/halo/halo.py", line 341, in _render_frame
    self._stream.write(output)
  File "/usr/local/lib/python2.7/dist-packages/colorama/ansitowin32.py", line 40, in write
    self.__convertor.write(text)
  File "/usr/local/lib/python2.7/dist-packages/colorama/ansitowin32.py", line 141, in write
    self.write_and_convert(text)
  File "/usr/local/lib/python2.7/dist-packages/colorama/ansitowin32.py", line 166, in write_and_convert
    self.write_plain_text(text, cursor, start)
  File "/usr/local/lib/python2.7/dist-packages/colorama/ansitowin32.py", line 174, in write_plain_text
    self.wrapped.write(text[start:end])
  File "/usr/local/lib/python2.7/dist-packages/colorama/ansitowin32.py", line 40, in write
    self.__convertor.write(text)
  File "/usr/local/lib/python2.7/dist-packages/colorama/ansitowin32.py", line 141, in write
    self.write_and_convert(text)
  File "/usr/local/lib/python2.7/dist-packages/colorama/ansitowin32.py", line 169, in write_and_convert
    self.write_plain_text(text, cursor, len(text))
  File "/usr/local/lib/python2.7/dist-packages/colorama/ansitowin32.py", line 174, in write_plain_text
    self.wrapped.write(text[start:end])
UnicodeEncodeError: 'ascii' codec can't encode character u'\u280b' in position 0: ordinal not in range(128)
Error in atexit._run_exitfuncs:
Traceback (most recent call last):
  File "/usr/lib/python2.7/atexit.py", line 24, in _run_exitfuncs
    func(*targs, **kargs)
  File "/home/agustin/git/halo/halo/halo.py", line 81, in clean_up
    self.stop()
  File "/home/agustin/git/halo/halo/halo.py", line 440, in stop
    self._spinner_thread.join()
  File "/usr/lib/python2.7/threading.py", line 929, in join
    raise RuntimeError("cannot join thread before it is started")
RuntimeError: cannot join thread before it is started
Error in sys.exitfunc:
Traceback (most recent call last):
  File "/usr/lib/python2.7/atexit.py", line 24, in _run_exitfuncs
    func(*targs, **kargs)
  File "/home/agustin/git/halo/halo/halo.py", line 81, in clean_up
    self.stop()
  File "/home/agustin/git/halo/halo/halo.py", line 440, in stop
    self._spinner_thread.join()
  File "/usr/lib/python2.7/threading.py", line 929, in join
    raise RuntimeError("cannot join thread before it is started")
RuntimeError: cannot join thread before it is started

Expected behaviour

Print the my CLI log to a file

Steps to recreate

Redirect the stdout to file

People to notify

@alarreine