Script: quick_force_color.py

Quickly add/del/change entry in option irc.look.nick_color_force.
Author: nils_2 — Version: 0.6.1 — License: GPL-3.0-or-later
For WeeChat ≥ 0.3.4.
Tags: irc, nick, color, py2, py3
Added: 2012-02-06 — Updated: 2017-08-27

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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# -*- coding: utf-8 -*-
#
# Copyright (c) 2012-2017 by nils_2 <weechatter@arcor.de>
#
# quickly add/del/change entry in nick_color_force
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
# 2017-08-17: nils_2,(freenode.#weechat)
#      0.6.1: print nicks in sorted order
# 2017-05-18: ticalc-travis (https://github.com/weechatter/weechat-scripts/pull/18)
#       0.6 : Clean up some redundant code
#           : Add nicks to irc.look.nick_color_force in sorted order for easier manual editing
#           : Display proper feedback on incorrect commands
#           : Fix inconsistencies in help syntax
#           : Don't retain nicks that have been manually removed from nick_color_force
#           : Provide feedback messages for successful operations
# 2016-04-17: nils_2,(freenode.#weechat)
#       0.5 : make script compatible with option weechat.look.nick_color_force (weechat >=1.5)
# 2013-01-25: nils_2,(freenode.#weechat)
#       0.4 : make script compatible with Python 3.x
# 2012-07-08: obiwahn, (freenode)
#     0.3.1 : fix: list nick
#           : - show nick: color if it is in list
#           :   else tell color for nick is not set
# 2012-05-23: unferth, (freenode.#weechat)
#       0.3 : add: show current colors
# 2012-02-14: nils_2, (freenode.#weechat)
#       0.2 : fix: problem with foreground/background color
#           : add: show only a given nick
# 2012-02-01: nils_2, (freenode.#weechat)
#       0.1 : initial release
#
# requires: WeeChat version 0.3.4
#
# Development is currently hosted at
# https://github.com/weechatter/weechat-scripts
#

try:
    import weechat, re

except Exception:
    print("This script must be run under WeeChat.")
    print("Get WeeChat now at: http://www.weechat.org/")
    quit()

SCRIPT_NAME     = "quick_force_color"
SCRIPT_AUTHOR   = "nils_2 <weechatter@arcor.de>"
SCRIPT_VERSION  = "0.6.1"
SCRIPT_LICENSE  = "GPL3"
SCRIPT_DESC     = "quickly add/del/change entry in nick_color_force"

# weechat standard colours.
DEFAULT_COLORS = {  0 : "darkgray", 1 : "red", 2 : "lightred", 3 : "green",
                    4 : "lightgreen", 5 : "brown", 6 : "yellow", 7 : "blue",
                    8 : "lightblue", 9 : "magenta", 10 : "lightmagenta", 11 : "cyan",
                   12 : "lightcyan", 13 : "white"}

colored_nicks = {}
nick_option_old = "irc.look.nick_color_force"
nick_option_new = "weechat.look.nick_color_force"
nick_option = ""
# ================================[ callback ]===============================
def print_usage(buffer):
    weechat.prnt(buffer, "Usage: /%s list [nick] | add nick color | del nick" % SCRIPT_NAME)

def nick_colors_cmd_cb(data, buffer, args):
    global colored_nicks

    if args == "":                                                                              # no args given. quit
        print_usage(buffer)
        return weechat.WEECHAT_RC_OK

    argv = args.strip().split(" ")
    if (len(argv) == 0) or (len(argv) >= 4):                                                    # maximum of 3 args!!
        print_usage(buffer)
        return weechat.WEECHAT_RC_OK

    bufpointer = weechat.window_get_pointer(buffer,'buffer')                                    # current buffer

    create_list()

    if argv[0].lower() == 'list':                                                               # list all nicks
        if len(colored_nicks) == 0:
            weechat.prnt(buffer,'%sno nicks in \"%s\"...' % (weechat.prefix("error"),nick_option))
        elif len(argv) == 2:
            if argv[1] in colored_nicks:
                color = colored_nicks[argv[1]]                                                  # get color from given nick
                weechat.prnt(buffer,"%s%s: %s" % (weechat.color(color),argv[1],color))
            else:
                weechat.prnt(buffer,"no color set for: %s" % (argv[1]))

        else:
            weechat.prnt(buffer,"List of nicks in : %s" % nick_option)

            for nick,color in sorted(list(colored_nicks.items())):
                weechat.prnt(buffer,"%s%s: %s" % (weechat.color(color),nick,color))

    elif (argv[0].lower() == 'add') and (len(argv) == 3):
        if argv[1] in colored_nicks:
            weechat.prnt(buffer, "Changing nick '%s' to color %s%s" % (argv[1], weechat.color(argv[2]), argv[2]))
        else:
            weechat.prnt(buffer, "Adding nick '%s' with color %s%s" % (argv[1], weechat.color(argv[2]), argv[2]))
        colored_nicks[argv[1]] = argv[2]
        save_new_force_nicks()

    elif (argv[0].lower() == 'del') and (len(argv) == 2):
        if argv[1] in colored_nicks:                                                            # search if nick exists
            del colored_nicks[argv[1]]
            save_new_force_nicks()
            weechat.prnt(buffer, "Removed nick '%s'" % argv[1])
        else:
            weechat.prnt(buffer, "Nick '%s' not found in nick_color_force" % argv[1])
    else:
        print_usage(buffer)

    return weechat.WEECHAT_RC_OK

