From edd93b5b55c66f3914f95738b9b23b2ac1ef7c75 Mon Sep 17 00:00:00 2001
From: crondog <crondog@gmail.com>
Date: Thu, 24 Dec 2015 13:55:17 +1100
Subject: [PATCH] swaybar: min_width and align

---
 swaybar/main.c | 52 +++++++++++++++++++++++++++++++-------------------
 1 file changed, 32 insertions(+), 20 deletions(-)

diff --git a/swaybar/main.c b/swaybar/main.c
index 74930075..37218c32 100644
--- a/swaybar/main.c
+++ b/swaybar/main.c
@@ -414,9 +414,29 @@ void render() {
 			struct status_block *block = status_line->items[i];
 			if (block->full_text && block->full_text[0]) {
 				get_text_size(window, &width, &height, "%s", block->full_text);
+
+				int textwidth = width;
+				
+				if (width < block->min_width) {
+					width = block->min_width;
+				}
+
 				moved += width + block->separator_block_width;
 				blockpos = window->width - margin - moved;
-				cairo_move_to(window->cairo, blockpos, margin);
+				
+				int offset = 0;
+
+				if (strncmp(block->align, "left", 5) == 0) {
+					offset = blockpos;
+				}
+				else if (strncmp(block->align, "right", 5) == 0) {
+					offset = blockpos + width - textwidth;
+				}
+				else if (strncmp(block->align, "center", 6) == 0) {
+					offset = blockpos + (width - textwidth) / 2;
+				}
+
+				cairo_move_to(window->cairo, offset, margin);
 				cairo_set_source_u32(window->cairo, block->color);
 				pango_printf(window, "%s", block->full_text);
 				if (corner) {
@@ -425,9 +445,9 @@ void render() {
 					cairo_set_source_u32(window->cairo, colors.separator);
 					cairo_set_line_width(window->cairo, 1);
 					cairo_move_to(window->cairo, blockpos + width
-						    + block->separator_block_width/2, margin);
+							+ block->separator_block_width/2, margin);
 					cairo_line_to(window->cairo, blockpos + width
-						    + block->separator_block_width/2, window->height - margin);
+							+ block->separator_block_width/2, window->height - margin);
 					cairo_stroke(window->cairo);
 				}
 			}
@@ -492,22 +512,6 @@ void free_status_block(void *item) {
 }
 
 void parse_json(const char *text) {
-/* the array of objects looks like this:
- * [ {
- *     "full_text": "E: 10.0.0.1 (1000 Mbit/s)",
- *     "short_text": "10.0.0.1",
- *     "color": "#00ff00",
- *     "min_width": 300,
- *     "align": "right",
- *     "urgent": false,
- *     "name": "ethernet",
- *     "instance": "eth0",
- *     "separator": true,
- *     "separator_block_width": 9
- *   },
- *   { ... }, ...
- * ]
- */
 	json_object *results = json_tokener_parse(text);
 	if (!results) {
 		sway_log(L_DEBUG, "xxx Failed to parse json");
@@ -565,7 +569,15 @@ void parse_json(const char *text) {
 		}
 
 		if (min_width) {
-			new->min_width = json_object_get_int(min_width);
+			json_type type = json_object_get_type(min_width);
+			if (type == json_type_int) {
+				new->min_width = json_object_get_int(min_width);
+			}
+			else if (type == json_type_string) {
+				int width, height;
+				get_text_size(window, &width, &height, "%s", json_object_get_string(min_width));
+				new->min_width = width;
+			}
 		}
 
 		if (align) {