Refactor keys_format_label()
Simplify the key formatting function and enforce display width properly using the UTF8 chopping function. Signed-off-by: Lukas Fleischer <lfleischer@calcurse.org>
This commit is contained in:
parent
bffa517250
commit
b0984e71b0
33
src/keys.c
33
src/keys.c
@ -470,29 +470,11 @@ char *keys_action_allkeys(enum key action)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* Need this to display keys properly inside status bar. */
|
/* Need this to display keys properly inside status bar. */
|
||||||
static char *keys_format_label(char *key, int keylen)
|
static unsigned keys_format_label(char **s, char *key, int width)
|
||||||
{
|
{
|
||||||
static char fmtkey[BUFSIZ];
|
*s = mem_strdup(key);
|
||||||
const int len = utf8_strwidth(key);
|
utf8_chop(*s, width);
|
||||||
const char dot = '.';
|
return utf8_strwidth(*s);
|
||||||
int i;
|
|
||||||
|
|
||||||
if (keylen > BUFSIZ)
|
|
||||||
return NULL;
|
|
||||||
|
|
||||||
memset(fmtkey, 0, sizeof(fmtkey));
|
|
||||||
if (len == 0) {
|
|
||||||
strncpy(fmtkey, "?", sizeof(fmtkey));
|
|
||||||
} else if (len <= keylen) {
|
|
||||||
for (i = 0; i < keylen - len; i++)
|
|
||||||
fmtkey[i] = ' ';
|
|
||||||
strncat(fmtkey, key, keylen);
|
|
||||||
} else {
|
|
||||||
for (i = 0; i < keylen - 1; i++)
|
|
||||||
fmtkey[i] = key[i];
|
|
||||||
fmtkey[keylen - 1] = dot;
|
|
||||||
}
|
|
||||||
return fmtkey;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
@ -518,6 +500,7 @@ keys_display_bindings_bar(WINDOW * win, int *bindings, int count,
|
|||||||
const int label_pos_y = key_pos_y;
|
const int label_pos_y = key_pos_y;
|
||||||
|
|
||||||
char key[UTF8_MAXLEN + 1], *fmtkey;
|
char key[UTF8_MAXLEN + 1], *fmtkey;
|
||||||
|
unsigned dpywidth, shift_x;
|
||||||
|
|
||||||
int binding_key;
|
int binding_key;
|
||||||
|
|
||||||
@ -566,8 +549,10 @@ keys_display_bindings_bar(WINDOW * win, int *bindings, int count,
|
|||||||
}
|
}
|
||||||
|
|
||||||
custom_apply_attr(win, ATTR_HIGHEST);
|
custom_apply_attr(win, ATTR_HIGHEST);
|
||||||
fmtkey = keys_format_label(key, KEYS_KEYLEN);
|
dpywidth = keys_format_label(&fmtkey, key, KEYS_KEYLEN);
|
||||||
mvwaddstr(win, key_pos_y, key_pos_x, fmtkey);
|
shift_x = KEYS_KEYLEN - dpywidth;
|
||||||
|
mvwaddstr(win, key_pos_y, key_pos_x + shift_x, fmtkey);
|
||||||
|
mem_free(fmtkey);
|
||||||
custom_remove_attr(win, ATTR_HIGHEST);
|
custom_remove_attr(win, ATTR_HIGHEST);
|
||||||
mvwaddstr(win, label_pos_y, label_pos_x, label);
|
mvwaddstr(win, label_pos_y, label_pos_x, label);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user