1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 07:48:11 +00:00

Shell: Cache PATH for faster tab completion

This patch reduces the O(n) tab completion to something like O(log(n)).
The cache is just a sorted vector of strings and we binary search it to
get a string matching our input, and then check the surrounding strings
to see if we need to remove any characters. Also we no longer stat each
file every time.

Also added an #include in BinarySearch since it was using size_t. Oops.

If `export` is called, we recache. Need to implement the `hash` builtin
for when an executable has been added to a directory in PATH.
This commit is contained in:
William McPherson 2019-12-03 17:36:46 +11:00 committed by Andreas Kling
parent 431abc8846
commit aa8b40dce6
4 changed files with 58 additions and 30 deletions

View file

@ -64,7 +64,12 @@ static int sh_export(int argc, char** argv)
return 1;
}
return setenv(parts[0].characters(), parts[1].characters(), 1);
int setenv_return = setenv(parts[0].characters(), parts[1].characters(), 1);
if (setenv_return == 0 && parts[0] == "PATH")
editor.cache_path();
return setenv_return;
}
static int sh_unset(int argc, char** argv)
@ -910,6 +915,8 @@ int main(int argc, char** argv)
load_history();
atexit(save_history);
editor.cache_path();
for (;;) {
auto line = editor.get_line(prompt());
if (line.is_empty())