From f220724d42465f85dcc16b04a74c2c997888d293 Mon Sep 17 00:00:00 2001 From: theBreadCompany Date: Sun, 15 Jun 2025 01:04:01 +0200 Subject: [PATCH] config outline --- src/cli/CMakeLists.txt | 6 +-- src/cli/config.c | 96 ++++++++++++++++++++++++++++++++++++++++ src/cli/include/config.h | 13 ++++++ src/cli/main.c | 23 +++++++++- src/cli/process.c | 1 + src/libklist/models.c | 13 ++++-- 6 files changed, 144 insertions(+), 8 deletions(-) create mode 100644 src/cli/config.c create mode 100644 src/cli/include/config.h diff --git a/src/cli/CMakeLists.txt b/src/cli/CMakeLists.txt index e54e6c6..dc3c98a 100644 --- a/src/cli/CMakeLists.txt +++ b/src/cli/CMakeLists.txt @@ -1,6 +1,4 @@ -add_executable(klist main.c process.c) +add_executable(klist main.c process.c config.c) target_include_directories(klist PRIVATE include) target_link_libraries(klist libklist) -target_compile_options(klist PRIVATE -Werror -Wall) #-Wextra) - -add_test(NAME klist_userCreation COMMAND klist user -c) \ No newline at end of file +target_compile_options(klist PRIVATE -Werror -Wall) #-Wextra) \ No newline at end of file diff --git a/src/cli/config.c b/src/cli/config.c new file mode 100644 index 0000000..f6dfe15 --- /dev/null +++ b/src/cli/config.c @@ -0,0 +1,96 @@ +#include "config.h" + +#include +#include +#include + +char *klist_config_key_to_string(KLIST_CONFIG_KEY key) { + switch (key) { + case CONFIG_DATABASE_NAME: + return "database"; + case _CONFIG_KEY_COUNT: + return ""; + } + return ""; +} + +KLIST_CONFIG_KEY klist_config_string_to_key(char *key) { + if (!strcmp(key, klist_config_key_to_string(CONFIG_DATABASE_NAME))) + return CONFIG_DATABASE_NAME; + return _CONFIG_KEY_COUNT; +} + +void klist_config_parse(klist_config *ctx, FILE *config) { + fseek(config, 0, SEEK_END); + int c; + char *key = NULL, *val = NULL; + bool found_key = false, ignore_line = false; + while ((c = fgetc(config)) != EOF) { + switch (c) { + case '\n': + ignore_line = true; + if (!key) + continue; + if (!val) { + fprintf(stderr, "ignoring key '%s'", key); + } + if (!strcmp(key, "database")) { + ctx->db_file = malloc(strlen(val)); + strcpy(ctx->db_file, val); + } else { + fprintf(stderr, "ignoring unknown key '%s'", key); + } + free(key); + free(val); + key = NULL; + val = NULL; + break; + case '=': + if (!found_key) + found_key = true; + else { + printf("config file is screwed, please review\n"); + exit(1); + } + break; + default: + if (!key && !val && c == '#') + ignore_line = true; + if (ignore_line) + continue; + if (found_key) { + char *_val = val ? realloc(val, (strlen(val) + 2) * sizeof(char)) + : malloc(2 * sizeof(char)); + if (_val) { + val = _val; + val[strlen(val) - 2] = (char)c; + val[strlen(val) - 1] = '\0'; + } + } else { + char *_key = key ? realloc(key, (strlen(key) + 2) * sizeof(char)) + : malloc(2 * sizeof(char)); + if (_key) { + key = _key; + key[strlen(key) - 2] = (char)c; + key[strlen(key) - 1] = '\0'; + } + } + } + } +} + +void klist_config_setup(char *name, FILE *config) { + char *data_home = getenv("XDG_DATA_HOME"); + + int db_file_len = snprintf(NULL, 0, "%s/%s", data_home, name); + char *db_file = malloc(db_file_len * sizeof(char)); + snprintf(db_file, db_file_len, "%s/%s", data_home, name); + + fseek(config, 0, SEEK_END); + fprintf(config, "%s=%s\n", klist_config_key_to_string(CONFIG_DATABASE_NAME), + db_file); + fclose(config); + + if (db_file) + free(db_file); +} \ No newline at end of file diff --git a/src/cli/include/config.h b/src/cli/include/config.h new file mode 100644 index 0000000..e049cc4 --- /dev/null +++ b/src/cli/include/config.h @@ -0,0 +1,13 @@ +#pragma once +#include + +enum KLIST_CONFIG_KEY { CONFIG_DATABASE_NAME, _CONFIG_KEY_COUNT }; +typedef enum KLIST_CONFIG_KEY KLIST_CONFIG_KEY; + +struct klist_config { + char *db_file; +}; +typedef struct klist_config klist_config; + +void klist_config_parse(klist_config *, FILE *); +void klist_config_setup(char *, FILE *); \ No newline at end of file diff --git a/src/cli/main.c b/src/cli/main.c index 59b8275..dfd2e03 100644 --- a/src/cli/main.c +++ b/src/cli/main.c @@ -3,6 +3,7 @@ #include #include +#include "config.h" #include "process.h" #include "util.h" @@ -10,7 +11,27 @@ void print_help(klist *ctx, char **argv); void setup(klist *ctx, int argc, char **argv); int main(int argc, char **argv) { - klist *ctx = klist_init("test.db"); + char *name = argv[0]; + char *config_home = getenv("XDG_CONFIG_HOME"); + + int config_file_len = snprintf(NULL, 0, "%s/%s", config_home, name); + char *config_file_name = malloc(config_file_len * sizeof(char)); + snprintf(config_file_name, config_file_len, "%s/%s", config_home, name); + + klist_config *config = malloc(sizeof(klist_config)); + + FILE *config_file = NULL; + if (access(config_file_name, F_OK) == -1) { + config_file = fopen(config_file_name, "w"); + klist_config_setup(argv[0], config_file); + } else + config_file = fopen(config_file_name, "a+"); + + klist_config_parse(config, config_file); + + klist *ctx = klist_init(config->db_file); + + free(config); if (argc == 1) { print_help(ctx, argv); diff --git a/src/cli/process.c b/src/cli/process.c index 02b1ea0..ae8f56f 100644 --- a/src/cli/process.c +++ b/src/cli/process.c @@ -55,6 +55,7 @@ int klist_app_list_add(klist *ctx) { list->desc = (unsigned char *)strdup(list_ctx->desc); list->is_preset = false; klist_list_save(ctx, list, user); + printf("Added list '%s'\n", (char *)list->name); int i = 0; for (; i < list_ctx->stages_len; i++) { klist_stage *stage = klist_stage_init(); diff --git a/src/libklist/models.c b/src/libklist/models.c index 03fa080..2c0e6a7 100644 --- a/src/libklist/models.c +++ b/src/libklist/models.c @@ -399,7 +399,7 @@ void klist_print_list(klist *ctx, klist_list *list, klist_task **tasks, struct winsize w; int i = 0; ioctl(STDOUT_FILENO, TIOCGWINSZ, &w); - int width = w.ws_col > 0 ? w.ws_col : 40; + u_int width = w.ws_col > 0 ? w.ws_col : 40; size_t stages_len = 0; klist_stage **stages = @@ -416,13 +416,20 @@ void klist_print_list(klist *ctx, klist_list *list, klist_task **tasks, for (i = 0; i < max_stages_id; i++) tasks_count_per_stage[i] = 0; - for (i = 0; i < tasks_len; i++) + u_long task_length = 0, max_task_length = 0; + for (i = 0; i < tasks_len; i++) { tasks_count_per_stage[tasks[i]->stage_id]++; + task_length = strlen((char *)tasks[i]->name); + if (task_length > max_task_length) + max_task_length = task_length; + } + width = max_task_length * stages_len; int tasks_max = 0; - for (i = 0; i < max_stages_id; i++) + for (i = 0; i < max_stages_id; i++) { if (tasks_count_per_stage[i] > tasks_max) tasks_max = tasks_count_per_stage[i]; + } print_table_line("╭", stages_len, "─", stage_col_width, "─", "╮");