aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoel Martin <github@martintribe.org>2014-04-27 16:55:31 -0500
committerJoel Martin <github@martintribe.org>2014-04-27 16:55:31 -0500
commitb58698b257fb6552e053cd245d63a140d3f7a478 (patch)
tree2310e9c259ee82534d5336d1c4beffd270030a51
parenta9993e0a683bc56ef091762e192fdb47676be82b (diff)
downloadmal-b58698b257fb6552e053cd245d63a140d3f7a478.tar.gz
mal-b58698b257fb6552e053cd245d63a140d3f7a478.zip
JS: add localStorage history load/save. Slurp using sync XHR.
-rw-r--r--js/core.js13
-rw-r--r--js/jq_readline.js18
-rw-r--r--mal.html3
3 files changed, 33 insertions, 1 deletions
diff --git a/js/core.js b/js/core.js
index b5a52e0..3ab2117 100644
--- a/js/core.js
+++ b/js/core.js
@@ -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);
diff --git a/mal.html b/mal.html
index 3bc5b21..89d9c41 100644
--- a/mal.html
+++ b/mal.html
@@ -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);
/*