Skip to content

Commit

Permalink
added new function
Browse files Browse the repository at this point in the history
  • Loading branch information
ptfn committed Jul 2, 2024
1 parent f6ff9ca commit 697e89f
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 41 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ find_package(Curses REQUIRED)
include_directories(${CURSES_INCLUDE_DIR})

set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "~/.local/bin")
set(CMAKE_C_FLAGS "-g")

add_executable(tasker src/main.c src/commands.c src/graphics.c src/utils.c src/tasker.h)

target_link_libraries(tasker m ${CURSES_LIBRARIES})
86 changes: 52 additions & 34 deletions src/commands.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ void load_file(char *path)
}

/* Add Item */
void add_task(char *text, int8_t i) // int8_t normally? wtf not get arg
void add_task(char *text)
{
if (tasker->count < NUM_TASK) {
strcpy(tasker->task[tasker->count].name, text);
Expand All @@ -51,30 +51,48 @@ void add_task(char *text, int8_t i) // int8_t normally? wtf not get arg
}

/* Delete Item */
void del_task(int8_t i)
void del_task(cursor_t *cursor)
{
int id_task = i;
if (i < NUM_TASK && i >= 0) {
memset(&tasker->task[i], 0, sizeof(task));
for (int j = i; j < tasker->count-1; j++) {
struct task *temp = (struct task*)calloc(1, sizeof(struct task));
// MAYBE TAKE OUT FUNCTION
memcpy(temp, &tasker->task[j+1], sizeof(struct task));
memcpy(&tasker->task[j+1], &tasker->task[j], sizeof(struct task));
memcpy(&tasker->task[j], temp, sizeof(struct task));
free(temp);
int id_task = cursor->task;
if (id_task < NUM_TASK && id_task >= 0) { // why? cursor?
if (!cursor->status) {
// method zero under task
memset(&tasker->task[cursor->task], 0, sizeof(struct task)); // why memset?
for (int j = cursor->task; j < tasker->count-1; j++) {
swap(&tasker->task[j+1], &tasker->task[j], sizeof(struct task));
}
tasker->count--;
} else {
memset(&tasker->task[cursor->task].under[cursor->under], 0, sizeof(struct under));
for (int j = cursor->under; j < tasker->task[cursor->task].count-1; j++) {
swap(&tasker->task[cursor->task].under[j+1],
&tasker->task[cursor->task].under[j],
sizeof(struct under));
}
tasker->task[cursor->task].count--;
}
tasker->count--;
}
}

/* Add Under Task */
void new_under(char *text, int8_t i)
void new_under(char *text, cursor_t *cursor)
{
if (tasker->task[i].count < NUM_UNDER) {
strcpy(tasker->task[i].under[tasker->task[i].count].description, text);
tasker->task[i].under[tasker->task[i].count].time = time(NULL);
tasker->task[i].count++;
if (tasker->task[cursor->task].count < NUM_UNDER) {
strcpy(tasker->task[cursor->task].under[tasker->task[cursor->task].count].description, text);
tasker->task[cursor->task].under[tasker->task[cursor->task].count].time = time(NULL);
tasker->task[cursor->task].count++;
}
free(text);
}

void update_task(char *text, cursor_t *cursor)
{
if (!cursor->status) {
if (cursor->task < NUM_TASK)
strcpy(tasker->task[cursor->task].name, text);
} else {
if (cursor->under < NUM_UNDER)
strcpy(tasker->task[cursor->task].under[cursor->under].description, text);
}
free(text);
}
Expand All @@ -83,59 +101,56 @@ void new_under(char *text, int8_t i)
void command(enum keys key, WINDOW *win_input, cursor_t *cursor, bool *run)
{
// REPLACE ARGS "i" to CURSOR AND UNDER ELEMENT STRUCT //
// READY? //
switch (key) {
case ADD:
add_task(input(win_input, "add"), 1);
add_task(input(win_input, "add"));
message("Added task");
break;
case DEL:
del_task(1);
del_task(cursor);
message("Delete task");
break;
case UPD:
update_task(input(win_input, "upd"), cursor);
message("Update task");
break;
case SAVE:
fwrite(tasker, sizeof(task_t), 1, file);
rewind(file);
message("Save tasks");
break;
case NEW:
new_under(input(win_input, "new"), 1);
new_under(input(win_input, "new"), cursor);
message("Added under task");
break;
case EXIT:
quit(win_input, run);
break;
case UP:
// cursor
#if 0
*i = *i - 1;
*i = (*i < 0) ? tasker->count-1 : *i;
#endif
if (cursor->status) {
cursor->under = cursor->under - 1;
cursor->under = (cursor->under < 0) ? tasker->task[cursor->task].count-1 : cursor->under;
} else {
cursor->under = 0;
cursor->task = cursor->task - 1;
cursor->task = (cursor->task < 0) ? tasker->count-1 : cursor->task;
}
break;
case DWN:
#if 0
*i = *i + 1;
*i = (*i > tasker->count-1) ? 0 : *i;
#endif
if (cursor->status) {
cursor->under = cursor->under + 1;
cursor->under = (cursor->under > tasker->task[cursor->task].count-1) ? 0 : cursor->under;
} else {
cursor->under = 0;
cursor->task = cursor->task + 1;
cursor->task = (cursor->task > tasker->count-1) ? 0 : cursor->task;
}
break;
case TAB:
cursor->status = cursor->status ? 0 : 1;
// switch cursor
break;
}
}
}

/* Main Window */
Expand Down Expand Up @@ -188,7 +203,7 @@ void task(void)
box(task, 0, 0);
box(under, 0, 0);

mvwprintw(win_input, 0, 1, "%s", "[A] Add [D] Del [S] Save [N] New");
mvwprintw(win_input, 0, 1, "%s", "[A] Add [D] Del [S] Save [N] New [U] Upd");
mvwprintw(title, 0, round(max_std.x/100.0*PERC_TASK)+2, "Description");
mvwprintw(title, 0, max_std.x-12, "Date");
print_table(title, task, under, i, cursor);
Expand Down Expand Up @@ -227,7 +242,7 @@ void task(void)
case KEY_DOWN:
command(DWN, win_input, cursor, &run);
break;
case TAB_KEY:
case TAB_KEY: case KEY_LEFT: case KEY_RIGHT:
command(TAB, win_input, cursor, &run);
break;
case 'a': case 'A':
Expand All @@ -242,6 +257,9 @@ void task(void)
case 's': case 'S':
command(SAVE, win_input, cursor, &run);
break;
case 'u': case 'U':
command(UPD, win_input, cursor, &run);
break;
case 'q': case 'Q':
command(EXIT, win_input, cursor, &run);
break;
Expand Down
17 changes: 14 additions & 3 deletions src/graphics.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,18 +57,28 @@ void print_table(WINDOW *title, WINDOW *main, WINDOW *task, int8_t i, cursor_t *
while (t < tasker->count) {
size_t x = 0, c = 0, len = strlen(tasker->task[t].name);
mvwprintw(main, CBAR+y, CID, "%ld", t+1);

if (cursor->task == t) {
char buffer_time[12];
// new print under task //
for (uint16_t j = 0; j < tasker->task[cursor->task].count; j++) {
mvwprintw(task, j+1, 2, "%s", tasker->task[i].under[j].description);
strftime(buffer_time, 12, "%Y-%m-%d", localtime(&tasker->task[i].under[j].time));
if (cursor->under == j && cursor->status) {
wattron(task, COLOR_PAIR(3));
} else if (cursor->under == j && !cursor->status) {
wattron(task, COLOR_PAIR(4));
}

mvwprintw(task, j+1, 2, "%s", tasker->task[cursor->task].under[j].description);
strftime(buffer_time, 12, "%Y-%m-%d", localtime(&tasker->task[cursor->task].under[j].time));
mvwprintw(task, j+1, max_task.x-12, "%s", buffer_time);
wattroff(task, COLOR_PAIR(3));
wattroff(task, COLOR_PAIR(4));
}
}

if (cursor->task == t && !cursor->status) {
wattron(main, COLOR_PAIR(3));
} else if (cursor->task == t && cursor->status){
wattron(main, COLOR_PAIR(4));
}

while (c < len) {
Expand All @@ -79,6 +89,7 @@ void print_table(WINDOW *title, WINDOW *main, WINDOW *task, int8_t i, cursor_t *
x++; c++;
}
wattroff(main, COLOR_PAIR(3));
wattroff(main, COLOR_PAIR(4));
t++; y++;
}
wrefresh(stdscr);
Expand Down
9 changes: 5 additions & 4 deletions src/tasker.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ typedef struct cursor_t
} cursor_t;

/* Enum Commands Tasker */
enum keys {ADD, DEL, SAVE, NEW, EXIT, UP, DWN, TAB};
enum keys {ADD, DEL, UPD, SAVE, NEW, EXIT, UP, DWN, TAB};

/* Extern Global Variable */
extern task_t *tasker;
Expand All @@ -71,6 +71,7 @@ void display_menu(WINDOW *win_menu, uint16_t xMaxM,
char *item, char list[3][5]);
uint8_t menu(void);
FILE *open_file(char *fname, char *mode);
void swap(void *a, void *b, size_t l);
void new_file(char *path);
void load_file(char *path);
void display_open(WINDOW *win_open, size max_open, const char *title);
Expand All @@ -80,9 +81,9 @@ void print_table(WINDOW *title, WINDOW *main, WINDOW *task, int8_t i, cursor_t *
char *input(WINDOW *win, const char *command);
void quit(WINDOW *win_input, bool *run);
void command(enum keys key, WINDOW *win_input, cursor_t *cursor, bool *run);
void add_task(char *text, int8_t i);
void del_task(int8_t i);
void new_under(char *text, int8_t i);
void add_task(char *text);
void del_task(cursor_t *cursor);
void new_under(char *text, cursor_t *cursor);
void task(void);
void init(void);

Expand Down
10 changes: 10 additions & 0 deletions src/utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,13 @@ FILE *open_file(char *fname, char *mode)
return file;
}

void swap(void *a, void *b, size_t l)
{
// REPLACE memcpy to memccpy function
void *temp = calloc(1, l);
memcpy(temp, a, l);
memcpy(a, b, l);
memcpy(b, temp, l);
free(temp);
}

0 comments on commit 697e89f

Please sign in to comment.