import textwrap
import re
def cowsay(string, length=39):
print (string)
return build_bubble(re.sub('\s{3,}',' ',string), length) + build_cow()
#return " ".join(string.split())
def build_cow():
return r'''
\ ^__^
\ (oo)\_______
(__)\ )\/\
||----w |
|| ||
'''
def build_bubble(string, length=39):
bubble = []
lines = normalize_text(string, length)
bordersize = len(lines[0])
bubble.append("")
bubble.append(" " + "_" * (bordersize+2))
for index, line in enumerate(lines):
border = get_border(lines, index)
print (line)
bubble.append("%s %s %s" % (border[0], line, border[1]))
bubble.append(" " + "-" * (bordersize+2))
return "\n".join(bubble)
def normalize_text(string, length):
lines = textwrap.wrap(string, length)
maxlen = len(max(lines, key=len))
return [ line.ljust(maxlen) for line in lines ]
def get_border(lines, index):
if len(lines) < 2:
return [ "<", ">" ]
elif index == 0:
return [ "/", "\\" ]
elif index == len(lines) - 1:
return [ "\\", "/" ]
else:
return [ "|", "|" ]
Created at: 2014/09/22 12:22; Updated at: 2014/09/23 07:49