cli: add support for partial completion with separator character

Useful for completing long lists of possible values with common prefix

Signed-off-by: Felix Fietkau <nbd@nbd.name>
This commit is contained in:
Felix Fietkau 2025-02-24 21:22:52 +01:00
parent 04ecccf3e9
commit 895b4e7caf
2 changed files with 34 additions and 2 deletions

View File

@ -311,7 +311,10 @@ function completion(count) {
add = " ";
cat = "";
add += sprintf("%-"+len+"s", entry.name);
let name = entry.name;
if (entry.incomplete)
name += "...";
add += sprintf("%-"+len+"s", name);
str += add;
x += length(add);

View File

@ -355,7 +355,36 @@ function complete_arg_list(e, ctx, arg_info, args, base_args, named_args)
for (let i = 0; i <= cur_idx; i++)
val = shift(args);
return complete_param(e, ctx, cur, val, base_args, named_args);
let ret = complete_param(e, ctx, cur, val, base_args, named_args);
if (!cur.prefix_separator)
return ret;
let prefix_len = length(val);
let vals = [];
let match_prefix;
for (let cur_val in ret.value) {
let cur_str = cur_val.name;
let cur_suffix = substr(cur_str, prefix_len);
let idx = index(cur_suffix, cur.prefix_separator);
if (idx < 0) {
push(vals, cur_val);
continue;
}
let cur_prefix = substr(cur_str, 0, prefix_len + idx + 1);
if (cur_prefix == match_prefix)
continue;
match_prefix = cur_prefix;
push(vals, {
...cur_val,
name: cur_prefix,
incomplete: true
});
}
ret.value = vals;
return ret;
}
function handle_empty_param(entry, spec, name, argv, named_args)