Tweak the default choose modes formats:

- Only show pane title if it is not default and not empty.
- Add a prettier time format and use that instead of long ctime().
- Remove clutter and change the order.
This commit is contained in:
nicm 2020-05-16 14:10:29 +00:00
parent aa7dccf8e1
commit 0dd1944206
5 changed files with 105 additions and 39 deletions

127
format.c
View File

@ -100,6 +100,7 @@ format_job_cmp(struct format_job *fj1, struct format_job *fj2)
#define FORMAT_SESSIONS 0x80
#define FORMAT_WINDOWS 0x100
#define FORMAT_PANES 0x200
#define FORMAT_PRETTY 0x400
/* Limit on recursion. */
#define FORMAT_LOOP_LIMIT 10
@ -1322,6 +1323,52 @@ format_quote(const char *s)
return (out);
}
/* Make a prettier time. */
static char *
format_pretty_time(time_t t)
{
struct tm now_tm, tm;
time_t now, age;
char s[6];
int m;
time(&now);
if (now < t)
now = t;
age = now - t;
localtime_r(&now, &now_tm);
localtime_r(&t, &tm);
/* Last 24 hours. */
if (age < 24 * 3600) {
strftime(s, sizeof s, "%H:%M", &tm);
return (xstrdup(s));
}
/* This month or last 28 days. */
if ((tm.tm_year == now_tm.tm_year && tm.tm_mon == now_tm.tm_mon) ||
age < 28 * 24 * 3600) {
strftime(s, sizeof s, "%a%d", &tm);
return (xstrdup(s));
}
/* Last 12 months. */
if (now_tm.tm_mon == 0)
m = 11;
else
m = now_tm.tm_mon - 1;
if ((tm.tm_year == now_tm.tm_year && tm.tm_mon < now_tm.tm_mon) ||
(tm.tm_year == now_tm.tm_year - 1 && tm.tm_mon > now_tm.tm_mon)) {
strftime(s, sizeof s, "%d%b", &tm);
return (xstrdup(s));
}
/* Older than that. */
strftime(s, sizeof s, "%h%y", &tm);
return (xstrdup(s));
}
/* Find a format entry. */
static char *
format_find(struct format_tree *ft, const char *key, int modifiers)
@ -1331,40 +1378,31 @@ format_find(struct format_tree *ft, const char *key, int modifiers)
static char s[64];
struct options_entry *o;
int idx;
char *found, *saved;
char *found = NULL, *saved;
const char *errstr;
time_t t = 0;
if (~modifiers & FORMAT_TIMESTRING) {
o = options_parse_get(global_options, key, &idx, 0);
if (o == NULL && ft->wp != NULL)
o = options_parse_get(ft->wp->options, key, &idx, 0);
if (o == NULL && ft->w != NULL)
o = options_parse_get(ft->w->options, key, &idx, 0);
if (o == NULL)
o = options_parse_get(global_w_options, key, &idx, 0);
if (o == NULL && ft->s != NULL)
o = options_parse_get(ft->s->options, key, &idx, 0);
if (o == NULL)
o = options_parse_get(global_s_options, key, &idx, 0);
if (o != NULL) {
found = options_tostring(o, idx, 1);
goto found;
}
o = options_parse_get(global_options, key, &idx, 0);
if (o == NULL && ft->wp != NULL)
o = options_parse_get(ft->wp->options, key, &idx, 0);
if (o == NULL && ft->w != NULL)
o = options_parse_get(ft->w->options, key, &idx, 0);
if (o == NULL)
o = options_parse_get(global_w_options, key, &idx, 0);
if (o == NULL && ft->s != NULL)
o = options_parse_get(ft->s->options, key, &idx, 0);
if (o == NULL)
o = options_parse_get(global_s_options, key, &idx, 0);
if (o != NULL) {
found = options_tostring(o, idx, 1);
goto found;
}
found = NULL;
fe_find.key = (char *) key;
fe_find.key = (char *)key;
fe = RB_FIND(format_entry_tree, &ft->tree, &fe_find);
if (fe != NULL) {
if (modifiers & FORMAT_TIMESTRING) {
if (fe->t == 0)
return (NULL);
ctime_r(&fe->t, s);
s[strcspn(s, "\n")] = '\0';
found = xstrdup(s);
goto found;
}
if (fe->t != 0) {
xasprintf(&found, "%lld", (long long)fe->t);
t = fe->t;
goto found;
}
if (fe->value == NULL && fe->cb != NULL)
@ -1390,7 +1428,28 @@ format_find(struct format_tree *ft, const char *key, int modifiers)
return (NULL);
found:
if (found == NULL)
if (modifiers & FORMAT_TIMESTRING) {
if (t == 0 && found != NULL) {
t = strtonum(found, 0, INT64_MAX, &errstr);
if (errstr != NULL)
t = 0;
free(found);
}
if (t == 0)
return (NULL);
if (modifiers & FORMAT_PRETTY)
found = format_pretty_time(t);
else {
ctime_r(&t, s);
s[strcspn(s, "\n")] = '\0';
found = xstrdup(s);
}
return (found);
}
if (t != 0)
xasprintf(&found, "%lld", (long long)t);
else if (found == NULL)
return (NULL);
if (modifiers & FORMAT_BASENAME) {
saved = found;
@ -1532,7 +1591,7 @@ format_build_modifiers(struct format_tree *ft, const char **s, u_int *count)
cp++;
/* Check single character modifiers with no arguments. */
if (strchr("lbdtqETSWP<>", cp[0]) != NULL &&
if (strchr("lbdqETSWP<>", cp[0]) != NULL &&
format_is_end(cp[1])) {
format_add_modifier(&list, count, cp, 1, NULL, 0);
cp++;
@ -1553,7 +1612,7 @@ format_build_modifiers(struct format_tree *ft, const char **s, u_int *count)
}
/* Now try single character with arguments. */
if (strchr("mCs=pe", cp[0]) == NULL)
if (strchr("mCst=pe", cp[0]) == NULL)
break;
c = cp[0];
@ -1978,7 +2037,7 @@ format_replace(struct format_tree *ft, const char *key, size_t keylen,
case 'e':
if (fm->argc < 1 || fm->argc > 3)
break;
mexp = fm;
mexp = fm;
break;
case 'l':
modifiers |= FORMAT_LITERAL;
@ -1991,6 +2050,10 @@ format_replace(struct format_tree *ft, const char *key, size_t keylen,
break;
case 't':
modifiers |= FORMAT_TIMESTRING;
if (fm->argc < 1)
break;
if (strchr(fm->argv[0], 'p') != NULL)
modifiers |= FORMAT_PRETTY;
break;
case 'q':
modifiers |= FORMAT_QUOTE;

4
tmux.1
View File

@ -4353,6 +4353,10 @@ gives
.Ql #{t:window_activity}
gives
.Ql Sun Oct 25 09:25:02 2015 .
Adding
.Ql p (
.Ql `t/p` )
will use shorter but less accurate time format for times in the past.
The
.Ql b:\&
and

View File

@ -37,7 +37,7 @@ static void window_buffer_key(struct window_mode_entry *,
#define WINDOW_BUFFER_DEFAULT_COMMAND "paste-buffer -b '%%'"
#define WINDOW_BUFFER_DEFAULT_FORMAT \
"#{buffer_size} bytes (#{t:buffer_created})"
"#{t/p:buffer_created}: #{buffer_sample}"
static const struct menu_item window_buffer_menu_items[] = {
{ "Paste", 'p', NULL },

View File

@ -37,8 +37,7 @@ static void window_client_key(struct window_mode_entry *,
#define WINDOW_CLIENT_DEFAULT_COMMAND "detach-client -t '%%'"
#define WINDOW_CLIENT_DEFAULT_FORMAT \
"session #{session_name} " \
"(#{client_width}x#{client_height}, #{t:client_activity})"
"#{t/p:client_activity}: session #{session_name}"
static const struct menu_item window_client_menu_items[] = {
{ "Detach", 'd', NULL },

View File

@ -38,13 +38,13 @@ static void window_tree_key(struct window_mode_entry *,
#define WINDOW_TREE_DEFAULT_FORMAT \
"#{?pane_format," \
"#{?pane_marked,#[reverse],}" \
"#{pane_current_command}#{?pane_active,*,}#{?pane_marked,M,} \"#{pane_title}\"" \
"#{pane_current_command}#{?pane_active,*,}#{?pane_marked,M,}" \
"#{?#{&&:#{pane_title},#{!=:#{pane_title},#{host_short}}},: \"#{pane_title}\",}" \
"," \
"#{?window_format," \
"#{?window_marked_flag,#[reverse],}" \
"#{window_name}#{window_flags} " \
"(#{window_panes} panes)" \
"#{?#{==:#{window_panes},1}, \"#{pane_title}\",}" \
"#{window_name}#{window_flags}" \
"#{?#{&&:#{==:#{window_panes},1},#{&&:#{pane_title},#{!=:#{pane_title},#{host_short}}}},: \"#{pane_title}\",}" \
"," \
"#{session_windows} windows" \
"#{?session_grouped, " \