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

Shell: Auto-completion shouldn't suggest non-executable files for the program name

This commit is contained in:
Gunnar Beutner 2021-04-20 16:17:38 +02:00 committed by Andreas Kling
parent e45e0eeb47
commit 111ac4b1f4
3 changed files with 13 additions and 7 deletions

View file

@ -1304,7 +1304,8 @@ Vector<Line::CompletionSuggestion> Shell::complete()
return ast->complete_for_editor(*this, line.length());
}
Vector<Line::CompletionSuggestion> Shell::complete_path(const String& base, const String& part, size_t offset)
Vector<Line::CompletionSuggestion> Shell::complete_path(const String& base,
const String& part, size_t offset, ExecutableOnly executable_only)
{
auto token = offset ? part.substring_view(0, offset) : "";
String path;
@ -1358,7 +1359,7 @@ Vector<Line::CompletionSuggestion> Shell::complete_path(const String& base, cons
struct stat program_status;
String file_path = String::format("%s/%s", path.characters(), file.characters());
int stat_error = stat(file_path.characters(), &program_status);
if (!stat_error) {
if (!stat_error && (executable_only == ExecutableOnly::No || access(file_path.characters(), X_OK) == 0)) {
if (S_ISDIR(program_status.st_mode)) {
suggestions.append({ escape_token(file), "/" });
} else {
@ -1381,7 +1382,7 @@ Vector<Line::CompletionSuggestion> Shell::complete_program_name(const String& na
[](auto& name, auto& program) { return strncmp(name.characters(), program.characters(), name.length()); });
if (!match)
return complete_path("", name, offset);
return complete_path("", name, offset, ExecutableOnly::Yes);
String completion = *match;
auto token_length = escape_token(name).length();