Script: glitter.py

Replace text wrapped in ***three asterisks*** with a rainbow colored version.
Author: jotham — Version: 0.1 — License: GPL3
For WeeChat ≥ 0.3.0.
Tags: input, py2, py3
Added: 2019-11-09

Download GitHub Repository

 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
import weechat, re

SCRIPT_NAME    = "glitter"
SCRIPT_AUTHOR  = "jotham.read@gmail.com"
SCRIPT_VERSION = "0.1"
SCRIPT_LICENSE = "GPL3"
SCRIPT_DESC    = "Replaces ***text*** you write with rainbow text"

if weechat.register(SCRIPT_NAME, SCRIPT_AUTHOR, SCRIPT_VERSION, SCRIPT_LICENSE, SCRIPT_DESC, "", ""):
   weechat.hook_command_run("/input return", "command_run_input", "")

glitter_pat = re.compile("\*\*\*([^\*]+)\*\*\*")
def glitter_it(match):
   lut = ("13","4","8","9","11","12") # len=6
   text = match.group(1)
   return "".join(["\03"+lut[i%6]+text[i] for i in range(len(text))]) + "\03"

def command_run_input(data, buffer, command):
   if command == "/input return":
      input = weechat.buffer_get_string(buffer, 'input')
      if input.startswith('/set '): # Skip modification of settings
         return weechat.WEECHAT_RC_OK
      input = glitter_pat.sub(glitter_it, input)
      weechat.buffer_set(buffer, 'input', input)
   return weechat.WEECHAT_RC_OK