summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authornicm <nicm@openbsd.org>2020-08-20 16:57:40 +0000
committernicm <nicm@openbsd.org>2020-08-20 16:57:40 +0000
commit8d34ed161955d5b3c94717cbdbeb4fb702afa0d1 (patch)
treee6fa694e9e4bc13004a74b05354caa71e3dd9797
parentFix build without NPCKBC and NUKBD (diff)
downloadwireguard-openbsd-8d34ed161955d5b3c94717cbdbeb4fb702afa0d1.tar.xz
wireguard-openbsd-8d34ed161955d5b3c94717cbdbeb4fb702afa0d1.zip
Add n: modifier to get length of a format, also automatically expand
variable name arguments again if they contain a #{.
-rw-r--r--usr.bin/tmux/format.c39
-rw-r--r--usr.bin/tmux/tmux.17
2 files changed, 32 insertions, 14 deletions
diff --git a/usr.bin/tmux/format.c b/usr.bin/tmux/format.c
index ee760f23f31..b78baf4e609 100644
--- a/usr.bin/tmux/format.c
+++ b/usr.bin/tmux/format.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: format.c,v 1.259 2020/06/23 05:23:26 nicm Exp $ */
+/* $OpenBSD: format.c,v 1.260 2020/08/20 16:57:40 nicm Exp $ */
/*
* Copyright (c) 2011 Nicholas Marriott <nicholas.marriott@gmail.com>
@@ -94,6 +94,7 @@ format_job_cmp(struct format_job *fj1, struct format_job *fj2)
#define FORMAT_WINDOWS 0x100
#define FORMAT_PANES 0x200
#define FORMAT_PRETTY 0x400
+#define FORMAT_LENGTH 0x800
/* Limit on recursion. */
#define FORMAT_LOOP_LIMIT 10
@@ -1647,7 +1648,7 @@ format_build_modifiers(struct format_tree *ft, const char **s, u_int *count)
/*
* Modifiers are a ; separated list of the forms:
- * l,m,C,b,d,t,q,E,T,S,W,P,<,>
+ * l,m,C,b,d,n,t,q,E,T,S,W,P,<,>
* =a
* =/a
* =/a/
@@ -1664,7 +1665,7 @@ format_build_modifiers(struct format_tree *ft, const char **s, u_int *count)
cp++;
/* Check single character modifiers with no arguments. */
- if (strchr("lbdqETSWP<>", cp[0]) != NULL &&
+ if (strchr("lbdnqETSWP<>", cp[0]) != NULL &&
format_is_end(cp[1])) {
format_add_modifier(&list, count, cp, 1, NULL, 0);
cp++;
@@ -2122,6 +2123,9 @@ format_replace(struct format_tree *ft, const char *key, size_t keylen,
case 'd':
modifiers |= FORMAT_DIRNAME;
break;
+ case 'n':
+ modifiers |= FORMAT_LENGTH;
+ break;
case 't':
modifiers |= FORMAT_TIMESTRING;
if (fm->argc < 1)
@@ -2301,13 +2305,17 @@ format_replace(struct format_tree *ft, const char *key, size_t keylen,
if (value == NULL)
value = xstrdup("");
} else {
- /* Neither: look up directly. */
- value = format_find(ft, copy, modifiers, time_format);
- if (value == NULL) {
- format_log(ft, "format '%s' not found", copy);
- value = xstrdup("");
- } else
- format_log(ft, "format '%s' found: %s", copy, value);
+ if (strstr(copy, "#{") != 0) {
+ format_log(ft, "expanding inner format '%s'", copy);
+ value = format_expand(ft, copy);
+ } else {
+ value = format_find(ft, copy, modifiers, time_format);
+ if (value == NULL) {
+ format_log(ft, "format '%s' not found", copy);
+ value = xstrdup("");
+ } else
+ format_log(ft, "format '%s' found: %s", copy, value);
+ }
}
done:
@@ -2316,8 +2324,7 @@ done:
new = format_expand(ft, value);
free(value);
value = new;
- }
- else if (modifiers & FORMAT_EXPANDTIME) {
+ } else if (modifiers & FORMAT_EXPANDTIME) {
new = format_expand_time(ft, value);
free(value);
value = new;
@@ -2371,6 +2378,14 @@ done:
format_log(ft, "applied padding width %d: %s", width, value);
}
+ /* Replace with the length if needed. */
+ if (modifiers & FORMAT_LENGTH) {
+ xasprintf(&new, "%zu", strlen(value));
+ free(value);
+ value = new;
+ format_log(ft, "replacing with length: %s", new);
+ }
+
/* Expand the buffer and copy in the value. */
valuelen = strlen(value);
while (*len - *off < valuelen + 1) {
diff --git a/usr.bin/tmux/tmux.1 b/usr.bin/tmux/tmux.1
index fd5782a5d15..9725182624d 100644
--- a/usr.bin/tmux/tmux.1
+++ b/usr.bin/tmux/tmux.1
@@ -1,4 +1,4 @@
-.\" $OpenBSD: tmux.1,v 1.790 2020/07/27 08:03:10 nicm Exp $
+.\" $OpenBSD: tmux.1,v 1.791 2020/08/20 16:57:40 nicm Exp $
.\"
.\" Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com>
.\"
@@ -14,7 +14,7 @@
.\" IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
.\" OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
.\"
-.Dd $Mdocdate: July 27 2020 $
+.Dd $Mdocdate: August 20 2020 $
.Dt TMUX 1
.Os
.Sh NAME
@@ -4574,6 +4574,9 @@ pads the string to a given width, for example
.Ql #{p10:pane_title}
will result in a width of at least 10 characters.
A positive width pads on the left, a negative on the right.
+.Ql n
+expands to the length of the variable, for example
+.Ql #{n:window_name} .
.Pp
Prefixing a time variable with
.Ql t:\&