-
Notifications
You must be signed in to change notification settings - Fork 2
/
command_processor.js
202 lines (167 loc) · 6.85 KB
/
command_processor.js
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
define(['jquery', 'screen', 'command_history'], function ($, screen, history) {
var CommandProcessor = function () {
this.processingCommand = false;
// NB: functions beginning with cmd_ are real BASH commands
this.validCommands = {
'commands': 'cmd_cmds',
'cat': 'cmd_more',
'cd': 'cmd_cd',
'clear': 'cmd_clear',
'finger': 'cmd_finger',
'grep': 'cmd_grep',
'head': 'cmd_more',
'history': 'cmd_history',
'less': 'cmd_more',
'ls': 'cmd_ls',
'man': 'cmd_man',
'more': 'cmd_more',
'pwd': 'cmd_pwd',
'rm': 'cmd_rm',
'sudo': 'cmd_sudo',
'tail': 'cmd_more',
'download': 'download',
'hello': 'hello',
'hi': 'hello',
'wassup': 'hello',
'help': 'help'
};
};
CommandProcessor.prototype = {
init: function (output, input) {
this.output = output;
this.input = input;
history.init(this.input);
},
clean: function (command) {
command = command.replace(/</g, '<');
command = command.replace(/>/g, '>');
command = command.replace(/"/g, '');
command = command.replace(/'/g, "");
return command;
},
process: function (command, callback) {
var firstPartOfCommand = command.substr(0, command.indexOf(' ')) || command,
commandArguments = command.replace(firstPartOfCommand, '').trim();
functionToCall = this.validCommands[firstPartOfCommand];
history.log(command);
if (typeof functionToCall === 'undefined') {
screen.stderr('-bash: '+ command +': command not found');
} else {
eval('this.' + functionToCall + '("' + commandArguments + '")');
}
this.performCallbackWhenReady(callback);
},
performCallbackWhenReady: function (callback) {
var processor = this;
if (processor.processingCommand === true) {
setTimeout(function () {
processor.performCallbackWhenReady(callback);
}, 50);
} else {
callback();
}
},
dotdotdot: function (line, numberOfDots) {
var self = this;
if (numberOfDots > 0) {
setTimeout(function () {
line.html(line.html() + '.');
numberOfDots--;
self.dotdotdot(line, numberOfDots);
}, 50);
} else {
line.html(line.html() + ' 100%. <br /> <br /> ...just kidding.');
this.processingCommand = false;
}
},
cmd_cd: function (args) {
var firstChar = args.length > 0 ? args.charAt(0):'';
var baseUrl = "//ashton.codes";
// @TODO - doesn't seem to work properly
if (
args === '..' ||
firstChar === '' ||
firstChar === '/' ||
firstChar === '.' ||
firstChar === '~'
) {
window.top.location.href = baseUrl;
}
window.top.location.href = baseUrl + '/' + args;
},
cmd_clear: function (args) {
screen.clear();
},
cmd_finger: function (args) {
screen.stdout("We'll have none of that in MY terminal! Leave '" + args + "' alone!");
},
cmd_grep: function (args) {
screen.stdout("Nobody really knows how to use `grep` properly. Let's not kid ourselves.");
},
cmd_history: function (args) {
if (args === '-c') {
history.clear();
} else {
var commands = history.get();
for (var i = 0; i < commands.length; i++) {
var commandNumber = i + 1;
screen.stdout(commandNumber + ' ' + commands[i]);
}
}
},
cmd_ls: function (args) {
screen.stdout("<ul class='terminal__list'><li>about</li><li>blog</li><li>portfolio</li><li>contact</li></ul>");
},
cmd_cmds: function () {
var listOfCommands = "<ul class='terminal__list'>";
for (var key in this.validCommands) {
listOfCommands = listOfCommands + '<li>' + key + '</li>';
}
listOfCommands = listOfCommands + '</ul>';
screen.stdout(listOfCommands);
},
cmd_more: function (filename) {
var descriptions = {
"about": "Find out more about Chris Ashton.",
"blog": "Read some of my thoughts, tech-related or otherwise.",
"portfolio": "See some of my lovely websites and applications.",
"contact": "Send me a message!"
};
if (typeof descriptions[filename] !== 'undefined') {
screen.stdout(descriptions[filename]);
} else {
screen.stderr('Invalid file specified!');
}
},
cmd_man: function (command) {
screen.stdout("There is no man page for `" + command + "` (or for any other command, for that matter). Try `help` if you're stuck.");
},
cmd_pwd: function () {
screen.stdout("/follow/the/yellow/brick/road/");
},
cmd_rm: function (args) {
this.processingCommand = true;
screen.stdout("Trying to sabotage my site, eh?");
screen.stdout("Downloading virus");
writeOnSameLine = this.output.find('.terminal__output').last();
this.dotdotdot(writeOnSameLine, 10);
},
cmd_sudo: function (args) {
screen.stdout("You are a guest user. No sudo for you.");
},
download: function (args) {
this.processingCommand = true;
screen.stdout("Downloading 3.4Gb of awesomeness");
writeOnSameLine = this.output.find('.terminal__output').last();
this.dotdotdot(writeOnSameLine, 100);
},
hello: function (args) {
screen.stdout('Right back at you!');
},
help: function () {
screen.stdout("Try typing `ls` to list the pages available, then redirect to a page by typing, for example, `cd about`. You can pull up this terminal on any page with SHIFT+T.");
screen.stdout("For a full list of recognised commands, type `commands`. Have fun!");
}
};
return new CommandProcessor();
});