-
Notifications
You must be signed in to change notification settings - Fork 0
/
navigation.h
196 lines (171 loc) · 6.57 KB
/
navigation.h
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
//navigation into the file explorer through keys
#ifndef HEADER
#define HEADER
#include "header.h"
#endif
void navigate(){
while(true){
char ch=getchar();
switch((int)ch){
case 27:{ //if escape detects
char ch2=getchar();
if(ch2==91){ //checks for [
char ch3=getchar();
switch((int)ch3){
case UP:{ //if up key then cursor moves to previous line
if(x==pos_top){
if(top_cu>0){
top_cu--;
prntVec();
x=2;y=1;
goto(x,y);
coloredText(); // for coloring the line
}
}
else if(x>pos_top){
goto(x,1);
normalText();
goto(--x,1);
coloredText();
}
break;
}
case DOWN:{ // moves cursor one line down
if(x==pos_bottom){
if(bottom_cu<(vec.size()-1)){
top_cu++;
prntVec();
x=pos_bottom;y=1;
goto(x,y);
coloredText();
}
}
else if(x<pos_bottom&&x<temp_x){
goto(x,1);
normalText();
goto(++x,1);
coloredText();
}
else ;
break;
}
case RIGHT:{ // takes the user to next dir
if(!fwd_stk.empty()){
back_stk.push((string)cwd);
strcpy(cwd,fwd_stk.top().c_str());
getDir();
fwd_stk.pop();
}
break;
}
case LEFT:{ // takes to previously visited dir
if(!back_stk.empty()){
fwd_stk.push((string)cwd);
strcpy(cwd,back_stk.top().c_str());
getDir();
back_stk.pop();
}
break;
}
}
}
break;
}
case 104:{ /* h keys takes user to its home dir that is
* from where the execution starts */
back_stk.push((string)cwd);
strcpy(cwd,root);
getDir();
prntVec();
x=2,y=1;
coloredText();
break;
}
case 107:{ // k key scrolls 10 lines down
if(top_cu+10<=(vec.size()-1)){
top_cu+=10;
prntVec();
x=2;y=1;
goto(x,y);
coloredText();
}
break;
}
case 105:{ // i key scrolls 10 lines up
if(top_cu-10>=0){
top_cu-=10;
}
else{
top_cu=0;
}
prntVec();
x=2;y=1;
goto(x,y);
coloredText();
break;
}
case 58:{ // : takes to the command mode
clrscreen();
cmdMode();
goto(winx,1);
printf("\033[31m");
printf("For command mode press \":\"");
printf("\033[39m");
getDir();
prntVec();
x=2;y=1;
goto(x,y);
coloredText();
break;
}
case 10:{
// enter takes inside the directory which cursor points
if(vec[top_cu+x-2].perm[0]=='d'){
if(vec[top_cu+x-2].nme==".."){
string str=(string)cwd;
int i;
for(i=str.length()-1;str[i]!='/';i--);
str=str.substr(0,i);
back_stk.push((string)cwd);
strcpy(cwd,str.c_str());
getDir();
}
// if its file then it opens up in vim editor inside the new terminal
else{
string str=(string)cwd+'/'+vec[top_cu+x-2].nme;
back_stk.push((string)cwd);
strcpy(cwd,str.c_str());
getDir();
}
}
else{
int i;
if((i=fork())==0){
string s="vi ";
s+=(string)cwd+"/"+vec[top_cu+x-2].nme;
char* ch;
strcpy(ch,s.c_str());
char *cmd[] = {"gnome-terminal","--","/bin/bash","-c",ch,NULL};
if(execvp("gnome-terminal",cmd)<0)
printf("error occured");
}
}
break;
}
case 127:{ // backspace takes one level up of the present dir
string prnt=(string)cwd;
int i;
if((string)cwd=="/home")break;
for(i=prnt.length()-1;prnt[i]!='/' && i>=0;i--);
prnt=prnt.substr(0,i);
back_stk.push((string)cwd);
strcpy(cwd,prnt.c_str());
getDir();
prntVec();
x=2,y=1;
coloredText();
break;
}
}
}
}