feat: support source field in config
This commit is contained in:
@@ -212,6 +212,9 @@ typedef struct {
|
|||||||
typedef void (*FuncType)(const Arg *);
|
typedef void (*FuncType)(const Arg *);
|
||||||
Config config;
|
Config config;
|
||||||
|
|
||||||
|
|
||||||
|
void parse_config_file(Config *config, const char *file_path);
|
||||||
|
|
||||||
// Helper function to trim whitespace from start and end of a string
|
// Helper function to trim whitespace from start and end of a string
|
||||||
void trim_whitespace(char *str) {
|
void trim_whitespace(char *str) {
|
||||||
if (str == NULL || *str == '\0') return;
|
if (str == NULL || *str == '\0') return;
|
||||||
@@ -1409,17 +1412,39 @@ if (parsed == 8) {
|
|||||||
config->gesture_bindings_count++;
|
config->gesture_bindings_count++;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
} else if (strncmp(key, "source", 6) == 0) {
|
||||||
|
parse_config_file(config, value);
|
||||||
} else {
|
} else {
|
||||||
fprintf(stderr, "Error: Unknown key: %s\n", key);
|
fprintf(stderr, "Error: Unknown key: %s\n", key);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void parse_config_file(Config *config, const char *file_path) {
|
void parse_config_file(Config *config, const char *file_path) {
|
||||||
FILE *file = fopen(file_path, "r");
|
FILE *file;
|
||||||
|
// 检查路径是否以 ~/ 开头
|
||||||
|
if (file_path[0] == '~' && (file_path[1] == '/' || file_path[1] == '\0')) {
|
||||||
|
const char *home = getenv("HOME");
|
||||||
|
if (!home) {
|
||||||
|
fprintf(stderr, "Error: HOME environment variable not set.\n");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 构建完整路径(家目录 + / + 原路径去掉 ~)
|
||||||
|
char full_path[1024];
|
||||||
|
snprintf(full_path, sizeof(full_path), "%s%s", home, file_path + 1);
|
||||||
|
|
||||||
|
file = fopen(full_path, "r");
|
||||||
if (!file) {
|
if (!file) {
|
||||||
perror("Error opening file");
|
perror("Error opening file");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
file = fopen(file_path, "r");
|
||||||
|
if (!file) {
|
||||||
|
perror("Error opening file");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
char line[512];
|
char line[512];
|
||||||
while (fgets(line, sizeof(line), file)) {
|
while (fgets(line, sizeof(line), file)) {
|
||||||
|
|||||||
Reference in New Issue
Block a user