Welcome, guest | Sign In | My Account | Store | Cart

A class providing a threaded spin cursor on the console.

Python, 54 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import threading
import sys, os
import time
import unicodedata

class SpinCursor(threading.Thread):
    """ A console spin cursor class """
    
    def __init__(self, msg='',maxspin=0,minspin=10,speed=5):
        # Count of a spin
        self.count = 0
        self.out = sys.stdout
        self.flag = False
        self.max = maxspin
        self.min = minspin
        # Any message to print first ?
        self.msg = msg
        # Complete printed string
        self.string = ''
        # Speed is given as number of spins a second
        # Use it to calculate spin wait time
        self.waittime = 1.0/float(speed*4)
        if os.name == 'posix':
            self.spinchars = (unicodedata.lookup('FIGURE DASH'),u'\\ ',u'| ',u'/ ')
        else:
            # The unicode dash character does not show
            # up properly in Windows console.
            self.spinchars = (u'-',u'\\ ',u'| ',u'/ ')        
        threading.Thread.__init__(self, None, None, "Spin Thread")
        
    def spin(self):
        """ Perform a single spin """

        for x in self.spinchars:
            self.string = self.msg + "...\t" + x + "\r"
            self.out.write(self.string.encode('utf-8'))
            self.out.flush()
            time.sleep(self.waittime)

    def run(self):

        while (not self.flag) and ((self.count<self.min) or (self.count<self.max)):
            self.spin()
            self.count += 1

        # Clean up display...
        self.out.write(" "*(len(self.string) + 1))
        
    def stop(self):
        self.flag = True
        
if __name__ == "__main__":
    spin = SpinCursor(msg="Spinning...",minspin=5,speed=5)
    spin.start()

Use the SpinCursor class to indicate that your program is busy doing something. The cursor can be customized to set its min spin count, max spin count and number of spins per second.

Example Usage...

This will spin 5 times even if the data arrives early...

spin = SpinCursor(minspin=5, msg="Waiting for data...") spin.start()

if data_arrived(): spin.join()

This will spin only during the waiting time...

spin = SpinCursor(msg="Waiting for data...") spin.start()

if data_arrived(): spin.stop()

This will spin really fast...!

spin = SpinCursor(msg="I am really busy...", speed=50) spin.start()