-
Notifications
You must be signed in to change notification settings - Fork 2
/
terminal.js
63 lines (50 loc) · 1.82 KB
/
terminal.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
define(['jquery', 'command_processor', 'screen'], function ($, command, screen) {
var Terminal = function () {
};
Terminal.prototype = {
init: function (opts) {
this.container = $(opts.container);
this.form = $(opts.form);
this.input = $(opts.input);
this.output = $(opts.output);
screen.init(this.output);
command.init(this.output, this.input);
this.setSizeAndFocusCursor();
this.listenForEvents();
this.welcome();
},
setSizeAndFocusCursor: function () {
var windowHeight = $(window).height(),
maxHeight = (windowHeight - this.input.height() - 50) + 'px';
this.container.height(windowHeight);
screen.setHeight(maxHeight);
this.input.focus();
},
listenForEvents: function () {
var terminal = this;
this.container.on('click', function () {
terminal.input.focus();
});
this.form.on('submit', function (e) {
terminal.processCommand();
e.preventDefault();
return false;
});
},
processCommand: function () {
var cmd = command.clean(this.input.val());
this.input.val('');
screen.stdout('$ ' + cmd);
command.process(cmd, function () {
screen.prompt();
});
},
welcome: function () {
screen.stdout("Welcome to Chris Ashton's terminal.");
screen.stdout("Don't know where to begin? Try typing `help` into the terminal, then hit Return.");
screen.stdout("===================================");
screen.prompt();
}
};
return new Terminal();
});