-
Notifications
You must be signed in to change notification settings - Fork 0
/
helper.c
179 lines (163 loc) · 5.82 KB
/
helper.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
#include <menu.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>
#include <curses.h>
#include <sys/types.h>
#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
#define CTRLD 4
#define ENTER 10
unsigned char isFolder =0x4;
void new_choice(char ***choices, char path[], int *n_choices) {
DIR *dir;
struct dirent *entry;
dir = opendir(path);
int j, i = 0;
while((entry = readdir(dir)) != NULL) {
i++;
};
*choices = (char **)malloc((i + 1)*sizeof(char *));
for(j = 0; j < i; j++) {
*(*choices + j) = (char *)malloc(256*sizeof(char));
}
dir = opendir(path);
int f = 0;
while ((entry = readdir(dir)) != NULL) {
*(*choices + f) = entry->d_name;
f++;
}
*(*choices + f) = (char *)NULL;
*n_choices = f + 1;
}
void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color);
int main() {
DIR *dir;
struct dirent *entry;
ITEM **my_items1, **my_items2;
int c;
MENU *my_menu1, *my_menu2;
WINDOW *my_menu_win1, *my_menu_win2;
int n_choices1, n_choices2, i, j, currwin = 0;
char path1[2048] = "/";
char path2[2048] = "/";
char **choices1;
char **choices2;
/* Initialize curses */
initscr();
start_color();
cbreak();
noecho();
keypad(stdscr, TRUE);
init_pair(1, COLOR_RED, COLOR_BLACK);
new_choice(&choices1, path1, &n_choices1);
new_choice(&choices2, path2, &n_choices2);
/* Create items */
/*n_choices1 = ARRAY_SIZE(choices1);*/
my_items1 = (ITEM **)calloc(n_choices1, sizeof(ITEM *));
for(i = 0; i < n_choices1; ++i)
my_items1[i] = new_item(choices1[i], "");
/*n_choices2 = ARRAY_SIZE(choices2);*/
my_items2 = (ITEM **)calloc(n_choices2, sizeof(ITEM *));
for(i = 0; i < n_choices2; ++i)
my_items2[i] = new_item(choices2[i], "");
/* Crate menu */
my_menu1 = new_menu((ITEM **)my_items1);
my_menu2 = new_menu((ITEM **)my_items2);
/* Create the window to be associated with the menu */
my_menu_win1 = newwin(22, 39, 1, 1);
keypad(my_menu_win1, TRUE);
my_menu_win2 = newwin(22, 39, 1, COLS/2 + 1);
keypad(my_menu_win2, TRUE);
/* Set main window and sub window */
set_menu_win(my_menu1, my_menu_win1);
set_menu_sub(my_menu1, derwin(my_menu_win1, 17, 38, 3, 1));
set_menu_format(my_menu1, 17, 1);
set_menu_win(my_menu2, my_menu_win2);
set_menu_sub(my_menu2, derwin(my_menu_win2, 17, 38, 3, 1));
set_menu_format(my_menu2, 17, 1);
/* Print a border around the main window and print a title */
box(my_menu_win1, 0, 0);
mvwaddch(my_menu_win1, 2, 39, ACS_RTEE);
box(my_menu_win2, 0, 0);
mvwaddch(my_menu_win2, 2, 39, ACS_RTEE);
mvprintw(LINES - 1, 0, "F10 to exit");
refresh();
/* Post the menu */
post_menu(my_menu1);
wrefresh(my_menu_win1);
post_menu(my_menu2);
wrefresh(my_menu_win2);
while((c = getch()) != KEY_F(10)) {
switch(c) {
case KEY_DOWN:
if (currwin == 0)
menu_driver(my_menu1, REQ_DOWN_ITEM);
else
menu_driver(my_menu2, REQ_DOWN_ITEM);
break;
case KEY_UP:
if (currwin == 0)
menu_driver(my_menu1, REQ_UP_ITEM);
else
menu_driver(my_menu2, REQ_UP_ITEM);
break;
case 9:
currwin = (currwin + 1) % 2;
break;
case ENTER:
if (currwin == 0) {
dir = opendir(path1);
entry = readdir(dir);
if (entry->d_type == isFolder) {
free_menu(my_menu1);
unpost_menu(my_menu1);
strcat(path1, "/");
strcat(path1, item_name(current_item(my_menu1)));
new_choice(&choices1, path1, &n_choices1);
my_items1 = (ITEM **)calloc(n_choices1, sizeof(ITEM *));
for(i = 0; i < n_choices1; ++i)
my_items1[i] = new_item(choices1[i], "");
my_menu1 = new_menu((ITEM **)my_items1);
set_menu_win(my_menu1, my_menu_win1);
set_menu_sub(my_menu1, derwin(my_menu_win1, 17, 38, 3, 1));
set_menu_format(my_menu1, 17, 1);
box(my_menu_win1, 0, 0);
mvwaddch(my_menu_win1, 2, 39, ACS_RTEE);
refresh();
post_menu(my_menu1);
wrefresh(my_menu_win1);}
} else {
dir = opendir(path2);
entry = readdir(dir);
if (entry->d_type == isFolder) {
free_menu(my_menu2);
unpost_menu(my_menu2);
strcat(path2, "/");
strcat(path2, item_name(current_item(my_menu2)));
new_choice(&choices2, path2, &n_choices2);
my_items2 = (ITEM **)calloc(n_choices2, sizeof(ITEM *));
for(i = 0; i < n_choices2; ++i)
my_items2[i] = new_item(choices2[i], "");
my_menu2 = new_menu((ITEM **)my_items2);
set_menu_win(my_menu2, my_menu_win2);
set_menu_sub(my_menu2, derwin(my_menu_win2, 17, 38, 3, 1));
set_menu_format(my_menu2, 17, 1);
box(my_menu_win2, 0, 0);
mvwaddch(my_menu_win2, 2, 39, ACS_RTEE);
refresh();
post_menu(my_menu2);
wrefresh(my_menu_win2);
}
}
break;
}
wrefresh(my_menu_win1);
wrefresh(my_menu_win2);
}
/* Unpost and free all the memory taken up */
unpost_menu(my_menu1);
free_menu(my_menu1);
for(i = 0; i < n_choices1; ++i)
free_item(my_items1[i]);
endwin();
}