m_property: add a way to switch on property values in property expansion

Allows for example: --status-msg='${?pause==yes:(Paused) } ...' to
emulate the normal terminal status line. It's useful in other situations
too.

I'm a bit worried about extending this mini-DSL, and sure hope nobody
will implement a generic formula evaluator at some point in the future.
But for now we're probably safe.
This commit is contained in:
wm4 2013-09-20 22:53:06 +02:00
parent ab706f9969
commit f988c63003
2 changed files with 24 additions and 6 deletions

View File

@ -509,15 +509,27 @@ The following expansions are supported:
fails, expand to an error string. (Use ``${NAME:}`` with a trailing fails, expand to an error string. (Use ``${NAME:}`` with a trailing
``:`` to expand to an empty string instead.) ``:`` to expand to an empty string instead.)
If ``NAME`` is prefixed with ``=``, expand to the raw value of the property If ``NAME`` is prefixed with ``=``, expand to the raw value of the property
(see below). (see section below).
``${NAME:STR}`` ``${NAME:STR}``
Expands to the value of the property ``NAME``, or ``STR`` if the Expands to the value of the property ``NAME``, or ``STR`` if the
property cannot be retrieved. ``STR`` is expanded recursively. property cannot be retrieved. ``STR`` is expanded recursively.
``${?NAME:STR}``
Expands to ``STR`` (recursively) if the property ``NAME`` is available.
``${!NAME:STR}`` ``${!NAME:STR}``
Expands to ``STR`` (recursively) if the property ``NAME`` cannot be Expands to ``STR`` (recursively) if the property ``NAME`` cannot be
retrieved. retrieved.
``${?NAME:STR}`` ``${?NAME==VALUE:STR}``
Expands to ``STR`` (recursively) if the property ``NAME`` is available. Expands to ``STR`` (recursively) if the property ``NAME`` expands to a
string equal to ``VALUE``. You can prefix ``NAME`` with ``=`` in order to
compare the raw value of a property (see section below). If the property
is unavailable, or other errors happen when retrieving it, the value is
never considered equal.
Note that ``VALUE`` can't contain any of the characters ``:`` or ``}``.
Also, it is possible that escaping with ``"`` or ``%`` might be added in
the future, should the need arise.
``${!NAME==VALUE:STR}``
Same as with the ``?`` variant, but ``STR`` is expanded if the value is
not equal. (Using the same semantics as with ``?``.)
``$$`` ``$$``
Expands to ``$``. Expands to ``$``.
``$}`` ``$}``

View File

@ -219,14 +219,20 @@ static int expand_property(const m_option_t *prop_list, char **ret, int *ret_len
{ {
bool cond_yes = bstr_eatstart0(&prop, "?"); bool cond_yes = bstr_eatstart0(&prop, "?");
bool cond_no = !cond_yes && bstr_eatstart0(&prop, "!"); bool cond_no = !cond_yes && bstr_eatstart0(&prop, "!");
bool test = cond_yes || cond_no;
bool raw = bstr_eatstart0(&prop, "="); bool raw = bstr_eatstart0(&prop, "=");
int method = bstr comp_with = {0};
(raw || cond_yes || cond_no) ? M_PROPERTY_GET_STRING : M_PROPERTY_PRINT; bool comp = test && bstr_split_tok(prop, "==", &prop, &comp_with);
if (test && !comp)
raw = true;
int method = raw ? M_PROPERTY_GET_STRING : M_PROPERTY_PRINT;
char *s = NULL; char *s = NULL;
int r = m_property_do_bstr(prop_list, prop, method, &s, ctx); int r = m_property_do_bstr(prop_list, prop, method, &s, ctx);
bool skip; bool skip;
if (cond_yes || cond_no) { if (comp) {
skip = ((s && bstr_equals0(comp_with, s)) != cond_yes);
} else if (test) {
skip = (!!s != cond_yes); skip = (!!s != cond_yes);
} else { } else {
skip = !!s; skip = !!s;