Skip to content

Instantly share code, notes, and snippets.

@kxtells
Created July 20, 2012 20:35
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save kxtells/3153079 to your computer and use it in GitHub Desktop.
Save kxtells/3153079 to your computer and use it in GitHub Desktop.
Rotating Stick Class for Python
import sys
import threading
import time
class cSpinner(threading.Thread):
"""
Print things to one line dynamically
"""
chars = ["\\","|","/","-"]
index = 0
keeprunning = True
def run(self):
while self.keeprunning:
self.printing(self.chars[self.index%len(self.chars)])
time.sleep(0.1)
self.index +=1
def printing(self,data):
sys.stdout.write("\r\x1b[K"+data.__str__())
sys.stdout.flush()
def stop(self):
self.keeprunning = False
@kxtells
Copy link
Author

kxtells commented Jul 20, 2012

Example:
S = cSpinner()
s.start()

It is not perfect, and is important to call stop when you want it finish. Your main program shoud check for system signals to avoid leaving the cSpinner hanging there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment