aboutsummaryrefslogtreecommitdiff
path: root/php/readline.php
diff options
context:
space:
mode:
authorJoel Martin <github@martintribe.org>2014-03-24 16:32:24 -0500
committerJoel Martin <github@martintribe.org>2014-03-24 16:32:24 -0500
commit3169070063b2cb877200117ebb384269d73bcb93 (patch)
tree23de3db1ea5c37afd21a45b6ed7771f56a08c0c4 /php/readline.php
downloadmal-3169070063b2cb877200117ebb384269d73bcb93.tar.gz
mal-3169070063b2cb877200117ebb384269d73bcb93.zip
Current state of mal for Clojure West lighting talk.
Diffstat (limited to 'php/readline.php')
-rw-r--r--php/readline.php34
1 files changed, 34 insertions, 0 deletions
diff --git a/php/readline.php b/php/readline.php
new file mode 100644
index 0000000..28d720d
--- /dev/null
+++ b/php/readline.php
@@ -0,0 +1,34 @@
+<?php
+
+$HISTORY_FILE = "/home/joelm/.mal-history";
+
+function mal_readline($prompt) {
+ global $HISTORY_FILE;
+ static $history_loaded = false;
+
+ // Load the history file
+ if (! $history_loaded) {
+ $history_loaded = true;
+ if ($file = fopen($HISTORY_FILE, "r")) {
+ while (!feof($file)) {
+ $line = fgets($file);
+ if ($line) { readline_add_history($line); }
+ }
+ fclose($file);
+ }
+ }
+
+ $line = readline($prompt);
+ if ($line === false) { return NULL; }
+ readline_add_history($line);
+
+ // Append to the history file
+ if ($file = fopen($HISTORY_FILE, "a")) {
+ fputs($file, $line . "\n");
+ fclose($file);
+ }
+
+ return $line;
+}
+
+?>