// Compile with: gcc task_tracker.c -Wall -O2 -m64 -lncurses -o task_tracker.x64 #include #include #include #include #include struct task_event { char* name; time_t start; time_t end; }; struct task_info { char* name; uint64_t total_seconds; }; int selected_task = -1; char** tasks_names = NULL; uint32_t tasks_names_size = 0; uint32_t tasks_names_capacity = 0; void add_task(char* name) { if (tasks_names_size >= tasks_names_capacity) { tasks_names_capacity = tasks_names_capacity == 0 ? 16 : tasks_names_capacity << 1; tasks_names = realloc(tasks_names, sizeof(char*) * tasks_names_capacity); /* uint32_t new_capacity = tasks_names_capacity == 0 ? 16 : tasks_names_capacity << 1; char** new_memory = malloc(sizeof(char*) * new_capacity); if (tasks_names != NULL) { memcpy(new_memory, tasks_names, sizeof(char*) * tasks_names_capacity); free(tasks_names); } tasks_names_capacity = new_capacity; tasks_names = (char *[])new_memory; */ } tasks_names[tasks_names_size] = name; tasks_names_size++; // TODO Missing the task_info array. } void add_task_event(char* name, uint64_t start, uint64_t end) { // TODO Missing the task_event array. } void fake_data() { const char* csv_head = "task_name,start,end"; const char* csv_line[] = { "PI-0000,1659479138,1659482738", "PI-0001,1659479127,1659482727", "XPTO-01 : testing long name,1659479138,1659482738", "Olá vamos cantar uma canção.,1659479127,1659482727", "[ TST-00 á],1659479138,1659482738", "[ XPTO- 01,1659479127,1659482727", "[ TST-00 á],1659479138,1659482738", "[ XPTO- 01,1659479127,1659482727", "[ TST-00 á],1659479138,1659482738", "[ XPTO- 01,1659479127,1659482727", "[ TST-00 á],1659479138,1659482738", "[ XPTO- 01,1659479127,1659482727", "[ TST-00 á],1659479138,1659482738", "[ XPTO- 01,1659479127,1659482727", "[ TST-00 á],1659479138,1659482738", "[ XPTO- 01,1659479127,1659482727", "[ TST-00 á],1659479138,1659482738", "[ XPTO- 01,1659479127,1659482727", "[ TST-00 á],1659479138,1659482738", "[ XPTO- 01,1659479127,1659482727", }; // const char* csv_line = "1659479138,1659482738"; // const char* csv_line = "zZ[ ã ]Yy,123"; // char name[] = { 'a', 'b', 'c', '\0' }; char* name; uint64_t start; uint64_t end; printf("--- --- àè.ªãº.éá --- ---\n"); uint32_t lines_count = sizeof(csv_line)/sizeof(char*); for (uint32_t idx = 0; idx < lines_count; idx++) { sscanf(csv_line[idx], "%m[^,\n],%" SCNu64 ",%" SCNu64, &name, &start, &end); add_task(name); printf("bazinga: %s : %d : %d\n", name, start, end); } printf("### ### --------- ### ###\n"); // tasks_names = // Free tasks names. for (uint32_t idx = tasks_names_size; idx > 0; idx--) { printf("> %s\n", tasks_names[idx-1]); // @dam TODO debug free(tasks_names[idx-1]); } free(tasks_names); return; /* char *token, *copy, *to_free; copy = to_free = strdup(csv_line); while ( token = strsep(&str, ",") ) { my_fn(token); } free(to_free); */ // tasks_names = malloc( ... time_t x = time(NULL); // x = (time_t)((1U << 32) - 1); printf("%s\n", ctime(&x)); x = x<<1; printf("%s\n", ctime(&x)); x += 1; printf("%s\n", ctime(&x)); x = x<<1; printf("%s\n", ctime(&x)); x = x<<1; printf("%s\n", ctime(&x)); x = x<<1; printf("%s\n", ctime(&x)); x = x<<1; printf("%s\n", ctime(&x)); } void draw_table() { struct task_info asd; } void draw_header() { const char *table_headers[] = { "Task", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun", "Total" }; const int table_headers_size = sizeof(table_headers)/sizeof(char*); int row,col; /* to store the number of rows and the number of colums of the screen */ initscr(); /* start the curses mode */ getmaxyx(stdscr,row,col); /* get the number of rows and columns */ mvaddch(0, 0, ACS_ULCORNER); for (int idx = 0; idx < table_headers_size; idx++) { if (idx > 0) { addch(ACS_TTEE); // printw(" %c", ACS_PLUS); } addch(ACS_HLINE); printw(" %s ", table_headers[idx]); addch(ACS_HLINE); } int cursor_y, cursor_x; getyx(stdscr, cursor_y, cursor_x); for (int idx = cursor_x; idx < col-1; idx++) { addch(ACS_HLINE); } addch(ACS_URCORNER); } void draw_footer() { const char *app_name = "Task Tracker"; const char *app_version = "v1.0"; int row, col; /* to store the number of rows and the number of colums of the screen */ initscr(); /* start the curses mode */ getmaxyx(stdscr,row,col); /* get the number of rows and columns */ printw("Row %d", row); mvaddch(row-1, 0, ACS_LLCORNER); addch(' '); printw(app_name); addch(' '); for (int idx = strlen(app_name) + 3; idx < col - strlen(app_version) - 3; idx ++) { addch(ACS_HLINE); } addch(' '); printw(app_version); addch(' '); addch(ACS_LRCORNER); } WINDOW *create_newwin(int height, int width, int starty, int startx); void destroy_win(WINDOW *local_win); int main(int argc, char *argv[]) { fake_data(); return 0; WINDOW *my_win; int startx, starty, width, height; int ch; initscr(); /* Start curses mode */ cbreak(); /* Line buffering disabled, Pass on * everty thing to me */ keypad(stdscr, TRUE); /* I need that nifty F1 */ curs_set(0); // Set cursor invisible. height = 3; width = 10; starty = (LINES - height) / 2; /* Calculating for a center placement */ startx = (COLS - width) / 2; /* of the window */ printw("Press F1 to exit"); refresh(); my_win = create_newwin(height, width, starty, startx); int rows; int colums; fake_data(); ch = KEY_RESIZE; do { switch(ch) { case KEY_RESIZE: erase(); break; case KEY_LEFT: destroy_win(my_win); my_win = create_newwin(height, width, starty,--startx); break; case KEY_RIGHT: destroy_win(my_win); my_win = create_newwin(height, width, starty,++startx); break; case KEY_UP: destroy_win(my_win); my_win = create_newwin(height, width, --starty,startx); break; case KEY_DOWN: destroy_win(my_win); my_win = create_newwin(height, width, ++starty,startx); break; } draw_header(); draw_table(); draw_footer(); } while((ch = getch()) != KEY_F(1)); endwin(); return 0; } WINDOW *create_newwin(int height, int width, int starty, int startx) { WINDOW *local_win; local_win = newwin(height, width, starty, startx); box(local_win, 0 , 0); /* 0, 0 gives default characters * for the vertical and horizontal * lines */ wrefresh(local_win); /* Show that box */ return local_win; } void destroy_win(WINDOW *local_win) { /* box(local_win, ' ', ' '); : This won't produce the desired * result of erasing the window. It will leave it's four corners * and so an ugly remnant of window. */ wborder(local_win, ' ', ' ', ' ',' ',' ',' ',' ',' '); /* The parameters taken are * 1. win: the window on which to operate * 2. ls: character to be used for the left side of the window * 3. rs: character to be used for the right side of the window * 4. ts: character to be used for the top side of the window * 5. bs: character to be used for the bottom side of the window * 6. tl: character to be used for the top left corner of the window * 7. tr: character to be used for the top right corner of the window * 8. bl: character to be used for the bottom left corner of the window * 9. br: character to be used for the bottom right corner of the window */ wrefresh(local_win); delwin(local_win); }