-
Notifications
You must be signed in to change notification settings - Fork 0
/
nobudget.c
214 lines (187 loc) · 3.37 KB
/
nobudget.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
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#include "nobudget.h"
int lenght = 0;
int longest = 0;
char title[] = "Angry Cow - Definitely Not a Cloud";
char footer_left[] = "pmr.angrycow.ru";
char footer_right[] = "nobudget v0.0.1";
int left_column_length = 28;
struct menu_ui start_menu;
char *choices[] = {
"N E W G U E S T",
"M A N A G E G U E S T S",
"S U P P O R T",
"Q U I T"};
int n_choices = sizeof(choices) / sizeof(char *);
int menu_is_active = 1;
void startwin()
{
initscr();
// debug mode allows ^C ^Z
cbreak();
//raw();
keypad(stdscr, TRUE);
noecho();
curs_set(0);
return;
}
int main()
{
int highlight = 1;
int choice = 0;
int c;
startwin();
int i;
for (i = 0; i < n_choices; ++i)
{
lenght = strlen(choices[i]);
if (lenght > longest)
longest = lenght;
}
print_menu(highlight);
while (menu_is_active)
{
c = getch();
switch (c)
{
case KEY_UP:
if (highlight > 1)
--highlight;
break;
case KEY_DOWN:
if (highlight < n_choices)
++highlight;
break;
// ascii code for ENTER
case 10:
{
choice = highlight;
break;
}
// ascii code for ESC
case 27:
{
menu_is_active = 0;
break;
}
}
menu_handler(choice);
choice = 0;
print_menu(highlight);
// if(choice != 0) /* User did a choice come out of the infinite loop */
// break;
}
// restore initial terminal settings
endwin();
return 0;
}
void print_menu(int highlight)
{
int x, y, i;
erase();
attron(A_BOLD);
mvprintw(0, (COLS - strlen(title)) / 2 - 2, "%s", title);
attroff(A_BOLD);
x = (COLS - longest) / 2 - 2;
y = (LINES - n_choices) / 2 - 1 ;
for (i = 0; i < n_choices; ++i)
{
if (highlight == i + 1) /* High light the present choice */
{
attron(A_REVERSE);
mvprintw(y, x, "%s", choices[i]);
attroff(A_REVERSE);
}
else
{
mvprintw(y, x, "%s", choices[i]);
}
// we want an empty line between the menu entries
++y;
++y;
}
mvprintw(LINES - 1, 1, "%s", footer_left);
mvprintw(LINES - 1, COLS - strlen(footer_right) - 1, "%s", footer_right);
refresh();
}
void menu_handler(int selected_item)
{
/* Processes user's menu item choice
Called in:
nobudget.c : main()
Args:
selected_item(int): a number of menu item selected
Returns:
None
*/
switch (selected_item)
{
case 1:
{
// NEW GUEST
endwin();
system("/usr/local/bin/nobudget-new-guest.ksh");
startwin();
break;
}
case 2:
{
// MANAGE GUESTS
endwin();
system("/usr/local/bin/nobudget-manage-guests.ksh");
startwin();
break;
}
case 3:
{
// SUPPORT
endwin();
//display_text("/usr/local/lib/support.txt");
system("/usr/local/bin/nobudget-display-support.ksh");
startwin();
break;
}
case 4:
{
// QUIT
endwin();
exit(0);
}
default:
{
// IDLING
break;
}
}
}
void display_text(char *support_file_path)
{
/* Displays content of support file on screen
Called in:
nobudget.c : menu_handler()
Args:
support_file_path(char *): a path to text file
Returns:
None
*/
// open a file
FILE *fp;
char *line = NULL;
size_t len = 0;
ssize_t read;
fp = fopen(support_file_path, "r");
// handle an exceprion when file cannot be opened
if (fp == NULL)
{
exit(EXIT_FAILURE);
}
// clear screen before printing output
erase();
// output the text line by line
int current_line = 1;
while ((read = getline(&line, &len, fp)) != -1)
{
mvprintw(current_line, 1, line);
++current_line;
}
fclose(fp);
}