linenoise icon indicating copy to clipboard operation
linenoise copied to clipboard

Auto-complete files

Open MangaD opened this issue 4 years ago • 6 comments

Is it possible to auto-complete file names on the file system? This is possible with readline and libedit.

MangaD avatar Nov 02 '21 16:11 MangaD

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 avatar Nov 04 '21 07:11 RauliL

@RauliL Thank you! This is not as good as readline (e.g. files that are not in current directory), but it is something.

MangaD avatar Nov 04 '21 08:11 MangaD

It's a simple thing that I just quickly wrote which you can use as a start and extend it.

RauliL avatar Nov 04 '21 11:11 RauliL

@RauliL I could do that. But don't you think that a library that claims to be a replacement for readline should do it?

MangaD avatar Nov 04 '21 12:11 MangaD

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 avatar Nov 04 '21 12:11 RauliL

@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.

MangaD avatar Nov 04 '21 12:11 MangaD