Add utf8_padcstr and use it to align columns in list-keys.

This commit is contained in:
nicm 2015-11-12 12:43:36 +00:00
parent 1da7475d0e
commit a209ea3953
3 changed files with 39 additions and 10 deletions

View File

@ -18,6 +18,7 @@
#include <sys/types.h> #include <sys/types.h>
#include <stdlib.h>
#include <string.h> #include <string.h>
#include "tmux.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_table *table;
struct key_binding *bd; struct key_binding *bd;
const char *key, *tablename, *r; const char *key, *tablename, *r;
char tmp[BUFSIZ]; char *cp, tmp[BUFSIZ];
size_t used; size_t used;
int repeat, width, tablewidth, keywidth; int repeat, width, tablewidth, keywidth;
u_int i;
if (self->entry == &cmd_list_commands_entry) if (self->entry == &cmd_list_commands_entry)
return (cmd_list_keys_commands(self, cmdq)); 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) if (bd->can_repeat)
repeat = 1; repeat = 1;
width = strlen(table->name); width = utf8_cstrwidth(table->name);
if (width > tablewidth) if (width > tablewidth)
tablewidth = width; tablewidth = width;
width = utf8_cstrwidth(key); width = utf8_cstrwidth(key);
@ -103,13 +103,20 @@ cmd_list_keys_exec(struct cmd *self, struct cmd_q *cmdq)
r = "-r "; r = "-r ";
else else
r = " "; r = " ";
used = xsnprintf(tmp, sizeof tmp, "%s-T %-*s %s", r, xsnprintf(tmp, sizeof tmp, "%s-T ", r);
(int)tablewidth, table->name, key);
for (i = 0; i < keywidth - utf8_cstrwidth(key); i++) { cp = utf8_padcstr(table->name, tablewidth);
if (strlcat(tmp, " ", sizeof tmp) < sizeof tmp) strlcat(tmp, cp, sizeof tmp);
used++; strlcat(tmp, " ", sizeof tmp);
} free(cp);
if (used < sizeof tmp) {
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, cmd_list_print(bd->cmdlist, tmp + used,
(sizeof tmp) - used); (sizeof tmp) - used);
} }

1
tmux.h
View File

@ -2194,6 +2194,7 @@ struct utf8_data *utf8_fromcstr(const char *);
char *utf8_tocstr(struct utf8_data *); char *utf8_tocstr(struct utf8_data *);
u_int utf8_cstrwidth(const char *); u_int utf8_cstrwidth(const char *);
char *utf8_trimcstr(const char *, u_int); char *utf8_trimcstr(const char *, u_int);
char *utf8_padcstr(const char *, u_int);
/* procname.c */ /* procname.c */
char *get_proc_name(int, char *); char *get_proc_name(int, char *);

21
utf8.c
View File

@ -713,3 +713,24 @@ utf8_trimcstr(const char *s, u_int width)
free(tmp); free(tmp);
return (out); 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);
}