-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
231 lines (187 loc) · 5.23 KB
/
main.c
File metadata and controls
231 lines (187 loc) · 5.23 KB
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
#include <ncurses.h>
#include <string.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
/**
$$$$ $$$$$$ $$$$ $$$$$$ $$$$$$
$$ $$ $$ $$$$ $$ $$
$$ $$ $$ $$$$$$ $$
$$ $$ $$ $$$$ $$ $$
$$$$$$ $$$$$$ $$$$ $$$$$$ $$
*/
WINDOW *time;
WINDOW *helpers;
WINDOW *status;
char *n0[] = { "$$$$$$", "$$ $$", "$$ $$", "$$ $$", "$$$$$$" };
char *n1[] = { "$$$$ ", " $$ ", " $$ ", " $$ ", "$$$$$$" };
char *n2[] = { "$$$$$$", " $$", "$$$$$$", "$$ ", "$$$$$$" };
char *n3[] = { "$$$$$$", " $$", "$$$$$$", " $$", "$$$$$$" };
char *n4[] = { "$$ $$", "$$ $$", "$$$$$$", " $$", " $$" };
char *n5[] = { "$$$$$$", "$$ ", "$$$$$$", " $$", "$$$$$$" };
char *n6[] = { "$$$$$$", "$$ ", "$$$$$$", "$$ $$", "$$$$$$" };
char *n7[] = { "$$$$$$", " $$", " $$", " $$", " $$" };
char *n8[] = { "$$$$$$", "$$ $$", "$$$$$$", "$$ $$", "$$$$$$" };
char *n9[] = { "$$$$$$", "$$ $$", "$$$$$$", " $$", "$$$$$$" };
char *ds[] = { "$$$$", "$$$$", " ", "$$$$", "$$$$" };
char **digits[] = { n0, n1, n2, n3, n4, n5, n6, n7, n8, n9 };
int time_data[] = { 2, 5, 0, 0 };
int intervals[] = { 25, 5, 25, 5, 25, 5, 25, 15 }; // time in minutes
int active_i = 0;
// print the current status
void print_status() {
char *status_type[] = { "WORK", "BREAK", "WORK", "BREAK", "WORK", "BREAK", "WORK", "LONG BREAK" };
char *additional_message[] = { "Let's do this!", "You deserve a break!" };
// status and message positions
int center_status = (44 - strlen(status_type[active_i])) / 2;
int center_message = (44 - strlen(additional_message[active_i % 2])) / 2;
// clear lines
mvwprintw(status, 0, 0, " ");
mvwprintw(status, 1, 0, " ");
// put status and message
mvwprintw(status, 0, center_status, "%s", status_type[active_i]);
mvwprintw(status, 1, center_message, "%s", additional_message[active_i % 2]);
wrefresh(status);
}
// prints a digit
void print_n(char *n[], int position_x) {
int y = 2;
for (int i = 0; i < 5; i++) {
mvwprintw(time, y++, position_x, "%s", (char*)n[i]);
}
}
// print dots between digits
void print_dots() {
int x = 20;
int y = 2;
for (int i = 0; i < 5; i++) {
mvwprintw(time, y++, x, "%s", ds[i]);
}
}
// print all digits on the screen
void print_timer() {
int positions_x[] = { 4, 12, 26, 34 };
for (int i = 0; i < 4; i++) {
print_n(digits[time_data[i]], positions_x[i]);
}
print_status();
wrefresh(time);
}
void print_before_start_text() {
char *helper_lines[6];
helper_lines[0] = "[s] Start Timer / Resume";
helper_lines[1] = "[p] Pause";
helper_lines[2] = "[r] Restart Timer";
helper_lines[3] = "[n] Next Interval";
helper_lines[4] = "[t] Test Bell Sound"; // verify file exists and play
helper_lines[5] = "[q] Quit";
for (int y = 0; y < 6; y++) {
mvwprintw(helpers, y, 0, "%s", helper_lines[y]);
}
wrefresh(time);
wrefresh(helpers);
}
// play a sound after finishing timer
void triiimm() {
system("aplay -q ~/.config/pmd-cli/sounds/magiaz-campainha-331260.wav");
}
// set up next time minutes
void next_phase() {
active_i++;
if (active_i == 8) active_i = 0;
time_data[0] = intervals[active_i] / 10;
time_data[1] = intervals[active_i] % 10;
time_data[2] = 0;
time_data[3] = 0;
print_timer();
}
// prepare timer to start again current session
void restart_timer() {
active_i = active_i - 1;
if (active_i == -1) {
active_i = 3;
}
next_phase();
}
// starts timer
void run_timer(int minutes) {
int key = 'a';
int deci = 0;
while (key != 'p') {
key = wgetch(time);
if (deci == 10) {
deci = 0;
time_data[3]--;
}
if (time_data[3] == -1) {
time_data[3] = 9;
time_data[2]--;
}
if (time_data[2] == -1) {
time_data[2] = 5;
time_data[1]--;
}
if (time_data[1] == -1) {
time_data[1] = 9;
time_data[0]--;
}
print_timer();
wrefresh(time);
deci++;
// finish when everything is zero
if (time_data[0] == 0 && time_data[1] == 0 && time_data[2] == 0 && time_data[3] == 0) {
triiimm();
next_phase();
break;
}
}
}
int main() {
int start_y, start_x, max_y, max_x;
int width = 44; // 4 spaces at each x
int height = 9; // 3 space at each y
initscr();
noecho();
raw();
cbreak();
halfdelay(1);
getmaxyx(stdscr, max_y, max_x);
start_y = (max_y - height) / 2 - 4;
start_x = (max_x - width) / 2;
time = newwin(height, width, start_y, start_x);
box(time, 0, 0);
// on start, print 25:00
print_n(n2, 4);
print_n(n5, 12);
print_dots();
print_n(n0, 26);
print_n(n0, 34);
helpers = newwin(6, width - 6, start_y + height + 1, start_x + 9);
print_before_start_text();
status = newwin(2, width, start_y - 3, start_x);
wrefresh(status);
wrefresh(time);
while (true) {
curs_set(0);
int key = wgetch(helpers);
wrefresh(helpers);
switch (key) {
case 'q':
return 0;
case 's':
run_timer(25);
break;
case 't':
triiimm();
break;
case 'n':
next_phase();
break;
case 'r':
restart_timer();
break;
}
}
endwin();
return 0;
}