-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
188 lines (157 loc) · 5.53 KB
/
Copy pathmain.cpp
File metadata and controls
188 lines (157 loc) · 5.53 KB
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
#include "ls.h"
#include <termios.h>
#include <stdio.h>
using namespace std;
char root[PATH_MAX];
string cmd_buffer="";
int mode=0; //start in normal mode
string getCmdBuffer(){
return cmd_buffer; //used in cmd mode
}
int getMode(){
return mode; //normal or cmd mode bit
}
void clearCmdBuffer(){
cmd_buffer=": "; //the string typed in cmd mode will be in this buffer
}
char * getRoot(){
return root; //will store the path in which the application is launched
}
int main(int argc, char const *argv[])
{
if (getcwd(root, sizeof(root)) == NULL){
cout << "Fetching root Path error" << endl;
}
initializeHistory();
addToHistory(root);
myLS(root); //prints initial directory
char c; //used to fetch char
FILE* input;
//setting non Canonical mode and disabling echo
struct termios initialTerimalSettings, newTerimalSettings;
input = fopen("/dev/tty", "r");
tcgetattr(fileno(input), &initialTerimalSettings);
newTerimalSettings = initialTerimalSettings;
newTerimalSettings.c_lflag &= ~ICANON;
newTerimalSettings.c_lflag &= ~ECHO;
newTerimalSettings.c_cc[VMIN] = 1;
newTerimalSettings.c_cc[VTIME] = 0;
newTerimalSettings.c_lflag &= ~ISIG;
if(tcsetattr(fileno(input), TCSAFLUSH, &newTerimalSettings) != 0) {
cout << "switching on non C mode and disabling echo failed " << endl;
}
else{
//normal mode looop
while(1){
c= getc(input);
if(c == 'q'){
break;
}
else if(c=='\n' || c=='\r'){
//Enter is pressed
showSelectedDir();
}
else if(c=='h' || c=='H'){
myLS(root); //display root dir
}
else if(c==127){
//need to go one dir up
backSpacePressed();
}
//arrow key
else if(c=='\033'){
getc(input);
switch(getc(input)) {
case 'A': //up
if(getIndex()==0)
{
//we are at the top, first item
//do nothing.
}
else if(getIndex()==getCurrentViewTopIndex())
{
//currently at the top but there are few entries above this
decrCurrentViewTopIndex();
decrIndex();
setScrollingFlag(2); //traversing up
char temp[PATH_MAX];
myLS(temp); //this call will refresh the screen acccordingly
}
else //if cursor is in the middle of the list go up
{
printf("\033[1A");
decrIndex(); //cursor index is decrmenteds
}
break;
case 'B'://down
if(getIndex()==(getCurListLen()-1)){
//at the last item
// do nothing
}
else{
//checking if cursor is at the end of the terminal
int top = getCurrentViewTopIndex();
int bottom = getIndex();
if(bottom - top == getCurrentViewTerminalLastRow()){
//at last row => More entries are there to display
incrCurrentViewTopIndex();
incrIndex();
setScrollingFlag(1);
char temp[PATH_MAX];
myLS(temp);
}
else{
printf("\033[1B");
incrIndex();
}
}
break;
case 'C'://right
// will traverse the history stored and call ls on in
rightArrowPressed();
break;
case 'D'://left
//traverseFlag
leftArrowPressed();
break;
}
}
//cmd mode
else if(c==':'){
cmd_buffer+=": ";
mode=1; //setting cmd mode
enterCmdMode();
while(1){
c= getc(input);
if(c==27)
{ //esc pressed
mode=0;
cmd_buffer="";
exitCmdMode();
break;
}
else if(c==127){
//cout << "delete pressed!"
if(cmd_buffer.size()>2){
cmd_buffer=cmd_buffer.substr(0,cmd_buffer.size()-1);
enterCmdMode();
}
}
else if(c=='\n'){
//cout << "enter pressed!";
processCmd(cmd_buffer);
cmd_buffer=": "; //clearing
enterCmdMode(); //torefresh
}
else{
cmd_buffer+=c;
enterCmdMode();
}
}
}
}
}
//settin back original settings, reseting terminal behaviour
tcsetattr(fileno(input), TCSANOW, &initialTerimalSettings);
return 0;
}