1
0
mirror of https://github.com/mpv-player/mpv synced 2025-01-18 04:51:52 +00:00

options: add string list -toggle action

This commit is contained in:
wm4 2018-01-24 05:12:05 +01:00 committed by Kevin Mitchell
parent 415fc6e327
commit 3deef308c8
2 changed files with 25 additions and 0 deletions

View File

@ -435,6 +435,7 @@ Suffix Meaning
-del Delete an existing item by integer index
-pre Prepend 1 or more items
-set Set a list of items
-toggle Append an item, or remove if if it already exists
============= ===============================================
Although some operations allow specifying multiple ``,``-separated items, using

View File

@ -1318,6 +1318,15 @@ static struct bstr get_nextsep(struct bstr *ptr, char sep, bool modify)
return bstr_splice(orig, 0, str.start - orig.start);
}
static int find_list_bstr(char **list, bstr item)
{
for (int n = 0; list && list[n]; n++) {
if (bstr_equals0(item, list[n]))
return n;
}
return -1;
}
static int parse_str_list_impl(struct mp_log *log, const m_option_t *opt,
struct bstr name, struct bstr param, void *dst,
int default_op)
@ -1339,6 +1348,20 @@ static int parse_str_list_impl(struct mp_log *log, const m_option_t *opt,
op = OP_CLR;
} else if (bstr_endswith0(name, "-set")) {
op = OP_NONE;
} else if (bstr_endswith0(name, "-toggle")) {
if (dst) {
char **list = VAL(dst);
int index = find_list_bstr(list, param);
if (index >= 0) {
char *old = list[index];
for (int n = index; list[n]; n++)
list[n] = list[n + 1];
talloc_free(old);
return 1;
}
}
op = OP_ADD;
multi = false;
}
// Clear the list ??
@ -1510,6 +1533,7 @@ const m_option_type_t m_option_type_string_list = {
{"del"},
{"pre"},
{"set"},
{"toggle"},
{0}
},
};