@@ -38,6 +38,26 @@ app.controller('geekController', ['$scope', '$location', function($scope, $locat
38
38
$scope . $apply ( ) ;
39
39
} , 0.1 ) ;
40
40
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
+ ] ;
41
61
42
62
/* ~Functions~ */
43
63
function tHelp ( ) {
@@ -73,35 +93,18 @@ app.controller('geekController', ['$scope', '$location', function($scope, $locat
73
93
} , 0.1 ) ;
74
94
}
75
95
76
- function tMan ( cmd ) {
96
+ function tMan ( argv ) {
77
97
debugger ;
78
98
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" ;
99
100
for ( var i in myCmds )
100
- if ( myCmds [ i ] . name === arg )
101
+ if ( myCmds [ i ] . name === argv [ 1 ] )
101
102
{
102
103
string = myCmds [ i ] . desc ;
103
104
break ;
104
- }
105
+ }
106
+ if ( argv . length == 1 )
107
+ string = "Wrong Usage!\n Usage: man [COMMAND]" ;
105
108
106
109
$scope . $broadcast ( 'terminal-output' , {
107
110
output : true ,
@@ -128,20 +131,20 @@ app.controller('geekController', ['$scope', '$location', function($scope, $locat
128
131
} , 0.1 ) ;
129
132
}
130
133
131
- function tCat ( cmd ) {
132
- debugger ;
134
+ function tCat ( argv ) {
133
135
setTimeout ( function ( ) {
134
- var check = cmd . command ;
135
136
var ce = 'Workshop on Website Penetration\n 27th August at 5:45 PM\n NLH 204' ;
136
137
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 ";
138
139
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 ;
141
142
case 'MUPy' : str = py ; break ;
142
143
case 'Turing/' : str = tur ; break ;
143
- default : str = def ;
144
+ default : str = wrngfile ;
144
145
}
146
+ if ( argv . length == 1 )
147
+ str = "Wrong Usage!\n Usage: cat [FILE]" ;
145
148
$scope . $broadcast ( 'terminal-output' , {
146
149
output : true ,
147
150
text : [ str ] ,
@@ -170,34 +173,66 @@ app.controller('geekController', ['$scope', '$location', function($scope, $locat
170
173
} , 0.1 ) ;
171
174
}
172
175
173
- function tDefault ( ) {
176
+ function tError ( int = 0 ) {
174
177
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
+ }
175
186
$scope . $broadcast ( 'terminal-output' , {
176
187
output : true ,
177
- text : [ 'Invalid Command!' ,
178
- 'type help to get help (duh)'
179
- ] ,
188
+ text : [ str ] ,
180
189
breakLine : true
181
190
} ) ;
182
191
$scope . $apply ( ) ;
183
192
} , 0.1 ) ;
193
+ return true ;
184
194
}
185
195
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
+ }
199
214
}
215
+ return err ;
216
+ //tError();
217
+ }
200
218
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
+ }
201
236
202
237
// $location.path('/newNgRouteYouWishToDisplay');
203
238
} ) ;
0 commit comments