diff --git a/mode-tree.c b/mode-tree.c index d43cf1b3..24cbea04 100644 --- a/mode-tree.c +++ b/mode-tree.c @@ -66,6 +66,7 @@ struct mode_tree_data { u_int line_size; u_int depth; + u_int maxdepth; u_int width; u_int height; @@ -196,6 +197,8 @@ mode_tree_build_lines(struct mode_tree_data *mtd, int flat = 1; mtd->depth = depth; + if (depth > mtd->maxdepth) + mtd->maxdepth = depth; TAILQ_FOREACH(mti, mtl, entry) { mtd->line_list = xreallocarray(mtd->line_list, mtd->line_size + 1, sizeof *mtd->line_list); @@ -528,6 +531,7 @@ mode_tree_build(struct mode_tree_data *mtd) TAILQ_INIT(&mtd->saved); mode_tree_clear_lines(mtd); + mtd->maxdepth = 0; mode_tree_build_lines(mtd, &mtd->children, 0); if (mtd->line_list != NULL && tag == UINT64_MAX) @@ -658,7 +662,7 @@ mode_tree_draw(struct mode_tree_data *mtd) char *text, *start, *key; const char *tag, *symbol; size_t size, n; - int keylen, pad; + int keylen, pad, namelen[mtd->maxdepth + 1]; if (mtd->line_size == 0) return; @@ -682,6 +686,15 @@ mode_tree_draw(struct mode_tree_data *mtd) keylen = mti->keylen + 3; } + for (i = 0; i < mtd->maxdepth + 1; i++) + namelen[i] = 0; + 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 (i = 0; i < mtd->line_size; i++) { if (i < mtd->offset) continue; @@ -731,8 +744,9 @@ mode_tree_draw(struct mode_tree_data *mtd) tag = "*"; else tag = ""; - xasprintf(&text, "%-*s%s%s%s%s", keylen, key, start, mti->name, - tag, (mti->text != NULL) ? ": " : "" ); + xasprintf(&text, "%-*s%s%*s%s%s", keylen, key, start, + namelen[line->depth], mti->name, tag, + (mti->text != NULL) ? ": " : "" ); width = utf8_cstrwidth(text); if (width > w) width = w;