i3bar: add min_width and align keys to blocks

This commit is contained in:
Antoine Millet
2012-12-02 13:20:06 +01:00
committed by Michael Stapelberg
parent fdf14b8f58
commit e8149c77b3
4 changed files with 69 additions and 4 deletions

View File

@ -132,6 +132,27 @@ static int stdin_string(void *context, const unsigned char *val, unsigned int le
if (strcasecmp(ctx->last_map_key, "color") == 0) {
sasprintf(&(ctx->block.color), "%.*s", len, val);
}
if (strcasecmp(ctx->last_map_key, "align") == 0) {
if (len == strlen("left") && !strncmp((const char*)val, "left", strlen("left"))) {
ctx->block.align = ALIGN_LEFT;
} else if (len == strlen("right") && !strncmp((const char*)val, "right", strlen("right"))) {
ctx->block.align = ALIGN_RIGHT;
} else {
ctx->block.align = ALIGN_CENTER;
}
}
return 1;
}
#if YAJL_MAJOR >= 2
static int stdin_integer(void *context, long long val) {
#else
static int stdin_integer(void *context, long val) {
#endif
parser_ctx *ctx = context;
if (strcasecmp(ctx->last_map_key, "min_width") == 0) {
ctx->block.min_width = (uint32_t)val;
}
return 1;
}
@ -313,6 +334,7 @@ void start_child(char *command) {
callbacks.yajl_map_key = stdin_map_key;
callbacks.yajl_boolean = stdin_boolean;
callbacks.yajl_string = stdin_string;
callbacks.yajl_integer = stdin_integer;
callbacks.yajl_start_array = stdin_start_array;
callbacks.yajl_end_array = stdin_end_array;
callbacks.yajl_start_map = stdin_start_map;

View File

@ -123,10 +123,31 @@ void refresh_statusline(void) {
continue;
block->width = predict_text_width(block->full_text);
/* Compute offset and append for text aligment in min_width. */
if (block->min_width <= block->width) {
block->x_offset = 0;
block->x_append = 0;
} else {
uint32_t padding_width = block->min_width - block->width;
switch (block->align) {
case ALIGN_LEFT:
block->x_append = padding_width;
break;
case ALIGN_RIGHT:
block->x_offset = padding_width;
break;
case ALIGN_CENTER:
block->x_offset = padding_width / 2;
block->x_append = padding_width / 2 + padding_width % 2;
break;
}
}
/* If this is not the last block, add some pixels for a separator. */
if (TAILQ_NEXT(block, blocks) != NULL)
block->width += 9;
statusline_width += block->width;
statusline_width += block->width + block->x_offset + block->x_append;
}
/* If the statusline is bigger than our screen we need to make sure that
@ -147,8 +168,8 @@ void refresh_statusline(void) {
uint32_t colorpixel = (block->color ? get_colorpixel(block->color) : colors.bar_fg);
set_font_colors(statusline_ctx, colorpixel, colors.bar_bg);
draw_text(block->full_text, statusline_pm, statusline_ctx, x, 0, block->width);
x += block->width;
draw_text(block->full_text, statusline_pm, statusline_ctx, x + block->x_offset, 0, block->width);
x += block->width + block->x_offset + block->x_append;
if (TAILQ_NEXT(block, blocks) != NULL) {
/* This is not the last block, draw a separator. */