TaKl/src/main/tasks.c

95 lines
2.3 KiB
C
Raw Normal View History

/*
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;
}
}