Commit 9ba77193 authored by Nils Görs's avatar Nils Görs Committed by Sébastien Helleu

quick_force_color.py 0.6: miscellaneous fixes and usability improvements

parent e3b2d403
# -*- coding: utf-8 -*-
#
# Copyright (c) 2012-2016 by nils_2 <weechatter@arcor.de>
# Copyright (c) 2012-2017 by nils_2 <weechatter@arcor.de>
#
# quickly add/del/change entry in nick_color_force
#
......@@ -17,8 +17,15 @@
# 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-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)
# 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)
......@@ -49,7 +56,7 @@ except Exception:
SCRIPT_NAME = "quick_force_color"
SCRIPT_AUTHOR = "nils_2 <weechatter@arcor.de>"
SCRIPT_VERSION = "0.5"
SCRIPT_VERSION = "0.6"
SCRIPT_LICENSE = "GPL3"
SCRIPT_DESC = "quickly add/del/change entry in nick_color_force"
......@@ -64,14 +71,19 @@ 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
......@@ -81,39 +93,41 @@ def nick_colors_cmd_cb(data, buffer, args):
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))
return weechat.WEECHAT_RC_OK
if len(argv) == 2:
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]))
return weechat.WEECHAT_RC_OK
weechat.prnt(buffer,"List of nicks in : %s" % nick_option)
# for nick,color in colored_nicks.items():
for nick,color in list(colored_nicks.items()):
weechat.prnt(buffer,"%s%s: %s" % (weechat.color(color),nick,color))
return weechat.WEECHAT_RC_OK
else:
weechat.prnt(buffer,"List of nicks in : %s" % nick_option)
for nick,color in list(colored_nicks.items()):
weechat.prnt(buffer,"%s%s: %s" % (weechat.color(color),nick,color))
if (argv[0].lower() == 'add') and (len(argv) == 3):
if argv[1] in colored_nicks: # search if nick exists
colored_nicks[argv[1]] = argv[2]
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:
colored_nicks[argv[1]] = argv[2] # add [nick] = [color]
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()
if (argv[0].lower() == 'del') and (len(argv) == 2):
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 colored_nicks.items()])
new_nick_color_force = ';'.join([ ':'.join(item) for item in list(colored_nicks.items())])
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)
......@@ -133,7 +147,8 @@ def force_nick_colors_completion_cb(data, completion_item, buffer, completion):
def create_list():
global nick_color_force,colored_nicks
# colored_nicks = dict([elem.split(':') for elem in nick_color_force.split(';')])
nick_color_force = weechat.config_string(weechat.config_get(nick_option)) # get list
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
......@@ -151,10 +166,10 @@ if __name__ == "__main__":
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',
'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'
'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'
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment