-
Notifications
You must be signed in to change notification settings - Fork 0
/
Todo.c
123 lines (108 loc) · 2.79 KB
/
Todo.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
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
// Exclusively for windows
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
int menu(int no_of_options, char* options[]);
int main()
{
printf("Welcome to the Todo App");
FILE* todo = fopen("todo.txt", "r");
if (todo == NULL)
{
char inp[200];
printf("No database yet, do you wanna create a todo entry?\n"\
"Type Yes/No to proceed!\n");
scanf("%s", inp);
if (strcmp("yes", inp))
todo = fopen("todo.txt", "w");
else if (strcmp("no", inp))
exit(EXIT_SUCCESS);
else
{
printf("You enterd wrong input\n Exiting!!!");
exit(EXIT_FAILURE);
}
}
char buffer[500];
char* options[4];
options[0] = "Create an entry!";
options[1] = "Edit an entry!";
options[2] = "Delete an entry!";
options[3] = "Exit!";
int o=-1;
while(o != 3)
{
o = menu(4, options);
if (o == 0)
{
todo = freopen("todo.txt", "a+", todo);
system("clear");
printf("Todo Items:-\n\n");
while (fgets(buffer, 499, todo))
puts(buffer);
char inp[501];
printf("The character limit is 500.\n\n");
fgets(inp, 500, stdin);
fputs(inp, todo);
} else if (o == 1)
{
todo = freopen("todo.txt", "r+", todo);
} else if (o == 2)
{
} else printf("\nBye!\n");
}
fclose(todo);
getch();
return 0;
}
int menu(int no_of_options, char* options[])
{
// two charachers to read arrow keys
char menu_ch1, menu_ch2;
// "a H" for KEY_UP
// "a P" for KEY_DOWN
// "a K" for KEY_LEFT
// "a M" for KEY_RIGHT
// lookup on Grey codes for more info
int option=1;
system("clear");
while(1)
{
for (int i=0; i<no_of_options; i++)
{
if (i == option)
printf("* ");
else printf(" ");
printf("%s\n", options[i]);
}
menu_ch1 = getch();
if (menu_ch1 == -32)
{
menu_ch2 = getch();
switch(menu_ch2)
{
case 'H': // UP
option--;
break;
case 'P': // DOWN
option++;
break;
case 'K': // LEFT
option--;
break;
case 'M': // RIGHT
option++;
break;
}
option%=no_of_options;
}
if (menu_ch1 == 'q')
exit(EXIT_SUCCESS);
else if (menu_ch1 == 13)
return option;
//printf("\e[1;1H\e[2J");
system("clear");
}
}