-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnavigation.cpp
501 lines (492 loc) · 14.1 KB
/
navigation.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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
//2018201005 Vatsal Soni
#include "config.h"
int row=1,col=0,cur_cursor=1,n;
//struct dirent **namelist;
#define esc 27 // ASCII code of ESC
#define pos() printf("%c[%d;%dH",esc,row,col) // For cursor move to specified pos.
#define pos1(point) printf("%c[%d;%dH",esc,point,col) // For cursor move to end of line
#define cls printf("\033[H\033[J") //For clear screen
struct dirent **namelist;
// For normal mode navigation utility
int navigate(int n1,struct dirent **namelist1,struct termios newrsettings,struct termios initialrsettings,string root)
{
char *path;
char ch;
n=n1;
stack<string> backstk,forstk; //backward stack and forward stack
namelist=namelist1;
struct winsize w; // To get terminal property
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
string current=root,display="Normal Mode"; //current string to store current directory and root for root directory
backstk.push(root);
do
{
int last_cursor_point=w.ws_row;
pos1(last_cursor_point);
cout<<display;
pos();
ch=cin.get(); // when user press key like up/down/left/right/enter/backspace/h
if(ch==27)
{
ch=cin.get();
ch=cin.get();
if(ch=='A') // If up arrow key
{
--row;
if(row>=1) // If cursor is not present at first position
pos(); // set cursor position
else if(cur_cursor>1 && row<1) // If cursor is at first position and current is not at first window.
{
cls; // clear screen
cur_cursor--; //set window position
if(current==root)
{
path=new char[root.length()+1];
strcpy(path,root.c_str());
int n1;
//n = scandir(path, &namelist, NULL,alphasort);
if(n+1<=w.ws_row-2)
n1=n;
else
n1=w.ws_row-2;
/*for(int i=0;i<n-1;i++)
{
if(string(namelist[i]->d_name)=="..")
{
int x=i;
while(x<n-1)
{
namelist[x]=namelist[x+1];
x++;
}
i--;
break;
}
}
n--;*/
for(int i=cur_cursor-1;i<=cur_cursor+n1-1;i++)
{
fileInfo(path,namelist[i]->d_name,0);
}
}
else
{
string s=current;
path=new char[s.length()+1];
strcpy(path,s.c_str());
int n1;
//n = scandir(path, &namelist, NULL,alphasort);
if(n<=w.ws_row-2)
n1=n;
else
n1=w.ws_row-2;
for(int i=cur_cursor-1;i<=cur_cursor+n1-1 && i<n;i++)
{
fileInfo(path,namelist[i]->d_name,0);
}
}
//printDirectoryList(current,root);
row=1;
pos(); //set cursor position
}
else // If cursor is at first position and current window is first only.
row=1;
//cout<<row<<" "<<cur_cursor;
}
else if(ch=='B') // If down arrow key
{
//printf ("lines %d\n", w.ws_row); Terminal height
//printf ("columns %d\n", w.ws_col); Terminal width
row++;
//cout<<row;
if(cur_cursor+row<=n+2 && row<=w.ws_row-1) // if cursor is not at last position
pos();
else if(row>w.ws_row-1 && cur_cursor+row<=n+2) // if cursor is at last position and cursor is not at last window.
{
cls; //clear screen
if(root==current)
{
path=new char[root.length()+1];
strcpy(path,root.c_str());
int n1;
//n = scandir(path, &namelist, NULL,alphasort);//store all files and dirctories into namelist from specified path
if(n+1<=w.ws_row-2)
n1=n;
else
n1=w.ws_row-2;
/*for(int i=0;i<n-1;i++)
{
if(string(namelist[i]->d_name)=="..")
{
int x=i;
while(x<n-1)
{
namelist[x]=namelist[x+1];
x++;
}
i--;
break;
}
}
n=n-2;*/
for(int i=cur_cursor;i<=cur_cursor+n1;i++)
{
fileInfo(path,namelist[i]->d_name,0);
}
}
else
{
string s=current;
path=new char[s.length()+1];
strcpy(path,s.c_str());
int n1;
//n = scandir(path, &namelist, NULL,alphasort);//store all files and dirctories into namelist from specified path
if(n+1<=w.ws_row-2)
n1=n;
else
n1=w.ws_row-2;
for(int i=cur_cursor;i<=cur_cursor+n1;i++)
{
fileInfo(path,namelist[i]->d_name,0);
}
}
cur_cursor++; //set window position
row--;
pos(); //set cursor position
}
else // if cursor is at last position and also at last window.
row--;
}
else if(ch=='C') // If right arrow key then DS
{
if(forstk.size()>=1)
{
backstk.push(forstk.top());
string s=forstk.top();
cls; // clear screen
forstk.pop();
char *s_ar=new char[s.length()+1];
strcpy(s_ar,s.c_str());
DIR *d=opendir(s_ar);
if(!d)
{
// Nothing should happen
}
else
{
current=s;
}
//char *root1=new char[s.length()+1];
//strcpy(root1,s.c_str());
//n = scandir(root1, &namelist, NULL,alphasort);
printDirectoryList(current,root); // Print directory and files
cur_cursor=1;
row=1;
pos();
}
}
else if(ch=='D') // If left arrow key then DS
{
if(backstk.size()>=2)
{
forstk.push(backstk.top());
cls;
backstk.pop();
string s=backstk.top();
char *s_ar=new char[s.length()+1];
strcpy(s_ar,s.c_str());
DIR *d=opendir(s_ar);
if(!d)
{
// Nothing should happen
}
else
current=s;
//char *root1=new char[s.length()+1];
//strcpy(root1,s.c_str());
//n = scandir(root1, &namelist, NULL,alphasort);
printDirectoryList(current,root); // Print directory and files
cur_cursor=1;
row=1;
pos();
}
}
}
else if(ch==104 || ch==72) // If h or H press then open root directory
{
if(current!=root)
{
cls;
cur_cursor=1;
backstk.push(root);
while(!forstk.empty()){
forstk.pop();
}
current=root;
searchResult.clear();
printDirectoryList(current,root); // Print directory and files
row=1;
pos();
cur_cursor=1;
}
}
else if(ch==127) // If BackSpace key then
{
if(current!=root && searchResult.size()==0)
{
while(!forstk.empty())
forstk.pop();
cur_cursor=1;
int f=current.find_last_of("/\\");
current=current.substr(0,f);
cls;
backstk.push(current);
printDirectoryList(current,root); // Print directory and files
row=1;
pos();
cur_cursor=1;
}
}
else if(ch==58) // If colon(:) then command mode
{
row=w.ws_row;
pos();
printf("%c[2K", 27);
cout<<":";
//newrsettings.c_lflag &= ECHO;
newrsettings=initialrsettings;
newrsettings.c_lflag &= ~ICANON; // change settings
tcgetattr(fileno(stdin), &newrsettings);
string getFromCommand=commandMode(w.ws_row,current,root);
printf("%c[2K", 27);
cls;
row=0;
cur_cursor=1;
pos();
newrsettings=initialrsettings;
newrsettings.c_lflag &= ~ICANON; // change settings
newrsettings.c_lflag &= ~ECHO;
tcgetattr(fileno(stdin), &newrsettings); // load newsettings
if(getFromCommand!="")
{
if(getFromCommand=="$$")
{
//cout<<getFromCommand<<endl;
backstk.push(current);
printsearch();
}
else
{
current=getFromCommand;
printDirectoryList(current,root);
}
}
else
printDirectoryList(current,root); // Print directory and files
}
else if(ch==10) // If enter key then open Dir or File
{
if(searchResult.size()>0)
{
backstk.push(current);
current=searchResult[cur_cursor+row-2];
char *temp2=new char[current.length()+1];
strcpy(temp2,current.c_str());
struct stat statObj;
if(stat(temp2,&statObj) < 0)
{
cout<<"Error";
//return 1;
}
if(!S_ISDIR(statObj.st_mode))
{
char *char_array;
string s=current;
char_array=new char[s.length()+1];
strcpy(char_array, s.c_str());
//system(char_array);
//int f=current.find_last_of("/\\");
//current=current.substr(0,f);
int st=open("/dev/null",O_WRONLY);
dup2(st,2);
close(st);
pid_t pid=fork();
if(pid==0)
{
execlp("xdg-open","xdg-open",char_array,NULL);
exit(0);
}
}
else
{
searchResult.clear();
printDirectoryList(current,root);
}
}
else
{
struct stat statObj;
string temp,temp1;
char *temp2;
if(string(namelist[cur_cursor+row-2]->d_name)=="..")
{
while(!forstk.empty())
forstk.pop();
int f=current.find_last_of("/\\");
current=current.substr(0,f);
temp=current;
backstk.push(temp);
temp2=new char[temp.length()+1];
}
else if(string(namelist[cur_cursor+row-2]->d_name)!=".")
{
temp="/";
temp1=namelist[cur_cursor+row-2]->d_name;
temp1=temp+temp1;
temp=current+temp1;
current=temp;
while(!forstk.empty())
forstk.pop();
temp2=new char[current.length()+1];
backstk.push(current);
}
else
{
continue;
}
strcpy(temp2,current.c_str());
if(stat(temp2,&statObj) < 0)
{
cout<<"Error";
//return 1;
}
if(!S_ISDIR(statObj.st_mode))
{
char *char_array;
//string s="bash -c 'xdg-open "+ temp +"' 2> /dev/null";
string s=temp;
char_array=new char[s.length()+1];
strcpy(char_array, s.c_str());
//system(char_array);
int f=current.find_last_of("/\\");
current=current.substr(0,f);
backstk.pop();
int st=open("/dev/null",O_WRONLY);
dup2(st,2);
close(st);
pid_t pid=fork();
if(pid==0)
{
execlp("xdg-open","xdg-open",char_array,NULL);
exit(0);
}
}
else if(string(namelist[cur_cursor+row-2]->d_name)!=".")
{
cls;
cur_cursor=1;
printDirectoryList(current,root); // Print directory and files
row=1;
pos();
}
}
}
}while(ch!=81 && ch!=113);
return 0;
}
void printDirectoryList(string current,string root) // For printing list of files and directories
{
char *path=new char[current.length()+1];
strcpy(path,current.c_str());
int n1;
struct winsize w; //to get terminal property
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
if(current==root)
{
n = scandir(path, &namelist, NULL,alphasort); //store all files and dirctories into namelist from specified path
//cout<<w.ws_row<<" "<<n;
if(n>0)
{
if((w.ws_row-n)==1)
n1=n-2;
else if(n<=w.ws_row-2)
n1=n-2;
else
n1=w.ws_row-2;
for(int i=0;i<n-1;i++)
{
if(string(namelist[i]->d_name)=="..")
{
int x=i;
while(x<n-1)
{
namelist[x]=namelist[x+1];
x++;
}
i--;
break;
}
}
for(int i=0;i<=n1;i++)
{
fileInfo(path,namelist[i]->d_name,0);
}
n=n-2;
}
/*else
{
pos1(w.ws_row);
printf("%c[2K", 27);
cout<<"No directory exist";
}*/
}
else
{
n = scandir(path, &namelist, NULL,alphasort); //store all files and dirctories into namelist from specified path
if(n>0)
{
if(n<=w.ws_row-2)
n1=n-1;
else
n1=w.ws_row-2;
for(int i=0;i<=n1;i++)
{
fileInfo(path,namelist[i]->d_name,0);
}
n--;
}
/*else
{
pos1(w.ws_row);
printf("%c[2K", 27);
cout<<"No directory exist";
}*/
}
}
void printsearch() // For printing search Result
{
n=searchResult.size();
int n1;
/*if(n<=w.ws_row-2)
n1=n-1;
else
n1=w.ws_row-2;*/
n1=n-1;
cls;
cur_cursor=1;
row=1;
pos();
//cout<<n;
for(int i=0;i<=n1;i++)
{
int f=searchResult[i].find_last_of("/\\");
string path=searchResult[i].substr(0,f);
char *fpath=new char[path.length()+1];
strcpy(fpath,path.c_str());
char *fname=new char[searchResult[i].length()+1];
strcpy(fname,searchResult[i].c_str());
fileInfo(fpath,fname,1);
}
n--;
//cout<<namelist[0]->d_name;
}