mirror of
https://github.com/weechat/weechat.git
synced 2026-06-29 22:36:38 +02:00
irc: add option irc.look.nick_color_hash: hash algorithm to find nick color (patch #8062)
This commit is contained in:
@@ -62,6 +62,7 @@ struct t_config_option *irc_config_look_new_pv_position;
|
||||
struct t_config_option *irc_config_look_nick_mode;
|
||||
struct t_config_option *irc_config_look_nick_mode_empty;
|
||||
struct t_config_option *irc_config_look_nick_color_force;
|
||||
struct t_config_option *irc_config_look_nick_color_hash;
|
||||
struct t_config_option *irc_config_look_nick_color_stop_chars;
|
||||
struct t_config_option *irc_config_look_nick_completion_smart;
|
||||
struct t_config_option *irc_config_look_display_away;
|
||||
@@ -564,12 +565,12 @@ irc_config_change_look_nick_color_force (void *data,
|
||||
}
|
||||
|
||||
/*
|
||||
* Callback for changes on option "irc.look.nick_color_stop_chars".
|
||||
* Callback for changes on options that change nick colors.
|
||||
*/
|
||||
|
||||
void
|
||||
irc_config_change_look_nick_color_stop_chars (void *data,
|
||||
struct t_config_option *option)
|
||||
irc_config_change_look_nick_colors (void *data,
|
||||
struct t_config_option *option)
|
||||
{
|
||||
/* make C compiler happy */
|
||||
(void) data;
|
||||
@@ -2231,6 +2232,14 @@ irc_config_init ()
|
||||
"case for nicks in this option"),
|
||||
NULL, 0, 0, "", NULL, 0, NULL, NULL,
|
||||
&irc_config_change_look_nick_color_force, NULL, NULL, NULL);
|
||||
irc_config_look_nick_color_hash = weechat_config_new_option (
|
||||
irc_config_file, ptr_section,
|
||||
"nick_color_hash", "integer",
|
||||
N_("hash algorithm used to find the color for a nick: djb2 = variant of "
|
||||
"djb2 (position of letters matters: anagrams of a nick have different "
|
||||
"color), sum = sum of letters"),
|
||||
"djb2|sum", 0, 0, "sum", NULL, 0, NULL, NULL,
|
||||
&irc_config_change_look_nick_colors, NULL, NULL, NULL);
|
||||
irc_config_look_nick_color_stop_chars = weechat_config_new_option (
|
||||
irc_config_file, ptr_section,
|
||||
"nick_color_stop_chars", "string",
|
||||
@@ -2239,7 +2248,7 @@ irc_config_init ()
|
||||
"stopping) (example: nick \"|nick|away\" with \"|\" in chars will "
|
||||
"return color of nick \"|nick\")"),
|
||||
NULL, 0, 0, "_|[", NULL, 0, NULL, NULL,
|
||||
&irc_config_change_look_nick_color_stop_chars, NULL, NULL, NULL);
|
||||
&irc_config_change_look_nick_colors, NULL, NULL, NULL);
|
||||
irc_config_look_nick_completion_smart = weechat_config_new_option (
|
||||
irc_config_file, ptr_section,
|
||||
"nick_completion_smart", "integer",
|
||||
|
||||
@@ -62,6 +62,12 @@ enum t_irc_config_look_notice_as_pv
|
||||
IRC_CONFIG_LOOK_NOTICE_AS_PV_ALWAYS,
|
||||
};
|
||||
|
||||
enum t_irc_config_look_nick_color_hash
|
||||
{
|
||||
IRC_CONFIG_LOOK_NICK_COLOR_HASH_DJB2 = 0,
|
||||
IRC_CONFIG_LOOK_NICK_COLOR_HASH_SUM,
|
||||
};
|
||||
|
||||
enum t_irc_config_look_nick_mode
|
||||
{
|
||||
IRC_CONFIG_LOOK_NICK_MODE_NONE = 0,
|
||||
@@ -104,6 +110,7 @@ extern struct t_config_option *irc_config_look_new_pv_position;
|
||||
extern struct t_config_option *irc_config_look_nick_mode;
|
||||
extern struct t_config_option *irc_config_look_nick_mode_empty;
|
||||
extern struct t_config_option *irc_config_look_nick_color_force;
|
||||
extern struct t_config_option *irc_config_look_nick_color_hash;
|
||||
extern struct t_config_option *irc_config_look_nick_color_stop_chars;
|
||||
extern struct t_config_option *irc_config_look_nick_completion_smart;
|
||||
extern struct t_config_option *irc_config_look_display_away;
|
||||
|
||||
@@ -144,7 +144,7 @@ irc_nick_strdup_for_color (const char *nickname)
|
||||
int
|
||||
irc_nick_hash_color (const char *nickname)
|
||||
{
|
||||
int color;
|
||||
unsigned long color;
|
||||
const char *ptr_nick;
|
||||
|
||||
if (!irc_config_nick_colors)
|
||||
@@ -153,12 +153,28 @@ irc_nick_hash_color (const char *nickname)
|
||||
if (irc_config_num_nick_colors == 0)
|
||||
return 0;
|
||||
|
||||
color = 0;
|
||||
ptr_nick = nickname;
|
||||
while (ptr_nick && ptr_nick[0])
|
||||
|
||||
switch (weechat_config_integer (irc_config_look_nick_color_hash))
|
||||
{
|
||||
color += weechat_utf8_char_int (ptr_nick);
|
||||
ptr_nick = weechat_utf8_next_char (ptr_nick);
|
||||
case IRC_CONFIG_LOOK_NICK_COLOR_HASH_DJB2:
|
||||
/* variant of djb2 hash */
|
||||
color = 5381;
|
||||
while (ptr_nick && ptr_nick[0])
|
||||
{
|
||||
color ^= (color << 5) + (color >> 2) + weechat_utf8_char_int (ptr_nick);
|
||||
ptr_nick = weechat_utf8_next_char (ptr_nick);
|
||||
}
|
||||
break;
|
||||
case IRC_CONFIG_LOOK_NICK_COLOR_HASH_SUM:
|
||||
/* sum of letters */
|
||||
color = 0;
|
||||
while (ptr_nick && ptr_nick[0])
|
||||
{
|
||||
color += weechat_utf8_char_int (ptr_nick);
|
||||
ptr_nick = weechat_utf8_next_char (ptr_nick);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
return (color % irc_config_num_nick_colors);
|
||||
|
||||
Reference in New Issue
Block a user