Moving away from c++ which was not needed

Improve list command (now sorted output)
Bump version to 1.0.1
This commit is contained in:
augustin64 2023-07-13 12:00:05 +02:00
parent 488499050b
commit 10c326c040
7 changed files with 125 additions and 65 deletions

View File

@ -8,11 +8,9 @@ $(shell mkdir -p out)
out/%.o: src/%.c out/%.o: src/%.c
$(CC) -c $(FLAGS) $^ -o $@ $(CC) -c $(FLAGS) $^ -o $@
out/%.o: src/%.cpp
$(CXX) -c $(FLAGS) $^ -o $@
takl: out/main.o out/db.o out/tasks.o takl: out/main.o out/db.o out/tasks.o
$(CXX) $(FLAGS) $(LD_FLAGS) $^ -o $@ $(CC) $(FLAGS) $(LD_FLAGS) $^ -o $@
install: takl install: takl
sudo cp takl /usr/bin/takl sudo cp takl /usr/bin/takl
@ -20,6 +18,7 @@ install: takl
uninstall: takl uninstall: takl
sudo rm /usr/bin/takl sudo rm /usr/bin/takl
clean: clean:
rm out -r rm out -r
rm ./takl rm ./takl

View File

@ -1,7 +1,7 @@
#ifndef DEF_CONFIG_H #ifndef DEF_CONFIG_H
#define 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 // By default, $HOME/.config/takl.sqlite3 is used. You can change this behaviour here
//#define DB_FILE "takl.sqlite3" //#define DB_FILE "takl.sqlite3"

View File

@ -3,9 +3,6 @@
#ifndef DEF_DB_H #ifndef DEF_DB_H
#define DEF_DB_H #define DEF_DB_H
#ifdef __cplusplus
extern "C" {
#endif
/* /*
Création de la base de données Création de la base de données
Arrête le programme en cas d'échec 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); 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é 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(); int get_new_task_id();
#ifdef __cplusplus
}
#endif
#endif #endif

View File

@ -3,8 +3,24 @@
#ifndef DEF_TASKS_H #ifndef DEF_TASKS_H
#define 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); task_t create_task(char* text, time_t due_to);
/*
Affiche une tâche, avec toutes ses informations
*/
void print_task(task_t task); 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 #endif

View File

@ -98,6 +98,14 @@ int add(int argc, char* argv[]) {
return 0; 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[]) { int info(int argc, char* argv[]) {
if (argc < 3) { if (argc < 3) {

95
src/tasks.c Normal file
View File

@ -0,0 +1,95 @@
/*
Fonctions utilitaires concernant les tâches
*/
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#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;
}
}

View File

@ -1,52 +0,0 @@
/*
Fonctions de création/ modification d'objet tâches
*/
#include <stdlib.h>
#include <stdio.h>
#include <ctime>
#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);
}
}