1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-25 22:35:07 +00:00

Shell: Initial support for 'option' completions

Take one small step towards #2357.
Handle completing barewords starting with '-' by piping the requests to
the Shell::complete_option(program_name, option) :^)

Also implements completion for a single builtin (setopt) until we figure out how
to handle #2357.
This commit is contained in:
AnotherTest 2020-06-29 06:26:06 +04:30 committed by Andreas Kling
parent b8d1edb2a2
commit ff857cd358
5 changed files with 162 additions and 31 deletions

View file

@ -828,6 +828,48 @@ Vector<Line::CompletionSuggestion> Shell::complete_user(const String& name, size
return suggestions;
}
Vector<Line::CompletionSuggestion> Shell::complete_option(const String& program_name, const String& option, size_t offset)
{
size_t start = 0;
while (start < option.length() && option[start] == '-')
++start;
auto option_pattern = offset > start ? option.substring_view(start, offset - start) : "";
editor->suggest(offset);
Vector<Line::CompletionSuggestion> suggestions;
dbg() << "Shell::complete_option(" << program_name << ", " << option_pattern << ")";
// FIXME: Figure out how to do this stuff.
if (has_builtin(program_name)) {
// Complete builtins.
if (program_name == "setopt") {
bool negate = false;
if (option_pattern.starts_with("no_")) {
negate = true;
option_pattern = option_pattern.substring_view(3, option_pattern.length() - 3);
}
auto maybe_negate = [&](const StringView& view) {
static StringBuilder builder;
builder.clear();
builder.append("--");
if (negate)
builder.append("no_");
builder.append(view);
return builder.to_string();
};
#define __ENUMERATE_SHELL_OPTION(name, d_, descr_) \
if (StringView { #name }.starts_with(option_pattern)) \
suggestions.append(maybe_negate(#name));
ENUMERATE_SHELL_OPTIONS();
#undef __ENUMERATE_SHELL_OPTION
return suggestions;
}
}
return suggestions;
}
bool Shell::read_single_line()
{
take_back_stdin();