-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileExplorerIndex.cpp
More file actions
executable file
·982 lines (877 loc) · 24.8 KB
/
FileExplorerIndex.cpp
File metadata and controls
executable file
·982 lines (877 loc) · 24.8 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
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
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <dirent.h>
#include <sys/stat.h>
#include <time.h>
#include <vector>
#include <string.h>
#include <termios.h>
#include <unistd.h>
#include <ctype.h>
#include <sys/select.h>
#include <sys/ioctl.h>
#include <string>
#include <iostream>
#include <iomanip>
#include <fstream>
#include <ctime>
#include <algorithm>
#include <regex>
#include <signal.h>
#include <pwd.h>
#include <grp.h>
#include <string.h>
#include <fcntl.h>
using namespace std;
#include "MetaData.h"
#define ClearScreen() printf("\033[2J")
#define MoveUp() printf("\033[A")
#define MoveDown() printf("\033[B")
#define MoveLeft() printf("\033[C")
#define MoveRight() printf("\033[D")
#define SetCursor(x,y) printf("\033[%d;%dH",x,y)
string title = "File Explorer";
// By Default its Normal Mode
static bool isNormalMode = 1;
static const int STDIN = 0;
struct winsize size,currentRC;
char *currDir = NULL,*startingDirectory = NULL;
char * UpFolderPath = NULL;
static vector<struct dirent *> fileList;
static vector<char *> navigationVector;
int isScrollingEnabled = 0;
static int xdiraccess(const char *path);
void CursorFunctionality();
void GoToNextDirectory(int index);
void showCurrentDirectoryDetails(int,int,int);
void printAtLast(string message,int restore);
void GoToPreviousDirectory();
void GoToUpFolder();
int EnableUpScrolling();
int EnableDownScrolling();
void GetCommandLine(string sInput);
int invokeCommands(string strCommand);
size_t split(const string &txt, vector<string> &strs, char ch);
void GetCommandLine(string str);
static int terminal_descriptor = -1;
static struct termios terminal_original;
static struct termios terminal_settings;
void setCommandMode();
/* Restore terminal to original settings
*/
static void terminal_done(void)
{
if (terminal_descriptor != -1)
tcsetattr(terminal_descriptor, TCSANOW, &terminal_original);
terminal_descriptor = -1;
}
static int terminal_init(void)
{
// struct sigaction act;
/* Already initialized? */
if (terminal_descriptor != -1)
return errno = 0;
/* Which standard stream is connected to our TTY? */
if (isatty(STDERR_FILENO))
terminal_descriptor = STDERR_FILENO;
else
if (isatty(STDIN_FILENO))
terminal_descriptor = STDIN_FILENO;
else
if (isatty(STDOUT_FILENO))
terminal_descriptor = STDOUT_FILENO;
else
return errno = ENOTTY;
/* Obtain terminal settings. */
if (tcgetattr(terminal_descriptor, &terminal_original) ||
tcgetattr(terminal_descriptor, &terminal_settings))
return errno = ENOTSUP;
/* Disable buffering for terminal streams. */
if (isatty(STDIN_FILENO))
setvbuf(stdin, NULL, _IONBF, 0);
if (isatty(STDOUT_FILENO))
setvbuf(stdout, NULL, _IONBF, 0);
if (isatty(STDERR_FILENO))
setvbuf(stderr, NULL, _IONBF, 0);
/* Let BREAK cause a SIGINT in input. */
terminal_settings.c_iflag &= ~IGNBRK;
terminal_settings.c_iflag |= BRKINT;
/* Ignore framing and parity errors in input. */
terminal_settings.c_iflag |= IGNPAR;
terminal_settings.c_iflag &= ~PARMRK;
/* Do not strip eighth bit on input. */
terminal_settings.c_iflag &= ~ISTRIP;
/* Do not do newline translation on input. */
//terminal_settings.c_iflag &= ~(INLCR | IGNCR | ICRNL);
// Is For Ignore Enter Key
terminal_settings.c_iflag &= ~(INLCR | IGNCR );
/* Use 8-bit characters. This too may affect standard streams,
* but any sane C library can deal with 8-bit characters. */
terminal_settings.c_cflag &= ~CSIZE;
terminal_settings.c_cflag |= CS8;
/* Enable receiver. */
terminal_settings.c_cflag |= CREAD;
/* Let INTR/QUIT/SUSP/DSUSP generate the corresponding signals. */
terminal_settings.c_lflag |= ISIG;
/* Enable noncanonical mode.
* This is the most important bit, as it disables line buffering etc. */
terminal_settings.c_lflag &= ~ICANON;
/* Disable echoing input characters. */
terminal_settings.c_lflag &= ~(ECHO | ECHOE | ECHOK | ECHONL);
/* Disable implementation-defined input processing. */
terminal_settings.c_lflag &= ~IEXTEN;
/* To maintain best compatibility with normal behaviour of terminals,
* we set TIME=0 and MAX=1 in noncanonical mode. This means that
* read() will block until at least one byte is available. */
terminal_settings.c_cc[VTIME] = 0;
terminal_settings.c_cc[VMIN] = 1;
/* Set the new terminal settings.
* Note that we don't actually check which ones were successfully
* set and which not, because there isn't much we can do about it. */
tcsetattr(terminal_descriptor, TCSANOW, &terminal_settings);
/* Done. */
return errno = 0;
}
// Flag 0 for Getting Index
// Flag 1 for Setting Index
int SetStartingIndexofFileItem =0 ;
int GetSetCurrentIndexOfFileList(int flag,int _I)
{
if(!fileList.empty())
{
// Set the Starting INdex
if(flag==1)
{
SetStartingIndexofFileItem = _I;
}
}
return SetStartingIndexofFileItem;
}
int SetLastIndexofFileItem =0 ;
int GetSetLastIndexOfFileList(int flag,int _I)
{
if(!fileList.empty())
{
// Set the Starting INdex
if(flag==1)
{
SetLastIndexofFileItem = fileList.size();
}
}
return SetLastIndexofFileItem;
}
string FromCharacterPointerToString(char * Input)
{
string temp;
temp.append(Input);
return temp;
}
string readable_fs(double spaceToConvert) {
int i = 0;
const char* bits[] = {"B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"};
while (spaceToConvert > 1024) {
spaceToConvert /= 1024;
i++;
}
char buf[10];
sprintf(buf, "%.*f%s", i, spaceToConvert, bits[i]);
string sFileSize; sFileSize.append(buf);
return sFileSize;
}
void ResetCursor()
{
currentRC.ws_row=STARTTINGROW;
currentRC.ws_col=1;
}
void ResetCursorToDataRow()
{
currentRC.ws_row=STARTTINGROWDATA;
currentRC.ws_col=1;
}
string PrintTime()
{
time_t now = time(0);
// convert now to string form
char* dt = ctime(&now);
string strDateTime; strDateTime.append(dt);
return strDateTime;
}
///////////////////////////////////// TO REMOVE
ofstream myfile;
void writeToFile(string message)
{
myfile.open("test.txt", std::ios_base::app);
string strFinalLog;
strFinalLog = PrintTime();
strFinalLog.append("(");
strFinalLog.append(to_string(currentRC.ws_row));
strFinalLog.append(",");
strFinalLog.append(to_string(currentRC.ws_col));
strFinalLog.append(")");
myfile << strFinalLog << " : " << message << endl;
myfile.close();
}
///////////////////////////////////// END REMOVE
void SetHeaderPath(char * currDir)
{
SetCursor(3,1);
if(currDir!=NULL)
{
if(xdiraccess(currDir))
{
printf("DIR : %s\n\n",currDir);
ResetCursor();
}
}
else
printAtLast("Some Error Encountered",1);
}
void SetWelcomeScreen()
{
ClearScreen();
SetCursor(0,0);
int mid = size.ws_col/2;
//string name = "File Explorer";
SetCursor(1,(int)(mid-(title.length()/2)));
cout << title;
SetCursor(2,mid/2);
int totalLength = (mid + (mid/2)) - (mid/2);
for(int i=(mid/2);i<mid/2 + totalLength;i++)
{
printf("*");
}
printf("\n");
if(currDir==NULL)
{
currDir = getenv("PWD");
startingDirectory = currDir;
navigationVector.push_back(currDir);
}
SetHeaderPath(currDir);
}
void SetParentPath(char * currDir)
{
if(currDir!=NULL && navigationVector.size()>1)
{
string s1;
s1.append(currDir);
int location = s1.find_last_of("\\/");
if(location>0)
{
s1 = s1.substr(0, location-1);
if(s1[location-1]!='/')
{
//printAtLast("hjshhsjhas",1);
s1.append("/");
}
char *cstr = new char[s1.length() + 1];
strcpy(cstr, s1.c_str());
// BackSpace Will be Active Only When more than one item in navigationVector
if(navigationVector.size()>1)
{
UpFolderPath = cstr;
}
}
}
}
void printAtLast(string message,int restore)
{
if(restore)
{
int rowI = currentRC.ws_row;
int colI = currentRC.ws_col;
SetCursor(size.ws_row-1,1);
cout << " ";
SetCursor(size.ws_row-1,1);
if(message=="") cout << " ";
else cout << message;
SetCursor(rowI,colI);
return;
}
else
{
SetCursor(size.ws_row-2,1);
cout << " ";
SetCursor(size.ws_row-2,1);
if(message=="") cout << " ";
else cout << message;
}
}
static int xdiraccess(const char *path)
{
static DIR *dirp;
dirp = opendir(path);
if (dirp == NULL) {
printAtLast("Directory doea not have Access to Open",1);
return 0;
}
closedir(dirp);
return 1;
}
int isFile(struct dirent *deptr)
{
struct stat filestat;
int isFile = 0;
stat(deptr->d_name, &filestat);
if(S_ISREG(filestat.st_mode))
isFile = 1;
return isFile;
}
void GetCurrentDirectoryDetails(char * currDir,int startIndex,int endingIndex,int isFromSameSource)
{
if(currDir!=NULL)
{
SetParentPath(currDir);
DIR *dp = opendir((const char*)currDir);
struct dirent *deptr = NULL;
// struct stat filestat;
if(dp == NULL){
printAtLast("Error: Could not open the current working directory",1);
ResetCursor();
}
else
{
if(isFromSameSource == 0)
{
fileList.clear();
//struct entry *temp = (struct entry *)malloc(sizeof(struct entry *));
while((deptr = readdir(dp)) != NULL){
//char * name = deptr->d_name;
//string strTemp; strTemp.append(name);
//if(strTemp.length()==1 && strTemp[0]=='.') continue;
//if(strTemp.length()==2 && strTemp == "..")
fileList.push_back(deptr);
}
}
showCurrentDirectoryDetails(startIndex,endingIndex,isFromSameSource);
}
}
}
void showCurrentDirectoryDetails(int startingIndex,int endingIndex,int isFromSameSource){
/*
i. File Name
ii. File size (Human readable format similar to ls -lh)
iii. Ownership (User & Group) & Permissions
iv. Last modified
*/
string s ="";
// Make Header
currentRC.ws_col = 3;
// int positionRow = currentRC.ws_row, positionColumn = currentRC.ws_col;
SetCursor(currentRC.ws_row,currentRC.ws_col);
s = "File Name"; s.append(38 - s.length(), ' '); cout << s ;
s = "File Size"; s.append(20 - s.length(), ' '); cout << s ;
s = "OwnerShip"; s.append(20 - s.length(), ' '); cout << s ;
s = "Last Modified"; s.append(20 - s.length(), ' '); cout << s << endl;
cout << endl;
currentRC.ws_row = currentRC.ws_row + 2;
struct dirent *deptr = NULL;
string strHeader = "File Name\t\tFile Size\tOwnerShip\tLast Modified\n";
struct stat filestat;
// limit the End Index to File Size
if(endingIndex>=((int)fileList.size()))
{
endingIndex = fileList.size();
endingIndex = endingIndex-1;
}
GetSetCurrentIndexOfFileList(1,startingIndex);
GetSetLastIndexOfFileList(1,endingIndex);
for(int i=startingIndex;i <= endingIndex;i++)
{
//int isFile = 0,
int isDirectory=0;
deptr = fileList[i];
stat(deptr->d_name, &filestat);
SetCursor(currentRC.ws_row,1);
//if(S_ISREG(filestat.st_mode))
// isFile = 1;
if(deptr->d_type == DT_DIR)
{
isDirectory = 1;
}
if(isDirectory)
cout << ">";
SetCursor(currentRC.ws_row,currentRC.ws_col);
cout << deptr->d_name;
string ownerdetails = "";
ownerdetails += ( isDirectory ? "d":"-");
ownerdetails += ((filestat.st_mode & S_IRUSR )? "r":"-");
ownerdetails += ((filestat.st_mode & S_IWUSR )? "w":"-");
ownerdetails += ((filestat.st_mode & S_IXUSR )? "x":"-");
ownerdetails += ((filestat.st_mode & S_IRGRP )? "r":"-");
ownerdetails += ((filestat.st_mode & S_IWGRP )? "w":"-");
ownerdetails += ((filestat.st_mode & S_IXGRP )? "x":"-");
ownerdetails += ((filestat.st_mode & S_IROTH )? "r":"-");
ownerdetails += ((filestat.st_mode & S_IWOTH )? "w":"-");
ownerdetails += ((filestat.st_mode & S_IXOTH )? "x":"-");
string fileSize;
fileSize = readable_fs((double)filestat.st_size);
SetCursor(currentRC.ws_row,40); cout << fileSize;
SetCursor(currentRC.ws_row,60); cout << ownerdetails;
SetCursor(currentRC.ws_row,80); cout << ctime(&filestat.st_mtime) << endl;
currentRC.ws_row++;
}
if(isFromSameSource==0)
{
//writeToFile(" Cursor is Reset because found Scrolling is Not Source");
ResetCursorToDataRow();
//currentRC.ws_row = currentRC.ws_row+2 ;
}
else
{
currentRC.ws_row--;
SetCursor(currentRC.ws_row,currentRC.ws_col);
//writeToFile(" Cursor is Not Reset because found Scrolling is Source");
}
}
void redraw(int mainCalling,int startIndex,int endingIndex,int isFromSameSource)
{
if(mainCalling)
ClearScreen();
ioctl(STDOUT_FILENO,TIOCGWINSZ,&size);
if(!navigationVector.empty())
{
currDir = navigationVector.back();
}
SetWelcomeScreen();
GetCurrentDirectoryDetails(currDir,startIndex,endingIndex,isFromSameSource);
printAtLast("",1);
if(isFromSameSource==0)
{
ResetCursor();
currentRC.ws_row = currentRC.ws_row+2 ;
SetCursor(currentRC.ws_row,currentRC.ws_col);
}
if(isNormalMode)
CursorFunctionality();
}
int main()
{
// 1,0,32-7+1,0
redraw(1,0,MAXIMUMROWNUMBER-STARTTINGROWDATA+1,0);
return 0;
}
void CursorFunctionality()
{
//struct termios oldattr, newattr;
int ch = 0;
//set terminal
/* tcgetattr( STDIN, &oldattr );
newattr = oldattr;
newattr.c_lflag &= ~( ICANON | ECHO );
tcsetattr( STDIN, TCSANOW, &newattr );
setbuf(stdin, NULL);
*/
terminal_init();
int cursorright =COLUMNWIDTH,cursorleft =1;
//NormalMode :
//ch = getchar();
string str(1,ch);
//NormalModeStart :
while ( (((ch = getchar()) != 'q') && (ch!='Q')) && isNormalMode == 1)
{
printAtLast("",1);
int rowI = currentRC.ws_row;
int colI = currentRC.ws_col;
if ( ch == BACKSPACE)
{
GoToUpFolder();
}
if ( ch == ARROWUP)
{
int currentPositionOfFileIndex = GetSetCurrentIndexOfFileList(0,0);
if((currentRC.ws_row-1)<STARTTINGROWDATA)
{
//printAtLast("Scrooling Need to Be Impleented for Up ",1);
if(currentPositionOfFileIndex>0)
{
//writeToFile("Scrooling Need to Be Impleented for Down"+
//to_string(currentPositionOfFileIndex));
int startIndex = currentPositionOfFileIndex-1;
int endingIndex = startIndex + (MAXIMUMROWNUMBER-STARTTINGROWDATA+1);
//currentRC.ws_row--;
//writeToFile("File Up Scrolling " + to_string(startIndex)+" : "+to_string(endingIndex));
redraw(1,startIndex,endingIndex,1);
//writeToFile("POSt Redraw File Up Scrolling " + to_string(startIndex)+" : "+to_string(endingIndex));
}
//else
//{
// writeToFile("Scrooling Need Not to be Done Because Current File Index is 0");
//}
}
else
{
printf ( "\033[A");//cursor up
currentRC.ws_row--;
}
}
if ( ch == ARROWRIGHT) {
printf ( "\033[C");//cursor right
cursorleft++;
cursorright--;
//writeToFile(" Index of Array RIGHT ARROW " +
//to_string(rowI-STARTTINGROWDATA+GetSetCurrentIndexOfFileList(0,0)-1));
GoToNextDirectory(rowI-STARTTINGROWDATA+GetSetCurrentIndexOfFileList(0,0));
//currentRC.ws_col = cursorleft;
SetCursor(rowI,colI);
}
if ( ch == ARROWLEFT) {
printf ( "\033[D");//cursor left
cursorleft--;
cursorright++;
GoToPreviousDirectory();
SetCursor(rowI,colI);
//currentRC.ws_col = cursorleft;
}
if ( ch == ARROWDOWN) {
if(currentRC.ws_row>MAXIMUMROWNUMBER)
{
//currentRC.ws_row++;
//writeToFile("Scrooling Need to Be Impleented for Down");
int currentPositionOfFileIndex = GetSetCurrentIndexOfFileList(0,0);
//writeToFile("Scrooling Need to Be Impleented for Down row : "+to_string(currentRC.ws_row)+" : current " +
//to_string(currentPositionOfFileIndex));
int startIndex = currentRC.ws_row-MAXIMUMROWNUMBER+currentPositionOfFileIndex;
int endingIndex = startIndex + (MAXIMUMROWNUMBER-STARTTINGROWDATA+1);
int lastIndexOfFile = GetSetLastIndexOfFileList(0,0);
//writeToFile("File " + to_string(startIndex)+" : "+to_string(endingIndex)
// +" Last Index Of File "+to_string(lastIndexOfFile)
// + " Index of Array " + to_string(rowI-STARTTINGROWDATA+startIndex-1));
//currentRC.ws_row--;
/*if(endingIndex>=lastIndexOfFile)
{
writeToFile("No Scrolling is Needed because max reached");
writeToFile("File " + to_string(startIndex)+" : "+to_string(endingIndex)
+" Last Index Of File "+to_string(lastIndexOfFile));
}
else*/
if(!(endingIndex>=lastIndexOfFile))
{
//currentRC.ws_row++;
//writeToFile("File " + to_string(startIndex)+" : "+to_string(endingIndex)
//+" Last Index Of File "+to_string(lastIndexOfFile));
redraw(1,startIndex,endingIndex,1);
}
}
else
{
printf ( "\033[B");//cursor down
currentRC.ws_row++;
}
}
if ( ch == '\n') {
//printAtLast(to_string(currentRC.ws_row) + " NM : " + to_string(currentRC.ws_col),1);
GoToNextDirectory(rowI-STARTTINGROWDATA+GetSetCurrentIndexOfFileList(0,0));
//currentRC.ws_col = cursorleft;
SetCursor(rowI,colI);
//printAtLast(to_string(currentRC.ws_row) + " NM : " + to_string(currentRC.ws_col)
//+ " Index of Array " + to_string(rowI-STARTTINGROWDATA) + " file List Size() "+
//to_string(fileList.size()),1);
}
if ( ch == ':') {
isNormalMode = 0;
terminal_done();
GetCommandLine("");
}
if ( ch == 'h' || ch == 'H') {
currDir = startingDirectory;
navigationVector.push_back(currDir);
redraw(1,0,MAXIMUMROWNUMBER-STARTTINGROWDATA+1,0);
}
}
if((ch == 'q' || ch == 'Q') && isNormalMode == 1)
{
// writeToFile("Going In Normal Mode Quit");
printf ( "\033[2J");
//tcsetattr( STDIN, TCSANOW, &oldattr );
terminal_done();
//return;
exit(0);
}
// Command Mode Region
//ch = getchar();
/* string strI;
//cin >> sInput;
//scanf("%s",sInput);
//cout.flush();
//cout << ":" ;
//cout.flush();
//fflush(stdin);
getline (cin, strI);
//cout.flush();
//cin.ignore();
//getline(cin,sInput);
//printAtLast(strI,1);
//ch = strI[0];
//cout << strI ;
char s[350];
if (!cin.fail()) {
cout << "Enter some text: ";
cin.getline(s, 11);
cout << "You entered '"<< s << "'" << endl;
}
strI = string(s);
writeToFile(strI);
writeToFile("testing &&&&&&&&&&& String");
do
{
if(ch == ESC)
{
writeToFile("Going to ESC via terminal Mode ");
struct termios GotToNormalMode;
GotToNormalMode = oldattr;
GotToNormalMode.c_lflag &= ~( ICANON | ECHO );
tcsetattr( STDIN, TCSANOW, &GotToNormalMode );
isNormalMode=1;
ResetCursorToDataRow();
goto NormalMode;
}
if ( ch == '\n') {
//printAtLast(to_string(currentRC.ws_row) + " CM : " + to_string(currentRC.ws_col),1);
GetCommandLine(strI);
}
//writeToFile(strI.append(" -- testing &&&&&&&&&&& String"));
}while (((ch = getchar()) != 'q' && (ch != 'Q')) && isNormalMode == 0);
*/
if((ch == 'q' || ch == 'Q') && isNormalMode == 0)
{
//writeToFile("Going In Command Mode Quit");
printf ( "\033[2J");
//tcsetattr( STDIN, TCSANOW, &oldattr );
terminal_done();
//exit(0);
return;
}
//tcsetattr( STDIN, TCSANOW, &oldattr );
terminal_done();
}
char * GetPreviousDirectoryPath()
{
if(navigationVector.size()<=1)
{
printAtLast(" Not Any Previous Directory ",1);
return NULL;
}
else
{
// char * currentDirectory = navigationVector.back();
navigationVector.pop_back();
char * previousDirectory = navigationVector.back();
return previousDirectory;
}
}
void GoToNextDirectory(int index)
{
if(currDir!=NULL)
{
struct dirent *deptr = NULL;
struct stat filestat;
deptr = fileList[index];
if(isFile(deptr))
{
//printAtLast("It is File So need to Open in Application. Press c to clear",1);
char filePath[512];
strcpy(filePath,"xdg-open ");
strcat(filePath,deptr->d_name);
int returnStatus = system(filePath);
//if(returnStatus==1) printAtLast(string(filePath),1);
//system("xdg-open http://www.google.com");
//writeToFile(string(filePath));
/*
char arr[100];
strcpy(arr,"xdg-open ");
strcat(arr,av[1]);
system(arr);
*/
}
else
{
string sname(deptr->d_name);
string snamedummy = string(deptr->d_name);
if(snamedummy!=".")
{
// We Can Go Back Only When We have more than one item in navigationVector
if(snamedummy=="..")
{
if(navigationVector.size()>1)
{
snamedummy = string(currDir);
size_t found = snamedummy.find_last_of("//");
snamedummy = snamedummy.substr(0,found);
char *cstr = new char[snamedummy.length() + 1];
strcpy(cstr, snamedummy.c_str());
snamedummy = string(cstr);
//navigationVector.push_back(cstr);
navigationVector.pop_back();
redraw(1,0,MAXIMUMROWNUMBER-STARTTINGROWDATA+1,0);
}
}
else
{
stat(deptr->d_name, &filestat);
char * p;
p = (char *)malloc(strlen(deptr->d_name) + strlen(currDir) + 1 + 1);
strcpy(p,currDir);
strcat(p,"/");
strcat(p,deptr->d_name);
navigationVector.push_back(p);
redraw(1,0,MAXIMUMROWNUMBER-STARTTINGROWDATA+1,0);
free(p);
}
}
else
{
printAtLast("You are in Current Directory",1);
}
}
}
}
void GoToPreviousDirectory()
{
char * prevDir = GetPreviousDirectoryPath();
if(prevDir!=NULL)
{
redraw(1,0,MAXIMUMROWNUMBER-STARTTINGROWDATA+1,0);
}
else
printAtLast("No Previous Directory Exist",1);
}
void GoToUpFolder()
{
char * upFolderPath = UpFolderPath;
if(upFolderPath!=NULL)
{
navigationVector.push_back(upFolderPath);
redraw(1,0,MAXIMUMROWNUMBER-STARTTINGROWDATA+1,0);
}
else
printAtLast("No Up Folder Path Defined",1);
}
int EnableUpScrolling()
{
if(isNormalMode)
{
if(currentRC.ws_row>MAXIMUMROWNUMBER)
{
return 1;
}
}
return 0;
}
int EnableDownScrolling()
{
if(isNormalMode)
{
if(currentRC.ws_row<STARTTINGROWDATA)
{
return 1;
}
}
return 0;
}
int invokeCommands(string strCommand)
{
// Added this Line Of Code For Changing Directory Logically
chdir(currDir);
printAtLast("",1);
char cwd[256];
if (getcwd(cwd, sizeof(cwd)) == NULL)
perror("getcwd() error");
// else
// printf("current working directory is: %s\n", cwd);
int i=executeCommands(strCommand,currDir);
// To Clear If Anyhting Written
printAtLast("",1);
//if(i==0)
// writeToFile("SucessRename");
//else
if(i==-11)
printAtLast("Problem in Parameters",1);
else if(i==110)
{
// writeToFile("Redarwing on Success ");
isNormalMode = 1;
redraw(1,0,MAXIMUMROWNUMBER-STARTTINGROWDATA+1,0);
isNormalMode = 0;
}
else if(i==-12)
{
//writeToFile("Invalid Command Name");
//printAtLast("Invalid Command",1);
//SetCursor(currentRC.ws_row-3,55);
}
return 0;
}
/*
int executeCommands(string strCommand){
vector<string> seperatedSpaceCommands;
split(strCommand,seperatedSpaceCommands,' ');
for(int i=0;i<seperatedSpaceCommands.size();i++)
{
writeToFile(seperatedSpaceCommands[i]);
}
exeRenameCommand(NULL,NULL);
return 0;
}
*/
void GetCommandLine(string str)
{
//writeToFile("to_string(ch)");
SetCursor(size.ws_row-2,2);
bool isEscPressed = false,isEnterPressed = false;
do {
int columnNumber =2;
SetCursor(size.ws_row-2,columnNumber);
printf(":");
columnNumber++;
//SetCursor(size.ws_row-3,2);
string str = "";
char ch;
while ((ch = getchar())) {
//writeToFile(to_string(ch));
if(ch == 10)
{
isEnterPressed = true;
break;
}
else if(ch == 27)
{
isEscPressed = true;
/* columnNumber -= 2;
SetCursor(size.ws_row-3,columnNumber);
printf(" "); printf(" ");
*/
break;
}
/* else if( ch == ARROWLEFT || ch == ARROWRIGHT || ch == ARROWDOWN || ch == ARROWUP )
{
writeToFile(" L R U D 7 ");
columnNumber -= 3;
SetCursor(size.ws_row-3,columnNumber);
printf(" "); printf(" "); printf(" ");
}
*/
else if(isalpha(ch) || isblank(ch) || isdigit(ch) || ch == '_' || ch == '.' || ch == '/')
{
str += ch;
columnNumber++;
}
}
if(isEscPressed == true)
{
isNormalMode = 1;
//tcsetattr( STDIN, TCSANOW, &newattr);
terminal_init();
break;
}
if(isEnterPressed == true)
{
SetCursor(size.ws_row-2,2);
for(int i=0;i<size.ws_col;i++){ printf(" "); }
SetCursor(size.ws_row-2,2);
}
//writeToFile(str);
invokeCommands(str);
}while(true);
}