Compare commits

..

10 Commits

Author SHA1 Message Date
synt-xerror
7bc499f740 removing external library 2026-02-20 16:39:50 -03:00
synt-xerror
a23637a2c8 Testing it on Termux 2026-02-19 19:02:05 -03:00
synt-xerror
e8b7cb97a3 New readme 2026-02-17 16:53:14 -03:00
synt-xerror
a78da65f0f New readme 2026-02-17 16:52:03 -03:00
synt-xerror
18916e86cd New commands: add, read and done. 2026-02-17 16:40:56 -03:00
synt-xerror
a73b97101f Simplified the get_dir function, and now we have directories and files ready :) 2026-02-14 20:49:52 -03:00
synt-xerror
7b829fde21 Adding temporary library files to gitignore 2026-02-13 10:58:28 -03:00
synt-xerror
6f564d1f4e Removing functions on task.c to use on a library 2026-02-13 10:57:13 -03:00
synt-xerror
813d178521 Adding new instructions to REAME 2026-02-13 10:56:35 -03:00
synt-xerror
5eaa8f8822 Adding my library to make code clean 2026-02-13 10:56:06 -03:00
5 changed files with 198 additions and 69 deletions

2
.gitignore vendored
View File

@@ -1,2 +1,4 @@
task.o task.o
task task
libtoybox.a
toybox.o

View File

@@ -1,12 +1,12 @@
CFLAGS := $(shell pkg-config --cflags glib-2.0) CFLAGS := $(shell pkg-config --cflags glib-2.0)
LDFLAGS := $(shell pkg-config --libs glib-2.0) LDFLAGS := $(shell pkg-config --libs glib-2.0)
HOME := $($HOME) HOME := $(HOME)
task: task:
gcc task.c $(CFLAGS) -o task $(LDFLAGS) gcc task.c $(CFLAGS) -o task $(LDFLAGS)
clean: clean:
rm task rm -f task
install: install:
mv task /usr/local/bin mv task /usr/local/bin

47
README.md Normal file
View File

