From c7732cd3594e2cc46e52c58451dd644e08dbf904 Mon Sep 17 00:00:00 2001 From: nicm Date: Mon, 25 Oct 2021 21:21:16 +0000 Subject: [PATCH] Add a way to force a colour to RGB and a format to display it. --- usr.bin/tmux/colour.c | 17 ++++++++++++++++- usr.bin/tmux/format.c | 26 +++++++++++++++++++++----- usr.bin/tmux/tmux.1 | 6 +++++- usr.bin/tmux/tmux.h | 3 ++- 4 files changed, 44 insertions(+), 8 deletions(-) diff --git a/usr.bin/tmux/colour.c b/usr.bin/tmux/colour.c index e84e9031ae7..967333b2eb5 100644 --- a/usr.bin/tmux/colour.c +++ b/usr.bin/tmux/colour.c @@ -1,4 +1,4 @@ -/* $OpenBSD: colour.c,v 1.22 2021/08/14 14:00:07 nicm Exp $ */ +/* $OpenBSD: colour.c,v 1.23 2021/10/25 21:21:16 nicm Exp $ */ /* * Copyright (c) 2008 Nicholas Marriott @@ -105,6 +105,21 @@ colour_split_rgb(int c, u_char *r, u_char *g, u_char *b) *b = c & 0xff; } +/* Force colour to RGB if not already. */ +int +colour_force_rgb(int c) +{ + if (c & COLOUR_FLAG_RGB) + return (c); + if (c & COLOUR_FLAG_256) + return (colour_256toRGB(c)); + if (c >= 0 && c <= 7) + return (colour_256toRGB(c)); + if (c >= 90 & c <= 97) + return (colour_256toRGB(8 + c - 90)); + return (-1); +} + /* Convert colour to a string. */ const char * colour_tostring(int c) diff --git a/usr.bin/tmux/format.c b/usr.bin/tmux/format.c index cb92fdd639e..ea6cd40844c 100644 --- a/usr.bin/tmux/format.c +++ b/usr.bin/tmux/format.c @@ -1,4 +1,4 @@ -/* $OpenBSD: format.c,v 1.298 2021/10/11 10:55:30 nicm Exp $ */ +/* $OpenBSD: format.c,v 1.299 2021/10/25 21:21:16 nicm Exp $ */ /* * Copyright (c) 2011 Nicholas Marriott @@ -101,6 +101,7 @@ format_job_cmp(struct format_job *fj1, struct format_job *fj2) #define FORMAT_WINDOW_NAME 0x4000 #define FORMAT_SESSION_NAME 0x8000 #define FORMAT_CHARACTER 0x10000 +#define FORMAT_COLOUR 0x20000 /* Limit on recursion. */ #define FORMAT_LOOP_LIMIT 100 @@ -3555,7 +3556,7 @@ format_build_modifiers(struct format_expand_state *es, const char **s, /* * Modifiers are a ; separated list of the forms: - * l,m,C,a,b,d,n,t,w,q,E,T,S,W,P,<,> + * l,m,C,a,b,c,d,n,t,w,q,E,T,S,W,P,<,> * =a * =/a * =/a/ @@ -3572,7 +3573,7 @@ format_build_modifiers(struct format_expand_state *es, const char **s, cp++; /* Check single character modifiers with no arguments. */ - if (strchr("labdnwETSWP<>", cp[0]) != NULL && + if (strchr("labcdnwETSWP<>", cp[0]) != NULL && format_is_end(cp[1])) { format_add_modifier(&list, count, cp, 1, NULL, 0); cp++; @@ -4052,10 +4053,10 @@ format_replace(struct format_expand_state *es, const char *key, size_t keylen, const char *errstr, *copy, *cp, *marker = NULL; const char *time_format = NULL; char *copy0, *condition, *found, *new; - char *value, *left, *right, c; + char *value, *left, *right; size_t valuelen; int modifiers = 0, limit = 0, width = 0; - int j; + int j, c; struct format_modifier *list, *cmp = NULL, *search = NULL; struct format_modifier **sub = NULL, *mexp = NULL, *fm; u_int i, count, nsub = 0; @@ -4126,6 +4127,9 @@ format_replace(struct format_expand_state *es, const char *key, size_t keylen, case 'b': modifiers |= FORMAT_BASENAME; break; + case 'c': + modifiers |= FORMAT_COLOUR; + break; case 'd': modifiers |= FORMAT_DIRNAME; break; @@ -4201,6 +4205,18 @@ format_replace(struct format_expand_state *es, const char *key, size_t keylen, goto done; } + /* Is this a colour? */ + if (modifiers & FORMAT_COLOUR) { + new = format_expand1(es, copy); + c = colour_fromstring(new); + if (c == -1 || (c = colour_force_rgb(c)) == -1) + value = xstrdup(""); + else + xasprintf(&value, "%06x", c & 0xffffff); + free(new); + goto done; + } + /* Is this a loop, comparison or condition? */ if (modifiers & FORMAT_SESSIONS) { value = format_loop_sessions(es, copy); diff --git a/usr.bin/tmux/tmux.1 b/usr.bin/tmux/tmux.1 index a91e0942b1f..d0107c43a01 100644 --- a/usr.bin/tmux/tmux.1 +++ b/usr.bin/tmux/tmux.1 @@ -1,4 +1,4 @@ -.\" $OpenBSD: tmux.1,v 1.866 2021/10/25 20:32:42 nicm Exp $ +.\" $OpenBSD: tmux.1,v 1.867 2021/10/25 21:21:16 nicm Exp $ .\" .\" Copyright (c) 2007 Nicholas Marriott .\" @@ -4831,6 +4831,10 @@ replaces a numeric argument by its ASCII equivalent, so .Ql #{a:98} results in .Ql b . +.Ql c +replaces a +.Nm +colour by its six-digit hexadecimal RGB value. .Pp A limit may be placed on the length of the resultant string by prefixing it by an diff --git a/usr.bin/tmux/tmux.h b/usr.bin/tmux/tmux.h index 93ad82f2115..f6a9d44b2bc 100644 --- a/usr.bin/tmux/tmux.h +++ b/usr.bin/tmux/tmux.h @@ -1,4 +1,4 @@ -/* $OpenBSD: tmux.h,v 1.1149 2021/10/25 09:38:36 nicm Exp $ */ +/* $OpenBSD: tmux.h,v 1.1150 2021/10/25 21:21:16 nicm Exp $ */ /* * Copyright (c) 2007 Nicholas Marriott @@ -2616,6 +2616,7 @@ int input_key_get_mouse(struct screen *, struct mouse_event *, u_int, int colour_find_rgb(u_char, u_char, u_char); int colour_join_rgb(u_char, u_char, u_char); void colour_split_rgb(int, u_char *, u_char *, u_char *); +int colour_force_rgb(int); const char *colour_tostring(int); int colour_fromstring(const char *s); int colour_256toRGB(int); -- 2.20.1