-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathindex.js
57 lines (46 loc) · 1.68 KB
/
index.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
'use strict';
var Terminal = require('./term')
, through = require('through')
;
function style(parentElem) {
var currentStyle = parentElem.getAttribute('style') || '';
// TODO: make white-space work
// white-space: pre has the following problem:
// If applied before the terminal is visible, things break horribly
// to the point that the output is either shifted to the left or not visible at all.
// (at least for hyperwatch, to repro: -- npm install hyperwatch; npm explore hyperwatch; npm run demo; )
// - most likely due to the fact that hyperwatch is positioned absolute
//
// However when this style is set after the parent element became visible, it works fine.
parentElem.setAttribute('style', currentStyle + 'overflow-y: auto; /* white-space: pre; */');
}
function scroll(elem) {
if (!elem) return;
elem.scrollTop = elem.scrollHeight;
}
module.exports = function (opts) {
var term = new Terminal(opts);
term.open();
var hypernal = through(term.write.bind(term));
hypernal.appendTo = function (parent) {
if (typeof parent === 'string') parent = document.querySelector(parent);
parent.appendChild(term.element);
style(parent);
hypernal.container = parent;
term.element.style.position = 'relative';
};
hypernal.writeln = function (line) {
term.writeln(line);
if (hypernal.tail) scroll(hypernal.container);
};
hypernal.write = function (data) {
term.write(data);
if (hypernal.tail) scroll(hypernal.container);
};
// convenience shortcuts
hypernal.reset = term.reset.bind(term);
hypernal.element = term.element;
// the underlying term for all other needs
hypernal.term = term;
return hypernal;
};