2021-02-26 16:16:17 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Safe;
|
|
|
|
|
|
|
|
use Safe\Exceptions\ReadlineException;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This function adds a line to the command line history.
|
|
|
|
*
|
2022-07-12 19:26:21 +00:00
|
|
|
* @param string $prompt The line to be added in the history.
|
2021-02-26 16:16:17 +00:00
|
|
|
* @throws ReadlineException
|
|
|
|
*
|
|
|
|
*/
|
2022-07-12 19:26:21 +00:00
|
|
|
function readline_add_history(string $prompt): void
|
2021-02-26 16:16:17 +00:00
|
|
|
{
|
|
|
|
error_clear_last();
|
2022-07-12 19:26:21 +00:00
|
|
|
$result = \readline_add_history($prompt);
|
2021-02-26 16:16:17 +00:00
|
|
|
if ($result === false) {
|
|
|
|
throw ReadlineException::createFromPhpError();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets up a readline callback interface then prints
|
|
|
|
* prompt and immediately returns.
|
|
|
|
* Calling this function twice without removing the previous
|
|
|
|
* callback interface will automatically and conveniently overwrite the old
|
|
|
|
* interface.
|
|
|
|
*
|
|
|
|
* The callback feature is useful when combined with
|
|
|
|
* stream_select as it allows interleaving of IO and
|
|
|
|
* user input, unlike readline.
|
|
|
|
*
|
|
|
|
* @param string $prompt The prompt message.
|
|
|
|
* @param callable $callback The callback function takes one parameter; the
|
|
|
|
* user input returned.
|
|
|
|
* @throws ReadlineException
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
function readline_callback_handler_install(string $prompt, callable $callback): void
|
|
|
|
{
|
|
|
|
error_clear_last();
|
|
|
|
$result = \readline_callback_handler_install($prompt, $callback);
|
|
|
|
if ($result === false) {
|
|
|
|
throw ReadlineException::createFromPhpError();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This function clears the entire command line history.
|
|
|
|
*
|
|
|
|
* @throws ReadlineException
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
function readline_clear_history(): void
|
|
|
|
{
|
|
|
|
error_clear_last();
|
|
|
|
$result = \readline_clear_history();
|
|
|
|
if ($result === false) {
|
|
|
|
throw ReadlineException::createFromPhpError();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This function registers a completion function. This is the same kind of
|
|
|
|
* functionality you'd get if you hit your tab key while using Bash.
|
|
|
|
*
|
2022-07-12 19:26:21 +00:00
|
|
|
* @param callable $callback You must supply the name of an existing function which accepts a
|
2021-02-26 16:16:17 +00:00
|
|
|
* partial command line and returns an array of possible matches.
|
|
|
|
* @throws ReadlineException
|
|
|
|
*
|
|
|
|
*/
|
2022-07-12 19:26:21 +00:00
|
|
|
function readline_completion_function(callable $callback): void
|
2021-02-26 16:16:17 +00:00
|
|
|
{
|
|
|
|
error_clear_last();
|
2022-07-12 19:26:21 +00:00
|
|
|
$result = \readline_completion_function($callback);
|
2021-02-26 16:16:17 +00:00
|
|
|
if ($result === false) {
|
|
|
|
throw ReadlineException::createFromPhpError();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This function reads a command history from a file.
|
|
|
|
*
|
|
|
|
* @param string $filename Path to the filename containing the command history.
|
|
|
|
* @throws ReadlineException
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
function readline_read_history(string $filename = null): void
|
|
|
|
{
|
|
|
|
error_clear_last();
|
|
|
|
if ($filename !== null) {
|
|
|
|
$result = \readline_read_history($filename);
|
|
|
|
} else {
|
|
|
|
$result = \readline_read_history();
|
|
|
|
}
|
|
|
|
if ($result === false) {
|
|
|
|
throw ReadlineException::createFromPhpError();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This function writes the command history to a file.
|
|
|
|
*
|
|
|
|
* @param string $filename Path to the saved file.
|
|
|
|
* @throws ReadlineException
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
function readline_write_history(string $filename = null): void
|
|
|
|
{
|
|
|
|
error_clear_last();
|
|
|
|
if ($filename !== null) {
|
|
|
|
$result = \readline_write_history($filename);
|
|
|
|
} else {
|
|
|
|
$result = \readline_write_history();
|
|
|
|
}
|
|
|
|
if ($result === false) {
|
|
|
|
throw ReadlineException::createFromPhpError();
|
|
|
|
}
|
|
|
|
}
|