summaryrefslogtreecommitdiff
path: root/usr.bin/tmux
diff options
context:
space:
mode:
authorNicholas Marriott <nicm@cvs.openbsd.org>2021-10-22 17:12:51 +0000
committerNicholas Marriott <nicm@cvs.openbsd.org>2021-10-22 17:12:51 +0000
commit2ba39c0716c86b5c00a7997a479400998115e54e (patch)
treee4c1a313ff1e89416dcb1b01b354814ebe718693 /usr.bin/tmux
parent0ae4bfe12a185bb606ced5478f5fe70798e44da2 (diff)
Remove key and trim text if menu cannot fit in available space, based on
a change from Alexis Hildebrandt.
Diffstat (limited to 'usr.bin/tmux')
-rw-r--r--usr.bin/tmux/menu.c30
1 files changed, 25 insertions, 5 deletions
diff --git a/usr.bin/tmux/menu.c b/usr.bin/tmux/menu.c
index 345964ee592..2ed80a5a21b 100644
--- a/usr.bin/tmux/menu.c
+++ b/usr.bin/tmux/menu.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: menu.c,v 1.39 2021/10/18 09:48:35 nicm Exp $ */
+/* $OpenBSD: menu.c,v 1.40 2021/10/22 17:12:50 nicm Exp $ */
/*
* Copyright (c) 2019 Nicholas Marriott <nicholas.marriott@gmail.com>
@@ -55,10 +55,11 @@ menu_add_item(struct menu *menu, const struct menu_item *item,
struct cmdq_item *qitem, struct client *c, struct cmd_find_state *fs)
{
struct menu_item *new_item;
- const char *key, *cmd;
+ const char *key = NULL, *cmd, *suffix = "";
char *s, *name;
- u_int width;
+ u_int width, max_width;
int line;
+ size_t keylen, slen;
line = (item == NULL || item->name == NULL || *item->name == '\0');
if (line && menu->count == 0)
@@ -80,11 +81,30 @@ menu_add_item(struct menu *menu, const struct menu_item *item,
menu->count--;
return;
}
+ max_width = c->tty.sx - 4;
+
+ slen = strlen(s);
if (*s != '-' && item->key != KEYC_UNKNOWN && item->key != KEYC_NONE) {
key = key_string_lookup_key(item->key, 0);
+ keylen = strlen(key) + 3; /* 3 = space and two brackets */
+
+ /*
+ * Only add the key if there is space for the entire item text
+ * and the key.
+ */
+ if (keylen >= max_width || slen >= max_width - keylen)
+ key = NULL;
+ }
+
+ if (key != NULL)
xasprintf(&name, "%s#[default] #[align=right](%s)", s, key);
- } else
- xasprintf(&name, "%s", s);
+ else {
+ if (slen > max_width) {
+ max_width--;
+ suffix = ">";
+ }
+ xasprintf(&name, "%.*s%s", (int)max_width, s, suffix);
+ }
new_item->name = name;
free(s);