-
Notifications
You must be signed in to change notification settings - Fork 0
/
terminal.cpp
121 lines (105 loc) · 3.14 KB
/
terminal.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
#include <bits/stdc++.h>
#include "utils.h"
#include "folder.h"
#include "fileSystem.h"
using namespace std;
#define int long long
#define fastIO ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define pii pair<long long, long long>
#define ff first
#define ss second
const int mod = 1e9+7;
/*
--- / - a
| | - a/c
| - b
| | - b/d
*/
void example() {
FileSystem *fs = new FileSystem();
fs->ls();
fs->mkdir("/a");
fs->mkdir("b");
fs->ls();
fs->cd("a");
fs->pwd();
cout<<"@\n";
fs->mkdir("c");
fs->ls();
fs->mkdir("/b/d");
fs->cd("*"); fs->pwd();
fs->cd(".."); fs->pwd();
fs->cd(".."); fs->pwd();
fs->cd(".."); fs->pwd();
fs->cd("b"); fs->pwd();
fs->ls();
fs->printdisk();
}
// readme outputs documentation on how to use terminal
void readme() {
cout<<"################### how to use terminal❓ ###################\n";
int functions =0;
string readme = "\n" + to_string(++functions) + ". help : to output documentation\n";
readme+= to_string(++functions) + ". mkdir <param path of folder> : make a directory\n";
readme+= to_string(++functions) + ". rm <param path of folder> : remove a directory recursively\n";
readme+= to_string(++functions) + ". cd <param path of folder> : change path to the specified directory\n";
readme+= to_string(++functions) + ". ls : list directory\n";
readme+= to_string(++functions) + ". pwd - print current working directory\n";
readme+= to_string(++functions) + ". printdir : print all folders with in the current directory\n";
readme+= to_string(++functions) + ". printdisk : print all folders from root directory\n";
cout<<readme<<"\n";
cout<<"################### ----------------- ###################\n";
}
void printDirHead(FileSystem *fs) {
cout<<"wtf1.1$ "<<fs->getCurrentDir()<<" > " ;
}
bool interact(string inp, FileSystem *fs) {
inp=utils::truncateTrailingSpace(inp);
auto wrongIn = [&] {
cout<<"❌invalid input, use \"help\" to get documentation\n";
};
vector<string> inpv = utils::splitString(inp, ' ');
switch(inpv.size()) {
case 0:
return true;
case 1:
if(inpv[0] == "help") readme();
else if(inpv[0] == "ls") fs->ls();
else if(inpv[0] == "pwd") fs->pwd();
else if(inpv[0] == "printdir") fs->printdir();
else if(inpv[0] == "printdisk") fs->printdisk();
else if(inpv[0] == "exit") return false;
else wrongIn();
break;
case 2:
if(inpv[0]=="cd") {
if(fs->cd(inpv[1])) cout<<"✅ changed directory\n";
else cout<<"❌ error changing directory\n";
} else if(inpv[0]=="mkdir") {
if(fs->mkdir(inpv[1])) cout<<"✅ created directory\n";
else cout<<"❌ error creating directory\n";
} else if(inpv[0]=="rm") {
if(fs->rm(inpv[1])) cout<<"✅ removed directory\n";
else cout<<"❌ error removing directory\n";
} else wrongIn();
break;
default:
wrongIn();
}
return true;
}
signed main() {
fastIO;
FileSystem *fs = new FileSystem();
cout<<"\n\n☺☺☺☺☺☺☺☺[WTF]☺☺☺☺☺☺☺☺\nwelcome to funny-terminal\n\n";
readme(); cout.flush();
printDirHead(fs); cout.flush();
while(true) {
string inp;
getline(cin, inp);
if(!interact(inp, fs)) break;
printDirHead(fs);
cout.flush();
}
return 0;
}