linenoise
linenoise copied to clipboard
Auto-complete files
Is it possible to auto-complete file names on the file system? This is possible with readline and libedit.
This works at least on Linux.
#include <sys/types.h>
#include <dirent.h>
#include <string.h>
#include "linenoise.h"
void completion(const char* buf, linenoiseCompletions* lc)
{
DIR* dirp;
struct dirent* entry;
if (!(dirp = opendir(".")))
{
return;
}
while ((entry = readdir(dirp)))
{
if (!strcmp(entry->d_name, ".") || !strcmp(entry->d_name, ".."))
{
continue;
}
if (strlen(buf) > strlen(entry->d_name))
{
continue;
}
if (!strncmp(buf, entry->d_name, strlen(buf)))
{
linenoiseAddCompletion(lc, entry->d_name);
}
}
closedir(dirp);
}
int main(int argc, char** argv)
{
char* line;
linenoiseSetCompletionCallback(completion);
while ((line = linenoise("> ")))
{
linenoiseFree(line);
}
return 0;
}
@RauliL Thank you! This is not as good as readline (e.g. files that are not in current directory), but it is something.
It's a simple thing that I just quickly wrote which you can use as a start and extend it.
@RauliL I could do that. But don't you think that a library that claims to be a replacement for readline should do it?
My personal opinion is no because linenoise is meant to be lightweight alternative to readline and libedit. Users who need such feature can easily implement it themselves.
@RauliL If it is easy to implement, then I welcome you to do it. Windows + Linux support. Show list of possible files on tab. Any file paths work.
Being lightweight is not an excuse. The library can have those extra features as optional. As it stands, it does not replace readline in my project, therefore it is not an alternative to readline.