@@ -0,0 +1,47 @@
# task-cli.
# how to use:
1. clone this repo
2. cd to the repo directory
3. run "make"
4. run "make install" (as root)
5. then run "task"
to clean, run "make clean"
# commands:
## add
to add a new task. example:
```
task add "take a bath (please)"
```
## read
to read all your tasks
example:
```
task read
```
the output will look like:
```
0. [ ] take a bath (please)
```
## done
to mark a task as done
example:
```
task done 0
```
the output of `task read` will no longer print the task 0
---
please give a star if you like it :( i love you

View File

@@ -1,15 +0,0 @@
task-cli.
=========
how to use:
-----------
1. clone this repo
2. cd to the repo directory
3. run 'make'
4. then run ./task
commands:
---------
no commands yet

187
task.c
View File

@@ -1,8 +1,8 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h>
#include <unistd.h> #include <unistd.h>
#include <pwd.h> #include <pwd.h>
#include <string.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <sys/types.h> #include <sys/types.h>
#include <dirent.h> #include <dirent.h>
@@ -13,6 +13,15 @@
// - Organização por prioridade // - Organização por prioridade
// - Data limite // - Data limite
// --- Global values ---
// directories (begin with nothing)
char* DATA_DIR = ".local/share/task-cli";
char* CONFIG_DIR = ".config/task-cli";
// files
char* TASK_FILE = "task.txt";
char* home() char* home()
{ {
struct passwd *u; struct passwd *u;
@@ -27,72 +36,158 @@ char* home()
} }
// DIR_NAME deve ser o nome do diretório, não o caminho (ex: task) // DIR_NAME deve ser o nome do diretório, não o caminho (ex: task)
// DIR_ROOT é o fallback caso a XDG_VAR não exista
// Caso o diretório não estiver na home, use NULL como primeiro argumento
// Se HOME = NULL, a função assume que a pasta que quer está fora da pasta /home/usuário // Se HOME = NULL, a função assume que a pasta que quer está fora da pasta /home/usuário
// Importante: essa função retorna um char*. Para abrir o diretório retornado, você deve usar // Importante: essa função retorna um char*. Para abrir o diretório retornado, você deve usar
// a função opendir() com o valor retornado de get_dir() // a função opendir() com o valor retornado de get_dir()
char* get_dir(char* HOME, char* DIR_ROOT, char* DIR_NAME) { char* get_dir(const char* HOME, const char* DIR_NAME) {
if (DIR_NAME[0] == '~') { if (DIR_NAME[0] == '~') {
perror("get_dir function doesn't expand '~'!"); perror("get_dir doesn't expand '~'");
return NULL; return NULL;
} else if (DIR_NAME[0] == '/') { }
perror("get_dir DIR_NAME (3rd argumment) can't begin with '/'!"); if (DIR_NAME[0] == '/') {
perror("DIR_NAME can't begin with '/'");
return NULL; return NULL;
} }
char *DIR_FINAL; const char *prefix = HOME ? HOME : "";
// se não tiver home, assume estar na raíz
if (HOME) {
int size = strlen(home()) + 1 + strlen(DIR_ROOT) + 1 + strlen(DIR_NAME);
DIR_FINAL = malloc(size);
DIR_FINAL[0] = '\0';
strcat(DIR_FINAL, HOME);
strcat(DIR_FINAL, "/");
strcat(DIR_FINAL, DIR_ROOT);
strcat(DIR_FINAL, "/");
strcat(DIR_FINAL, DIR_NAME);
} else {
int size = 1 + strlen(DIR_ROOT) + 1 + strlen(DIR_NAME) + 1;
DIR_FINAL = malloc(size);
DIR_FINAL[0] = '\0';
strcat(DIR_FINAL, "/");
strcat(DIR_FINAL, DIR_ROOT);
strcat(DIR_FINAL, "/");
strcat(DIR_FINAL, DIR_NAME);
}
int size = strlen(prefix) + 1 + strlen(DIR_NAME) + 1;
char *DIR_FINAL = malloc(size);
if (!DIR_FINAL) { if (!DIR_FINAL) {
fprintf(stderr, "Return of %s in get_dir is NULL!", perror("malloc failed");
DIR_FINAL);
return NULL;
} else {
const DIR *dir_exist = opendir(DIR_FINAL);
if (!dir_exist) {
const int status = mkdir(DIR_FINAL, 0755);
if (status != 0) {
fprintf(stderr, "'%s' doesn't exist and cannot be created!",
DIR_FINAL);
return NULL; return NULL;
} }
sprintf(DIR_FINAL, "%s/%s", prefix, DIR_NAME);
DIR *dir = opendir(DIR_FINAL);
if (!dir) {
if (mkdir(DIR_FINAL, 0755) != 0) {
fprintf(stderr, "'%s' cannot be created\n", DIR_FINAL);
free(DIR_FINAL);
return NULL;
}
} else {
closedir(dir);
} }
return DIR_FINAL; return DIR_FINAL;
} }
char* get_file(char* ROOT, char* FILE_NAME) {
char* FINAL_FILE;
FINAL_FILE = malloc(strlen(ROOT) + strlen(FILE_NAME) + 1);
sprintf(FINAL_FILE, "%s/%s", ROOT, FILE_NAME);
return FINAL_FILE;
} }
int main() int main(int argc, char* argv[])
{ {
const char* TASK_DIR = get_dir(home(), ".local/share", "task"); if (argc < 2) {
printf("Commands:\n\tadd <task>: add a new task.\n\tread: read all the tasks.\n\tdone <index>: mark a task as done.\n");
return 1;
}
printf("[DEBUG]: Tentando imprimir...\n"); char* DEF_DATA_DIR = get_dir(home(), DATA_DIR);
printf("%s", TASK_DIR); char* DEF_CONFIG_DIR = get_dir(home(), CONFIG_DIR);
free(TASK_DIR); char* DEF_TASK_FILE = get_file(DEF_DATA_DIR, TASK_FILE);
FILE *task_f;
if (strcmp(argv[1], "add") == 0) {
task_f = fopen(DEF_TASK_FILE, "a+");
if (!task_f) {
printf("error.");
return 1;
}
rewind(task_f);
int lines = 0;
char buffer[256];
while (fgets(buffer, sizeof(buffer), task_f)) {
lines++;
}
char* new_task = argv[2];
if (lines == 0) {
fprintf(task_f, " [ ] %s", new_task);
} else {
fprintf(task_f, "\n [ ] %s", new_task);
}
fclose(task_f);
} else if (strcmp(argv[1], "read") == 0) {
task_f = fopen(DEF_TASK_FILE, "r");
if (!task_f) {
printf("error.");
return 1;
}
int lines = 0;
char buffer[256];
while (fgets(buffer, sizeof(buffer), task_f)) {
printf("%d. %s", lines, buffer);
lines++;
}
rewind(task_f);
printf("\n");
fclose(task_f);
} else if (strcmp(argv[1], "done") == 0) {
task_f = fopen(DEF_TASK_FILE, "r");
if (!task_f) {
printf("error."); // im too lazy, ill do a better error message later.
return 1;
}
char *endptr;
long index = strtol(argv[2], &endptr, 10); // 10 = decimal base
if (*endptr != '\0') {
printf("Error: '%s' is not a valid number \n", argv[2]);
}
int lines = 0;
char buffer[256];
while (fgets(buffer, sizeof(buffer), task_f)) {
lines++;
}
rewind(task_f);
char **array = malloc(lines * sizeof(char*));
int n = 0;
while (fgets(buffer, sizeof(buffer), task_f)) {
array[n] = strdup(buffer);
n++;
}
fclose(task_f);
if (index < 0 || index >= n) {
printf("Error: task %ld does not exist", index);
return 1;
}
task_f = fopen(DEF_TASK_FILE, "w");
for (int i = 0; i < n; i++) {
if (i != index) fputs(array[i], task_f);
free(array[i]);
}
fclose(task_f);
}
free(DEF_DATA_DIR);
free(DEF_CONFIG_DIR);
free(DEF_TASK_FILE);
return 0; return 0;
} }