From a9aa980009f1d86e6ea5e45036fbca8fa44d53bf Mon Sep 17 00:00:00 2001 From: Marcin Zelent Date: Tue, 23 Jan 2018 11:49:14 +0100 Subject: Some refactoring. --- timetracker.c | 310 ++++++++++++++++++++++++++++------------------------------ 1 file changed, 149 insertions(+), 161 deletions(-) diff --git a/timetracker.c b/timetracker.c index 8bb702c..b07f029 100644 --- a/timetracker.c +++ b/timetracker.c @@ -7,136 +7,53 @@ #include #include -void print_new_activity(WINDOW *win); -void print_activities(WINDOW *win); -void start_new_activity(); -void stop_new_activity(); -void edit_new_activity(); -void save_to_file(char *filepath); -void load_file(char *filepath); -char *create_data_files(); - -typedef struct activity -{ - time_t start_time; - time_t end_time; - char description[100]; +typedef struct activity { + time_t start; + time_t end; + char name[100]; } activity; -activity new_activity; -activity activities_list[100]; - -int main(int argc, char *argv[]) -{ - char command; - - initscr(); - noecho(); - cbreak(); - keypad(stdscr, TRUE); - curs_set(0); - - strcpy(new_activity.description,"N/A"); - new_activity.start_time = time(NULL); - - load_file(create_data_files()); - - while(command != 'q') - { - WINDOW *cur_act_win = newwin(5, COLS, 0, 0); - wborder(cur_act_win, '|', '|', '-', '-', '+', '+', '+', '+'); - print_new_activity(cur_act_win); - wrefresh(cur_act_win); +activity new; +activity activities[100]; - WINDOW *past_act_win = newwin(LINES-5, COLS, 5, 0); - wborder(past_act_win, '|', '|', ' ', '-', '|', '|', '+', '+'); - print_activities(past_act_win); - wrefresh(past_act_win); - - command = wgetch(cur_act_win); - switch(command) - { - case 's': - start_new_activity(); - break; - case 'p': - stop_new_activity(); - break; - case 'e': - edit_new_activity(); - break; - case 'v': - save_to_file(create_data_files()); - break; - case 'q': - break; - default : - break; - } - } - endwin(); - return 0; -} -void print_new_activity(WINDOW *win) +void print_new(WINDOW *win) { time_t time_now = time(NULL); - char start_time[80]; + char start[80]; - strftime(start_time, sizeof(start_time), "%H:%M:%S", localtime(&new_activity.start_time)); + strftime(start, sizeof(start), "%H:%M:%S", localtime(&new.start)); + + mvwprintw(win, 1, 1, "Current activity: %.*s", COLS - 20, new.name); + mvwprintw(win, 2, 1, "Start time: %s", start); + mvwprintw(win, 3, 1, "Duration: %ld", (time_now - new.start)/60); - mvwprintw(win, 1, 1, "Current activity: %.*s", COLS-20, new_activity.description); - mvwprintw(win, 2, 1, "Start time: %s", start_time); - mvwprintw(win, 3, 1, "Duration: %ld", (time_now - new_activity.start_time)/60); - wrefresh(win); } void print_activities(WINDOW *win) { - char start_time[80], end_time[80]; + char start[80], end[80]; mvwprintw(win, 0, 1, "Past activities:"); - for(int i = 0; activities_list[i].start_time; i++) - { - strftime(start_time, sizeof(start_time), "%H:%M:%S", localtime(&activities_list[i].start_time)); - strftime(end_time, sizeof(end_time), "%H:%M:%S", localtime(&activities_list[i].end_time)); - - mvwprintw(win, 4 * i + 1, 1, "Start time: %s", start_time); - mvwprintw(win, 4 * i + 2, 1, "End time: %s", end_time); - mvwprintw(win, 4 * i + 3, 1, "Activity: %.*s", COLS-13, activities_list[i].description); + for (int i = 0; activities[i].start; i++) { + strftime(start, sizeof(start), "%H:%M:%S", + localtime(&activities[i].start)); + strftime(end, sizeof(end), "%H:%M:%S", + localtime(&activities[i].end)); + + mvwprintw(win, 4 * i + 1, 1, "Start time: %s", start); + mvwprintw(win, 4 * i + 2, 1, "End time: %s", end); + mvwprintw(win, 4 * i + 3, 1, "Activity: %.*s", COLS - 13, + activities[i].name); } - - wrefresh(win); -} -void start_new_activity() -{ - new_activity.start_time = time(NULL); - memset(&new_activity.description[0], 0, sizeof(new_activity.description)); - edit_new_activity(); -} - -void stop_new_activity() -{ - new_activity.end_time = time(NULL); - - for(int i = 0; i < 100; i++) - { - if(!activities_list[i].start_time) - { - activities_list[i] = new_activity; - strcpy(new_activity.description, "N/A"); - break; - } - } - - save_to_file(create_data_files()); + wrefresh(win); } -void edit_new_activity() +void edit_new() { - WINDOW *win = newwin(10, 50, (LINES-10)/2, (COLS-50)/2); + WINDOW *win = newwin(10, 50, (LINES - 10) / 2, (COLS - 50) / 2); FORM *form; FIELD *field[2]; int ch, rows, cols; @@ -152,37 +69,35 @@ void edit_new_activity() mvwprintw(win, 1, 1, "What are you doing: "); wmove(win, 2, 1); curs_set(1); - while(ch != 10 && ch != 27) - { + while (ch != 10 && ch != 27) { ch = wgetch(win); - switch(ch) - { - /* - case KEY_LEFT: - form_driver(form, REQ_PREV_CHAR); - break; - case KEY_RIGHT: - form_driver(form, REQ_NEXT_CHAR); - break; - case KEY_UP: - form_driver(form, REQ_NEXT_LINE); - break; - case KEY_DOWN: - form_driver(form, REQ_PREV_LINE); - break; - */ - case 127: - form_driver(form, REQ_PREV_CHAR); - form_driver(form, REQ_DEL_CHAR); - new_activity.description[strlen(new_activity.description)-1] = '\0'; - break; - case 27: - memset(&new_activity.description[0], 0, sizeof(new_activity.description)); - break; - default: - form_driver(form, ch); - new_activity.description[strlen(new_activity.description)] = ch; - break; + switch (ch) { + /* + case KEY_LEFT: + form_driver(form, REQ_PREV_CHAR); + break; + case KEY_RIGHT: + form_driver(form, REQ_NEXT_CHAR); + break; + case KEY_UP: + form_driver(form, REQ_NEXT_LINE); + break; + case KEY_DOWN: + form_driver(form, REQ_PREV_LINE); + break; + */ + case 127: + form_driver(form, REQ_PREV_CHAR); + form_driver(form, REQ_DEL_CHAR); + new.name[strlen(new.name) - 1] = '\0'; + break; + case 27: + memset(&new.name[0], 0, sizeof(new.name)); + break; + default: + form_driver(form, ch); + new.name[strlen(new.name)] = ch; + break; } } curs_set(0); @@ -190,7 +105,26 @@ void edit_new_activity() free_form(form); free_field(field[0]); delwin(win); - new_activity.description[strcspn(new_activity.description, "\n")] = 0; + new.name[strcspn(new.name, "\n")] = 0; +} + +void start_new() +{ + new.start = time(NULL); + memset(&new.name[0], 0, sizeof(new.name)); + edit_new(); +} + +void save_to_file(char *filepath) +{ + FILE *fp; + + fp = fopen(filepath, "w"); + for (int i = 0; activities[i].start; i++) { + fprintf(fp, "%ld;%ld;%s\n", activities[i].start, + activities[i].end, activities[i].name); + } + fclose(fp); } char *create_data_files() @@ -198,18 +132,17 @@ char *create_data_files() FILE *fp; time_t rawtime; struct tm *info; - char timesheets_dir[80], date[11]; - static char filepath[80]; + char dir[80], date[11]; + static char filepath[80]; struct stat st = {0}; - - snprintf(timesheets_dir, sizeof(timesheets_dir), "%s/Timesheets", getenv("HOME")); - if(stat(timesheets_dir, &st) == -1) mkdir(timesheets_dir, 0700); - + + snprintf(dir, sizeof(dir), "%s/Timesheets", getenv("HOME")); + if (stat(dir, &st) == -1) mkdir(dir, 0700); + time(&rawtime); strftime(date, sizeof(date), "%Y-%m-%d", localtime(&rawtime)); - snprintf(filepath, sizeof(filepath), "%s/timesheet-%s.txt", timesheets_dir, date); - if(access(filepath, F_OK) == -1) - { + snprintf(filepath, sizeof(filepath), "%s/timesheet-%s.txt", dir, date); + if (access(filepath, F_OK) == -1) { fp = fopen(filepath, "w"); fclose(fp); } @@ -217,26 +150,81 @@ char *create_data_files() return filepath; } -void save_to_file(char *filepath) +void stop_new() { - FILE *fp; + new.end = time(NULL); - fp = fopen(filepath, "w"); - for(int i = 0; activities_list[i].start_time; i++) - { - fprintf(fp, "%ld;%ld;%s\n", activities_list[i].start_time, activities_list[i].end_time, - activities_list[i].description); + for(int i = 0; i < 100; i++) { + if (!activities[i].start) { + activities[i] = new; + strcpy(new.name, "N/A"); + break; + } } - fclose(fp); + + save_to_file(create_data_files()); } void load_file(char *filepath) { FILE *fp; int i; - + fp = fopen(filepath, "r"); - while (EOF != fscanf(fp, "%ld;%ld;%[^\n]", &activities_list[i].start_time, - &activities_list[i].end_time, activities_list[i].description)) i++; + while (EOF != fscanf(fp, "%ld;%ld;%[^\n]", &activities[i].start, + &activities[i].end, activities[i].name)) + i++; fclose(fp); } + + +int main(int argc, char *argv[]) +{ + char command; + + initscr(); + noecho(); + cbreak(); + keypad(stdscr, TRUE); + curs_set(0); + + strcpy(new.name,"N/A"); + new.start = time(NULL); + + load_file(create_data_files()); + + while(command != 'q') { + WINDOW *cur_act_win = newwin(5, COLS, 0, 0); + wborder(cur_act_win, '|', '|', '-', '-', '+', '+', '+', '+'); + print_new(cur_act_win); + wrefresh(cur_act_win); + + WINDOW *past_act_win = newwin(LINES-5, COLS, 5, 0); + wborder(past_act_win, '|', '|', ' ', '-', '|', '|', '+', '+'); + print_activities(past_act_win); + wrefresh(past_act_win); + + command = wgetch(cur_act_win); + switch(command) { + case 's': + start_new(); + break; + case 'p': + stop_new(); + break; + case 'e': + edit_new(); + break; + case 'v': + save_to_file(create_data_files()); + break; + case 'q': + break; + default : + break; + } + } + endwin(); + + return 0; +} -- cgit v1.2.3