Skip to content

Commit eb6a5a4

Browse files
committed
0.4.4, Syntax highlight, better scrolling
1 parent f83368d commit eb6a5a4

File tree

5 files changed

+78
-9
lines changed

5 files changed

+78
-9
lines changed

build/plainterm.js.min.css

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

build/plainterm.js.min.js

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "plainterm.js",
3-
"version": "0.4.3",
3+
"version": "0.4.4",
44
"description": "A dead simple lightweight pure Javascript terminal \"emulator\" that mimics terminal behaviour in browser.",
55
"main": "plainterm.js",
66
"devDependencies": {

plainterm.js

+62-5
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,65 @@
11
/* plainterm.js
22
https://github.com/mkrl/plainterm.js */
33

4+
/*
5+
element.scrollIntoViewIfNeeded() implementation for non-webkit browsers
6+
full creadit goes to
7+
https://gist.github.com/doxxx/8987233
8+
and the original gist author
9+
*/
10+
11+
12+
if (!Element.prototype.scrollIntoViewIfNeeded) {
13+
Element.prototype.scrollIntoViewIfNeeded = function (centerIfNeeded) {
14+
centerIfNeeded = arguments.length === 0 ? true : !!centerIfNeeded;
15+
16+
var parent = this.parentNode,
17+
parentComputedStyle = window.getComputedStyle(parent, null),
18+
parentBorderTopWidth = parseInt(parentComputedStyle.getPropertyValue('border-top-width')),
19+
parentBorderLeftWidth = parseInt(parentComputedStyle.getPropertyValue('border-left-width')),
20+
overTop = this.offsetTop - parent.offsetTop < parent.scrollTop,
21+
overBottom = (this.offsetTop - parent.offsetTop + this.clientHeight - parentBorderTopWidth) > (parent.scrollTop + parent.clientHeight),
22+
overLeft = this.offsetLeft - parent.offsetLeft < parent.scrollLeft,
23+
overRight = (this.offsetLeft - parent.offsetLeft + this.clientWidth - parentBorderLeftWidth) > (parent.scrollLeft + parent.clientWidth);
24+
25+
if (centerIfNeeded) {
26+
if (overTop || overBottom) {
27+
parent.scrollTop = this.offsetTop - parent.offsetTop - parent.clientHeight / 2 - parentBorderTopWidth + this.clientHeight / 2;
28+
}
29+
30+
if (overLeft || overRight) {
31+
parent.scrollLeft = this.offsetLeft - parent.offsetLeft - parent.clientWidth / 2 - parentBorderLeftWidth + this.clientWidth / 2;
32+
}
33+
}
34+
else {
35+
if (overTop) {
36+
parent.scrollTop = this.offsetTop - parent.offsetTop - parentBorderTopWidth;
37+
}
38+
39+
if (overBottom) {
40+
parent.scrollTop = this.offsetTop - parent.offsetTop - parentBorderTopWidth - parent.clientHeight + this.clientHeight;
41+
}
42+
43+
if (overLeft) {
44+
parent.scrollLeft = this.offsetLeft - parent.offsetLeft - parentBorderLeftWidth;
45+
}
46+
47+
if (overRight) {
48+
parent.scrollLeft = this.offsetLeft - parent.offsetLeft - parentBorderLeftWidth - parent.clientWidth + this.clientWidth;
49+
}
50+
}
51+
};
52+
}
53+
54+
/*
55+
end of gist
56+
*/
457

558
var plainterm = (function() {
659

760
//Master object
861
var bash = {commands: {}, history: [], last: 0, process: false,
9-
version: "0.4.3"
62+
version: "0.4.4"
1063
};
1164

1265
//Master command constructor
@@ -38,7 +91,8 @@ var plainterm = (function() {
3891
function term_init(settings) {
3992
settings.id = settings.id || "terminal"; /* do not use "||" for default values and use custom function instead */
4093
settings.welcome = settings.welcome || "plainterm.js v. " + bash.version;
41-
bash.prompt = settings.prompt || "$ ";
94+
settings.prompt = '<span class="terminal-prompt">' + settings.prompt + '</span>' || '<span class="terminal-prompt">$ </span>';
95+
bash.prompt = settings.prompt;
4296
if (settings.help === undefined) {
4397
settings.help = true;
4498
}
@@ -90,14 +144,15 @@ var plainterm = (function() {
90144
if (c === true) {
91145
var cmd = document.createElement("span");
92146
cmd.innerHTML = content;
93-
cmd.className = "command";
147+
cmd.className = "termainal-command";
94148
line.innerHTML = bash.prompt;
95149
bash.container.display.appendChild(line);
96150
line.appendChild(cmd);
97151

98152
} else {
99153
line.innerHTML = content;
100154
bash.container.display.appendChild(line);
155+
bash.container.input.scrollIntoViewIfNeeded();
101156
}
102157
}
103158

@@ -113,7 +168,7 @@ var plainterm = (function() {
113168
bash.history.unshift(cmd);
114169
bash.container.dispatchEvent(valid_event);
115170
} else {
116-
println(comm + " - command not found");
171+
println('<span class="terminal-error">' + comm + ' - command not found</span>');
117172
bash.container.dispatchEvent(invalid_event);
118173
}
119174
}
@@ -127,7 +182,7 @@ var plainterm = (function() {
127182
}
128183
cmd.value = "";
129184
bash.last = 0;
130-
bash.container.input.scrollIntoView();
185+
bash.container.input.scrollIntoViewIfNeeded();
131186
}
132187
}
133188

@@ -160,11 +215,13 @@ var plainterm = (function() {
160215
line.innerHTML += text.charAt(i);
161216
i++;
162217
setTimeout(typing, speed);
218+
line.scrollIntoViewIfNeeded();
163219
} else if (bash.process === false) {
164220
bash.container.area.style.display = "flex";
165221
}
166222
}
167223
setTimeout(typing, speed);
224+
bash.container.input.scrollIntoViewIfNeeded();
168225
}
169226

170227
//History

plainterm.js.css

+12
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,16 @@ https://github.com/mkrl/plainterm.js */
3535

3636
.terminal-container > p {
3737
margin: 0;
38+
}
39+
40+
span.terminal-prompt {
41+
color: #929292;
42+
}
43+
44+
span.termainal-command {
45+
color: #ffff7d;
46+
}
47+
48+
span.terminal-error {
49+
color: #cc1010;
3850
}

0 commit comments

Comments
 (0)