1
1
mirror of https://github.com/kanaka/mal.git synced 2024-10-27 14:52:16 +03:00
mal/impls/coffee/node_readline.coffee
Joel Martin 8a19f60386 Move implementations into impls/ dir
- Reorder README to have implementation list after "learning tool"
  bullet.

- This also moves tests/ and libs/ into impls. It would be preferrable
  to have these directories at the top level.  However, this causes
  difficulties with the wasm implementations which need pre-open
  directories and have trouble with paths starting with "../../". So
  in lieu of that, symlink those directories to the top-level.

- Move the run_argv_test.sh script into the tests directory for
  general hygiene.
2020-02-10 23:50:16 -06:00

40 lines
960 B
CoffeeScript

# 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-napi')
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
try
fs.appendFileSync HISTORY_FILE, line + "\n"
catch exc
true
line
# vim: ts=2:sw=2