Script: aformat.py

Alternate text formatting, useful for relays without formatting features (glowing-bear, weechat-android, ...).
Author: Hairo R. Carela — Version: 0.2 — License: WTFPL
For WeeChat ≥ 0.4.2.
Tags: display, py2, py3
Added: 2017-05-08 — Updated: 2018-06-21

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
 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# -*- coding: utf-8 -*-
#
# Copyright (c) 2016 Hairo R. Carela <hairocr8@gmail.com>
#
# Everyone is permitted to copy and distribute verbatim or modified
# copies of this license document, and changing it is allowed as long
# as the name is changed.
#
# DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
# TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
#
# 0. You just DO WHAT THE FUCK YOU WANT TO.
#
# Alternate way of text formatting, useful for relays without text formatting
# features (Glowingbear, WeechatAndroid, etc)
#
# Usage:
# /aformat *text* for bold text
# /aformat /text/ for italic text
# /aformat _text_ for underlined text
# /aformat |text| for reversed (black on white) text
#
# History:
#   2016-09-24:
#       v0.1: Initial release
#   2018-06-19:
#       v0.2: py3k-ok
#
# TODO:
# - Colors support

import sys

try:
    import weechat
    from weechat import WEECHAT_RC_OK
    import_ok = True
except ImportError:
    print("This script must be run under WeeChat.")
    print("Get WeeChat now at: http://www.weechat.org/")
    import_ok = False

SCRIPT_NAME = "aformat"
SCRIPT_AUTHOR = "Hairo R. Carela <hairocr8@gmail.com>"
SCRIPT_VERSION = "0.2"
SCRIPT_LICENSE = "WTFPL"
SCRIPT_DESC = ("Alternate way of text formatting, see /help for instructions")

PY3 = sys.version > '3'

class format:
    # Special byte sequences, using weechat.color("stuff") had some unwanted
    # results, i'll look into it if needed. Colors are unused for now
   BOLD = '\x02'
   ITALIC = '\x1D'
   UNDERLINE = '\x1F'
   REVERSE = '\x16'
   END = '\x0F'

if PY3:
    unichr = chr
    def send(buf, text):
        weechat.command(buf, "/input send {}".format(text))
else:
    def send(buf, text):
        weechat.command(buf, "/input send {}".format(text.encode("utf-8")))

def cb_aformat_cmd(data, buf, args):
    if not PY3:
        args = args.decode("utf-8")

    # Get the indexes of the separators (*/_|) in the string
    bolds = [i for i, ltr in enumerate(args) if ltr == "*"]
    italics = [i for i, ltr in enumerate(args) if ltr == "/"]
    underlines = [i for i, ltr in enumerate(args) if ltr == "_"]
    reverses = [i for i, ltr in enumerate(args) if ltr == "|"]

    if len(bolds) != 0:
        for i, v in enumerate(bolds):
            if i%2 == 0:
                args = args[:v] + format.BOLD + args[v+1:]
            else:
                args = args[:v] + format.END + args[v+1:]

    if len(italics) != 0:
        for i, v in enumerate(italics):
            if i%2 == 0:
                args = args[:v] + format.ITALIC + args[v+1:]
            else:
                args = args[:v] + format.END + args[v+1:]

    if len(underlines) != 0:
        for i, v in enumerate(underlines):
            if i%2 == 0:
                args = args[:v] + format.UNDERLINE + args[v+1:]
            else:
                args = args[:v] + format.END + args[v+1:]

    if len(reverses) != 0:
        for i, v in enumerate(reverses):
            if i%2 == 0:
                args = args[:v] + format.REVERSE + args[v+1:]
            else:
                args = args[:v] + format.END + args[v+1:]

    send(buf, args)
    return weechat.WEECHAT_RC_OK


if import_ok and __name__ == "__main__":
    weechat.register(SCRIPT_NAME, SCRIPT_AUTHOR, SCRIPT_VERSION,
                     SCRIPT_LICENSE, SCRIPT_DESC, '', '')
    weechat.hook_command("aformat", "Alternate way of text formatting, useful for relays without text formatting features (Glowingbear, WeechatAndroid, etc)",
                         "text <*/_|> text <*/_|> more text",
                         "    *: bold text\n"
                         "    /: italic text\n"
                         "    _: underlined text\n"
                         "    |: reversed (black on white) text\n\n"
                         "    eg.: typing: /aformat This /must/ be the *work* of an _enemy_ |stand|\n"
                         "    will output: This {0}must{4} be the {1}work{4} of an {2}enemy{4} {3}stand{4}".format(weechat.color("italic"), weechat.color("bold"), weechat.color("underline"), weechat.color("reverse"), weechat.color("reset")),
                         "", "cb_aformat_cmd", "")