def save_new_force_nicks():
    global colored_nicks
    new_nick_color_force = ';'.join([ ':'.join(item) for item in sorted(colored_nicks.items())])
    config_pnt = weechat.config_get(nick_option)
    weechat.config_option_set(config_pnt,new_nick_color_force,1)

def nick_colors_completion_cb(data, completion_item, buffer, completion):
#    for id,color in DEFAULT_COLORS.items():
    for id,color in list(DEFAULT_COLORS.items()):
        weechat.hook_completion_list_add(completion, color, 0, weechat.WEECHAT_LIST_POS_SORT)
    return weechat.WEECHAT_RC_OK

def force_nick_colors_completion_cb(data, completion_item, buffer, completion):
    create_list()
#    for nick,color in colored_nicks.items():
    for nick,color in list(colored_nicks.items()):
        weechat.hook_completion_list_add(completion, nick, 0, weechat.WEECHAT_LIST_POS_SORT)
    return weechat.WEECHAT_RC_OK

def create_list():
    global nick_color_force,colored_nicks
#        colored_nicks = dict([elem.split(':') for elem in nick_color_force.split(';')])
    colored_nicks = {}
    nick_color_force = weechat.config_string(weechat.config_get(nick_option))                   # get list
    if nick_color_force != '':
        nick_color_force = nick_color_force.strip(';')                                          # remove ';' at beginning and end of string
        for elem in nick_color_force.split(';'):                                                # split nick1:color;nick2:color
            counter = elem.count(':')
            if counter == 1:
                nick,colors = elem.split(':')                                                   # nick1:color_fg,color_bg
                colored_nicks.setdefault(nick,colors)
            elif counter == 2:
                nick,color_fg,color_bg = elem.split(':')                                        # nick1:color_fg:color_bg
                colored_nicks.setdefault(nick,color_fg+':'+color_bg)

# ================================[ main ]===============================
if __name__ == "__main__":
    if weechat.register(SCRIPT_NAME, SCRIPT_AUTHOR, SCRIPT_VERSION, SCRIPT_LICENSE, SCRIPT_DESC, '', ''):
        version = weechat.info_get('version_number', '') or 0
        if int(version) >= 0x00030400:
            weechat.hook_command(SCRIPT_NAME,SCRIPT_DESC,
            'add <nick> <color> || del <nick> <color> || list [<nick>]',
            'add <nick> <color>: add a nick with its color to nick_color_force\n'
            'del <nick>        : delete given nick with its color from nick_color_force\n'
            'list [<nick>]     : list all forced nicks with its assigned color or optional from one nick\n\n'
            'Examples:\n'
            ' add nick nils_2 with color red:\n'
            '  /' + SCRIPT_NAME + ' add nils_2 red\n'
            ' recolor nick nils_2 with foreground color yellow and background color blue:\n'
            '  /' + SCRIPT_NAME + ' add nils_2 yellow:blue\n'
            ' delete nick nils_2:\n'
            '  /' + SCRIPT_NAME + ' del nils_2\n',
            'add %(nick) %(plugin_nick_colors) %-||'
            'del %(plugin_force_nick) %-||'
            'list %(plugin_force_nick) %-',
            'nick_colors_cmd_cb', '')
            nick_option = nick_option_old
            if int(version) >= 0x01050000:
                nick_option = nick_option_new
            weechat.hook_completion('plugin_nick_colors', 'nick_colors_completion', 'nick_colors_completion_cb', '')
            weechat.hook_completion('plugin_force_nick', 'force_nick_colors_completion', 'force_nick_colors_completion_cb', '')
        else:
            weechat.prnt("","%s%s %s" % (weechat.prefix("error"),SCRIPT_NAME,": needs version 0.3.4 or higher"))