archive- Random tools & helpful resources for IRC |
git clone git://git.acid.vegas/archive.git |
Log | Files | Refs | Archive |
prismx.py (4571B)
1 # Copyright (c) 2010 Alex Barrett <al.barrett@gmail.com> 2 # 3 # Everyone is permitted to copy and distribute verbatim or modified 4 # copies of this license document, and changing it is allowed as long 5 # as the name is changed. 6 # 7 # DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE 8 # TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 9 # 10 # 0. You just DO WHAT THE FUCK YOU WANT TO. 11 12 import weechat as w 13 import random 14 import re 15 16 SCRIPT_NAME = "prismx" 17 SCRIPT_AUTHOR = "Alex Barrett <al.barrett@gmail.com>" 18 SCRIPT_VERSION = "0.3.1" 19 SCRIPT_LICENSE = "WTFPL" 20 SCRIPT_DESC = "Taste the rainbow." 21 22 # red, lightred, brown, yellow, green, lightgreen, cyan, 23 # lightcyan, blue, lightblue, magenta, lightmagenta 24 ncolors = [5, 4, 7, 8, 3, 9, 10, 11, 2, 12, 6, 13] 25 xcolors = [ 26 16,28,40,52,64,65,53,41,29,17,18,30,42,54,66,67,55,43,31,19,20,32,44, 27 56,68,69,57,45,33,21,22,34,46,58,70,71,59,47,35,23,24,36,48,60,72,73, 28 61,49,37,25,26,38,50,62,74,75,63,51,39,27] 29 xxcolors = range(100) 30 31 # we set this later 32 color_count = 0 33 34 # keeping a global index means the coloring will pick up where it left off 35 color_index = 0 36 37 # spaces don't need to be colored and commas cannot be because mIRC is dumb 38 chars_neutral = " ," 39 chars_control = "\x01-\x1f\x7f-\x9f" 40 41 regex_chars = "[^%(n)s%(s)s][%(n)s%(s)s]*" % { 'n': chars_neutral, 's': chars_control } 42 regex_words = "[^%(n)s]+[%(n)s%(s)s]*" % { 'n': chars_neutral, 's': chars_control } 43 44 45 if w.register(SCRIPT_NAME, SCRIPT_AUTHOR, SCRIPT_VERSION, 46 SCRIPT_LICENSE, SCRIPT_DESC, "", ""): 47 w.hook_command("prism", 48 SCRIPT_DESC, 49 "[-rwmbexsp] [palette] text|-c[wbexsp] [palette] <sep> <command> <sep>text", 50 " -r: randomizes the order of the color sequence\n" 51 " -w: color entire words instead of individual characters\n" 52 " -m: append /me to beginning of output\n" 53 " -b: backwards text (entire string is reversed)\n" 54 " -e: eye-destroying colors (randomized background colors)\n" 55 " -c: specify a separator to turn on colorization\n" 56 " eg. -c : /topic :howdy howdy howdy\n" 57 " -x: extended color set, requires 256color terminal\n" 58 " -s: stretch to fit text\n" 59 " -p: specify color palette to use, comma separated\n" 60 " text: text to be colored", 61 "-r|-w|-m|-b|-e|-c", "prism_cmd_cb", "") 62 63 def prism_cmd_cb(data, buffer, args): 64 global color_index 65 color_local = color_index 66 color_index += 1 67 68 input = args.decode("UTF-8") 69 input_method = "command" 70 71 if not input or (input[0] == '-' and input.find(' ') == -1): 72 input = (input + ' ' if input else '') + w.buffer_get_string(buffer, "input") 73 input = input.decode("UTF-8") 74 input_method = "keybinding" 75 76 if not input: 77 return w.WEECHAT_RC_OK 78 79 optstop = input and input[0] == '-' and input.find(' ') 80 opts = input[1:optstop] if optstop else '' 81 cmdstop = 'c' in opts and input.find(' ', optstop+1) 82 cmd = '' 83 if 'm' in opts: cmd = '/me ' 84 if 'c' in opts: 85 find = input[optstop+1:cmdstop] 86 where = input.find(find, cmdstop+1) 87 cmd = input[cmdstop+1:where] 88 input = input[where+len(find):] 89 else: 90 input = input[optstop+bool(optstop):] 91 regex = regex_words if 'w' in opts else regex_chars 92 inc = 'r' not in opts 93 bs = 'e' in opts 94 colors = ncolors if 'x' not in opts else (xxcolors if bs or not inc else xcolors) 95 if 'p' in opts: 96 i = input.find(' ') 97 colors = input[:i].split(',') 98 input = input[i+1:] 99 input = input[::-1] if 'b' in opts else input 100 output = u"" 101 tokens = re.findall(regex, input) 102 103 if 's' in opts: 104 color_local = 0 105 colors = [colors[int(float(i)/len(tokens)*len(colors))] 106 for i in xrange(len(tokens))] 107 108 color_count = len(colors) 109 for token in tokens: 110 # prefix each token with a color code 111 c1 = unicode(colors[color_local % color_count]).rjust(2, "0") 112 if bs: 113 c2 = random.randint(1, color_count - 1) % color_count 114 c2 = unicode(colors[c2 + 1 if c2 == color_local % color_count else c2]).rjust(2,"0") 115 output += u'\x03' + c1 + ',' + c2 + token 116 else: 117 output += u"\x03" + c1 + token 118 119 # select the next color or another color at 120 # random depending on the options specified 121 if not inc: 122 color_local += random.randint(1, color_count - 1) 123 else: 124 color_local += inc 125 output += u'\x0f' 126 127 # output starting with a / will be executed as a 128 # command unless we escape it with a preceding / 129 # Commands should use the -c flag 130 if len(output) > 0 and output[0] == "/": 131 output = "/" + output 132 if len(cmd) > 0: 133 output = cmd + output 134 if input_method == "keybinding": 135 w.buffer_set(buffer, "input", output.encode("UTF-8")) 136 else: 137 w.command(buffer, output.encode("UTF-8")) 138 return w.WEECHAT_RC_OK