Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit be05e63

Browse files
committed
Add Error Handling n Stuff
1 parent f8b0a73 commit be05e63

File tree

1 file changed

+82
-47
lines changed

1 file changed

+82
-47
lines changed

js/app.js

Lines changed: 82 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,26 @@ app.controller('geekController', ['$scope', '$location', function($scope, $locat
3838
$scope.$apply();
3939
}, 0.1);
4040

41+
//Commands
42+
43+
44+
function Cmd(name,desc,argc=0,multi_arg) {
45+
this.name = name; //Name of cmd
46+
this.desc = desc; //Man Description
47+
this.argc = argc; //Number of arguements it supports
48+
this.multi_arg = multi_arg; //If the cmd should supports multiple arguements like cat
49+
}
50+
//js doesn't support default arguements >_<
51+
myCmds = [
52+
new Cmd("help","No manual entry for help",0,false),
53+
new Cmd("ls", "List current working directory contents",0,false),
54+
new Cmd("man","Man page for commands\n Usage: man [PATH]",1,false),
55+
new Cmd("whoami","A description of our activities and what we represent",0,false),
56+
new Cmd("cat","Prints the contents of the file to the standard output",1,true),
57+
new Cmd("clr","No manual entry for clr",0,false),
58+
new Cmd("cd","Cmd for change working directory\n Usage: cd [PATH]",1,false),
59+
new Cmd("exit","Navigate back to the home page",0,false),
60+
];
4161

4262
/* ~Functions~ */
4363
function tHelp() {
@@ -73,35 +93,18 @@ app.controller('geekController', ['$scope', '$location', function($scope, $locat
7393
}, 0.1);
7494
}
7595

76-
function tMan(cmd) {
96+
function tMan(argv) {
7797
debugger;
7898
setTimeout(function() {
79-
80-
var def="Usage: man [PATH]";
81-
function Cmd(name,desc) {
82-
this.name = name;
83-
this.desc = desc;
84-
}
85-
var myCmds = [
86-
new Cmd("help","No manual entry for help"),
87-
new Cmd("ls", "List current working directory contents"),
88-
new Cmd("man","Man page for commands\n "+def),
89-
new Cmd("whoami","A description of our activities and what we represent"),
90-
new Cmd("cat","Prints the contents of the file to the standard output"),
91-
new Cmd("clr","No manual entry for clr"),
92-
new Cmd("cd","Cmd for change working directory\n Usage: cd [PATH]"),
93-
new Cmd("exit","Navigate back to the home page"),
94-
];
95-
96-
var arg=cmd.command;
97-
arg=arg.substr(arg.indexOf(' ')+1);
98-
var string="Invalid Command!\n "+def;
99+
var string=argv[1]+" is not a valid Command";
99100
for(var i in myCmds)
100-
if(myCmds[i].name===arg)
101+
if(myCmds[i].name===argv[1])
101102
{
102103
string=myCmds[i].desc;
103104
break;
104-
}
105+
}
106+
if(argv.length==1)
107+
string="Wrong Usage!\n Usage: man [COMMAND]";
105108

106109
$scope.$broadcast('terminal-output', {
107110
output: true,
@@ -128,20 +131,20 @@ app.controller('geekController', ['$scope', '$location', function($scope, $locat
128131
}, 0.1);
129132
}
130133

131-
function tCat(cmd) {
132-
debugger;
134+
function tCat(argv) {
133135
setTimeout(function() {
134-
var check=cmd.command;
135136
var ce='Workshop on Website Penetration\n 27th August at 5:45 PM\n NLH 204';
136137
var py="PyPals is organizing MUPy, a conference to foster interest and awareness about Python.\n Along with several alumni of the college, PyPals shall also be inviting renowned speakers from different parts of the country to motivate and inspire coders and beginners alike.\n Check them out at www.pypals.org";
137-
var def="Usage: cat [FILE]";
138+
var wrngfile=argv[1]+" is not a valid file name";
138139
var tur="Can't open directories";
139-
switch(check.substr(check.indexOf(' ')+1)) {
140-
case 'Current Events': str=ce; break;
140+
switch(argv[1]) {
141+
case 'Current_Events': str=ce; break;
141142
case 'MUPy': str=py; break;
142143
case 'Turing/': str=tur; break;
143-
default: str=def;
144+
default: str=wrngfile;
144145
}
146+
if(argv.length==1)
147+
str="Wrong Usage!\n Usage: cat [FILE]";
145148
$scope.$broadcast('terminal-output', {
146149
output: true,
147150
text: [str],
@@ -170,34 +173,66 @@ app.controller('geekController', ['$scope', '$location', function($scope, $locat
170173
}, 0.1);
171174
}
172175

173-
function tDefault() {
176+
function tError(int=0) {
174177
setTimeout(function() {
178+
var str="Error: ";
179+
switch(int)
180+
{
181+
case 0: str+='Invalid Command!\n type help to get help (duh)'; break;
182+
case 1: str+="Command doesn't take arguements"; break;
183+
case 2: str+="No support for multiple arguement right now"; break;
184+
case 3: str+="Command should take one arguement"; break;
185+
}
175186
$scope.$broadcast('terminal-output', {
176187
output: true,
177-
text: ['Invalid Command!',
178-
'type help to get help (duh)'
179-
],
188+
text: [str],
180189
breakLine: true
181190
});
182191
$scope.$apply();
183192
}, 0.1);
193+
return true;
184194
}
185195

186-
/* Take user input */
187-
$scope.$on('terminal-input', function(e, consoleInput) {
188-
var cmd = consoleInput[0];
189-
switch(cmd.command.split(" ")[0]) {
190-
case 'help': tHelp(); break;
191-
case 'whoami': tWhoAmI(); break;
192-
case 'man' : tMan(cmd); break;
193-
case 'ls' : tLs(); break;
194-
case 'cat' : tCat(cmd); break;
195-
case 'cd' : tCd(cmd); break;
196-
case 'clr': tClr(); break;
197-
case 'exit': tExit(); break;
198-
default: tDefault();
196+
function errorCheck(argv,argc)
197+
{
198+
var err=false;
199+
for(var i in myCmds)
200+
{
201+
if(myCmds[i].name===argv[0])
202+
{
203+
if(myCmds[i].argc==0 && argc!=1)
204+
err=tError(1);
205+
else if(myCmds[i].argc==1 && argc>2)
206+
{
207+
if(myCmds[i].multi_arg)
208+
err=tError(2);
209+
else
210+
err=tError(3);
211+
}
212+
break;
213+
}
199214
}
215+
return err;
216+
//tError();
217+
}
200218

219+
/* Take user input */
220+
$scope.$on('terminal-input', function(e, consoleInput) {
221+
var cmd = consoleInput[0].command;
222+
var argv = cmd.split(" ");
223+
var argc = cmd.match(/(\w+)/g).length;
224+
if(!errorCheck(argv,argc))
225+
switch(argv[0]) {
226+
case 'help': tHelp(); break;
227+
case 'whoami': tWhoAmI(); break;
228+
case 'ls' : tLs(); break;
229+
case 'clr': tClr(); break;
230+
case 'exit': tExit(); break;
231+
case 'man' : tMan(argv); break;
232+
case 'cat' : tCat(argv); break;
233+
case 'cd' : tCd(cmd); break;
234+
default: tError();
235+
}
201236

202237
// $location.path('/newNgRouteYouWishToDisplay');
203238
});

0 commit comments

Comments
 (0)