-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcowsay.py
64 lines (46 loc) · 1.44 KB
/
cowsay.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# A python implementation of cowsay <http://www.nog.net/~tony/warez/cowsay.shtml>
# Copyright 2011 Jesse Chan-Norris <[email protected]>
# Licensed under the GNU LGPL version 3.0
import sys
import textwrap
from sopel import module
def cowsay(str, length=40):
return build_bubble(str, length) + build_cow()
def build_cow():
return """
\ ^__^
\ (oo)\_______
(__)\ )\/\\
||----w |
|| ||
"""
def build_bubble(str, length=40):
bubble = []
lines = normalize_text(str, length)
bordersize = len(lines[0])
bubble.append(" " + "_" * bordersize)
for index, line in enumerate(lines):
border = get_border(lines, index)
bubble.append("%s %s %s" % (border[0], line, border[1]))
bubble.append(" " + "-" * bordersize)
return "\n".join(bubble)
def normalize_text(str, length):
lines = textwrap.wrap(str, 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 [ "|", "|" ]
def cowsay_cmd(bot, input):
input_text = input.group(2)
if input_text:
moo = cowsay(input_text)
for l in moo.rsplit('\n'):
bot.say(l)
cowsay_cmd.commands = ['cowsay']