diff options
| author | Joel Martin <github@martintribe.org> | 2014-04-27 16:55:31 -0500 |
|---|---|---|
| committer | Joel Martin <github@martintribe.org> | 2014-04-27 16:55:31 -0500 |
| commit | b58698b257fb6552e053cd245d63a140d3f7a478 (patch) | |
| tree | 2310e9c259ee82534d5336d1c4beffd270030a51 | |
| parent | a9993e0a683bc56ef091762e192fdb47676be82b (diff) | |
| download | mal-b58698b257fb6552e053cd245d63a140d3f7a478.tar.gz mal-b58698b257fb6552e053cd245d63a140d3f7a478.zip | |
JS: add localStorage history load/save. Slurp using sync XHR.
| -rw-r--r-- | js/core.js | 13 | ||||
| -rw-r--r-- | js/jq_readline.js | 18 | ||||
| -rw-r--r-- | mal.html | 3 |
3 files changed, 33 insertions, 1 deletions
@@ -39,7 +39,18 @@ function println() { } function slurp(f) { - return require('fs').readFileSync(f, 'utf-8'); + if (typeof require !== 'undefined') { + return require('fs').readFileSync(f, 'utf-8'); + } else { + var req = new XMLHttpRequest(); + req.open("GET", f, false); + req.send(); + if (req.status == 200) { + return req.responseText; + } else { + throw new Error("Failed to slurp file: " + f); + } + } } diff --git a/js/jq_readline.js b/js/jq_readline.js index e9f624f..51bb571 100644 --- a/js/jq_readline.js +++ b/js/jq_readline.js @@ -1,3 +1,21 @@ +var max_history_length = 1000; + +function jq_load_history(jq) { + if (localStorage['mal_history']) { + var lines = JSON.parse(localStorage['mal_history']); + if (lines.length > max_history_length) { + lines = lines.slice(lines.length-max_history_length); + } + jq.SetHistory(lines); + } +} + +function jq_save_history(jq) { + var lines = jq.GetHistory(); + localStorage['mal_history'] = JSON.stringify(lines); +} + + var readline = { 'readline': function(prompt_str) { return prompt(prompt_str); @@ -208,6 +208,8 @@ this software. rep("(println (str \"Mal [\" *host-language* \"]\"))"); + jq_load_history(jqconsole); + // Abort prompt on Ctrl+C. jqconsole.RegisterShortcut('C', function() { jqconsole.AbortPrompt(); @@ -240,6 +242,7 @@ this software. jqconsole.Write(exc + '\n', 'jqconsole-error'); } } + jq_save_history(jqconsole); } jqconsole.Prompt(true, handler); /* |
