Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- # vim:fileencoding=utf8
- # txtfx by drop table - many thanks to stack_overflow - jsbeuno, mark toleno, com4, aix etc...
- # no licence
- from sys import stdout
- import random
- import sys
- import time
- import os
- import threading
- esc = '\x1b[' # improved
- reset = '%s0m'%esc
- format = '1;%dm'
- fgoffset, bgoffset = 30, 40
- attrs = dict ((name, val) for val, name in enumerate('none bold faint italic underline blink fast reverse concealed' .split()))
- colours = dict((name, val) for val, name in enumerate('grey red green yellow blue magenta cyan white' .split()))
- bpoints = ( " [*] ", " [!] ", "[@]" )
- spinner="|/-\\"
- #convert the utf8 spinner string to a list
- chars=[c.encode("utf-8") for c in unicode(spinner,"utf-8")]
- colist='grey blue cyan white cyan blue'.split()
- class txtfx(threading.Thread):
- def __init__(self,arg=None):
- super(txtfx,self).__init__()
- self._stop = False
- self.arg=arg
- #self.cursave='\033[s'
- #self.curload='\033[u'
- self.spinpos=1
- self.spinanim=0
- self.txtpos=1
- self.initpos=1
- def spin_advance (self):
- #blah = [reset], []
- #tmpword = colist[colpos]
- #blah.append(format % (colours[tmpword]+fgoffset))
- stdout.write('\033[%dG'%self.spinpos + "{"+chars[self.spinanim] + "}"),
- stdout.flush()
- self.spinanim+=1
- self.spinanim%=len(chars)
- time.sleep(0.01)
- def esc_convert(self,w):
- word = w
- if word.startswith('^') or word.startswith('@'):
- truth = True
- else:
- truth = False
- #truth = word.startswith('@')
- cmd,txt = [reset], []
- if truth:
- # First check for a colour command
- # word without first @ symbol
- tmpword = word[1:]
- if tmpword in colours:
- if word.startswith('@'):
- tmpoffset=fgoffset
- elif word.startswith('^'):
- tmpoffset=bgoffset
- cmd.append(format % (colours[tmpword]+tmpoffset))
- #c=format % attrs[tmpword] if tmpword in attrs else None
- #if c and c not in cmd:
- # cmd.append(c)
- last_colour=esc.join(cmd)
- return truth,esc.join(cmd)
- # positioning (starts with @)
- if tmpword=='@':
- # clear the screen
- cmd.append('2J')
- cmd.append('H')
- return truth, esc.join(cmd)
- else:
- # move cursor position
- cmd.append('%sH'%tmpword)
- # set column to initial text position
- self.initpos=int(tmpword.split(';')[1])
- return truth, esc.join(cmd)
- else:
- if self.rndcase:
- word = ''.join(random.choice((str.upper,str.lower))(x) for x in word)
- # # fastest
- # #caps = word[1].upper()
- # #lowers = word[1].lower()
- # #self.arg_process[listindex] = ''.join(random.choice(x) for x in zip(lowers, caps))
- return truth,word
- def process(self):
- # split the line up into true and false statement values and text arg_process[0] = truth test, [1] = command or text
- # self.esc_convert should return a tuple - ie thats why theres a function here that returns two values...
- self.arg_process = [self.esc_convert(w) for w in self.arg.split()]
- print self.arg_process
- def run (self):
- spsw=True
- sep=' '
- end='\n'
- cursor="_"
- rndcase=True
- txtspeed=10
- spinspeed=5
- bnum=0
- # do you have to do all this ?
- self.sep=sep
- self.end=end
- self.rndcase=rndcase
- self.txtspeed=txtspeed
- self.spinspeed=spinspeed
- self.bnum=bnum
- self.spsw=spsw
- last_colour=''
- self.process()
- self.cursor_invisible()
- #while not self._stop:
- #
- # test if spinner is wanted and set the text position to be moved across a bit to allow for it
- if self.spsw is True:
- self.txtpos= (4+self.initpos)
- self.spinpos = self.initpos
- else:
- self._stop=True
- # if bnum != 0:
- # for a in bpoints[bnum-1]:
- # stdout.write(a)
- # stdout.flush()
- # time.sleep(0.1)
- #print self.curload,
- # go through each entry in self.arg_process
- for word in self.arg_process:
- # x is False / text
- if word[0] is False:
- # print out the words letter by letter
- for letter in word[1]:
- stdout.write(last_colour + '\033[%dG'%self.txtpos + letter)
- self.spin_advance()
- time.sleep(0.02)
- self.txtpos +=1
- # put a space at the end of the word
- #stdout.write('\033[1D')
- stdout.write (' ')
- self.txtpos +=1
- # x is True / command
- else:
- pass
- stdout.write(word[1])
- stdout.flush()
- while not self._stop:
- self.spin_advance()
- def cursor_visible(self):
- stdout.write("\033[?25h")
- def cursor_invisible(self):
- stdout.write("\033[?25l")
- pass
- def stop(self):
- self._stop = True
- self.cursor_visible()
- stdout.write("\033[0m") # restore default text
- def stopped(self):
- return self._stop == True
- if __name__ == '__main__':
- # clears screen
- t3=txtfx('@@')
- t3.start()
- t3.stop()
- print
- # t1=txtfx('@grey this @red text @blue has @cyan a @yellow woo @green spinner effect, and a typing effect, uses @cyan threads and a class and is programmed by someone')
- # t1.start()
- # time.sleep(5)
- # t1.stop()
- # print
- t4=txtfx('@22;22 this ^blue default coloured text appears at 22,22')
- t4.start()
- time.sleep(5)
- t4.stop()
- print
- t2=txtfx('@green this @blue text @blue has @cyan a @yellow whatever @green spinner effect, and a typing effect, uses threads and a class ^grey and is programmed by someone who hasnt got a clue')
- t2.start()
- time.sleep(5)
- t2.stop()
- print
Add Comment
Please, Sign In to add comment