Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Auto-complete files #199

Open
MangaD opened this issue Nov 2, 2021 · 6 comments
Open

Auto-complete files #199

MangaD opened this issue Nov 2, 2021 · 6 comments

Comments

@MangaD
Copy link

MangaD commented Nov 2, 2021

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

@RauliL
Copy link

RauliL commented Nov 4, 2021

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;
}

@MangaD
Copy link
Author

MangaD commented Nov 4, 2021

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

@RauliL
Copy link

RauliL commented Nov 4, 2021

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

@MangaD
Copy link
Author

MangaD commented Nov 4, 2021

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

@RauliL
Copy link

RauliL commented Nov 4, 2021

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.

@MangaD
Copy link
Author

MangaD commented Nov 4, 2021

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants