mirror of
https://github.com/weechat/weechat.git
synced 2026-06-29 14:26:39 +02:00
core: add a way to count the suffix length in max chars displayed in cut of string ("cut:" and "cutscr:") (closes #963)
The format to use is one of:
- ${cut:+max,suffix,string}
- ${cutscr:+max,suffix,string}
With the "+" before max, WeeChat ensures there are at most "max" chars in
output, including the length of suffix string.
This commit is contained in:
@@ -7269,9 +7269,11 @@ command_init ()
|
||||
" 1. an evaluated sub-string (format: \"eval:xxx\")\n"
|
||||
" 2. a string with escaped chars (format: \"esc:xxx\" or \"\\xxx\")\n"
|
||||
" 3. a string with chars to hide (format: \"hide:char,string\")\n"
|
||||
" 4. a string with max chars (format: \"cut:max,suffix,string\")\n"
|
||||
" 4. a string with max chars (format: \"cut:max,suffix,string\" "
|
||||
"or \"cut:+max,suffix,string\")\n"
|
||||
" or max chars displayed on screen "
|
||||
"(format: \"cutscr:max,suffix,string\")\n"
|
||||
"(format: \"cutscr:max,suffix,string\" or "
|
||||
"\"cutscr:+max,suffix,string\")\n"
|
||||
" 5. a color (format: \"color:xxx\", see \"Plugin API "
|
||||
"reference\", function \"color\")\n"
|
||||
" 6. an info (format: \"info:name,arguments\", arguments are "
|
||||
@@ -7311,7 +7313,8 @@ command_init ()
|
||||
" /eval -n ${window.buffer.number} ==> 1\n"
|
||||
" /eval -n ${\\t} ==> <tab>\n"
|
||||
" /eval -n ${hide:-,${relay.network.password}} ==> --------\n"
|
||||
" /eval -n ${cut:2,+,test} ==> te+\n"
|
||||
" /eval -n ${cut:3,+,test} ==> tes+\n"
|
||||
" /eval -n ${cut:+3,+,test} ==> te+\n"
|
||||
" /eval -n ${date:%H:%M:%S} ==> 07:46:40\n"
|
||||
" /eval -n ${if:${info:term_width}>80?big:small} ==> big\n"
|
||||
"\n"
|
||||
|
||||
+8
-1
@@ -326,6 +326,7 @@ eval_replace_vars_cb (void *data, const char *text)
|
||||
struct t_hdata *hdata;
|
||||
void *pointer;
|
||||
int i, length_hide_char, length, index, rc, extra_vars_eval, screen;
|
||||
int count_suffix;
|
||||
long number;
|
||||
long unsigned int ptr;
|
||||
time_t date;
|
||||
@@ -436,6 +437,12 @@ eval_replace_vars_cb (void *data, const char *text)
|
||||
pos = strchr (text + length, ',');
|
||||
if (!pos)
|
||||
return strdup ("");
|
||||
count_suffix = 0;
|
||||
if (text[length] == '+')
|
||||
{
|
||||
length++;
|
||||
count_suffix = 1;
|
||||
}
|
||||
pos2 = strchr (pos + 1, ',');
|
||||
if (!pos2)
|
||||
return strdup ("");
|
||||
@@ -452,7 +459,7 @@ eval_replace_vars_cb (void *data, const char *text)
|
||||
tmp = strndup (pos + 1, pos2 - pos - 1);
|
||||
if (!tmp)
|
||||
return strdup ("");
|
||||
value = string_cut (pos2 + 1, number, screen, tmp);
|
||||
value = string_cut (pos2 + 1, number, count_suffix, screen, tmp);
|
||||
free (tmp);
|
||||
return value;
|
||||
}
|
||||
|
||||
+23
-2
@@ -96,13 +96,16 @@ string_strndup (const char *string, int length)
|
||||
* Cuts a string after max "length" chars, adds an optional suffix
|
||||
* after the string if it is cut.
|
||||
*
|
||||
* If count_suffix == 1, the length of suffix is counted in the max length.
|
||||
*
|
||||
* If screen == 1, the cut is based on width of chars displayed.
|
||||
*
|
||||
* Note: result must be freed after use.
|
||||
*/
|
||||
|
||||
char *
|
||||
string_cut (const char *string, int length, int screen, const char *cut_suffix)
|
||||
string_cut (const char *string, int length, int count_suffix, int screen,
|
||||
const char *cut_suffix)
|
||||
{
|
||||
int length_result, length_cut_suffix;
|
||||
char *result;
|
||||
@@ -113,7 +116,7 @@ string_cut (const char *string, int length, int screen, const char *cut_suffix)
|
||||
else
|
||||
ptr_string = gui_chat_string_add_offset (string, length);
|
||||
|
||||
if (!ptr_string[0])
|
||||
if (!ptr_string || !ptr_string[0])
|
||||
{
|
||||
/* no cut */
|
||||
return strdup (string);
|
||||
@@ -122,6 +125,24 @@ string_cut (const char *string, int length, int screen, const char *cut_suffix)
|
||||
if (cut_suffix && cut_suffix[0])
|
||||
{
|
||||
length_cut_suffix = strlen (cut_suffix);
|
||||
if (count_suffix)
|
||||
{
|
||||
if (screen)
|
||||
length -= utf8_strlen_screen (cut_suffix);
|
||||
else
|
||||
length -= length_cut_suffix;
|
||||
if (length < 0)
|
||||
return strdup ("");
|
||||
if (screen)
|
||||
ptr_string = gui_chat_string_add_offset_screen (string, length);
|
||||
else
|
||||
ptr_string = gui_chat_string_add_offset (string, length);
|
||||
if (!ptr_string || !ptr_string[0])
|
||||
{
|
||||
/* no cut */
|
||||
return strdup (string);
|
||||
}
|
||||
}
|
||||
length_result = (ptr_string - string) + length_cut_suffix + 1;
|
||||
result = malloc (length_result);
|
||||
if (!result)
|
||||
|
||||
@@ -36,8 +36,8 @@ struct t_string_dyn
|
||||
struct t_hashtable;
|
||||
|
||||
extern char *string_strndup (const char *string, int length);
|
||||
extern char *string_cut (const char *string, int length, int screen,
|
||||
const char *cut_suffix);
|
||||
extern char *string_cut (const char *string, int length, int count_suffix,
|
||||
int screen, const char *cut_suffix);
|
||||
extern void string_tolower (char *string);
|
||||
extern void string_toupper (char *string);
|
||||
extern int string_strcasecmp (const char *string1, const char *string2);
|
||||
|
||||
Reference in New Issue
Block a user