-
Notifications
You must be signed in to change notification settings - Fork 0
/
timer.c
77 lines (64 loc) · 1.7 KB
/
timer.c
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
void displaySymbols(int count);
int calculateNumberOfSymbolsToDisplay(int time, int secondsPerSymbol);
void displayEmptySpaces(int count);
void displayHelp();
int main(int argc, char **argv){
char arguments;
int timeInMinutes;
char *message;
int seconds;
float secondsPerSymbol;
int timeElapsed = 0;
float totalSymbols = 20.0;
while((arguments = getopt(argc, argv, "t:hm:")) != -1){
if(arguments == 't') {
timeInMinutes = atoi(optarg);
}
else if(arguments == 'm') {
message = optarg;
}
else if(arguments == 'h') {
displayHelp();
return 0;
}
}
seconds = timeInMinutes * 60;
secondsPerSymbol = seconds / totalSymbols;
while(timeElapsed <= seconds){
int numberOfSymbolsToDisplay = (timeElapsed / secondsPerSymbol);
printf("\r%s: %02d [", message, seconds - timeElapsed);
sleep(1);
displaySymbols(numberOfSymbolsToDisplay);
displayEmptySpaces(totalSymbols - numberOfSymbolsToDisplay);
printf("]");
timeElapsed++;
fflush(stdout);
}
printf("\n");
return 0;
}
void displaySymbols(int count){
for(int i = 0; i <= count; i++){
printf("=");
}
}
int calculateNumberOfSymbolsToDisplay(int time, int secondsPerSymbol){
int total = 0;
for(int i = 0; i <= time; i += secondsPerSymbol){
if(i < time) total++;
}
return total;
}
void displayEmptySpaces(int count){
for(int i = 1; i < count; i++){
printf(" ");
}
}
void displayHelp(){
printf("\nUsage: timer -t (time) -m (message) [-h]\n");
printf("example: timer -t 5 -m \"starting soon\"\n");
printf("\noptions\n\n");
printf("-t time in minutes to count down");
printf("-m message to display next to counter");
printf("-h this help message\n");
}