jgtrosh

boxesalign.py

Aug 5th, 2012
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.58 KB | None | 0 0
  1. import os
  2. class box:
  3.     def __init__(self, text):
  4.         t = []
  5.         maxlen = []
  6.         text = text.split("[b]")
  7.         for i, p in enumerate(text):
  8.             p = p.split('[s]')
  9.             maxlen.append(max(len(line.replace('[r]', '').replace('[c]', '')) for line in p))
  10.             t.append('')
  11.             t.append(' \033[47;30m '+' '*maxlen[i]+' \033[0m')
  12.             for line in p:
  13.                 if '[r]' in line:
  14.                     t.append(' \033[47;30m '+' '*(maxlen[i]-len(line)+3)+line.replace('[r]', '')+' \033[0m')
  15.                 elif '[c]' in line:
  16.                     t.append(' \033[47;30m '+line.replace('[c]', '').center(maxlen[i], ' ')+' \033[0m')
  17.                 else:
  18.                     t.append(' \033[47;30m '+line+' '*(maxlen[i]-len(line))+' \033[0m')
  19.             t.append(' \033[47;30m '+' '*maxlen[i]+' \033[0m')
  20.         t.append('')
  21.         self.t = t
  22.         del t, maxlen, i
  23.     def output(self):
  24.         for line in self.t:
  25.             print line
  26.  
  27. if __name__ == '__main__':
  28.     a = box('this is the first box\
  29. [s]as you can see\
  30. [b]now you\'re reading the content from the second box\
  31. [b]we just switched to the third box\
  32. [s]not as large a box\
  33. [s]though slightly higher\
  34. [s]you can see that even though\
  35. [s]it goes on and on\
  36. [s]nothing broke yet\
  37. [s](well i hope so at least)\
  38. [b]anyway we reached the last box\
  39. [s]so it\'s time for good byes\
  40. [b][r]ACTUALLY, NO!\
  41. [s][r]you\'re getting treated with some sweet right align action :D\
  42. [s][c]PLUS mixing center align, whatever\
  43. [s][r]NOW, good night.')
  44.     a.output()
  45.     if 'win' in os.sys.platform:
  46.         os.system('PAUSE')
Advertisement
Add Comment
Please, Sign In to add comment