Skip to content

Commit

Permalink
Shell: Suggest aliases when completing program names :^)
Browse files Browse the repository at this point in the history
  • Loading branch information
alimpfard authored and awesomekling committed Jul 7, 2020
1 parent 8ab601f commit 173effc
Showing 1 changed file with 25 additions and 18 deletions.
43 changes: 25 additions & 18 deletions Shell/Shell.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -631,27 +631,34 @@ void Shell::cache_path()
if (!cached_path.is_empty())
cached_path.clear_with_capacity();

String path = getenv("PATH");
if (path.is_empty())
return;
// Add shell builtins to the cache.
for (const auto& builtin_name : builtin_names)
cached_path.append(escape_token(builtin_name));

auto directories = path.split(':');
for (const auto& directory : directories) {
Core::DirIterator programs(directory.characters(), Core::DirIterator::SkipDots);
while (programs.has_next()) {
auto program = programs.next_path();
String program_path = String::format("%s/%s", directory.characters(), program.characters());
auto escaped_name = escape_token(program);
if (cached_path.contains_slow(escaped_name))
continue;
if (access(program_path.characters(), X_OK) == 0)
cached_path.append(escaped_name);
}
// Add aliases to the cache.
for (const auto& alias : m_aliases) {
auto name = escape_token(alias.key);
if (cached_path.contains_slow(name))
continue;
cached_path.append(name);
}

// add shell builtins to the cache
for (const auto& builtin_name : builtin_names)
cached_path.append(escape_token(builtin_name));
String path = getenv("PATH");
if (!path.is_empty()) {
auto directories = path.split(':');
for (const auto& directory : directories) {
Core::DirIterator programs(directory.characters(), Core::DirIterator::SkipDots);
while (programs.has_next()) {
auto program = programs.next_path();
String program_path = String::format("%s/%s", directory.characters(), program.characters());
auto escaped_name = escape_token(program);
if (cached_path.contains_slow(escaped_name))
continue;
if (access(program_path.characters(), X_OK) == 0)
cached_path.append(escaped_name);
}
}
}

quick_sort(cached_path);
}
Expand Down

0 comments on commit 173effc

Please sign in to comment.