-
Notifications
You must be signed in to change notification settings - Fork 0
/
enableNCanon.cpp
250 lines (246 loc) · 7.67 KB
/
enableNCanon.cpp
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
/*============================================================
@author - Rushitkumar Jasani @rollno - 2018201034
=============================================================*/
#ifndef MYHEADER_H
#define MYHEADER_H
#include "myheader.h"
#endif
#ifndef GLOBAL_H
#define GLOBAL_H
#include "global.h"
#endif
string parent = "..";
string current = ".";
bool search_flag = false;
/*============================================================
method to enter into Non-Canonical mode.
=============================================================*/
void enableNCanon()
{
//getting the initial terminal settings
tcgetattr(STDIN_FILENO, &raw);
newraw = raw;
//changing ICANON for entering Non Canonical mode.
newraw.c_lflag &= ~(ICANON | ECHO | IEXTEN | ISIG);
newraw.c_iflag &= ~(BRKINT);
//set new terminal settings.
if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &newraw) != 0)
fprintf(stderr, "Could not set attributes\n");
else {
char ch[3] = { 0 };
while (1) {
unsigned int tmp = terminal.ws_row;
//cy = 1;
printf("%c[%d;%dH", 27, tmp, 1);
printf("::Normal Mode::");
CURSER;
fflush(0);
if (read(STDIN_FILENO, ch, 3) == 0)
continue;
else if (ch[0] == 27 && ch[1] == '[' && ch[2] == 'A')
UpArrow();
else if (ch[0] == 27 && ch[1] == '[' && ch[2] == 'B')
DownArrow();
else if (ch[0] == 27 && ch[1] == '[' && ch[2] == 'C')
RightArrow();
else if (ch[0] == 27 && ch[1] == '[' && ch[2] == 'D')
LeftArrow();
else if (ch[0] == 'H' || ch[0] == 'h')
HomeKey();
else if (ch[0] == 127)
BackspaceKey();
else if (ch[0] == 10)
EnterKey();
else if (ch[0] == ':') {
int ret = command_mode();
cx = 1;
CURSER;
if (ret == 2) {
search_flag = true;
continue;
}
else
listdir(cur_dir);
}
else if (ch[0] == 'q') {
write(STDOUT_FILENO, "\x1b[2J", 4);
tcsetattr(STDIN_FILENO, TCSAFLUSH, &raw);
cx = 1;
cy = 1;
CURSER;
exit(1);
}
fflush(0);
memset(ch, 0, 3 * sizeof(ch[0]));
}
}
}
/*============================================================
method will handle behaviour when Up arrow key is pressed.
it also manages variables to map into vector and scroll.
=============================================================*/
void UpArrow()
{
if (cx > 1) {
cx--;
CURSER;
}
else if (cx == 1 && cx + cur_window > 1) {
cur_window--;
//listdir(cur_dir);
update_list();
CURSER;
}
}
/*============================================================
method will handle behaviour when Down arrow key is pressed.
it also manages variables to map into vector and scroll.
=============================================================*/
void DownArrow()
{
if (cx <= term_row && cx < dlist.size()) {
cx++;
CURSER;
}
else if (cx > term_row && cx + cur_window < dlist.size()) {
cur_window++;
//listdir(cur_dir);
update_list();
CURSER;
}
}
/*============================================================
RightArrow method will handle behaviour when Right arrow key is
pressed. if any path is there in forw_stack then pop it and
print content of that.
=============================================================*/
void RightArrow()
{
reset_curser_top();
if (!forw_stack.empty()) {
string p = forw_stack.top();
forw_stack.pop();
strcpy(cur_dir, p.c_str());
back_stack.push(cur_dir);
listdir(cur_dir);
}
}
/*============================================================
LeftArrow method will handle behaviour when Left arrow key is
pressed. if any path is there in back_stack then pop it and
print content of that.
=============================================================*/
void LeftArrow()
{
reset_curser_top();
if (back_stack.size() > 1) {
string p = back_stack.top();
forw_stack.push(p);
back_stack.pop();
p = back_stack.top();
strcpy(cur_dir, p.c_str());
listdir(cur_dir);
}
else if (back_stack.size() == 1) {
string p = back_stack.top();
strcpy(cur_dir, p.c_str());
listdir(cur_dir);
}
}
/*============================================================
Navigate to Home.
=============================================================*/
void HomeKey()
{
reset_curser_top();
strcpy(cur_dir, root);
if( strcmp(cur_dir,root) != 0 )back_stack.push(cur_dir);
while (!forw_stack.empty())
forw_stack.pop();
listdir(cur_dir);
}
/*============================================================
Navigate to Parent if exist. (root has no parent :P)
=============================================================*/
void BackspaceKey()
{
if (search_flag)
return;
reset_curser_top();
if ((strcmp(cur_dir, root)) != 0) {
string s_name = SplitFilename(string(cur_dir));
strcpy(cur_dir, s_name.c_str());
back_stack.push(cur_dir);
while (!forw_stack.empty())
forw_stack.pop();
listdir(cur_dir);
}
else {
}
}
/*============================================================
if EnterKey pressed on directory then go to dir. else opens
file in default application.
=============================================================*/
void EnterKey()
{
if (dlist[cur_window + cx - 1] == current) {
// if selected directory is '.' .
reset_curser_top();
listdir(cur_dir);
}
else if (dlist[cur_window + cx - 1] == parent) {
// if selected directory is '..' .
string s_name = cur_dir;
s_name = SplitFilename(s_name);
strcpy(cur_dir, s_name.c_str());
back_stack.push(cur_dir);
while (!forw_stack.empty())
forw_stack.pop();
reset_curser_top();
listdir(cur_dir);
}
else {
// if selected directory is except '.' and '..'.
// taking file name and making full path
char* p_path;
char* f_path;
if (!search_flag) {
string cur_d = "/" + dlist[cur_window + cx - 1];
CURSER;
p_path = cur_dir;
f_path = new char[cur_d.length() + strlen(p_path) + 1];
strcpy(f_path, p_path);
strcat(f_path, cur_d.c_str());
strcpy(cur_dir, f_path);
}
else {
string search_path = create_absolute_path(dlist[cur_window + cx - 1]);
strcpy(cur_dir, search_path.c_str());
f_path = new char[strlen(cur_dir) + 5];
strcpy(f_path, cur_dir);
}
//Maintaing forward and backward stacks.
back_stack.push(cur_dir);
//clear forward stack if entering in any directory manually.
while (!forw_stack.empty())
forw_stack.pop();
//check if selected file is FILE or DIRECTORY.
if (isDirectory(f_path)) {
reset_curser_top();
listdir(cur_dir);
search_flag = false;
}
else {
back_stack.pop();
string top = back_stack.top();
strcpy(cur_dir, top.c_str());
pid_t pid = fork();
if (pid == 0) {
close(2);
execlp("xdg-open", "xdg-open", f_path, NULL);
exit(0);
}
}
}
}