summaryrefslogtreecommitdiff
path: root/vendor/thecodingmachine/safe/generated/readline.php
blob: a1c52c75505c0cee44a4ec77d3b6255a193014e1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
<?php

namespace Safe;

use Safe\Exceptions\ReadlineException;

/**
 * This function adds a line to the command line history.
 *
 * @param string $line The line to be added in the history.
 * @throws ReadlineException
 *
 */
function readline_add_history(string $line): void
{
    error_clear_last();
    $result = \readline_add_history($line);
    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.
 *
 *
 * Readline Callback Interface Example
 *
 * 10) {
 * $prompting = false;
 * readline_callback_handler_remove();
 * } else {
 * readline_callback_handler_install("[$c] Enter something: ", 'rl_callback');
 * }
 * }
 *
 * $c = 1;
 * $prompting = true;
 *
 * readline_callback_handler_install("[$c] Enter something: ", 'rl_callback');
 *
 * while ($prompting) {
 * $w = NULL;
 * $e = NULL;
 * $n = stream_select($r = array(STDIN), $w, $e, null);
 * if ($n && in_array(STDIN, $r)) {
 * // read a character, will call the callback when a newline is entered
 * readline_callback_read_char();
 * }
 * }
 *
 * echo "Prompting disabled. All done.\n";
 * ?>
 * ]]>
 *
 *
 *
 * @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.
 *
 * @param callable $function You must supply the name of an existing function which accepts a
 * partial command line and returns an array of possible matches.
 * @throws ReadlineException
 *
 */
function readline_completion_function(callable $function): void
{
    error_clear_last();
    $result = \readline_completion_function($function);
    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();
    }
}