1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
|
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <ncurses.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <panel.h>
void print_new_activity(WINDOW *win);
void print_activities(WINDOW *win);
WINDOW *start_new_activity();
void stop_new_activity();
WINDOW *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];
} activity;
activity new_activity;
activity activities_list[100];
int main(int argc, char *argv[])
{
char command;
WINDOW *new_act_win;
initscr();
noecho();
cbreak();
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);
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':
new_act_win = 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)
{
time_t time_now = time(NULL);
char start_time[80];
strftime(start_time, sizeof(start_time), "%H:%M:%S", localtime(&new_activity.start_time));
mvwprintw(win, 1, 1, "Current activity: %s", 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];
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", activities_list[i].description);
}
wrefresh(win);
}
WINDOW *start_new_activity()
{
new_activity.start_time = time(NULL);
WINDOW *win = newwin(10, 50, (LINES-10)/2, (COLS-50)/2);
wborder(win, '|', '|', '-', '-', '+', '+', '+', '+');
mvwprintw(win, 1, 1, "What are you doing: ");
echo();
mvwgetstr(win, 2, 1, new_activity.description);
noecho();
new_activity.description[strcspn(new_activity.description, "\n")] = 0;
wrefresh(win);
return win;
}
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;
}
}
}
WINDOW *edit_new_activity()
{
WINDOW *win = newwin(10, 50, (LINES-10)/2, (COLS-50)/2);
wborder(win, '|', '|', '-', '-', '+', '+', '+', '+');
mvwprintw(win, 1, 1, "What are you doing: ");
echo();
mvwgetstr(win, 2, 1, new_activity.description);
noecho();
new_activity.description[strcspn(new_activity.description, "\n")] = 0;
wrefresh(win);
return win;
}
char *create_data_files()
{
FILE *fp;
time_t rawtime;
struct tm *info;
char timesheets_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);
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)
{
fp = fopen(filepath, "w");
fclose(fp);
}
return filepath;
}
void save_to_file(char *filepath)
{
FILE *fp;
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);
}
fclose(fp);
}
void load_file(char *filepath)
{
FILE *fp;
int i;
fp = fopen(filepath, "r");
while (EOF != fscanf(fp, "%ld;%ld;%s", &activities_list[i].start_time, &activities_list[i].end_time,
activities_list[i].description)) i++;
}
|