Last active
April 2, 2017 18:51
-
-
Save ClaireNeveu/2e45f7439d5e20fd421ef7b858a489db to your computer and use it in GitHub Desktop.
Python Wait Spinner
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/env python3 | |
import sys | |
import time | |
def main(): | |
spinner = Spinner() | |
for x in range(20): | |
time.sleep(0.2) | |
spinner.update() | |
spinner.done() | |
class Spinner: | |
HIDE_CURSOR = '\x1b[?25l' | |
SHOW_CURSOR = '\x1b[?25h' | |
#_phases = ['⎺', '⎻', '⎼', '⎽', '⎼', '⎻'] | |
_phases = ['••• ', ' ••• ', ' •••', '• ••', '•• •'] | |
_status = 0 | |
_spinner_length = 5 | |
def __init__(self): | |
sys.stdout.write(self.HIDE_CURSOR) | |
sys.stdout.flush() | |
def update(self): | |
new_status = self._phases[self._status] | |
sys.stdout.write('\b' * self._spinner_length) | |
sys.stdout.write(new_status) | |
sys.stdout.flush() | |
if self._status == len(self._phases) - 1: | |
self._status = 0 | |
else: | |
self._status += 1 | |
def done(self): | |
sys.stdout.write('\b' * self._spinner_length) | |
sys.stdout.write(' ' * self._spinner_length) | |
sys.stdout.write('\b' * self._spinner_length) | |
sys.stdout.write(self.SHOW_CURSOR) | |
sys.stdout.flush() | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment