From e054b061744473b9e1108a9ac2f1c2170a0613b0 Mon Sep 17 00:00:00 2001
From: David Mandelberg <david@mandelberg.org>
Date: Fri, 28 Mar 2025 21:59:57 -0400
Subject: [PATCH] Fix alignment for non-digit session names
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

When session names are all digits, they're aligned right:

```
(0)   -  0: 1 windows
(1)   └─> + 0: bash*: "dseomn@a4b80476102d:/tmp/tmp.mMXuTzbXGm"
(2)   -  1: 1 windows
(3)   └─> + 0: bash*: "dseomn@a4b80476102d:/tmp/tmp.mMXuTzbXGm"
(4)   -  2: 1 windows
(5)   └─> + 0: bash*: "dseomn@a4b80476102d:/tmp/tmp.mMXuTzbXGm"
(6)   -  3: 1 windows
(7)   └─> + 0: bash*: "dseomn@a4b80476102d:/tmp/tmp.mMXuTzbXGm"
(8)   -  4: 1 windows
(9)   └─> + 0: bash*: "dseomn@a4b80476102d:/tmp/tmp.mMXuTzbXGm"
(M-a) -  5: 1 windows
(M-b) └─> + 0: bash*: "dseomn@a4b80476102d:/tmp/tmp.mMXuTzbXGm"
(M-c) -  6: 1 windows
(M-d) └─> + 0: bash*: "dseomn@a4b80476102d:/tmp/tmp.mMXuTzbXGm"
(M-e) -  7: 1 windows
(M-f) └─> + 0: bash*: "dseomn@a4b80476102d:/tmp/tmp.mMXuTzbXGm"
(M-g) -  8: 1 windows
(M-h) └─> + 0: bash*: "dseomn@a4b80476102d:/tmp/tmp.mMXuTzbXGm"
(M-i) -  9: 1 windows
(M-j) └─> + 0: bash*: "dseomn@a4b80476102d:/tmp/tmp.mMXuTzbXGm"
(M-k) - 10: 1 windows (attached)
(M-l) └─> + 0: [tmux]*: "dseomn@a4b80476102d:/tmp/tmp.mMXuTzbXGm"
```

When not all digits, they're aligned left, with no additional padding:

```
(0)   - 0: 1 windows
(1)   └─> + 0: bash*: "dseomn@a4b80476102d:/tmp/tmp.mMXuTzbXGm"
(2)   - 1: 1 windows
(3)   └─> + 0: bash*: "dseomn@a4b80476102d:/tmp/tmp.mMXuTzbXGm"
(4)   - 2: 1 windows
(5)   └─> + 0: bash*: "dseomn@a4b80476102d:/tmp/tmp.mMXuTzbXGm"
(6)   - 3: 1 windows
(7)   └─> + 0: bash*: "dseomn@a4b80476102d:/tmp/tmp.mMXuTzbXGm"
(8)   - 4: 1 windows
(9)   └─> + 0: bash*: "dseomn@a4b80476102d:/tmp/tmp.mMXuTzbXGm"
(M-a) - 5: 1 windows
(M-b) └─> + 0: bash*: "dseomn@a4b80476102d:/tmp/tmp.mMXuTzbXGm"
(M-c) - 6: 1 windows
(M-d) └─> + 0: bash*: "dseomn@a4b80476102d:/tmp/tmp.mMXuTzbXGm"
(M-e) - 7: 1 windows
(M-f) └─> + 0: bash*: "dseomn@a4b80476102d:/tmp/tmp.mMXuTzbXGm"
(M-g) - 8: 1 windows
(M-h) └─> + 0: bash*: "dseomn@a4b80476102d:/tmp/tmp.mMXuTzbXGm"
(M-i) - 9: 1 windows
(M-j) └─> + 0: bash*: "dseomn@a4b80476102d:/tmp/tmp.mMXuTzbXGm"
(M-k) - 10: 1 windows (attached)
(M-l) └─> + 0: [tmux]*: "dseomn@a4b80476102d:/tmp/tmp.mMXuTzbXGm"
(M-m) - foo: 1 windows
(M-n) └─> + 0: bash*: "dseomn@a4b80476102d:/tmp/tmp.mMXuTzbXGm"
```

Fixes #4442
---
 mode-tree.c | 14 +++++++++++---
 1 file changed, 11 insertions(+), 3 deletions(-)

diff --git a/mode-tree.c b/mode-tree.c
index 508e870c..aef25023 100644
--- a/mode-tree.c
+++ b/mode-tree.c
@@ -694,6 +694,7 @@ mode_tree_draw(struct mode_tree_data *mtd)
 	const char		*tag, *symbol;
 	size_t			 size, n;
 	int			 keylen, pad, namelen[mtd->maxdepth + 1];
+	int			 names_all_digits[mtd->maxdepth + 1];
 
 	if (mtd->line_size == 0)
 		return;
@@ -717,13 +718,20 @@ mode_tree_draw(struct mode_tree_data *mtd)
 			keylen = mti->keylen + 3;
 	}
 
-	for (i = 0; i < mtd->maxdepth + 1; i++)
+	for (i = 0; i < mtd->maxdepth + 1; i++) {
 		namelen[i] = 0;
+		names_all_digits[i] = 1;
+	}
 	for (i = 0; i < mtd->line_size; i++) {
 		line = &mtd->line_list[i];
 		mti = line->item;
 		if ((int)strlen(mti->name) > namelen[line->depth])
 			namelen[line->depth] = strlen(mti->name);
+		for (j = 0; mti->name[j] && names_all_digits[line->depth];
+		    j++) {
+			if (mti->name[j] < '0' || mti->name[j] > '9')
+				names_all_digits[line->depth] = 0;
+		}
 	}
 
 	for (i = 0; i < mtd->line_size; i++) {
@@ -776,8 +784,8 @@ mode_tree_draw(struct mode_tree_data *mtd)
 		else
 			tag = "";
 		xasprintf(&text, "%-*s%s%*s%s%s", keylen, key, start,
-		    namelen[line->depth], mti->name, tag,
-		    (mti->text != NULL) ? ": " : "" );
+		    names_all_digits[line->depth] ? namelen[line->depth] : 0,
+		    mti->name, tag, (mti->text != NULL) ? ": " : "" );
 		width = utf8_cstrwidth(text);
 		if (width > w)
 			width = w;