1 // IMPORTANT: choose one
2 var RL_LIB
= "libreadline"; // NOTE: libreadline is GPL
3 //var RL_LIB = "libedit";
5 var HISTORY_FILE
= require('path').join(process
.env
.HOME
, '.mal-history');
7 var rlwrap
= {}; // namespace for this module in web context
9 var ffi
= require('ffi-napi'),
12 var rllib
= ffi
.Library(RL_LIB
, {
13 'readline': [ 'string', [ 'string' ] ],
14 'add_history': [ 'int', [ 'string' ] ]});
16 var rl_history_loaded
= false;
18 exports
.readline
= rlwrap
.readline = function(prompt
) {
19 prompt
= typeof prompt
!== 'undefined' ? prompt
: "user> ";
21 if (!rl_history_loaded
) {
22 rl_history_loaded
= true;
24 if (fs
.existsSync(HISTORY_FILE
)) {
25 lines
= fs
.readFileSync(HISTORY_FILE
).toString().split("\n");
28 lines
= lines
.slice(Math
.max(lines
.length
- 2000, 0));
29 for (var i
=0; i
<lines
.length
; i
++) {
30 if (lines
[i
]) { rllib
.add_history(lines
[i
]); }
34 var line
= rllib
.readline(prompt
);
36 rllib
.add_history(line
);
38 fs
.appendFileSync(HISTORY_FILE
, line
+ "\n");
46 var readline
= exports
;