Guest User

Untitled

a guest
Apr 20th, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.77 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # vim:fileencoding=utf8
  3.  
  4. # txtfx by drop table - many thanks to stack_overflow - jsbeuno, mark toleno, com4, aix etc...
  5. # no licence
  6.  
  7. from sys import stdout
  8. import random
  9. import sys
  10. import time
  11. import os
  12. import threading
  13.  
  14. esc = '\x1b[' # improved
  15. reset = '%s0m'%esc
  16. format = '1;%dm'
  17. fgoffset, bgoffset = 30, 40
  18.  
  19. attrs = dict ((name, val) for val, name in enumerate('none bold faint italic underline blink fast reverse concealed' .split()))
  20. colours = dict((name, val) for val, name in enumerate('grey red green yellow blue magenta cyan white' .split()))
  21.  
  22. bpoints = ( " [*] ", " [!] ", "[@]" )
  23.  
  24. spinner="|/-\\"
  25.  
  26. #convert the utf8 spinner string to a list
  27. chars=[c.encode("utf-8") for c in unicode(spinner,"utf-8")]
  28.  
  29. colist='grey blue cyan white cyan blue'.split()
  30.  
  31. class txtfx(threading.Thread):
  32.  
  33.     def __init__(self,arg=None):
  34.         super(txtfx,self).__init__()
  35.         self._stop = False
  36.     self.arg=arg
  37.     #self.cursave='\033[s'
  38.     #self.curload='\033[u'
  39.         self.spinpos=1
  40.         self.spinanim=0
  41.     self.txtpos=1
  42.     self.initpos=1
  43.  
  44.     def spin_advance (self):
  45.  
  46.     #blah = [reset], []
  47.         #tmpword = colist[colpos]
  48.         #blah.append(format % (colours[tmpword]+fgoffset))
  49.     stdout.write('\033[%dG'%self.spinpos + "{"+chars[self.spinanim] + "}"),
  50.     stdout.flush()
  51.     self.spinanim+=1
  52.     self.spinanim%=len(chars)
  53.     time.sleep(0.01)
  54.  
  55.     def esc_convert(self,w):
  56.  
  57.     word = w
  58.     if word.startswith('^') or word.startswith('@'):
  59.         truth = True
  60.     else:
  61.         truth = False
  62.     #truth = word.startswith('@')
  63.     cmd,txt = [reset], []
  64.  
  65.     if truth:
  66.                 # First check for a colour command
  67.         # word without first @ symbol
  68.                 tmpword = word[1:]
  69.  
  70.                 if tmpword in colours:
  71.  
  72.             if word.startswith('@'):
  73.                 tmpoffset=fgoffset
  74.             elif word.startswith('^'):
  75.                 tmpoffset=bgoffset
  76.  
  77.                         cmd.append(format % (colours[tmpword]+tmpoffset))
  78.                         #c=format % attrs[tmpword] if tmpword in attrs else None
  79.                         #if c and c not in cmd:
  80.                         #   cmd.append(c)
  81.             last_colour=esc.join(cmd)
  82.             return truth,esc.join(cmd)
  83.        
  84.                 # positioning (starts with @)
  85.                 if tmpword=='@':
  86.             # clear the screen
  87.                         cmd.append('2J')
  88.                         cmd.append('H')
  89.                         return truth, esc.join(cmd)
  90.                 else:
  91.             # move cursor position
  92.                         cmd.append('%sH'%tmpword)
  93.             # set column to initial text position
  94.             self.initpos=int(tmpword.split(';')[1])
  95.                         return truth, esc.join(cmd)
  96.        
  97.     else:
  98.                 if self.rndcase:
  99.             word = ''.join(random.choice((str.upper,str.lower))(x) for x in word)
  100. #           # fastest
  101. #           #caps = word[1].upper()
  102. #           #lowers = word[1].lower()
  103. #           #self.arg_process[listindex] = ''.join(random.choice(x) for x in zip(lowers, caps))
  104.             return truth,word
  105.  
  106.     def process(self):
  107.    
  108.         # split the line up into true and false statement values and text arg_process[0] = truth test, [1] = command or text
  109.     # self.esc_convert should return a tuple - ie thats why theres a function here that returns two values...
  110.     self.arg_process = [self.esc_convert(w) for w in self.arg.split()]
  111.  
  112.     print self.arg_process
  113.  
  114.     def run (self):
  115.  
  116.         spsw=True
  117.     sep=' '
  118.     end='\n'
  119.     cursor="_"
  120.     rndcase=True
  121.     txtspeed=10
  122.     spinspeed=5
  123.     bnum=0
  124.     # do you have to do all this ?
  125.     self.sep=sep
  126.     self.end=end
  127.     self.rndcase=rndcase
  128.     self.txtspeed=txtspeed
  129.     self.spinspeed=spinspeed
  130.     self.bnum=bnum
  131.     self.spsw=spsw
  132.     last_colour=''
  133.  
  134.     self.process()
  135.     self.cursor_invisible()
  136.  
  137.     #while not self._stop:
  138. #
  139.     # test if spinner is wanted and set the text position to be moved across a bit to allow for it
  140.     if self.spsw is True:
  141.         self.txtpos= (4+self.initpos)
  142.         self.spinpos = self.initpos
  143.     else:
  144.         self._stop=True
  145.  
  146.  #               if bnum != 0:
  147.   #                      for a in bpoints[bnum-1]:
  148.    #                             stdout.write(a)
  149.     #                            stdout.flush()
  150.      #                           time.sleep(0.1)
  151.  
  152.  
  153.     #print self.curload,
  154.     # go through each entry in self.arg_process
  155.         for word in self.arg_process:
  156.         # x is False / text
  157.         if word[0] is False:
  158.             # print out the words letter by letter
  159.             for letter in word[1]:
  160.                             stdout.write(last_colour + '\033[%dG'%self.txtpos + letter)
  161.                 self.spin_advance()
  162.                 time.sleep(0.02)
  163.                 self.txtpos +=1
  164.             # put a space at the end of the word
  165.                         #stdout.write('\033[1D')
  166.             stdout.write (' ')
  167.             self.txtpos +=1
  168.         # x is True / command
  169.         else:
  170.             pass
  171.                         stdout.write(word[1])
  172.                         stdout.flush()
  173.  
  174.     while not self._stop:
  175.         self.spin_advance()
  176.  
  177.     def cursor_visible(self):
  178.     stdout.write("\033[?25h")  
  179.     def cursor_invisible(self):
  180.     stdout.write("\033[?25l")  
  181.     pass
  182.     def stop(self):
  183.         self._stop = True
  184.     self.cursor_visible()
  185.     stdout.write("\033[0m") # restore default text
  186.     def stopped(self):
  187.         return self._stop == True
  188.  
  189. if __name__ == '__main__':
  190.  
  191.     # clears screen
  192.         t3=txtfx('@@')
  193.     t3.start()
  194.     t3.stop()
  195.     print
  196.    
  197. #        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')
  198. #   t1.start()
  199. #   time.sleep(5)
  200. #   t1.stop()
  201. #   print
  202.    
  203.         t4=txtfx('@22;22 this ^blue default coloured text appears at 22,22')
  204.     t4.start()
  205.     time.sleep(5)
  206.     t4.stop()
  207.     print
  208.  
  209.         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')
  210.     t2.start()
  211.     time.sleep(5)
  212.     t2.stop()
  213.     print
Add Comment
Please, Sign In to add comment