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.o
|
||||||
task
|
task
|
||||||
|
libtoybox.a
|
||||||
|
toybox.o
|
||||||
|
|||||||
4
Makefile
4
Makefile
@@ -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
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
|
|
||||||
199
task.c
199
task.c
@@ -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
|
int size = strlen(prefix) + 1 + strlen(DIR_NAME) + 1;
|
||||||
if (HOME) {
|
char *DIR_FINAL = malloc(size);
|
||||||
int size = strlen(home()) + 1 + strlen(DIR_ROOT) + 1 + strlen(DIR_NAME);
|
if (!DIR_FINAL) {
|
||||||
DIR_FINAL = malloc(size);
|
perror("malloc failed");
|
||||||
DIR_FINAL[0] = '\0';
|
return NULL;
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
sprintf(DIR_FINAL, "%s/%s", prefix, DIR_NAME);
|
||||||
if (!DIR_FINAL) {
|
|
||||||
fprintf(stderr, "Return of %s in get_dir is NULL!",
|
DIR *dir = opendir(DIR_FINAL);
|
||||||
DIR_FINAL);
|
if (!dir) {
|
||||||
return NULL;
|
if (mkdir(DIR_FINAL, 0755) != 0) {
|
||||||
|
fprintf(stderr, "'%s' cannot be created\n", DIR_FINAL);
|
||||||
|
free(DIR_FINAL);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
const DIR *dir_exist = opendir(DIR_FINAL);
|
closedir(dir);
|
||||||
if (!dir_exist) {
|
}
|
||||||
const int status = mkdir(DIR_FINAL, 0755);
|
|
||||||
if (status != 0) {
|
return DIR_FINAL;
|
||||||
fprintf(stderr, "'%s' doesn't exist and cannot be created!",
|
}
|
||||||
DIR_FINAL);
|
|
||||||
return NULL;
|
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[])
|
||||||
|
{
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
char* DEF_DATA_DIR = get_dir(home(), DATA_DIR);
|
||||||
|
char* DEF_CONFIG_DIR = get_dir(home(), CONFIG_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++;
|
||||||
}
|
}
|
||||||
|
|
||||||
return DIR_FINAL;
|
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);
|
||||||
int main()
|
free(DEF_CONFIG_DIR);
|
||||||
{
|
free(DEF_TASK_FILE);
|
||||||
const char* TASK_DIR = get_dir(home(), ".local/share", "task");
|
|
||||||
|
|
||||||
printf("[DEBUG]: Tentando imprimir...\n");
|
|
||||||
printf("%s", TASK_DIR);
|
|
||||||
|
|
||||||
free(TASK_DIR);
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user