Skip to content
This repository has been archived by the owner on Jun 10, 2022. It is now read-only.

Commit

Permalink
Merge pull request #20 from darconeous/completion-fix
Browse files Browse the repository at this point in the history
nyocictl: Improve autocompletion
  • Loading branch information
darconeous committed Apr 16, 2019
2 parents 3caa2eb + 5aac9f9 commit 11a6e1b
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 32 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Expand Up @@ -14,7 +14,7 @@ addons:


os: os:
- linux - linux
- osx # - osx


compiler: compiler:
- clang - clang
Expand Down
2 changes: 1 addition & 1 deletion .travis/script.sh
Expand Up @@ -15,7 +15,7 @@ then


cd "${BUILD_MAKEPATH}" || die cd "${BUILD_MAKEPATH}" || die


../configure ${BUILD_CONFIGFLAGS} || die ../configure --disable-dependency-tracking ${BUILD_CONFIGFLAGS} || die


cd "${PREV_PATH}" || die cd "${PREV_PATH}" || die
fi fi
Expand Down
81 changes: 51 additions & 30 deletions src/nyocictl/main.c
Expand Up @@ -440,6 +440,8 @@ nyoci_directory_generator(
// Don't add the internal commands to the history. // Don't add the internal commands to the history.
history_disabled = true; history_disabled = true;


bool currPathIsRoot = url_is_root(getenv("NYOCI_CURRENT_PATH"));

/* If this is a new word to complete, initialize now. This includes /* If this is a new word to complete, initialize now. This includes
saving the length of TEXT for efficiency, and initializing the index saving the length of TEXT for efficiency, and initializing the index
variable to 0. */ variable to 0. */
Expand All @@ -462,40 +464,42 @@ nyoci_directory_generator(
// Figure out where the last path component starts. // Figure out where the last path component starts.
for(i=strlen(prefix);i && prefix[i]!='/';i--); for(i=strlen(prefix);i && prefix[i]!='/';i--);


if(prefix[i]=='/') { if (i == 0) {
prefix[i] = 0;
if(i==0) {
prefix = strdup("/");
}
fragment = strdup(prefix+i+1);
} else {
fragment = strdup(prefix); fragment = strdup(prefix);
free(prefix); free(prefix);
prefix = strdup("."); prefix = strdup("./");

} else {
fragment = strdup(prefix+i+1);
prefix[i+1] = 0;
} }

char* cmdline = NULL; char* cmdline = NULL;
FILE* real_stdout = stdout; FILE* real_stdout = stdout;


if(url_is_root(getenv("NYOCI_CURRENT_PATH")) && !url_is_root(prefix)) { //fprintf(stderr,"\n[text=\"%s\"] ",text);
if(!i) { //fprintf(stderr,"\n[prefix=\"%s\"] ",prefix);
asprintf(&cmdline, "list --filename-only --timeout 750 /.well-known/core"); //fprintf(stderr,"\n[fragment=\"%s\"] ",fragment);
require(cmdline,bail);
//fprintf(stderr,"\n[cmd=\"%s\"] ",cmdline);


fprintf(temp_file,".well-known/\n"); if(strequal_const(prefix, ".well-known/")) {
fprintf(temp_file,"core\n");
}


stdout = temp_file; if(currPathIsRoot && !url_is_root(prefix) && i == 0) {
process_input_line(cmdline); asprintf(&cmdline, "list --filename-only --timeout 1000 /.well-known/core");
stdout = real_stdout; require(cmdline,bail);
free(cmdline); //fprintf(stderr,"\n[cmd=\"%s\"] ",cmdline);
} else {
if(strequal_const(prefix, ".well-known")) { fprintf(temp_file,".well-known/\n");
fprintf(temp_file,"core\n");
} stdout = temp_file;
} process_input_line(cmdline);
stdout = real_stdout;
free(cmdline);
} else { } else {
asprintf(&cmdline, "list --filename-only --timeout 1000 \"%s\"",prefix); asprintf(&cmdline, "list --filename-only --timeout 1000 \"%s\"",prefix);
require(cmdline,bail); require(cmdline,bail);
//fprintf(stderr,"\n[cmd=\"%s\"] ",cmdline);


stdout = temp_file; stdout = temp_file;
if(strequal_const(fragment, ".")) if(strequal_const(fragment, "."))
Expand All @@ -513,25 +517,42 @@ nyoci_directory_generator(


while ((name = fgetln(temp_file, &namelen))) while ((name = fgetln(temp_file, &namelen)))
{ {
if(namelen<len) // Remove any trailing whitespace
while (namelen && isspace(name[namelen-1])) { namelen--; }

//fprintf(stderr,"\n[candidate=\"%.*s\" namelen=%d] ",namelen,name,namelen);

// Ignore candidates that have a length smaller than our candidate
if (namelen < len) {
continue; continue;
//fprintf(stderr,"\n[candidate=\"%s\" namelen=%d] ",name,namelen); }
if(url_is_root(getenv("NYOCI_CURRENT_PATH")) && strequal_const(prefix,".")) {
if (name[0] == '.') {
if (namelen == 1) {
continue;
} else if (namelen == 2 && name[1] == '/') {
continue;
}
}

if (namelen == 2 && name[0] == '.') {
continue;
}

if(currPathIsRoot && strequal_const(prefix,"./")) {
while(name[0]=='/') { while(name[0]=='/') {
name++; name++;
namelen--; namelen--;
} }
} }
if (strncmp (name, fragment, len) == 0) { if (strncmp (name, fragment, len) == 0) {
while(namelen && isspace(name[namelen-1])) { namelen--; }
//namelen--;
if(name[namelen-1]=='/') if(name[namelen-1]=='/')
rl_completion_append_character = 0; rl_completion_append_character = 0;


if( strequal_const(prefix, "/") if( strequal_const(prefix, "/")
|| strequal_const(prefix, ".") || strequal_const(prefix, "./")
|| name[0]=='/' || name[0]=='/'
||(url_is_root(getenv("NYOCI_CURRENT_PATH")) && strequal_const(prefix,".")) || (currPathIsRoot && strequal_const(prefix,"./"))
) { ) {
ret = strndup(name,namelen); ret = strndup(name,namelen);
} else { } else {
Expand Down

0 comments on commit 11a6e1b

Please sign in to comment.