-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandleCommands.cpp
153 lines (142 loc) · 4.92 KB
/
handleCommands.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
/********************************************************************************/
/* Name: Priyendu Mori */
/* Roll no: 2018201103 */
/********************************************************************************/
#include "header.h"
#include "macro.h"
int lineno=0;
extern vector<string> filelist, results;
extern stack<string> backstack,frontstack;
extern string currentPath;
extern ofstream myfile;
extern string home;
extern struct winsize w;
/*
this function handles all the key functions like
arrow, backspace, h, etc, maintains the cursor
position and opens file or directory on pressing
enter appropriately
*/
void handleCommands(bool isSearchResult){
struct termios initialrsettings, newrsettings;
char ch;
tcgetattr(fileno(stdin), &initialrsettings);
newrsettings = initialrsettings;
newrsettings.c_lflag &= ~ICANON;
newrsettings.c_lflag &= ~ECHO;
if(tcsetattr(fileno(stdin), TCSAFLUSH, &newrsettings) != 0) {
fprintf(stderr,"Could not set attributes\n");
}
else {
while(ch=cin.get()){
if(ch==27){
ch=cin.get();
ch=cin.get();
if(ch=='A'){
if(lineno>0){
lineno--;
up;
}
}
else if(ch=='B'){
if(lineno<filelist.size()-1){
lineno++;
down;
}
}
else if(ch=='C'){
if(!frontstack.empty()){
string file=frontstack.top();
frontstack.pop();
backstack.push(file);
listContent(file);
pos(0,0);
lineno=0;
}
}
else if(ch=='D'){
if(backstack.size()>1){
string file=backstack.top();
backstack.pop();
frontstack.push(file);
file=backstack.top();
listContent(file);
pos(0,0);
lineno=0;
}
}
else{
}
}
else if(ch==104 || ch==72){
backstack.push(home);
while(!frontstack.empty()) frontstack.pop();
listContent(home);
pos(0,0);
lineno=0;
}
else if(ch==127){
if(currentPath != home){
while(!frontstack.empty()) frontstack.pop();
currentPath=currentPath.substr(0, currentPath.find_last_of("\\/"));
listContent(currentPath);
pos(0,0);
lineno=0;
}
}
else if(ch=='\n'){
string file;
if(isSearchResult){
file=results[lineno];
isSearchResult=false;
}
else{
file=filelist[lineno];
}
struct stat sb;
stat(file.c_str(), &sb);
bool isDirectory=S_ISDIR(sb.st_mode);
if(isDirectory){
string currentFile=file.substr(file.find_last_of("\\/")+1,file.length());
if(currentFile.compare(".")==0){
listContent(currentPath);
pos(0,0);
lineno=0;
}
else if(currentFile.compare("..")==0){
currentPath=currentPath.substr(0, currentPath.find_last_of("\\/"));
backstack.push(currentPath);
listContent(currentPath);
pos(0,0);
lineno=0;
}
else{
backstack.push(file);
listContent(file);
lineno=0;
pos(0,0);
}
}
else{
int pid = fork();
if (pid == 0) {
close(2);
execlp("/usr/bin/xdg-open", "xdg-open", file.c_str(), NULL);
exit(1);
}
}
}
else if(ch==':'){
commands(false);
}
else if(ch=='q'){
printf("\033[?1049l"); //out of alternate buffer
tcsetattr(fileno(stdin), TCSANOW, &initialrsettings);
exit(1);
}
else{
}
}
}
tcsetattr(fileno(stdin), TCSANOW, &initialrsettings);
}