From d65fb5a58a5a1cb3b72470c096cca14bb5f18f54 Mon Sep 17 00:00:00 2001 From: Pekka Laiho Date: Sun, 18 Oct 2020 09:18:07 +0700 Subject: [PATCH] add readline functions --- README.md | 4 ++++ src/Lib/IO.php | 18 ++++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/README.md b/README.md index 62ddda2..687f4cb 100644 --- a/README.md +++ b/README.md @@ -234,6 +234,10 @@ fwrite | `(fwrite f "abc")` | `3` | Write to a file resource. fflush | `(fflush f)` | `true` | Persist buffered writes to disk for a file resource. fread | `(fread f 16)` | `"abc"` | Read from a file resource. feof? | `(feof? f)` | `true` | Return true if end of file has been reached for a file resource. +readline | `(readline "What is your name? ")` | `What is your name? ` | Read line of user input using [readline](https://www.php.net/manual/en/function.readline.php). +readlineAdd | `(readlineAdd "What is your name? ")` | `true` | Add line of user input to readline history using [readline_add_history](https://www.php.net/manual/en/function.readline-add-history.php). +readlineLoad | `(readlineLoad "historyfile")` | `true` | Read readline history from file using [readline_read_history](https://www.php.net/manual/en/function.readline-read-history.php). +readlineSave | `(readlineSave "historyfile")` | `true` | Write readline history into file using [readline_write_history](https://www.php.net/manual/en/function.readline-write-history.php). ### Json functions diff --git a/src/Lib/IO.php b/src/Lib/IO.php index f38d6bb..5acaca3 100644 --- a/src/Lib/IO.php +++ b/src/Lib/IO.php @@ -65,5 +65,23 @@ class IO implements ILib $env->set('feof?', new CoreFunc('feof?', 'Return true if end of file has been reached for a file resource.', 1, 1, fn ($handle) => @feof($handle) )); + + // Readline support + + $env->set('readline', new CoreFunc('readline', 'Read line of user input.', 0, 1, + fn ($prompt = null) => readline($prompt) + )); + + $env->set('readlineAdd', new CoreFunc('readlineAdd', 'Add new line of input to history.', 1, 1, + fn (string $line) => readline_add_history($line) + )); + + $env->set('readlineLoad', new CoreFunc('readlineLoad', 'Load the history for readline from a file.', 1, 1, + fn (string $file) => readline_read_history($file) + )); + + $env->set('readlineSave', new CoreFunc('readlineSave', 'Save the readline history into a file.', 1, 1, + fn (string $file) => readline_write_history($file) + )); } }