Commit Diff


commit - afd6d93b3fae27ba5075fad222c638d095573af1
commit + 485331c7b91c5ef04485d8fb089ae6d2f07fa137
blob - 3775ac346cafa6bccf10727d937746f2674f9b4c
blob + ecfbc8c0d146d4a015d938ed753bcc5da73671d6
--- bxbar.c
+++ bxbar.c
@@ -229,9 +229,22 @@ readstdin(void)
     }
 }
 
+static int
+text_fit(const char *s, int len, int max_w, XGlyphInfo *ext)
+{
+    for (int i = len; i > 0; i--) {
+        XftTextExtents8(dpy, font, (FcChar8 *)s, i, ext);
+        if (ext->width <= max_w)
+            return i;
+    }
+    return 0;
+}
+
 void
 draw(void)
 {
+    logmsg("draw() called, stext='%s'", stext);
+
     /* Paint background */
     XftDrawRect(xft_draw, &color[1], 0, 0, screen_w, BAR_HEIGHT);
 
@@ -239,25 +252,47 @@ draw(void)
     if (strlen(stext) == 0)
         return;
 
-    /* Measure text width */
-    XGlyphInfo extents;
-    XftTextExtents8(dpy, font, (FcChar8 *)stext, strlen(stext), &extents);
-
-    /* Truncate if wider than bar minus padding */
-    int max_w = screen_w - (PADDING * 2);
-    int draw_len = strlen(stext);
-    if (extents.width > max_w) {
-        while (draw_len > 0) {
-            XftTextExtents8(dpy, font, (FcChar8 *)stext, draw_len, &extents);
-            if (extents.width <= max_w)
-                break;
-            draw_len--;
-        }
-    }
-
     int y = (BAR_HEIGHT - (font->ascent + font->descent)) / 2 + font->ascent;
-    XftDrawStringUtf8(xft_draw, &color[0], font, PADDING, y,
-                       (FcChar8 *)stext, draw_len);
+    int half_w = (screen_w / 2) - (PADDING * 2);
+    XGlyphInfo ext;
+
+    /* Find delimiter for left/right split */
+    char *delim = strchr(stext, DELIMITER);
+
+    if (delim) {
+        /* Split into left and right sections */
+        *delim = '\0';
+        char *left = stext;
+        char *right = delim + 1;
+
+        /* Trim leading spaces from right section */
+        while (*right == ' ')
+            right++;
+
+        /* Left section — left-aligned */
+        int left_len = text_fit(left, strlen(left), half_w, &ext);
+        XftDrawStringUtf8(xft_draw, &color[0], font, PADDING, y,
+                           (FcChar8 *)left, left_len);
+
+        /* Right section — right-aligned */
+        int right_len = text_fit(right, strlen(right), half_w, &ext);
+        int right_x = screen_w - PADDING - ext.width;
+        XftDrawStringUtf8(xft_draw, &color[0], font, right_x, y,
+                           (FcChar8 *)right, right_len);
+
+        logmsg("drawing left: '%.*s' len=%d at x=%d", left_len, left, left_len, PADDING);
+        logmsg("drawing right: '%.*s' len=%d at x=%d", right_len, right, right_len, right_x);
+
+
+        /* Restore delimiter */
+        *delim = DELIMITER;
+    } else {
+        /* No delimiter — left-aligned with truncation */
+        int max_w = screen_w - (PADDING * 2);
+        int len = text_fit(stext, strlen(stext), max_w, &ext);
+        XftDrawStringUtf8(xft_draw, &color[0], font, PADDING, y,
+                           (FcChar8 *)stext, len);
+    }
 }
 
 static void cleanup(int status) {