Compare commits

...

2 Commits

Author SHA1 Message Date
10c326c040 Moving away from c++ which was not needed
Improve list command (now sorted output)
Bump version to 1.0.1
2023-07-13 12:00:05 +02:00
488499050b Added task_list_t 2023-07-13 11:55:50 +02:00
9 changed files with 144 additions and 83 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

@ -213,7 +213,7 @@ void get_task(int id, task_t* t) {
// t->id will be (-1) if not found // t->id will be (-1) if not found
} }
int list_tasks(bool show_completed) { task_list_t* get_task_list(bool include_completed) {
sqlite3* db = get_db(); sqlite3* db = get_db();
sqlite3_stmt *stmt; sqlite3_stmt *stmt;
@ -221,38 +221,33 @@ int list_tasks(bool show_completed) {
if (ret != SQLITE_OK) { if (ret != SQLITE_OK) {
printf("(get_task) failure fetching data\n"); printf("(get_task) failure fetching data\n");
return 1; return NULL;
} }
time_t now = time(0); task_list_t* list = NULL;
while (sqlite3_step(stmt) == SQLITE_ROW) { while (sqlite3_step(stmt) == SQLITE_ROW) {
task_t t; task_t t;
t.id = strtol((char*)sqlite3_column_text(stmt, 0), NULL, 10); t.id = strtol((char*)sqlite3_column_text(stmt, 0), NULL, 10);
t.text = (char*)sqlite3_column_text(stmt, 1);
t.done = strtol((char*)sqlite3_column_text(stmt, 2), NULL, 10); t.done = strtol((char*)sqlite3_column_text(stmt, 2), NULL, 10);
t.due_to = strtol((char*)sqlite3_column_text(stmt, 3), NULL, 10); t.due_to = strtol((char*)sqlite3_column_text(stmt, 3), NULL, 10);
//* Warning: t just lives here (particularly t.text)
if (!t.done || include_completed) { // We don't want to allocate memory for things that will not be used
char* text = (char*)sqlite3_column_text(stmt, 1);
t.text = malloc(sizeof(char)*(strlen(text)+1));
strcpy(t.text, text);
if (t.done) { task_list_t* cur = malloc(sizeof(task_list_t)); // Add the parsed task to the beginning of the list
if (show_completed) { cur->task = t;
printf(BOLD GREEN); // Task completed cur->next = list;
} list = cur;
} else if (difftime(now, t.due_to) <= 0 || t.due_to == 0) {
printf(BOLD YELLOW); // Task not completed but that is fine
} else {
printf(BOLD RED); // Task not completed but should be !
}
if (!t.done || show_completed) {
printf("[%d]" RESET " %s\n", t.id, t.text);
} }
} }
sqlite3_finalize(stmt); sqlite3_finalize(stmt);
sqlite3_close(db); sqlite3_close(db);
return 0; return list;
} }
int get_new_task_id() { int get_new_task_id() {

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

@ -12,4 +12,10 @@ struct task {
}; };
typedef struct task task_t; typedef struct task task_t;
struct task_list {
task_t task;
struct task_list* next;
};
typedef struct task_list task_list_t;
#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);
}
}