diff options
| author | Joel Martin <github@martintribe.org> | 2014-11-15 23:53:58 -0600 |
|---|---|---|
| committer | Joel Martin <github@martintribe.org> | 2015-01-09 16:16:46 -0600 |
| commit | 95ec2d81d8661e56b7b98a1c19ad503c5083c1e4 (patch) | |
| tree | 86dd55f155891bd0221bfc80a2380da07f5e5729 | |
| parent | 891c3f3b478292ad0bfca44b0dc098a2aecc9a5d (diff) | |
| download | mal-95ec2d81d8661e56b7b98a1c19ad503c5083c1e4.tar.gz mal-95ec2d81d8661e56b7b98a1c19ad503c5083c1e4.zip | |
CoffeeScript: add missed node_readline.coffee
| -rw-r--r-- | coffee/node_readline.coffee | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/coffee/node_readline.coffee b/coffee/node_readline.coffee new file mode 100644 index 0000000..31e5ca0 --- /dev/null +++ b/coffee/node_readline.coffee @@ -0,0 +1,36 @@ +# IMPORTANT: choose one +RL_LIB = "libreadline" # NOTE: libreadline is GPL +#RL_LIB = "libedit" + +HISTORY_FILE = require('path').join(process.env.HOME, '.mal-history') + +rlwrap = {} # namespace for this module in web context + +ffi = require('ffi') +fs = require('fs') + +rllib = ffi.Library(RL_LIB, { + 'readline': ['string', ['string']], + 'add_history': ['int', ['string']]}) + +rl_history_loaded = false + +exports.readline = rlwrap.readline = (prompt = 'user> ') -> + if !rl_history_loaded + rl_history_loaded = true + lines = [] + if fs.existsSync(HISTORY_FILE) + lines = fs.readFileSync(HISTORY_FILE).toString().split("\n"); + + # Max of 2000 lines + lines = lines[Math.max(lines.length - 2000, 0)..] + rllib.add_history(line) for line in lines when line != "" + + line = rllib.readline prompt + if line + rllib.add_history line + fs.appendFileSync HISTORY_FILE, line + "\n" + + line + +# vim: ts=2:sw=2 |
