From 10c326c040b3487492e57f0e7bf6af1bed4af390 Mon Sep 17 00:00:00 2001 From: augustin64 Date: Thu, 13 Jul 2023 12:00:05 +0200 Subject: [PATCH] Moving away from c++ which was not needed Improve list command (now sorted output) Bump version to 1.0.1 --- Makefile | 7 ++-- src/include/config.h | 2 +- src/include/db.h | 10 +---- src/include/tasks.h | 16 ++++++++ src/main.c | 8 ++++ src/tasks.c | 95 ++++++++++++++++++++++++++++++++++++++++++++ src/tasks.cpp | 52 ------------------------ 7 files changed, 125 insertions(+), 65 deletions(-) create mode 100644 src/tasks.c delete mode 100644 src/tasks.cpp diff --git a/Makefile b/Makefile index f84eb59..a11d888 100644 --- a/Makefile +++ b/Makefile @@ -8,11 +8,9 @@ $(shell mkdir -p out) out/%.o: src/%.c $(CC) -c $(FLAGS) $^ -o $@ -out/%.o: src/%.cpp - $(CXX) -c $(FLAGS) $^ -o $@ - takl: out/main.o out/db.o out/tasks.o - $(CXX) $(FLAGS) $(LD_FLAGS) $^ -o $@ + $(CC) $(FLAGS) $(LD_FLAGS) $^ -o $@ + install: takl sudo cp takl /usr/bin/takl @@ -20,6 +18,7 @@ install: takl uninstall: takl sudo rm /usr/bin/takl + clean: rm out -r rm ./takl \ No newline at end of file diff --git a/src/include/config.h b/src/include/config.h index 4ab1a73..489e610 100644 --- a/src/include/config.h +++ b/src/include/config.h @@ -1,7 +1,7 @@ #ifndef DEF_CONFIG_H #define DEF_CONFIG_H -#define VERSION "1.0.0" +#define VERSION "1.0.1" // By default, $HOME/.config/takl.sqlite3 is used. You can change this behaviour here //#define DB_FILE "takl.sqlite3" diff --git a/src/include/db.h b/src/include/db.h index 0d2e2cc..f04ece3 100644 --- a/src/include/db.h +++ b/src/include/db.h @@ -3,9 +3,6 @@ #ifndef DEF_DB_H #define DEF_DB_H -#ifdef __cplusplus -extern "C" { -#endif /* Création de la base de données Arrête le programme en cas d'échec @@ -36,9 +33,9 @@ t.id vaut -1 si non trouvé void get_task(int id, task_t* t); /* -Liste les tâches dans un format prédéfini +Renvoie la liste des tâches de la base de données */ -int list_tasks(bool show_completed); +task_list_t* get_task_list(bool include_completed); /* Renvoie un identifiant de tâche encore non utilisé @@ -46,7 +43,4 @@ Renvoie un identifiant de tâche encore non utilisé int get_new_task_id(); -#ifdef __cplusplus -} -#endif #endif \ No newline at end of file diff --git a/src/include/tasks.h b/src/include/tasks.h index 738b724..55a2836 100644 --- a/src/include/tasks.h +++ b/src/include/tasks.h @@ -3,8 +3,24 @@ #ifndef DEF_TASKS_H #define DEF_TASKS_H +/* +Renvoie une tâche avec un identifiant aléatoire valide +*/ task_t create_task(char* text, time_t due_to); + +/* +Affiche une tâche, avec toutes ses informations +*/ void print_task(task_t task); +/* +Affiche une liste de tâches de manière compacte (et par catégorie: urgent, à faire, [complétée]) +*/ +void print_task_list(task_list_t* list, bool show_completed); + +/* +Libère la mémoire allouée à une liste de tâches +*/ +void free_task_list(task_list_t* list); #endif \ No newline at end of file diff --git a/src/main.c b/src/main.c index 5132eb7..b7802c7 100644 --- a/src/main.c +++ b/src/main.c @@ -98,6 +98,14 @@ int add(int argc, char* argv[]) { return 0; } +int list_tasks(bool show_completed) { + task_list_t* tasks = get_task_list(show_completed); + print_task_list(tasks, show_completed); // show_completed: true would work as all the tasks are not loaded if false + free_task_list(tasks); + + return 0; +} + int info(int argc, char* argv[]) { if (argc < 3) { diff --git a/src/tasks.c b/src/tasks.c new file mode 100644 index 0000000..44bbfbf --- /dev/null +++ b/src/tasks.c @@ -0,0 +1,95 @@ +/* +Fonctions utilitaires concernant les tâches +*/ +#include +#include +#include + +#include "include/db.h" +#include "include/struct.h" +#include "include/colors.h" + + +task_t create_task(char* text, time_t due_to) { + task_t task; + + task.id = get_new_task_id(); + task.text = text; + task.done = false; + task.due_to = due_to; + + return task; +} + + +void print_task(task_t task) { + printf(YELLOW "=== Tâche " BOLD "[%d]" RESET YELLOW " ===\n" RESET, task.id); + printf(BOLD "%s\n" RESET, task.text); + + printf("Statut: "); + if (task.done) { + printf(GREEN "Complétée\n" RESET); + } else { + time_t now = time(0); + if (difftime(now, task.due_to) <= 0 || task.due_to == 0) { + printf(BLUE "À faire\n" RESET); + } else { + printf(RED "À faire\n" RESET); + } + + } + + if (task.due_to != 0) { + struct tm * timeinfo; + char buffer[80]; + + timeinfo = localtime(&task.due_to); + + strftime(buffer,sizeof(buffer),"%d-%m-%Y %H:%M:%S",timeinfo); + + printf("Dûe le %s\n", buffer); + } +} + + +void print_task_list(task_list_t* list, bool show_completed) { + task_list_t* cur = list; + time_t now = time(0); + while (cur) { // Show not completed red tasks first + task_t t = cur->task; + if (!t.done && difftime(now, t.due_to) >= 0 && t.due_to != 0) { + printf(BOLD RED "[%d]" RESET " %s\n", t.id, t.text); // Task not completed but should be ! + } + cur = cur->next; + } + + cur = list; + while (cur) { // Show not completed but not due_to then + task_t t = cur->task; + if (!t.done && (difftime(now, t.due_to) <= 0 || t.due_to == 0)) { + printf(BOLD YELLOW "[%d]" RESET " %s\n", t.id, t.text); // Task not completed but should be ! + } + cur = cur->next; + } + + if (show_completed) { + cur = list; + while (cur) { // Show not completed but not due_to then + task_t t = cur->task; + if (t.done) { + printf(BOLD GREEN "[%d]" RESET " %s\n", t.id, t.text); // Task not completed but should be ! + } + cur = cur->next; + } + } +} + + +void free_task_list(task_list_t* list) { + while (list) { + task_list_t* next = list->next; + free(list->task.text); + free(list); + list = next; + } +} \ No newline at end of file diff --git a/src/tasks.cpp b/src/tasks.cpp deleted file mode 100644 index e681481..0000000 --- a/src/tasks.cpp +++ /dev/null @@ -1,52 +0,0 @@ -/* -Fonctions de création/ modification d'objet tâches -*/ -#include -#include -#include - -#include "include/db.h" -#include "include/struct.h" -#include "include/colors.h" - -extern "C" -task_t create_task(char* text, time_t due_to) { - task_t task; - - task.id = get_new_task_id(); - task.text = text; - task.done = false; - task.due_to = due_to; - - return task; -} - -extern "C" -void print_task(task_t task) { - printf(YELLOW "=== Tâche " BOLD "[%d]" RESET YELLOW " ===\n" RESET, task.id); - printf(BOLD "%s\n" RESET, task.text); - - printf("Statut: "); - if (task.done) { - printf(GREEN "Complétée\n" RESET); - } else { - time_t now = time(0); - if (difftime(now, task.due_to) <= 0 || task.due_to == 0) { - printf(BLUE "À faire\n" RESET); - } else { - printf(RED "À faire\n" RESET); - } - - } - - if (task.due_to != 0) { - struct tm * timeinfo; - char buffer[80]; - - timeinfo = localtime(&task.due_to); - - strftime(buffer,sizeof(buffer),"%d-%m-%Y %H:%M:%S",timeinfo); - - printf("Dûe le %s\n", buffer); - } -} \ No newline at end of file