1
- var clipboard = [ ] // To store files to clipboard to copy or move them
1
+ var clipboard = [ ] // To store files to clipboard to copy or move them
2
2
var pre = "user@root" // Beginning of each terminal line
3
- var current_path = "/" // Current location of user
3
+ var current_path = "/" // Current location of user
4
4
var pointer = - 1 // pointer points to the index of the current directory
5
5
//pointer = -1 corresponds to root directory
6
6
var file_dir = [ ] // List of files and directories
7
- var commands = [ "create" , "vi" , "rm" , "truncate" , "ls" , "rename" , "properties" , "command_list" ]
7
+ var commands = [ "create" , "vi" , "rm" , "truncate" , "ls" , "rename" , "properties" , "command_list" ] //List of predefined commands
8
8
9
9
/*
10
+ ------------COMMANDS AVAILABLE-------------
10
11
create : For creating a file
11
12
mkdir : Creating a directory
12
13
rmdir : Deleting a directory
@@ -26,8 +27,9 @@ var lslabel
26
27
var index
27
28
28
29
29
- start ( )
30
+ start ( ) //Intializing the terminal UI
30
31
32
+ //Function to run terminal UI
31
33
function start ( )
32
34
{
33
35
var div = document . getElementById ( 'divx' )
@@ -36,42 +38,42 @@ function start()
36
38
textbox = document . createElement ( 'input' )
37
39
textbox . setAttribute ( "class" , "textbox" )
38
40
textbox . setAttribute ( "type" , "text" )
39
- textbox . setAttribute ( "onkeydown" , "nextline(event,textbox.value)" )
41
+ textbox . setAttribute ( "onkeydown" , "nextline(event,textbox.value)" ) //Detect every key pressed from terminal and send to nextline() function
40
42
div . appendChild ( label )
41
43
div . appendChild ( textbox )
42
44
div . appendChild ( br )
43
45
textbox . focus ( )
44
46
}
45
47
46
-
48
+ //Function to run the input command from terminal
47
49
function nextline ( event , text )
48
50
{
49
51
var x = event . keyCode ;
50
- if ( x == 13 )
52
+ if ( x == 13 ) // On ENTER key press (Keycode of ENTER is 13)
51
53
{
52
- divide = text . split ( " " )
54
+ divide = text . split ( " " ) //Tokenize command
53
55
user_com = divide [ 0 ]
54
56
for ( i = 0 ; i < commands . length ; i ++ )
55
57
{
56
- if ( user_com == commands [ i ] )
58
+ if ( user_com == commands [ i ] ) //Detecting the command (Which command it is)
57
59
{
58
60
break
59
61
}
60
62
}
61
- if ( i < commands . length )
63
+ if ( i < commands . length ) //Checking if command is predefined ie., is among the predefined list
62
64
{
63
- switch ( i )
65
+ switch ( i ) //Running the command
64
66
{
65
67
case 0 :create ( divide [ 1 ] )
66
- break
68
+ break
67
69
case 1 :vi ( divide [ 1 ] )
68
- break
70
+ break
69
71
case 2 :rm ( divide [ 1 ] )
70
72
break
71
73
case 3 :truncate ( divide [ 1 ] )
72
- break
74
+ break
73
75
case 4 :ls ( )
74
- break
76
+ break
75
77
case 5 :rename ( divide [ 1 ] , divide [ 2 ] )
76
78
break
77
79
case 6 :properties ( divide [ 1 ] )
@@ -92,48 +94,50 @@ Each entry is a list
92
94
4. Size : Length of content of file - String format
93
95
5. Protection : User, Group, Other
94
96
6. Location : Location of file
95
- 7. Content : String
97
+ 7. Content : String
96
98
*/
97
99
98
- function create ( file ) // Working
100
+ //Function to create a text file. Syntax: create <filename>
101
+ function create ( file )
99
102
{
100
- for ( i = 0 ; i < file_dir . length ; i ++ )
103
+ for ( i = 0 ; i < file_dir . length ; i ++ ) //Traversing through all files to check if filename already exists
101
104
{
102
- if ( file_dir [ i ] [ 0 ] == file && file_dir [ i ] [ 2 ] == pointer ) // Second condition verifies whether elements of current directory are checked
103
- { // or not
105
+ if ( file_dir [ i ] [ 0 ] == file && file_dir [ i ] [ 2 ] == pointer ) // Second condition verifies whether elements of current directory are checked
106
+ { // or not
104
107
alert ( "File already exists" )
105
108
start ( )
106
109
return
107
110
}
108
111
}
109
- file_dir . push ( [ file , 0 , pointer , 0 , 666 , current_path , "" ] )
112
+ file_dir . push ( [ file , 0 , pointer , 0 , 666 , current_path , "" ] ) //Creating empty file
110
113
start ( )
111
114
}
112
115
113
-
114
- function ls ( ) // Working
116
+ //Function to list all content in the directory ie., system
117
+ function ls ( )
115
118
{
116
119
var div = document . getElementById ( 'divx' )
117
- for ( i = 0 ; i < file_dir . length ; i ++ )
120
+ for ( i = 0 ; i < file_dir . length ; i ++ ) //Getting names of all files present
118
121
{
119
122
if ( file_dir [ i ] [ 2 ] == pointer )
120
123
{
121
- var label = document . createTextNode ( String ( file_dir [ i ] [ 0 ] ) + " " )
122
- div . appendChild ( label )
124
+ var label = document . createTextNode ( String ( file_dir [ i ] [ 0 ] ) + " " ) //Getting filename
125
+ div . appendChild ( label )
123
126
}
124
- }
127
+ }
125
128
var br = document . createElement ( "br" )
126
- div . appendChild ( br )
129
+ div . appendChild ( br )
127
130
start ( )
128
131
}
129
132
130
- function rm ( file ) // Working
133
+ //Function to remove a file. Syntax: rm <filename>
134
+ function rm ( file )
131
135
{
132
136
for ( i = 0 ; i < file_dir . length ; i ++ )
133
137
{
134
- if ( file_dir [ i ] [ 0 ] == file && file_dir [ i ] [ 2 ] == pointer && file_dir [ i ] [ 1 ] == 0 )
135
- {
136
- file_dir . splice ( i , 1 )
138
+ if ( file_dir [ i ] [ 0 ] == file && file_dir [ i ] [ 2 ] == pointer && file_dir [ i ] [ 1 ] == 0 ) //3rd Check is to confirm this is a file, not a folder
139
+ {
140
+ file_dir . splice ( i , 1 ) //Removing file
137
141
start ( )
138
142
return
139
143
}
@@ -146,28 +150,29 @@ function rm(file) // Working
146
150
start ( )
147
151
}
148
152
149
- function vi ( file ) // Working
153
+ //Function to edit a text file. Syntax: vi <filename>
154
+ function vi ( file )
150
155
{
151
156
div = document . getElementById ( 'divx' )
152
157
var flag = 0
153
158
for ( i = 0 ; i < file_dir . length ; i ++ )
154
159
{
155
- if ( file_dir [ i ] [ 0 ] == file && file_dir [ i ] [ 2 ] == pointer && file_dir [ i ] [ 1 ] == 0 )
156
- {
160
+ if ( file_dir [ i ] [ 0 ] == file && file_dir [ i ] [ 2 ] == pointer && file_dir [ i ] [ 1 ] == 0 )
161
+ {
157
162
flag = 1 // File is found
158
163
var br = document . createElement ( "br" )
159
164
var br1 = document . createElement ( "br" )
160
- var prompt = document . createTextNode ( "Press Ctrl to Save and Exit" )
161
- contents = document . createElement ( 'TextArea' )
165
+ var prompt = document . createTextNode ( "Press Ctrl to Save and Exit" )
166
+ contents = document . createElement ( 'TextArea' ) // Create/display textbox to edit/add content to the file
162
167
contents . setAttribute ( "rows" , "15" )
163
168
contents . setAttribute ( "cols" , "120" )
164
169
index = i
165
- contents . setAttribute ( "onkeydown" , "savefile(event,contents.value,index,contents)" )
170
+ contents . setAttribute ( "onkeydown" , "savefile(event,contents.value,index,contents)" ) //Add attribute to DOM to send data on each key press
166
171
contents . value = file_dir [ i ] [ 6 ] // Contents of file are copied to the textbox
167
172
div . appendChild ( prompt )
168
173
div . appendChild ( br )
169
174
div . appendChild ( contents )
170
- div . appendChild ( br1 )
175
+ div . appendChild ( br1 )
171
176
contents . focus ( )
172
177
}
173
178
}
@@ -181,27 +186,31 @@ function vi(file) // Working
181
186
}
182
187
}
183
188
184
- function savefile ( event , text , index , contents ) // Save contents of file // Working
189
+
190
+ //Function to save contents of a file
191
+ function savefile ( event , text , index , contents )
185
192
{
186
193
var x = event . keyCode
187
- if ( x == 17 )
194
+ if ( x == 17 ) //Check if CTRL key is pressed
188
195
{
189
196
file_dir [ index ] [ 6 ] = text
190
197
file_dir [ index ] [ 3 ] = String ( text . length )
191
198
alert ( "File saved" )
192
- contents . setAttribute ( "disabled" , true )
199
+ contents . setAttribute ( "disabled" , true ) //Disable textbox
193
200
start ( )
194
201
}
195
- }
202
+ }
203
+
196
204
205
+ //Function to truncate a file. Syntax: truncate <filename>
197
206
function truncate ( file )
198
207
{
199
208
for ( i = 0 ; i < file_dir . length ; i ++ )
200
209
{
201
- if ( file_dir [ i ] [ 0 ] == file && file_dir [ i ] [ 2 ] == pointer && file_dir [ i ] [ 1 ] == 0 )
202
- {
203
- file_dir [ i ] [ 6 ] = ""
204
- file_dir [ i ] [ 3 ] = "0"
210
+ if ( file_dir [ i ] [ 0 ] == file && file_dir [ i ] [ 2 ] == pointer && file_dir [ i ] [ 1 ] == 0 )
211
+ {
212
+ file_dir [ i ] [ 6 ] = "" //Set content empty
213
+ file_dir [ i ] [ 3 ] = "0" //Set file size to 0
205
214
start ( )
206
215
return
207
216
}
@@ -214,13 +223,15 @@ function truncate(file)
214
223
start ( )
215
224
}
216
225
226
+
227
+ //Function to rename file. Syntax: rename <old filename> <new filename>
217
228
function rename ( file , name )
218
229
{
219
230
for ( i = 0 ; i < file_dir . length ; i ++ )
220
231
{
221
- if ( file_dir [ i ] [ 0 ] == file && file_dir [ i ] [ 2 ] == pointer && file_dir [ i ] [ 1 ] == 0 )
222
- {
223
- file_dir [ i ] [ 0 ] = name
232
+ if ( file_dir [ i ] [ 0 ] == file && file_dir [ i ] [ 2 ] == pointer && file_dir [ i ] [ 1 ] == 0 )
233
+ {
234
+ file_dir [ i ] [ 0 ] = name //Set filename
224
235
start ( )
225
236
return
226
237
}
@@ -233,15 +244,16 @@ function rename(file,name)
233
244
start ( )
234
245
}
235
246
236
- function properties ( file ) // Working
247
+ //Function to display file properties. Syntax: properties <filename>
248
+ function properties ( file )
237
249
{
238
250
for ( i = 0 ; i < file_dir . length ; i ++ )
239
251
{
240
- if ( file_dir [ i ] [ 0 ] == file && file_dir [ i ] [ 2 ] == pointer && file_dir [ i ] [ 1 ] == 0 ) // For files
241
- {
252
+ if ( file_dir [ i ] [ 0 ] == file && file_dir [ i ] [ 2 ] == pointer && file_dir [ i ] [ 1 ] == 0 ) // For files
253
+ {
242
254
var div = document . getElementById ( 'divx' )
243
255
var l1 = document . createTextNode ( "Name : " + file_dir [ i ] [ 0 ] )
244
- var br1 = document . createElement ( "br" )
256
+ var br1 = document . createElement ( "br" )
245
257
var l2 = document . createTextNode ( "Type : text" )
246
258
var br2 = document . createElement ( "br" )
247
259
var l3 = document . createTextNode ( "Size : " + file_dir [ i ] [ 3 ] )
@@ -268,6 +280,7 @@ function properties(file) // Working
268
280
start ( )
269
281
}
270
282
283
+ //Function to display available commands list
271
284
function command_list ( )
272
285
{
273
286
var div = document . getElementById ( "divx" )
0 commit comments