diff options
author | 2015-11-12 12:43:36 +0000 | |
---|---|---|
committer | 2015-11-12 12:43:36 +0000 | |
commit | 1d1963bb56ed1a191f2ca988a4393deb582dfc38 (patch) | |
tree | b0ae883d954d64d9e7d64a2cbc97526923585e49 | |
parent | fix a typo in NAME and add two missing entries; (diff) | |
download | wireguard-openbsd-1d1963bb56ed1a191f2ca988a4393deb582dfc38.tar.xz wireguard-openbsd-1d1963bb56ed1a191f2ca988a4393deb582dfc38.zip |
Add utf8_padcstr and use it to align columns in list-keys.
Diffstat (limited to '')
-rw-r--r-- | usr.bin/tmux/cmd-list-keys.c | 29 | ||||
-rw-r--r-- | usr.bin/tmux/tmux.h | 3 | ||||
-rw-r--r-- | usr.bin/tmux/utf8.c | 23 |
3 files changed, 42 insertions, 13 deletions
diff --git a/usr.bin/tmux/cmd-list-keys.c b/usr.bin/tmux/cmd-list-keys.c index edcc768178d..89107b70336 100644 --- a/usr.bin/tmux/cmd-list-keys.c +++ b/usr.bin/tmux/cmd-list-keys.c @@ -1,4 +1,4 @@ -/* $OpenBSD: cmd-list-keys.c,v 1.28 2015/11/12 11:05:34 nicm Exp $ */ +/* $OpenBSD: cmd-list-keys.c,v 1.29 2015/11/12 12:43:36 nicm Exp $ */ /* * Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net> @@ -18,6 +18,7 @@ #include <sys/types.h> +#include <stdlib.h> #include <string.h> #include "tmux.h" @@ -54,10 +55,9 @@ cmd_list_keys_exec(struct cmd *self, struct cmd_q *cmdq) struct key_table *table; struct key_binding *bd; const char *key, *tablename, *r; - char tmp[BUFSIZ]; + char *cp, tmp[BUFSIZ]; size_t used; int repeat, width, tablewidth, keywidth; - u_int i; if (self->entry == &cmd_list_commands_entry) return (cmd_list_keys_commands(self, cmdq)); @@ -82,7 +82,7 @@ cmd_list_keys_exec(struct cmd *self, struct cmd_q *cmdq) if (bd->can_repeat) repeat = 1; - width = strlen(table->name); + width = utf8_cstrwidth(table->name); if (width > tablewidth) tablewidth = width; width = utf8_cstrwidth(key); @@ -103,13 +103,20 @@ cmd_list_keys_exec(struct cmd *self, struct cmd_q *cmdq) r = "-r "; else r = " "; - used = xsnprintf(tmp, sizeof tmp, "%s-T %-*s %s", r, - (int)tablewidth, table->name, key); - for (i = 0; i < keywidth - utf8_cstrwidth(key); i++) { - if (strlcat(tmp, " ", sizeof tmp) < sizeof tmp) - used++; - } - if (used < sizeof tmp) { + xsnprintf(tmp, sizeof tmp, "%s-T ", r); + + cp = utf8_padcstr(table->name, tablewidth); + strlcat(tmp, cp, sizeof tmp); + strlcat(tmp, " ", sizeof tmp); + free(cp); + + cp = utf8_padcstr(key, keywidth); + strlcat(tmp, cp, sizeof tmp); + strlcat(tmp, " ", sizeof tmp); + free(cp); + + used = strlen(tmp); + if (used < (sizeof tmp) - 1) { cmd_list_print(bd->cmdlist, tmp + used, (sizeof tmp) - used); } diff --git a/usr.bin/tmux/tmux.h b/usr.bin/tmux/tmux.h index f19ed3c3c5a..b0c047d5220 100644 --- a/usr.bin/tmux/tmux.h +++ b/usr.bin/tmux/tmux.h @@ -1,4 +1,4 @@ -/* $OpenBSD: tmux.h,v 1.574 2015/11/12 12:19:57 nicm Exp $ */ +/* $OpenBSD: tmux.h,v 1.575 2015/11/12 12:43:36 nicm Exp $ */ /* * Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net> @@ -2194,6 +2194,7 @@ struct utf8_data *utf8_fromcstr(const char *); char *utf8_tocstr(struct utf8_data *); u_int utf8_cstrwidth(const char *); char *utf8_trimcstr(const char *, u_int); +char *utf8_padcstr(const char *, u_int); /* procname.c */ char *get_proc_name(int, char *); diff --git a/usr.bin/tmux/utf8.c b/usr.bin/tmux/utf8.c index f3d210ebefd..ad8947ce751 100644 --- a/usr.bin/tmux/utf8.c +++ b/usr.bin/tmux/utf8.c @@ -1,4 +1,4 @@ -/* $OpenBSD: utf8.c,v 1.17 2015/11/12 12:19:57 nicm Exp $ */ +/* $OpenBSD: utf8.c,v 1.18 2015/11/12 12:43:36 nicm Exp $ */ /* * Copyright (c) 2008 Nicholas Marriott <nicm@users.sourceforge.net> @@ -713,3 +713,24 @@ utf8_trimcstr(const char *s, u_int width) free(tmp); return (out); } + +/* Pad UTF-8 string to width. Caller frees. */ +char * +utf8_padcstr(const char *s, u_int width) +{ + size_t slen; + char *out; + u_int n, i; + + n = utf8_cstrwidth(s); + if (n >= width) + return (xstrdup(s)); + + slen = strlen(s); + out = xmalloc(slen + 1 + (width - n)); + memcpy(out, s, slen); + for (i = n; i < width; i++) + out[slen++] = ' '; + out[slen] = '\0'; + return (out); +} |