Advertisement
jgtrosh

boxes_compare.py

Aug 5th, 2012
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.03 KB | None | 0 0
  1. import os
  2. def boxes(title, text):
  3.     t = []
  4.     lines = text.split('\n')
  5.     maxlen = max(len(line) for line in lines)
  6.     t.append(' '+'_'*(len(title)+2))
  7.     t.append('|'+' '*(len(title)+2)+'|')
  8.     t.append('| '+title+' |')
  9.     if len(title) < maxlen:
  10.         t.append('|'+'_'*(len(title)+2)+'|'+'_'*(maxlen-len(title)-1))
  11.     else:
  12.         t.append('|'+'_'*(len(title)+2)+'|')
  13.     t.append('|'+' '*(maxlen+2)+'|')
  14.     for line in lines:
  15.         t.append('| '+line+' '*(maxlen-len(line))+' |')
  16.     t.append('|'+'_'*(maxlen+2)+'|')
  17.     return t
  18.  
  19. short_title = boxes(\
  20. 'this is a short title',\
  21. '''this is a bunch of text
  22. which takes up more space than the title
  23. and it goes on
  24. and on
  25. and on''')
  26. long_title = boxes(\
  27. 'this is a long title',\
  28. '''compared
  29. with the
  30. short text
  31. in here''')
  32. tablen = max(len(t) for t in short_title)+8
  33. for i in range(max(len(t) for t in [short_title, long_title])):
  34.     if i < len(short_title):
  35.         t = short_title[i]
  36.         t += ' '*(tablen-len(short_title[i]))
  37.     else:
  38.         t = ' '*tablen
  39.     if i < len(long_title):
  40.         t += long_title[i]
  41.     print(t)
  42.  
  43. if 'win' in os.sys.platform:
  44.     os.system('PAUSE')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement