Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from __future__ import unicode_literals
- import sys
- import threading
- import time
- class Spinner(object):
- '''A console status indicator
- Akin to a throbber image or a busy cursor, a console spinner is a
- visual indicator that informs the user that the application is
- currently performing a long process. This class makes drawing
- a spinner simple and effective.
- The simplest way to use a ``Spinner`` is to instantiate one and
- then call :py:meth:`Spinner.spin`::
- spinner = Spinner()
- spinner.spin()
- To stop the spinner, call :py:meth:`Spinner.stop`.
- A more appropriate way to use a ``Spinner`` is to use it as a
- `Context Manager`_ using the `with statement`_::
- with Spinner():
- do_something()
- This will spawn a worker thread to draw the spinner while the
- operation inside the with statement continues. When the code inside
- the with statement block completes, the spinner will stop.
- :param start_text: Text to be printed before the spinner
- :param finish_text: Text to be printed in place of the spinner when
- it stops
- .. py:attr: chars
- A sequence of characters that will be printed one at a time,
- each replacing the previous, on the console. The characters
- should appear to form some kind of loop when printed in this
- way.
- .. _Context Manager:
- http://docs.python.org/3.2/reference/datamodel.html#context-managers
- .. _with statement:
- http://docs.python.org/3.2/reference/compound_stmts.html#with
- '''
- chars = '\|/-'
- def __init__(self, start_text='', finish_text=''):
- self.start_text = start_text
- self.finish_text = finish_text
- self.event = threading.Event()
- def __enter__(self):
- thread = threading.Thread(target=self.spin)
- thread.start()
- def __exit__(self, exc_type, exc_value, tb):
- self.stop()
- def spin(self):
- # Send the DECTCEM ANSI escape sequence to hide the cursor and
- # display the start text
- sys.stdout.write('\x1b[?25l{0}'.format(self.start_text))
- i = 0
- while not self.event.is_set():
- # Print the next character in the sequence and immediately
- # flush the buffer
- sys.stdout.write(self.chars[i])
- sys.stdout.flush()
- time.sleep(.1)
- # Print the "backspace" character to delete the previous
- # character
- sys.stdout.write('\b')
- # Increment or reset the character index
- i = (i + 1) % len(self.chars)
- # Send the DECTCEM ANSI escape sequence to show the cursor and
- # display the finish text
- sys.stdout.write('\x1b[?25h{0}\n'.format(self.finish_text))
- def stop(self):
- self.event.set()
- class TrigramSpinner(Spinner):
- '''A :py:class:`Spinner` that draws using trigrams'''
- chars = '☰☱☲☴☳☵☶☷'
- class NarySpinner(Spinner):
- '''A :py:class:`Spinner` that draws using N-ary operators'''
- chars = '⨁⨂'
- class QuarterCircleSpinner(Spinner):
- '''A :py:class:`Spinner` that draws using circles with segments'''
- chars = '◴◵◶◷'
- class FilledDiamondSpinner(Spinner):
- '''A :py:class:`Spinner` that draws using partially-filled diamonds'''
- chars = '⬖⬗⬘⬙'
- class FilledSquareSpinner(Spinner):
- '''A :py:class:`Spinner` that draws using partially-filled squares'''
- chars = '⬒⬓⬔⬕'
- class FilledCircleSpinner(Spinner):
- '''A :py:class:`Spinner` that draws using partially-filled circles'''
- chars = '○◔◑◕●◕◓◑◔'
- class SmallTriangleSpinner(Spinner):
- '''A :py:class:`Spinner` that draws using small triangles'''
- chars = '▵▹▿◃'
- class FilledSmallTriangleSpinner(Spinner):
- '''A :py:class:`Spinner` that draws using small filled triangles'''
- chars = '▴▸▾◂'
- class FilledTriangleSpinner(Spinner):
- '''A :py:class:`Spinner` that draws using filled triangles'''
- chars = '▲▶▼◀'
- class HalfSquareSpinner(Spinner):
- '''A :py:class:`Spinner` that draws using half-filled squares'''
- chars = '◢◣◤◥'
- class DitheredSquareSpinner(Spinner):
- '''A :py:class:`Spinner` that draws using dithered squares'''
- chars = '▤▧▥▨'
- class DiceSpinner(Spinner):
- '''A :py:class:`Spinner` that draws using die faces'''
- chars = '⚀⚁⚂⚃⚄⚅'
- if __name__ == '__main__':
- with Spinner('Working...', 'Done!'):
- try:
- time.sleep(1.5)
- except KeyboardInterrupt:
- sys.exit(0)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement