Autocomplete.
This commit is contained in:
parent
64a2407f4d
commit
44b288497a
31
line.h
31
line.h
@ -5,6 +5,7 @@
|
||||
// External includes:
|
||||
// - <readline/readline.h>
|
||||
// - <readline/history.h>
|
||||
// - <glob.h>
|
||||
|
||||
// MIT License: Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction.
|
||||
|
||||
@ -12,13 +13,14 @@
|
||||
#include <readline/history.h>
|
||||
#include <string.h>
|
||||
#include <stdbool.h>
|
||||
#include <glob.h>
|
||||
|
||||
#define HISTORY_FILE ".r_history"
|
||||
|
||||
bool line_initialized = false;
|
||||
|
||||
char* line_command_generator(const char* text, int state) {
|
||||
static int list_index, len;
|
||||
static int list_index, len = 0;
|
||||
const char* commands[] = {"help", "exit", "list", "review", "refactor", "obfuscate", NULL};
|
||||
|
||||
if (!state) {
|
||||
@ -36,8 +38,33 @@ char* line_command_generator(const char* text, int state) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
char* line_file_generator(const char* text, int state) {
|
||||
static int list_index;
|
||||
glob_t glob_result;
|
||||
char pattern[1024];
|
||||
|
||||
if (!state) {
|
||||
list_index = 0;
|
||||
snprintf(pattern, sizeof(pattern), "%s*", text); // Create a pattern for glob
|
||||
glob(pattern, GLOB_NOSORT, NULL, &glob_result);
|
||||
}
|
||||
|
||||
if (list_index < glob_result.gl_pathc) {
|
||||
return strdup(glob_result.gl_pathv[list_index++]);
|
||||
}
|
||||
|
||||
globfree(&glob_result);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
char** line_command_completion(const char* text, int start, int end) {
|
||||
rl_attempted_completion_over = 1;
|
||||
|
||||
// Check if the input is a file path
|
||||
if (start > 0 && text[0] != ' ') {
|
||||
return rl_completion_matches(text, line_file_generator);
|
||||
}
|
||||
|
||||
return rl_completion_matches(text, line_command_generator);
|
||||
}
|
||||
|
||||
@ -61,4 +88,4 @@ char* line_read(char* prefix) {
|
||||
void line_add_history(char* data) {
|
||||
add_history(data);
|
||||
write_history(HISTORY_FILE);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user