-
Notifications
You must be signed in to change notification settings - Fork 0
/
matix.py
107 lines (79 loc) · 2.4 KB
/
matix.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#!/usr/bin/python
from random import choice, randint
import weechat
NAME = 'matix'
AUTHOR = 'aji <http://ajitek.net>'
VERSION = '1.0'
LICENSE = 'MIT'
DESC = 'matix has u'
CMD_DESC = 'toggle matix to a buffer'
CMD_ARGS = '[off | <time>]'
CMD_ARGS_DESC = '''
use with no arg or "off" to turn off. with "time" value,
send to channel at one line per time (in secs)'''
# the algorithm thing
alphabet = '!@#$%^&*()[]{}?+=\\|",'
def cell_display(c):
#return ' 0123456789abcdefghijklmnopqrstuvwxyz'[c+1]
if c == 0:
return ' '
ch = choice(alphabet)
if c == -1:
return '\2' + ch + '\2'
return '\3' + choice('39') + ch + '\3'
def cell_step(c):
if c == 0 or c == -1:
return int(randint(0, 10+c) / 10)
else:
return (c + 1) if randint(0, c) < 13 else -1
def cells_display(s):
return ' '.join([cell_display(c) for c in s])
def cells_step(s):
return [cell_step(c) for c in s]
def new_cells():
return [(0 if randint(0, 3) else randint(0, 13)) for x in range(30)]
# the weechat half
targets = {}
def do_matix(data, remaining_calls):
if not data in targets:
return
tgt = targets[data]
weechat.command(data, cells_display(tgt['cells']))
tgt['cells'] = cells_step(tgt['cells'])
return weechat.WEECHAT_RC_OK
def add_target(buf, dur):
tgt = {}
if buf in targets:
tgt = targets[buf]
weechat.unhook(tgt['timer'])
else:
targets[buf] = tgt
tgt['cells'] = new_cells()
# call once, since timer won't fire when first set
do_matix(buf, 0)
tgt['timer'] = weechat.hook_timer(dur, 0, 0, 'do_matix', buf)
def cancel_target(buf):
if buf not in targets:
return
tgt = targets[buf]
del targets[buf]
weechat.unhook(tgt['timer'])
def toggle_target(buf):
if buf in targets:
cancel_target(buf)
else:
add_target(buf)
def cmd_matix(data, buf, args):
if len(args) == 0 or args.lower() == 'off':
cancel_target(buf)
return weechat.WEECHAT_RC_OK
dur = int(float(args) * 1000)
if dur < 10:
weechat.prnt(buf, 'too fast!')
return weechat.WEECHAT_RC_ERROR
add_target(buf, dur)
return weechat.WEECHAT_RC_OK
if __name__ == '__main__':
weechat.register(NAME, AUTHOR, VERSION, LICENSE, DESC, '', '')
weechat.hook_command('matix', CMD_DESC, CMD_ARGS, CMD_ARGS_DESC,
'', 'cmd_matix', '')