-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconsole.js
More file actions
62 lines (58 loc) · 1.77 KB
/
console.js
File metadata and controls
62 lines (58 loc) · 1.77 KB
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
/*
WINDOWS93 CONSOLE
- create: new $console(title)
- print: obj.print(text) or obj.print(text, color)
- input: obj.input(text, callback)
*/
class $console {
constructor(title) {
//Create element and set stile for console
this.el = document.createElement('div');
this.el.style.width = '100%';
this.el.style.height = '100%';
this.el.style.backgroundColor = 'white';
this.el.style.border = '2px inset';
this.el.style.overflowY = 'auto';
this.el.style.overflowX = 'hidden';
this.el.style.font = '12px Consolas';
//Create a window with given title and add el to it.
$window({title: title}).el.body.appendChild(this.el);
//Print function
this.print = function(text, color) {
//If color entered, then create a span
if(color != undefined) {
var s = document.createElement('span');
s.style.color = color;
s.innerText = text.split(' ').join(String.fromCharCode(160)) + '\n';
this.el.appendChild(s);
} else {
this.el.innerText += text.split(' ').join(String.fromCharCode(160)) + '\n';
}
//Scroll to bottom of printed text
this.el.scrollTop = this.el.scrollHeight;
}
//Input function
this.input = function(text, cb, bas) {
//print text and create an input field
this.el.innerText += text.split(' ').join(String.fromCharCode(160));
this.el.scrollTop = this.el.scrollHeight;
var inp = document.createElement('input');
inp.style.width = '50%';
inp.style.outline = 'none';
inp.style.border = 'none';
inp.style.height = '12px';
inp.style.font = '12px Consolas';
var term = this;
var calb = cb;
//When enter clicked, run callback
inp.onkeydown = function(e) {
if(e.keyCode == 13) {
term.print(inp.value);
calb(inp.value, bas);
}
}
this.el.appendChild(inp);
inp.select();
}
}
}