forked from chjj/blessed
-
Notifications
You must be signed in to change notification settings - Fork 0
/
transform.js
105 lines (86 loc) · 2.52 KB
/
transform.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
/**
* transform.js - browserify workaround for blessed
* Copyright (c) 2013-2015, Christopher Jeffrey and contributors (MIT License).
* https://github.com/chjj/blessed
*/
var Transform = require('stream').Transform
, path = require('path')
, fs = require('fs');
/**
* Transformer
*/
function transformer(code) {
var stream = new Transform;
stream._transform = function(chunk, encoding, callback) {
return callback(null, chunk);
};
stream._flush = function(callback) {
if (code) {
stream.push(code);
}
return callback();
};
return stream;
}
/**
* Explicitly require all widgets in widget.js
*/
var widgets = fs.readdirSync(__dirname + '/../lib/widgets');
var requireWidgets = widgets.reduce(function(out, name) {
name = path.basename(name, '.js');
out += '\nrequire(\'./widgets/' + name + '\');';
return out;
}, '');
/**
* Do not make filesystem calls in tput.js for
* terminfo or termcap, just use xterm terminfo/cap.
*/
var infoPath = path.resolve(__dirname, '..', 'usr', 'xterm-256color')
, capPath = path.resolve(__dirname, '..', 'usr', 'xterm.termcap');
var infoPathFake = path.resolve(
path.sep, 'usr', 'share', 'terminfo',
path.basename(infoPath)[0],
path.basename(infoPath)
);
function readMethods() {
Tput._infoBuffer = new Buffer(TERMINFO, 'base64');
Tput.prototype.readTerminfo = function() {
this.terminal = TERMINFO_NAME;
return this.parseTerminfo(Tput._infoBuffer, TERMINFO_PATH);
};
Tput.cpaths = [];
Tput.termcap = TERMCAP;
Tput.prototype._readTermcap = Tput.prototype.readTermcap;
Tput.prototype.readTermcap = function() {
this.terminal = TERMCAP_NAME;
return this._readTermcap(this.terminal);
};
Tput.prototype.detectUnicode = function() {
return true;
};
}
readMethods = readMethods.toString().slice(24, -2)
.replace(/^ /gm, '')
.replace('TERMINFO', JSON.stringify(fs.readFileSync(infoPath, 'base64')))
.replace('TERMINFO_NAME', JSON.stringify(path.basename(infoPath)))
.replace('TERMINFO_PATH', JSON.stringify(infoPathFake))
.replace('TERMCAP', JSON.stringify(fs.readFileSync(capPath, 'utf8')))
.replace('TERMCAP_NAME', JSON.stringify(path.basename(capPath, '.termcap')));
/**
* Helpers
*/
function end(file, offset) {
return file.split(path.sep).slice(-offset).join('/');
}
/**
* Expose
*/
module.exports = function(file) {
if (end(file, 2) === 'lib/widget.js') {
return transformer(requireWidgets);
}
if (end(file, 2) === 'lib/tput.js') {
return transformer(readMethods);
}
return transformer();
};