Compare commits
10 Commits
3de70e9893
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7bc499f740 | ||
|
|
a23637a2c8 | ||
|
|
e8b7cb97a3 | ||
|
|
a78da65f0f | ||
|
|
18916e86cd | ||
|
|
a73b97101f | ||
|
|
7b829fde21 | ||
|
|
6f564d1f4e | ||
|
|
813d178521 | ||
|
|
5eaa8f8822 |
2
.gitignore
vendored
2
.gitignore
vendored
@@ -1,2 +1,4 @@
|
||||
task.o
|
||||
task
|
||||
libtoybox.a
|
||||
toybox.o
|
||||
|
||||
4
Makefile
4
Makefile
@@ -1,12 +1,12 @@
|
||||
CFLAGS := $(shell pkg-config --cflags glib-2.0)
|
||||
LDFLAGS := $(shell pkg-config --libs glib-2.0)
|
||||
HOME := $($HOME)
|
||||
HOME := $(HOME)
|
||||
|
||||
task:
|
||||
gcc task.c $(CFLAGS) -o task $(LDFLAGS)
|
||||
|
||||
clean:
|
||||
rm task
|
||||
rm -f task
|
||||
|
||||
install:
|
||||
mv task /usr/local/bin
|
||||
|
||||
47
README.md
Normal file
47
README.md
Normal 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
|
||||
15
README.txt
15
README.txt
@@ -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
|
||||
189
task.c
189
task.c
@@ -1,8 +1,8 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <pwd.h>
|
||||
#include <string.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
#include <dirent.h>
|
||||
@@ -13,6 +13,15 @@
|
||||
// - Organização por prioridade
|
||||
// - 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()
|
||||
{
|
||||
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_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
|
||||
|
||||
// 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()
|
||||
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] == '~') {
|
||||
perror("get_dir function doesn't expand '~'!");
|
||||
perror("get_dir doesn't expand '~'");
|
||||
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;
|
||||
}
|
||||
|
||||
char *DIR_FINAL;
|
||||
|
||||
// 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);
|
||||
}
|
||||
|
||||
const char *prefix = HOME ? HOME : "";
|
||||
|
||||
int size = strlen(prefix) + 1 + strlen(DIR_NAME) + 1;
|
||||
char *DIR_FINAL = malloc(size);
|
||||
if (!DIR_FINAL) {
|
||||
fprintf(stderr, "Return of %s in get_dir is NULL!",
|
||||
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);
|
||||
perror("malloc failed");
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
int main()
|
||||
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 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");
|
||||
printf("%s", TASK_DIR);
|
||||
char* DEF_DATA_DIR = get_dir(home(), DATA_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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user