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
|
#include <time.h>
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <stdlib.h>
#define DATEFMT "%a %d %b %Y"
#define SEP " ~> "
typedef struct event {
char *name;
time_t time;
} event;
static bool cal = true;
static const char *days[] = {
"Su",
"Mo",
"Tu",
"We",
"Th",
"Fr",
"Sa"
};
static const char *months[] = {
"Jan",
"Feb",
"Mar",
"Apr",
"May",
"Jun",
"Jul",
"Aug",
"Sep",
"Oct",
"Nov",
"Dec"
};
static const int daysinmonth[] = {
31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
};
static bool
isleap(int year) {
if ((year % 4) == 0 && (year % 100) != 0)
return true;
else if ((year % 400) == 0)
return true;
return false;
}
static void
print_month(struct tm *t) {
printf(" %s %d\n", months[t->tm_mon], t->tm_year + 1900);
}
static void
print_days() {
for (int i = 0; i < 7; i++)
printf("%s ", days[i]);
putchar('\n');
}
static void
print_padded(int n, int l) {
int i = 1;
loop:
if ((n / i) > 0) {
i *= 10;
l--;
goto loop;
}
if (l != 0)
printf("%*s", l, " ");
if (l < 0)
return;
printf("%d", n);
}
static void
print_month_grid(struct tm *t) {
int day = 1;
int total = daysinmonth[t->tm_mon];
if (t->tm_mon == 1 && isleap(t->tm_year + 1900))
total += 1;
int offset = (t->tm_yday % 7) - 1;
for (int i = 0; i < offset; i++)
printf(" ");
for (; day < total + 1; day++) {
if (day == t->tm_mday) {
printf("\e[4;34m");
print_padded(day, 2);
printf("\e[0m");
} else
print_padded(day, 2);
putchar(' ');
if (((day + offset) % 7) == 0)
putchar('\n');
}
putchar('\n');
}
static void
print_cal(struct tm *t) {
print_month(t);
print_days();
print_month_grid(t);
}
static event *
read_event(FILE *f) {
event *e = malloc(sizeof(event));
char buf[256] = {0};
if (!fgets(buf, 256, f)) {
free(e);
return NULL;
}
char *time = buf;
char *name = strchr(buf, '|');
if (!name) {
fprintf(stderr, "invalid event! %s\n", buf);
exit(1);
}
*name = 0;
name++;
e->name = strdup(name);
e->time = atoll(time);
return e;
}
static void
print_event(event *e) {
struct tm *t = localtime(&e->time);
char buf[256];
strftime(buf, 256, DATEFMT, t);
printf("%s" SEP "%s", buf, e->name);
}
static void
free_event(event *e) {
free(e->name);
free(e);
}
int
main(int argc, char **argv) {
if (argc > 1)
if (strcmp(argv[1], "-h") == 0)
cal = false;
time_t ulocal = time(NULL);
struct tm *local = localtime(&ulocal);
if (cal)
print_cal(local);
bool dirty = false;
event *e;
while (e = read_event(stdin)) {
if (ulocal + (86400 * 30) > e->time) {
print_event(e);
dirty = true;
}
free_event(e);
}
if (!dirty)
printf("You have nothing upcoming\n");
return 0;
}
|