[markup] Add generic substitution of elements and use it for titlebar
Signed-off-by: Julien Danjou <julien@danjou.info>
This commit is contained in:
parent
a4159bbaf7
commit
7a8e39b3e7
|
@ -197,11 +197,11 @@ cfg_opt_t titlebar_opts[] =
|
|||
/** Text alignment. */
|
||||
CFG_ALIGNMENT((char *) "text_align", (char *) "center", CFGF_NONE),
|
||||
/** Titlebar markup string for normal windows. */
|
||||
CFG_STR((char *) "text_normal", (char *) "<bg color=\"#444444\"/>%t", CFGF_NONE),
|
||||
CFG_STR((char *) "text_normal", (char *) "<bg color=\"#444444\"/><title/>", CFGF_NONE),
|
||||
/** Titlebar markup string for focused windows. */
|
||||
CFG_STR((char *) "text_focus", (char *) "<bg color=\"#535d6c\"/>%t", CFGF_NONE),
|
||||
CFG_STR((char *) "text_focus", (char *) "<bg color=\"#535d6c\"/><title/>", CFGF_NONE),
|
||||
/** Titlebar markup string for urgent windows. */
|
||||
CFG_STR((char *) "text_urgent", (char *) "<bg color=\"#ff4500\"/>%t", CFGF_NONE),
|
||||
CFG_STR((char *) "text_urgent", (char *) "<bg color=\"#ff4500\"/><title/>", CFGF_NONE),
|
||||
CFG_AWESOME_END()
|
||||
};
|
||||
/** This section defines general options. */
|
||||
|
|
|
@ -228,19 +228,19 @@ static bool
|
|||
draw_text_markup_expand(draw_parser_data_t *data,
|
||||
const char *str, ssize_t slen)
|
||||
{
|
||||
const char *elements[] = { "bg", NULL};
|
||||
const char *elements[] = { "bg", NULL };
|
||||
markup_parser_data_t *p;
|
||||
int i;
|
||||
|
||||
p = markup_parser_data_new(elements, countof(elements));
|
||||
p = markup_parser_data_new(elements, NULL, countof(elements));
|
||||
|
||||
if(!markup_parse(p, str, slen))
|
||||
return false;
|
||||
|
||||
/* bg */
|
||||
if(p->attribute_values[0])
|
||||
for(i = 0; p->attribute_values[0][i]; i++)
|
||||
if(!strcmp(p->attribute_values[0][i], "color"))
|
||||
if(p->attribute_names[0])
|
||||
for(i = 0; p->attribute_names[0][i]; i++)
|
||||
if(!a_strcmp(p->attribute_names[0][i], "color"))
|
||||
data->has_bg_color = draw_color_new(data->connection, data->phys_screen,
|
||||
p->attribute_values[0][i], &data->bg_color);
|
||||
|
||||
|
|
|
@ -64,6 +64,12 @@ markup_parse_start_element(GMarkupParseContext *context __attribute__ ((unused))
|
|||
p->attribute_names[i][j] = a_strdup(attribute_names[j]);
|
||||
p->attribute_values[i][j] = a_strdup(attribute_values[j]);
|
||||
}
|
||||
if(p->elements_sub && p->elements_sub[i])
|
||||
{
|
||||
asprintf(&newtext, "%s%s", NONULL(p->text), p->elements_sub[i]);
|
||||
p_delete(&p->text);
|
||||
p->text = newtext;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -150,18 +156,21 @@ markup_parse_text(GMarkupParseContext *context __attribute__ ((unused)),
|
|||
|
||||
/** Create a markup_parser_data_t structure with elements list.
|
||||
* \param elements an array of elements to look for, NULL terminated
|
||||
* \param number of elements in the array (without NULL)
|
||||
* \param elements_sub an optionnal array of values to substitude to elements, NULL
|
||||
* terminated, or NULL if not needed
|
||||
* \param nelem number of elements in the array (without NULL)
|
||||
* \return a pointer to an allocated markup_parser_data_t which must be freed
|
||||
* with markup_parser_data_delete()
|
||||
* with markup_parser_data_delete()
|
||||
*/
|
||||
markup_parser_data_t *
|
||||
markup_parser_data_new(const char **elements, ssize_t nelem)
|
||||
markup_parser_data_new(const char **elements, const char **elements_sub, ssize_t nelem)
|
||||
{
|
||||
markup_parser_data_t *p;
|
||||
|
||||
p = p_new(markup_parser_data_t, 1);
|
||||
|
||||
p->elements = elements;
|
||||
p->elements_sub = elements_sub;
|
||||
p->attribute_names = p_new(char **, nelem);
|
||||
p->attribute_values = p_new(char **, nelem);
|
||||
|
||||
|
|
|
@ -28,11 +28,12 @@ typedef struct
|
|||
{
|
||||
char *text;
|
||||
const char **elements;
|
||||
const char **elements_sub;
|
||||
char ***attribute_names;
|
||||
char ***attribute_values;
|
||||
} markup_parser_data_t;
|
||||
|
||||
markup_parser_data_t * markup_parser_data_new(const char **, ssize_t);
|
||||
markup_parser_data_t * markup_parser_data_new(const char **, const char **, ssize_t);
|
||||
void markup_parser_data_delete(markup_parser_data_t **);
|
||||
bool markup_parse(markup_parser_data_t *data, const char *, ssize_t);
|
||||
|
||||
|
|
33
titlebar.c
33
titlebar.c
|
@ -27,38 +27,23 @@
|
|||
#include "titlebar.h"
|
||||
#include "screen.h"
|
||||
#include "layouts/floating.h"
|
||||
#include "common/markup.h"
|
||||
|
||||
extern AwesomeConf globalconf;
|
||||
|
||||
static char *
|
||||
titlebar_markup_parse(client_t *c, const char *str, ssize_t len)
|
||||
{
|
||||
const char *ps;
|
||||
char *new;
|
||||
int i = 0;
|
||||
ssize_t clen;
|
||||
|
||||
new = p_new(char, ++len);
|
||||
const char *elements[] = { "title", NULL };
|
||||
const char *elements_sub[] = { c->name , NULL };
|
||||
markup_parser_data_t *p;
|
||||
|
||||
for(ps = str; *ps; ps++, i++)
|
||||
if(*ps == '%')
|
||||
{
|
||||
ps++;
|
||||
if(*ps == '%')
|
||||
new[i] = '%';
|
||||
else if(*ps == 't')
|
||||
{
|
||||
clen = a_strlen(c->name);
|
||||
len += clen;
|
||||
p_realloc(&new, len);
|
||||
a_strcat(new + i, len - i, c->name);
|
||||
i += clen - 1;
|
||||
}
|
||||
}
|
||||
else
|
||||
new[i] = *ps;
|
||||
p = markup_parser_data_new(elements, elements_sub, countof(elements));
|
||||
|
||||
return new;
|
||||
if(!markup_parse(p, str, len))
|
||||
return a_strdup(str);
|
||||
|
||||
return p->text;
|
||||
}
|
||||
|
||||
static char *
|
||||
|
|
Loading…
Reference in New Issue