-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
159 lines (133 loc) · 4.18 KB
/
main.c
File metadata and controls
159 lines (133 loc) · 4.18 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
#include <stdio.h>
#include <stdlib.h>
#include <termios.h>
#include <unistd.h>
#include <time.h>
#include <signal.h>
#define SESSION_WORK_TIME_MINUTES 25
#define SESSION_BREAK_SHORT_MINUTES 5
#define SESSION_BREAK_LONG_MINUTES 15
#define COLOR_CYAN "\033[38;5;81m"
#define COLOR_RED "\033[31m"
#define COLOR_RESET "\033[0m"
typedef enum {
SESSION_WORK,
SESSION_BREAK_SHORT,
SESSION_BREAK_LONG
} SessionType;
typedef struct {
SessionType session;
int seconds_remaining;
int pomodoros_completed;
int paused;
} PomodoroState;
static struct termios original_termios;
void enable_raw_mode(void) {
if (tcgetattr(STDIN_FILENO, &original_termios) == -1) {
perror("tcgetattr");
exit(1);
}
struct termios raw = original_termios;
raw.c_lflag &= ~(ICANON | ECHO); // Disable canonical mode and echo
raw.c_cc[VMIN] = 0; // Return immediately
raw.c_cc[VTIME] = 0; // No timeout
if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &raw) == -1) {
perror("tcsetattr");
exit(1);
}
}
void disable_raw_mode(void) {
tcsetattr(STDIN_FILENO, TCSAFLUSH, &original_termios);
}
long get_elapsed_time(const struct timespec *last, const struct timespec *now) {
long const elapsed_ns = (now->tv_sec - last->tv_sec) * 1000000000L + (now->tv_nsec - last->tv_nsec);
return elapsed_ns;
}
void next_session(PomodoroState *state) {
if (state->session == SESSION_WORK) {
state->pomodoros_completed++;
if (state->pomodoros_completed % 4 == 0) {
state->session = SESSION_BREAK_LONG;
state->seconds_remaining = SESSION_BREAK_LONG_MINUTES * 60;
} else {
state->session = SESSION_BREAK_SHORT;
state->seconds_remaining = SESSION_BREAK_SHORT_MINUTES * 60;
}
} else {
state->session = SESSION_WORK;
state->seconds_remaining = SESSION_WORK_TIME_MINUTES * 60;
}
}
void update_display(const PomodoroState *state) {
const char *label;
switch (state->session) {
case SESSION_WORK: label = "WORK"; break;
case SESSION_BREAK_SHORT: // Fallthrough
case SESSION_BREAK_LONG: label = "BREAK"; break;
default: label = "UNKNOWN"; break;
}
int const m = state->seconds_remaining / 60;
int const s = state->seconds_remaining % 60;
printf("\033c\033[?25l"); // Clear terminal and hide cursor
if (state->session == SESSION_WORK) {
printf("%s[%s]%s ", COLOR_CYAN, label, COLOR_RESET);
} else {
printf("%s[%s]%s ", COLOR_RED, label, COLOR_RESET);
}
printf("%02d:%02d | Pomodoros: %d ", m, s, state->pomodoros_completed);
if (state->paused) {
printf("[PAUSED]");
}
fflush(stdout);
}
void main_loop(void) {
PomodoroState state = {
.session = SESSION_WORK,
.seconds_remaining = SESSION_WORK_TIME_MINUTES * 60,
.pomodoros_completed = 0,
.paused = 0
};
struct timespec last, now;
clock_gettime(CLOCK_MONOTONIC, &last);
while (1) {
// Input
char c;
if (read(STDIN_FILENO, &c, 1) == 1) {
if (c == ' ') state.paused = !state.paused;
if (c == 'q') break;
}
// Increment time
if (state.paused) {
clock_gettime(CLOCK_MONOTONIC, &last);
}
clock_gettime(CLOCK_MONOTONIC, &now);
long const elapsed_ns = get_elapsed_time(&last, &now);
if (!state.paused && elapsed_ns > 1000000000L) {
last = now;
state.seconds_remaining--;
if (state.seconds_remaining <= 0) {
next_session(&state);
}
}
update_display(&state);
struct timespec sleep_ts = { .tv_sec = 0, .tv_nsec = 10000000 };
(void)nanosleep(&sleep_ts, NULL);
}
}
void handle_sigint(int const sig) {
(void)sig;
disable_raw_mode();
write(STDOUT_FILENO, "\033[?25h\n", 7);
_exit(0);
}
int main(void) {
atexit(disable_raw_mode);
enable_raw_mode();
// Handle termination
signal(SIGINT, handle_sigint);
signal(SIGTERM, handle_sigint);
signal(SIGHUP, handle_sigint);
main_loop();
printf("\033[?25h\n"); // Show cursor
return 0;
}