summaryrefslogtreecommitdiff
path: root/usr.bin/tmux/format.c
diff options
context:
space:
mode:
authorNicholas Marriott <nicm@cvs.openbsd.org>2020-05-16 14:10:30 +0000
committerNicholas Marriott <nicm@cvs.openbsd.org>2020-05-16 14:10:30 +0000
commit565144c4220108a293dac7b85b2bdc71b277e872 (patch)
tree2469c434a471f154fb1e038c90e05aa3670f5c21 /usr.bin/tmux/format.c
parent606370526fe111db397b0695d93d31bc67073f3d (diff)
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.
Diffstat (limited to 'usr.bin/tmux/format.c')
-rw-r--r--usr.bin/tmux/format.c131
1 files changed, 97 insertions, 34 deletions
diff --git a/usr.bin/tmux/format.c b/usr.bin/tmux/format.c
index 319e69e7712..ac561eaa843 100644
--- a/usr.bin/tmux/format.c
+++ b/usr.bin/tmux/format.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: format.c,v 1.247 2020/04/22 20:47:00 nicm Exp $ */
+/* $OpenBSD: format.c,v 1.248 2020/05/16 14:10:29 nicm Exp $ */
/*
* Copyright (c) 2011 Nicholas Marriott <nicholas.marriott@gmail.com>
@@ -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;
-
- 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;
- }
+ char *found = NULL, *saved;
+ const char *errstr;
+ time_t t = 0;
+
+ 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;