config outline

This commit is contained in:
theBreadCompany 2025-06-15 01:04:01 +02:00
parent d6cb8f0ce8
commit f220724d42
6 changed files with 144 additions and 8 deletions

View file

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

96
src/cli/config.c Normal file
View file

@ -0,0 +1,96 @@
#include "config.h"
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
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);
}

13
src/cli/include/config.h Normal file
View file

@ -0,0 +1,13 @@
#pragma once
#include <stdio.h>
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 *);

View file

@ -3,6 +3,7 @@
#include <string.h>
#include <unistd.h>
#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);

View file

@ -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();

View file

@ -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, "", "");