Simplify statusbar_item_default_handler by merging update_statusbar_bg and

reverse_controls and by using a GString.


git-svn-id: file:///var/www/svn.irssi.org/SVN/irssi/trunk@4934 dbcabf3a-b0e7-0310-adc4-f8d773084564
This commit is contained in:
Emanuele Giaquinta 2008-11-30 23:14:43 +00:00 committed by exg
commit 227e90e52b

View file

@ -618,28 +618,6 @@ STATUSBAR_REC *statusbar_find(STATUSBAR_GROUP_REC *group, const char *name,
return NULL; return NULL;
} }
static char *update_statusbar_bg(const char *str, const char *color)
{
GString *out;
char *ret;
out = g_string_new(color);
while (*str != '\0') {
if (*str == '%' && str[1] == 'n') {
g_string_append(out, color);
str += 2;
continue;
}
g_string_append_c(out, *str);
str++;
}
ret = out->str;
g_string_free(out, FALSE);
return ret;
}
const char *statusbar_item_get_value(SBAR_ITEM_REC *item) const char *statusbar_item_get_value(SBAR_ITEM_REC *item)
{ {
const char *value; const char *value;
@ -653,12 +631,11 @@ const char *statusbar_item_get_value(SBAR_ITEM_REC *item)
return value; return value;
} }
static char *reverse_controls(const char *str) static GString *finalize_string(const char *str, const char *color)
{ {
GString *out; GString *out;
char *ret;
out = g_string_new(NULL); out = g_string_new(color);
while (*str != '\0') { while (*str != '\0') {
if ((unsigned char) *str < 32 || if ((unsigned char) *str < 32 ||
@ -667,6 +644,9 @@ static char *reverse_controls(const char *str)
/* control char */ /* control char */
g_string_sprintfa(out, "%%8%c%%8", g_string_sprintfa(out, "%%8%c%%8",
'A'-1 + (*str & 0x7f)); 'A'-1 + (*str & 0x7f));
} else if (*str == '%' && str[1] == 'n') {
g_string_append(out, color);
str++;
} else { } else {
g_string_append_c(out, *str); g_string_append_c(out, *str);
} }
@ -674,9 +654,7 @@ static char *reverse_controls(const char *str)
str++; str++;
} }
ret = out->str; return out;
g_string_free(out, FALSE);
return ret;
} }
void statusbar_item_default_handler(SBAR_ITEM_REC *item, int get_size_only, void statusbar_item_default_handler(SBAR_ITEM_REC *item, int get_size_only,
@ -720,38 +698,30 @@ void statusbar_item_default_handler(SBAR_ITEM_REC *item, int get_size_only,
tmpstr = strip_codes(tmpstr2); tmpstr = strip_codes(tmpstr2);
g_free(tmpstr2); g_free(tmpstr2);
/* show all control chars reversed */
tmpstr2 = reverse_controls(tmpstr);
g_free(tmpstr);
tmpstr = tmpstr2;
if (get_size_only) { if (get_size_only) {
item->min_size = item->max_size = format_get_length(tmpstr); item->min_size = item->max_size = format_get_length(tmpstr);
} else { } else {
GString *out;
if (item->size < item->min_size) { if (item->size < item->min_size) {
/* they're forcing us smaller than minimum size.. */ /* they're forcing us smaller than minimum size.. */
len = format_real_length(tmpstr, item->size); len = format_real_length(tmpstr, item->size);
tmpstr[len] = '\0'; tmpstr[len] = '\0';
} }
out = finalize_string(tmpstr, item->bar->color);
/* make sure the str is big enough to fill the /* make sure the str is big enough to fill the
requested size, so it won't corrupt screen */ requested size, so it won't corrupt screen */
len = format_get_length(tmpstr); len = format_get_length(tmpstr);
if (len < item->size) { if (len < item->size) {
char *fill; int i;
len = item->size-len; len = item->size-len;
fill = g_malloc(len + 1); for (i = 0; i < len; i++)
memset(fill, ' ', len); fill[len] = '\0'; g_string_append_c(out, ' ');
tmpstr2 = g_strconcat(tmpstr, fill, NULL);
g_free(fill);
g_free(tmpstr);
tmpstr = tmpstr2;
} }
tmpstr2 = update_statusbar_bg(tmpstr, item->bar->color); gui_printtext(item->xpos, item->bar->real_ypos, out->str);
gui_printtext(item->xpos, item->bar->real_ypos, tmpstr2); g_string_free(out, TRUE);
g_free(tmpstr2);
} }
g_free(tmpstr); g_free(tmpstr);
} }