-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclock.c
More file actions
85 lines (70 loc) · 1.76 KB
/
clock.c
File metadata and controls
85 lines (70 loc) · 1.76 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
// clock is an analog clock in your terminal
// written by reese (reesporte.github.io)
#include <math.h>
#include <ncurses.h>
#include <time.h>
#include <unistd.h>
#define PI 3.1415926535
#define OFFSET 20
#define RADIUS 18
#define MIDX OFFSET
#define MIDY OFFSET / 2
#define GREEN 1
typedef struct {
int x;
int y;
} coord;
// getCoord gets a coordinate on the clock based on the number of degrees `i`
void getCoord(double i, coord* c) {
// taking the ceiling so my clock runs a little fast :)
c->x = MIDX + (int)ceil(RADIUS * cos((i * (PI / 180)) - (PI / 2)));
c->y = MIDY + (int)ceil((RADIUS / 2) * sin((i * (PI / 180)) - (PI / 2)));
}
// showClock shows a clock
void showClock(int hour, int min, int sec) {
char buf[3];
coord c;
// the clock circle
for (int i = 0; i < 12; i++) {
getCoord(i * 30, &c);
if (i == 0) {
sprintf(buf, "%d", 12);
} else {
sprintf(buf, "%d", i);
}
mvprintw(c.y, c.x, buf);
}
// use color for the other stuff
attron(COLOR_PAIR(GREEN));
// midpoint
mvprintw(MIDY, MIDX, "*");
// hour hand
getCoord(((hour * 60) + min) / 2, &c);
mvprintw(c.y, c.x, "h");
// minute hand
getCoord(min * 6, &c);
mvprintw(c.y, c.x, "m");
// second hand
getCoord(sec * 6, &c);
mvprintw(c.y, c.x, "s");
// turn off color for next time
attroff(COLOR_PAIR(GREEN));
}
int main() {
time_t now;
struct tm* t;
initscr();
curs_set(0);
start_color();
init_pair(GREEN, COLOR_GREEN, COLOR_BLACK);
while (1) {
time(&now);
t = localtime(&now);
clear();
showClock(t->tm_hour % 12, t->tm_min, t->tm_sec);
refresh();
sleep(1);
}
endwin();
return 0;
}