mirror of
https://github.com/weechat/weechat.git
synced 2026-07-07 18:23:13 +02:00
api: add function string_color_code_size (issue #1547)
This commit is contained in:
@@ -562,6 +562,179 @@ gui_color_convert_rgb_to_term (int rgb, int limit)
|
||||
return best_color;
|
||||
}
|
||||
|
||||
/*
|
||||
* Returns the size (in bytes) of the WeeChat color code at the beginning
|
||||
* of "string".
|
||||
*
|
||||
* If "string" is NULL, empty or does not start with a color code,
|
||||
* it returns 0.
|
||||
*
|
||||
* If "string" begins with multiple color codes, only the size of the first
|
||||
* one is returned.
|
||||
*/
|
||||
|
||||
int
|
||||
gui_color_code_size (const char *string)
|
||||
{
|
||||
const char *ptr_string;
|
||||
|
||||
if (!string)
|
||||
return 0;
|
||||
|
||||
ptr_string = string;
|
||||
|
||||
switch (ptr_string[0])
|
||||
{
|
||||
case GUI_COLOR_COLOR_CHAR:
|
||||
ptr_string++;
|
||||
switch (ptr_string[0])
|
||||
{
|
||||
case GUI_COLOR_FG_CHAR:
|
||||
ptr_string++;
|
||||
if (ptr_string[0] == GUI_COLOR_EXTENDED_CHAR)
|
||||
{
|
||||
ptr_string++;
|
||||
while (gui_color_attr_get_flag (ptr_string[0]) > 0)
|
||||
{
|
||||
ptr_string++;
|
||||
}
|
||||
if (ptr_string[0] && ptr_string[1] && ptr_string[2]
|
||||
&& ptr_string[3] && ptr_string[4])
|
||||
{
|
||||
ptr_string += 5;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
while (gui_color_attr_get_flag (ptr_string[0]) > 0)
|
||||
{
|
||||
ptr_string++;
|
||||
}
|
||||
if (ptr_string[0] && ptr_string[1])
|
||||
ptr_string += 2;
|
||||
}
|
||||
break;
|
||||
case GUI_COLOR_BG_CHAR:
|
||||
ptr_string++;
|
||||
if (ptr_string[0] == GUI_COLOR_EXTENDED_CHAR)
|
||||
{
|
||||
ptr_string++;
|
||||
if (ptr_string[0] && ptr_string[1] && ptr_string[2]
|
||||
&& ptr_string[3] && ptr_string[4])
|
||||
{
|
||||
ptr_string += 5;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (ptr_string[0] && ptr_string[1])
|
||||
ptr_string += 2;
|
||||
}
|
||||
break;
|
||||
case GUI_COLOR_FG_BG_CHAR:
|
||||
ptr_string++;
|
||||
if (ptr_string[0] == GUI_COLOR_EXTENDED_CHAR)
|
||||
{
|
||||
ptr_string++;
|
||||
while (gui_color_attr_get_flag (ptr_string[0]) > 0)
|
||||
{
|
||||
ptr_string++;
|
||||
}
|
||||
if (ptr_string[0] && ptr_string[1] && ptr_string[2]
|
||||
&& ptr_string[3] && ptr_string[4])
|
||||
{
|
||||
ptr_string += 5;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
while (gui_color_attr_get_flag (ptr_string[0]) > 0)
|
||||
{
|
||||
ptr_string++;
|
||||
}
|
||||
if (ptr_string[0] && ptr_string[1])
|
||||
ptr_string += 2;
|
||||
}
|
||||
/*
|
||||
* note: the comma is an old separator not used any
|
||||
* more (since WeeChat 2.6), but we still use it here
|
||||
* so in case of/upgrade this will not break colors in
|
||||
* old messages
|
||||
*/
|
||||
if ((ptr_string[0] == ',') || (ptr_string[0] == '~'))
|
||||
{
|
||||
if (ptr_string[1] == GUI_COLOR_EXTENDED_CHAR)
|
||||
{
|
||||
ptr_string += 2;
|
||||
if (ptr_string[0] && ptr_string[1]
|
||||
&& ptr_string[2] && ptr_string[3]
|
||||
&& ptr_string[4])
|
||||
{
|
||||
ptr_string += 5;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
ptr_string++;
|
||||
if (ptr_string[0] && ptr_string[1])
|
||||
ptr_string += 2;
|
||||
}
|
||||
}
|
||||
break;
|
||||
case GUI_COLOR_EXTENDED_CHAR:
|
||||
ptr_string++;
|
||||
if ((isdigit (ptr_string[0])) && (isdigit (ptr_string[1]))
|
||||
&& (isdigit (ptr_string[2])) && (isdigit (ptr_string[3]))
|
||||
&& (isdigit (ptr_string[4])))
|
||||
{
|
||||
ptr_string += 5;
|
||||
}
|
||||
break;
|
||||
case GUI_COLOR_EMPHASIS_CHAR:
|
||||
ptr_string++;
|
||||
break;
|
||||
case GUI_COLOR_BAR_CHAR:
|
||||
ptr_string++;
|
||||
switch (ptr_string[0])
|
||||
{
|
||||
case GUI_COLOR_BAR_FG_CHAR:
|
||||
case GUI_COLOR_BAR_BG_CHAR:
|
||||
case GUI_COLOR_BAR_DELIM_CHAR:
|
||||
case GUI_COLOR_BAR_START_INPUT_CHAR:
|
||||
case GUI_COLOR_BAR_START_INPUT_HIDDEN_CHAR:
|
||||
case GUI_COLOR_BAR_MOVE_CURSOR_CHAR:
|
||||
case GUI_COLOR_BAR_START_ITEM:
|
||||
case GUI_COLOR_BAR_START_LINE_ITEM:
|
||||
ptr_string++;
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case GUI_COLOR_RESET_CHAR:
|
||||
ptr_string++;
|
||||
break;
|
||||
default:
|
||||
if (isdigit (ptr_string[0]) && isdigit (ptr_string[1]))
|
||||
ptr_string += 2;
|
||||
break;
|
||||
}
|
||||
return ptr_string - string;
|
||||
break;
|
||||
case GUI_COLOR_SET_ATTR_CHAR:
|
||||
case GUI_COLOR_REMOVE_ATTR_CHAR:
|
||||
ptr_string++;
|
||||
if (ptr_string[0])
|
||||
ptr_string++;
|
||||
return ptr_string - string;
|
||||
break;
|
||||
case GUI_COLOR_RESET_CHAR:
|
||||
ptr_string++;
|
||||
return ptr_string - string;
|
||||
break;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* Removes WeeChat color codes from a message.
|
||||
*
|
||||
|
||||
@@ -186,6 +186,7 @@ extern void gui_color_attr_build_string (int color, char *str_attr);
|
||||
extern const char *gui_color_get_custom (const char *color_name);
|
||||
extern int gui_color_convert_term_to_rgb (int color);
|
||||
extern int gui_color_convert_rgb_to_term (int rgb, int limit);
|
||||
extern int gui_color_code_size (const char *string);
|
||||
extern char *gui_color_decode (const char *string, const char *replacement);
|
||||
extern char *gui_color_decode_ansi (const char *string, int keep_colors);
|
||||
extern char *gui_color_encode_ansi (const char *string);
|
||||
|
||||
@@ -428,6 +428,20 @@ weechat_guile_api_string_format_size (SCM size)
|
||||
API_RETURN_STRING_FREE(result);
|
||||
}
|
||||
|
||||
SCM
|
||||
weechat_guile_api_string_color_code_size (SCM string)
|
||||
{
|
||||
int size;
|
||||
|
||||
API_INIT_FUNC(1, "string_color_code_size", API_RETURN_INT(0));
|
||||
if (!scm_is_string (string))
|
||||
API_WRONG_ARGS(API_RETURN_INT(0));
|
||||
|
||||
size = weechat_string_color_code_size (API_SCM_TO_STRING(string));
|
||||
|
||||
API_RETURN_INT(size);
|
||||
}
|
||||
|
||||
SCM
|
||||
weechat_guile_api_string_remove_color (SCM string, SCM replacement)
|
||||
{
|
||||
@@ -5012,6 +5026,7 @@ weechat_guile_api_module_init (void *data)
|
||||
API_DEF_FUNC(string_has_highlight_regex, 2);
|
||||
API_DEF_FUNC(string_mask_to_regex, 1);
|
||||
API_DEF_FUNC(string_format_size, 1);
|
||||
API_DEF_FUNC(string_color_code_size, 1);
|
||||
API_DEF_FUNC(string_remove_color, 2);
|
||||
API_DEF_FUNC(string_is_command_char, 1);
|
||||
API_DEF_FUNC(string_input_for_buffer, 1);
|
||||
|
||||
@@ -376,6 +376,19 @@ API_FUNC(string_format_size)
|
||||
API_RETURN_STRING_FREE(result);
|
||||
}
|
||||
|
||||
API_FUNC(string_color_code_size)
|
||||
{
|
||||
int size;
|
||||
|
||||
API_INIT_FUNC(1, "string_color_code_size", "s", API_RETURN_INT(0));
|
||||
|
||||
v8::String::Utf8Value string(args[0]);
|
||||
|
||||
size = weechat_string_color_code_size (*string);
|
||||
|
||||
API_RETURN_INT(size);
|
||||
}
|
||||
|
||||
API_FUNC(string_remove_color)
|
||||
{
|
||||
char *result;
|
||||
@@ -4945,6 +4958,7 @@ WeechatJsV8::loadLibs()
|
||||
API_DEF_FUNC(string_has_highlight_regex);
|
||||
API_DEF_FUNC(string_mask_to_regex);
|
||||
API_DEF_FUNC(string_format_size);
|
||||
API_DEF_FUNC(string_color_code_size);
|
||||
API_DEF_FUNC(string_remove_color);
|
||||
API_DEF_FUNC(string_is_command_char);
|
||||
API_DEF_FUNC(string_input_for_buffer);
|
||||
|
||||
@@ -423,6 +423,22 @@ API_FUNC(string_format_size)
|
||||
API_RETURN_STRING_FREE(result);
|
||||
}
|
||||
|
||||
API_FUNC(string_color_code_size)
|
||||
{
|
||||
const char *string;
|
||||
int size;
|
||||
|
||||
API_INIT_FUNC(1, "string_color_code_size", API_RETURN_INT(0));
|
||||
if (lua_gettop (L) < 1)
|
||||
API_WRONG_ARGS(API_RETURN_INT(0));
|
||||
|
||||
string = lua_tostring (L, -1);
|
||||
|
||||
size = weechat_string_color_code_size (string);
|
||||
|
||||
API_RETURN_INT(size);
|
||||
}
|
||||
|
||||
API_FUNC(string_remove_color)
|
||||
{
|
||||
const char *string, *replacement;
|
||||
@@ -5309,6 +5325,7 @@ const struct luaL_Reg weechat_lua_api_funcs[] = {
|
||||
API_DEF_FUNC(string_has_highlight_regex),
|
||||
API_DEF_FUNC(string_mask_to_regex),
|
||||
API_DEF_FUNC(string_format_size),
|
||||
API_DEF_FUNC(string_color_code_size),
|
||||
API_DEF_FUNC(string_remove_color),
|
||||
API_DEF_FUNC(string_is_command_char),
|
||||
API_DEF_FUNC(string_input_for_buffer),
|
||||
|
||||
@@ -383,6 +383,20 @@ API_FUNC(string_format_size)
|
||||
API_RETURN_STRING_FREE(result);
|
||||
}
|
||||
|
||||
API_FUNC(string_color_code_size)
|
||||
{
|
||||
int size;
|
||||
dXSARGS;
|
||||
|
||||
API_INIT_FUNC(1, "string_color_code_size", API_RETURN_INT(0));
|
||||
if (items < 1)
|
||||
API_WRONG_ARGS(API_RETURN_INT(0));
|
||||
|
||||
size = weechat_string_color_code_size (SvPV_nolen (ST (0))); /* string */
|
||||
|
||||
API_RETURN_INT(size);
|
||||
}
|
||||
|
||||
API_FUNC(string_remove_color)
|
||||
{
|
||||
char *result, *string, *replacement;
|
||||
@@ -5266,6 +5280,7 @@ weechat_perl_api_init (pTHX)
|
||||
API_DEF_FUNC(string_has_highlight_regex);
|
||||
API_DEF_FUNC(string_mask_to_regex);
|
||||
API_DEF_FUNC(string_format_size);
|
||||
API_DEF_FUNC(string_color_code_size);
|
||||
API_DEF_FUNC(string_remove_color);
|
||||
API_DEF_FUNC(string_is_command_char);
|
||||
API_DEF_FUNC(string_input_for_buffer);
|
||||
|
||||
@@ -497,6 +497,23 @@ API_FUNC(string_format_size)
|
||||
API_RETURN_STRING_FREE(result);
|
||||
}
|
||||
|
||||
API_FUNC(string_color_code_size)
|
||||
{
|
||||
zend_string *z_string;
|
||||
char *string;
|
||||
int result;
|
||||
|
||||
API_INIT_FUNC(1, "string_color_code_size", API_RETURN_INT(0));
|
||||
if (zend_parse_parameters (ZEND_NUM_ARGS(), "S", &z_string) == FAILURE)
|
||||
API_WRONG_ARGS(API_RETURN_INT(0));
|
||||
|
||||
string = ZSTR_VAL(z_string);
|
||||
|
||||
result = weechat_string_color_code_size ((const char *)string);
|
||||
|
||||
API_RETURN_INT(result);
|
||||
}
|
||||
|
||||
API_FUNC(string_remove_color)
|
||||
{
|
||||
zend_string *z_string, *z_replacement;
|
||||
|
||||
@@ -59,6 +59,7 @@ PHP_FUNCTION(weechat_string_has_highlight);
|
||||
PHP_FUNCTION(weechat_string_has_highlight_regex);
|
||||
PHP_FUNCTION(weechat_string_mask_to_regex);
|
||||
PHP_FUNCTION(weechat_string_format_size);
|
||||
PHP_FUNCTION(weechat_string_color_code_size);
|
||||
PHP_FUNCTION(weechat_string_remove_color);
|
||||
PHP_FUNCTION(weechat_string_is_command_char);
|
||||
PHP_FUNCTION(weechat_string_input_for_buffer);
|
||||
|
||||
@@ -112,6 +112,7 @@ const zend_function_entry weechat_functions[] = {
|
||||
PHP_FE(weechat_string_has_highlight_regex, NULL)
|
||||
PHP_FE(weechat_string_mask_to_regex, NULL)
|
||||
PHP_FE(weechat_string_format_size, NULL)
|
||||
PHP_FE(weechat_string_color_code_size, NULL)
|
||||
PHP_FE(weechat_string_remove_color, NULL)
|
||||
PHP_FE(weechat_string_is_command_char, NULL)
|
||||
PHP_FE(weechat_string_input_for_buffer, NULL)
|
||||
|
||||
@@ -632,6 +632,7 @@ plugin_load (const char *filename, int init_plugin, int argc, char **argv)
|
||||
new_plugin->string_split_command = &string_split_command;
|
||||
new_plugin->string_free_split_command = &string_free_split_command;
|
||||
new_plugin->string_format_size = &string_format_size;
|
||||
new_plugin->string_color_code_size = &gui_color_code_size;
|
||||
new_plugin->string_remove_color = &gui_color_decode;
|
||||
new_plugin->string_base_encode = &string_base_encode;
|
||||
new_plugin->string_base_decode = &string_base_decode;
|
||||
|
||||
@@ -369,6 +369,21 @@ API_FUNC(string_format_size)
|
||||
API_RETURN_STRING_FREE(result);
|
||||
}
|
||||
|
||||
API_FUNC(string_color_code_size)
|
||||
{
|
||||
char *string;
|
||||
int size;
|
||||
|
||||
API_INIT_FUNC(1, "string_color_code_size", API_RETURN_INT(0));
|
||||
string = NULL;
|
||||
if (!PyArg_ParseTuple (args, "s", &string))
|
||||
API_WRONG_ARGS(API_RETURN_INT(0));
|
||||
|
||||
size = weechat_string_color_code_size (string);
|
||||
|
||||
API_RETURN_INT(size);
|
||||
}
|
||||
|
||||
API_FUNC(string_remove_color)
|
||||
{
|
||||
char *string, *replacement, *result;
|
||||
@@ -5217,6 +5232,7 @@ PyMethodDef weechat_python_funcs[] =
|
||||
API_DEF_FUNC(string_has_highlight_regex),
|
||||
API_DEF_FUNC(string_mask_to_regex),
|
||||
API_DEF_FUNC(string_format_size),
|
||||
API_DEF_FUNC(string_color_code_size),
|
||||
API_DEF_FUNC(string_remove_color),
|
||||
API_DEF_FUNC(string_is_command_char),
|
||||
API_DEF_FUNC(string_input_for_buffer),
|
||||
|
||||
@@ -448,6 +448,25 @@ weechat_ruby_api_string_format_size (VALUE class, VALUE size)
|
||||
API_RETURN_STRING_FREE(result);
|
||||
}
|
||||
|
||||
static VALUE
|
||||
weechat_ruby_api_string_color_code_size (VALUE class, VALUE string)
|
||||
{
|
||||
char *c_string;
|
||||
int size;
|
||||
|
||||
API_INIT_FUNC(1, "string_color_code_size", API_RETURN_INT(0));
|
||||
if (NIL_P (string))
|
||||
API_WRONG_ARGS(API_RETURN_INT(0));
|
||||
|
||||
Check_Type (string, T_STRING);
|
||||
|
||||
c_string = StringValuePtr (string);
|
||||
|
||||
size = weechat_string_color_code_size (c_string);
|
||||
|
||||
API_RETURN_INT(size);
|
||||
}
|
||||
|
||||
static VALUE
|
||||
weechat_ruby_api_string_remove_color (VALUE class, VALUE string,
|
||||
VALUE replacement)
|
||||
@@ -6424,6 +6443,7 @@ weechat_ruby_api_init (VALUE ruby_mWeechat)
|
||||
API_DEF_FUNC(string_has_highlight_regex, 2);
|
||||
API_DEF_FUNC(string_mask_to_regex, 1);
|
||||
API_DEF_FUNC(string_format_size, 1);
|
||||
API_DEF_FUNC(string_color_code_size, 1);
|
||||
API_DEF_FUNC(string_remove_color, 2);
|
||||
API_DEF_FUNC(string_is_command_char, 1);
|
||||
API_DEF_FUNC(string_input_for_buffer, 1);
|
||||
|
||||
@@ -530,6 +530,20 @@ API_FUNC(string_format_size)
|
||||
API_RETURN_STRING_FREE(result);
|
||||
}
|
||||
|
||||
API_FUNC(string_color_code_size)
|
||||
{
|
||||
Tcl_Obj *objp;
|
||||
int result, i;
|
||||
|
||||
API_INIT_FUNC(1, "string_color_code_size", API_RETURN_INT(0));
|
||||
if (objc < 2)
|
||||
API_WRONG_ARGS(API_RETURN_INT(0));
|
||||
|
||||
result = weechat_string_color_code_size (Tcl_GetStringFromObj (objv[1], &i)); /* string */
|
||||
|
||||
API_RETURN_INT(result);
|
||||
}
|
||||
|
||||
API_FUNC(string_remove_color)
|
||||
{
|
||||
Tcl_Obj *objp;
|
||||
@@ -5739,6 +5753,7 @@ void weechat_tcl_api_init (Tcl_Interp *interp)
|
||||
API_DEF_FUNC(string_has_highlight_regex);
|
||||
API_DEF_FUNC(string_mask_to_regex);
|
||||
API_DEF_FUNC(string_format_size);
|
||||
API_DEF_FUNC(string_color_code_size);
|
||||
API_DEF_FUNC(string_remove_color);
|
||||
API_DEF_FUNC(string_is_command_char);
|
||||
API_DEF_FUNC(string_input_for_buffer);
|
||||
|
||||
@@ -67,7 +67,7 @@ struct timeval;
|
||||
* please change the date with current one; for a second change at same
|
||||
* date, increment the 01, otherwise please keep 01.
|
||||
*/
|
||||
#define WEECHAT_PLUGIN_API_VERSION "20200621-01"
|
||||
#define WEECHAT_PLUGIN_API_VERSION "20200822-01"
|
||||
|
||||
/* macros for defining plugin infos */
|
||||
#define WEECHAT_PLUGIN_NAME(__name) \
|
||||
@@ -334,6 +334,7 @@ struct t_weechat_plugin
|
||||
char **(*string_split_command) (const char *command, char separator);
|
||||
void (*string_free_split_command) (char **split_command);
|
||||
char *(*string_format_size) (unsigned long long size);
|
||||
int (*string_color_code_size) (const char *string);
|
||||
char *(*string_remove_color) (const char *string, const char *replacement);
|
||||
int (*string_base_encode) (int base, const char *from, int length,
|
||||
char *to);
|
||||
@@ -1265,6 +1266,8 @@ extern int weechat_plugin_end (struct t_weechat_plugin *plugin);
|
||||
(weechat_plugin->string_free_split_command)(__split_command)
|
||||
#define weechat_string_format_size(__size) \
|
||||
(weechat_plugin->string_format_size)(__size)
|
||||
#define weechat_string_color_code_size(__string) \
|
||||
(weechat_plugin->string_color_code_size)(__string)
|
||||
#define weechat_string_remove_color(__string, __replacement) \
|
||||
(weechat_plugin->string_remove_color)(__string, __replacement)
|
||||
#define weechat_string_base_encode(__base, __from, __length, __to) \
|
||||
|
||||
Reference in New Issue
Block a user