From b58698b257fb6552e053cd245d63a140d3f7a478 Mon Sep 17 00:00:00 2001 From: Joel Martin Date: Sun, 27 Apr 2014 16:55:31 -0500 Subject: JS: add localStorage history load/save. Slurp using sync XHR. --- js/core.js | 13 ++++++++++++- js/jq_readline.js | 18 ++++++++++++++++++ mal.html | 3 +++ 3 files changed, 33 insertions(+), 1 deletion(-) 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); /* -- cgit v1.2.3