From 0b2d8156f6f0334862a8ef5691f46b5b5d188d92 Mon Sep 17 00:00:00 2001 From: wowaname Date: Tue, 9 Aug 2016 06:55:47 +0000 Subject: [PATCH] update and rename embellish to colo --- scripts/{embellish.py => colo.py} | 82 ++++++++++++++++--------------- scripts/coloconv.pl | 41 ++++++++++++++++ 2 files changed, 84 insertions(+), 39 deletions(-) rename scripts/{embellish.py => colo.py} (71%) create mode 100755 scripts/coloconv.pl diff --git a/scripts/embellish.py b/scripts/colo.py similarity index 71% rename from scripts/embellish.py rename to scripts/colo.py index ada3f8d..b48df6b 100644 --- a/scripts/embellish.py +++ b/scripts/colo.py @@ -2,8 +2,11 @@ # # Released into the Public Domain -"""embellish: make your chats noticable""" +"""colo: make your chats noticable""" +# v2.0 - wowaname +# format strings instead of pre/suf +# rename # v1.3 - wowaname # changed hook so script doesn't modify the input buffer # v1.2 - wowaname @@ -21,21 +24,19 @@ import random import re import weechat -SCRIPT_NAME = "embellish" +SCRIPT_NAME = "colo" SCRIPT_AUTHOR = "The Krusty Krab " -SCRIPT_VERSION = "1.3" +SCRIPT_VERSION = "2.0" SCRIPT_LICENSE = "Public domain" SCRIPT_DESC = "Makes your chats noticable" # script options settings = { - "pre": ( - "\x0313♥", - "Text to add to the beginning of messages.", - ), - "suf": ( - "\x0313♥", - "Text to add to the end of messages.", + "fmt": ( + "%c13♥ %0%s%o %c13♥", + "Format string for text. %0 - %9 are different colours, %s is text, " + "%c %b %u %r %o are ^C ^B ^U ^R ^O respectively, and %% is a literal " + "percent sign.", ), "fgs": ( "04,05,06,13", @@ -60,7 +61,7 @@ settings = { ), "whitelist_cmds": ( "me,amsg,say", - "Commands to embellish.", + "Commands to colour.", ), "profiles": ( "> greentext,! alert", @@ -69,10 +70,8 @@ settings = { "the options will switch to (profile)_pre, " "(profile)_suf, (profile)_fgs, and (profile)_bgs. ", ), - "greentext_pre": ">", - "greentext_suf": "", - "greentext_fgs": "03", - "greentext_bgs": "", + "greentext_fmt": "%c3> %s", + "alert_fmt": "%c1,8/!\\ %c8,1%s%o %c1,8/!\\" } @@ -85,14 +84,14 @@ if weechat.register(SCRIPT_NAME, SCRIPT_AUTHOR, SCRIPT_VERSION, SCRIPT_LICENSE, if weechat.config_is_set_plugin(opt): continue weechat.config_set_plugin(opt, setting) - weechat.hook_modifier("input_text_for_buffer", "cb_embellish", "") + weechat.hook_modifier("input_text_for_buffer", "cb_colo", "") def glob_match (haystack, needle): return re.search("^%s$" % re.escape(haystack).replace(r"\?", ".").replace(r"\*", ".*?"), needle) -def cb_embellish(data, mod, buf, input): +def cb_colo (data, mod, buf, input): buffer_name = weechat.buffer_get_string(buf, "name").lower() output = "" profile = "" @@ -127,37 +126,42 @@ def cb_embellish(data, mod, buf, input): if not input.startswith("%s " % prefix): continue profile = "%s_" % name input = input.split(" ",1)[1] if " " in input else "" - for opt in ("pre", "suf", "fgs", "bgs"): + for opt in ("fmt", "fgs", "bgs"): if weechat.config_is_set_plugin(profile + opt): continue weechat.config_set_plugin(profile + opt, "") break fgs = weechat.config_get_plugin("%sfgs" % profile).split(",") bgs = weechat.config_get_plugin("%sbgs" % profile).split(",") - base = "\x0f\x03%s%s%s" % ( - random.choice(fgs), - "," if bgs != [""] else "", - random.choice(bgs), - ) if fgs != [""] or bgs != [""] else "" - if base: - input = re.sub( - "\x03([^0-9])", - "\x03%s\\1" % base, - input.replace("\x0f","\x0f%s" % base)) + fmt = weechat.config_get_plugin("%sfmt" % profile).split("%%") + + for i in xrange(len(fmt)): + fmt[i] = fmt[i].replace("%c", "\x03").replace("%b", + "\x02").replace("%u", "\x1f").replace("%r", + "\x16").replace("%o", "\x0f") + if fgs == [""] and bgs == [""]: continue + for j in xrange(10): + base = "\x0f\x03%s%s%s" % ( + random.choice(fgs), + "," if bgs != [""] else "", + random.choice(bgs), + ) + fmt[i] = fmt[i].replace("%%%d" % j, base) + if j: continue + input = re.sub( + "\x03([^0-9])", + "\x03%s\\1" % base, + input.replace("\x0f","\x0f%s" % base)) - #l = (409 - len(base) * 3 - - # weechat.info_get("irc_server_isupport_value", "*,NICKLEN") - l = 400 - len(base) * 3 + fmt = "%".join(fmt) + + l = (409 - len(fmt) - + int( weechat.info_get("irc_server_isupport_value", "%s,NICKLEN" % + weechat.buffer_get_string(buf, "localvar_server")) ) + ) o = [] for line in input.replace("\r", "").split("\n"): for i in xrange(0, len(line), l): - o.append("%s%s%s %s%s %s" % ( - base, - weechat.config_get_plugin("%spre" % profile), - base, - line[i:i+l].rstrip(), - base, - weechat.config_get_plugin("%ssuf" % profile), - )) + o.append(fmt.replace("%s", line[i:i+l].rstrip())) return output + "\n".join(o) diff --git a/scripts/coloconv.pl b/scripts/coloconv.pl new file mode 100755 index 0000000..19556b1 --- /dev/null +++ b/scripts/coloconv.pl @@ -0,0 +1,41 @@ +#!/usr/bin/perl + +=pod + +Reads plugins.conf from stdin, writes new plugins.conf to stdout, and +writes commands to restore the rest of the config options to stderr + +Suggested operation: +cd .weechat +./coloconv.pl < plugins.conf > plugins.conf.new 2> commands +diff plugins.conf plugins.conf.new # to make sure nothing got clobbered + +Then in WeeChat: +/exec -o .weechat/commands + +=cut + +my %profs; + +while (<>) { + $_ !~ /^python\.embellish\./ and print, next; + s/^python\.embellish/python.colo/; + $_ !~ /^python\.colo\..*(?:pre|suf)/ and print, next; + + my ($prof, $k, $v) = /^python\.colo\.(.*)(pre|suf) = "(.*)"$/; + $v =~ s/\x02/%b/g; + $v =~ s/\x03/%c/g; + $v =~ s/\x0f/%o/g; + $v =~ s/\x16/%r/g; + $v =~ s/\x1f/%u/g; + + if ($k eq 'pre') { + $profs{$prof} = "%0$v%o%0 %s%o%0 "; + } elsif ($k eq 'suf') { + $profs{$prof} .= $v; + } +} + +for my $prof (keys %profs) { + print STDERR "/set plugins.var.python.colo.${prof}fmt $profs{$prof}\n"; +}