@@ -75,13 +75,28 @@ public function setPrompt($prompt)
75
75
}
76
76
77
77
/**
78
- * whether or not to echo input
78
+ * sets whether/how to echo text input
79
79
*
80
- * disabling echo output is a good idea for password prompts. Will redraw
81
- * the current prompt and only echo the current input buffer according to
82
- * the new setting .
80
+ * The default setting is `true`, which means that every character will be
81
+ * echo'ed as-is, i.e. you can see what you're typing.
82
+ * For example: Typing "test" shows "test" .
83
83
*
84
- * @param boolean $echo
84
+ * You can turn this off by supplying `false`, which means that *nothing*
85
+ * will be echo'ed while you're typing. This could be a good idea for
86
+ * password prompts. Note that this could be confusing for users, so using
87
+ * a character replacement as following is often preferred.
88
+ * For example: Typing "test" shows "" (nothing).
89
+ *
90
+ * Alternative, you can supply a single character replacement character
91
+ * that will be echo'ed for each character in the text input. This could
92
+ * be a good idea for password prompts, where an asterisk character ("*")
93
+ * is often used to indicate typing activity and password length.
94
+ * For example: Typing "test" shows "****" (with asterisk replacement)
95
+ *
96
+ * Changing this setting will redraw the current prompt and echo the current
97
+ * input buffer according to the new setting.
98
+ *
99
+ * @param boolean|string $echo echo can be turned on (boolean true) or off (boolean true), or you can supply a single character replacement string
85
100
* @return self
86
101
* @uses self::redraw()
87
102
*/
@@ -91,7 +106,7 @@ public function setEcho($echo)
91
106
return $ this ;
92
107
}
93
108
94
- $ this ->echo = !! $ echo ;
109
+ $ this ->echo = $ echo ;
95
110
96
111
// only redraw if there is any input
97
112
if ($ this ->linebuffer !== '' ) {
@@ -153,7 +168,7 @@ public function setInput($input)
153
168
$ this ->linepos = $ this ->strlen ($ this ->linebuffer );
154
169
155
170
// only redraw if input should be echo'ed (i.e. is not hidden anyway)
156
- if ($ this ->echo ) {
171
+ if ($ this ->echo !== false ) {
157
172
$ this ->redraw ();
158
173
}
159
174
@@ -219,8 +234,12 @@ public function redraw()
219
234
{
220
235
// Erase characters from cursor to end of line
221
236
$ output = "\r\033[K " . $ this ->prompt ;
222
- if ($ this ->echo ) {
223
- $ output .= $ this ->linebuffer ;
237
+ if ($ this ->echo !== false ) {
238
+ if ($ this ->echo === true ) {
239
+ $ output .= $ this ->linebuffer ;
240
+ } else {
241
+ $ output .= str_repeat ($ this ->echo , $ this ->strlen ($ this ->linebuffer ));
242
+ }
224
243
225
244
$ len = $ this ->strlen ($ this ->linebuffer );
226
245
if ($ this ->linepos !== $ len ) {
@@ -237,7 +256,7 @@ public function redraw()
237
256
238
257
public function clear ()
239
258
{
240
- if ($ this ->prompt !== '' || ($ this ->echo && $ this ->linebuffer !== '' )) {
259
+ if ($ this ->prompt !== '' || ($ this ->echo !== false && $ this ->linebuffer !== '' )) {
241
260
$ this ->write ("\r\033[K " );
242
261
}
243
262
// $output = str_repeat("\x09 \x09", strlen($this->prompt . $this->linebuffer));
@@ -275,7 +294,7 @@ public function onKeyTab()
275
294
276
295
public function onKeyEnter ()
277
296
{
278
- if ($ this ->echo ) {
297
+ if ($ this ->echo !== false ) {
279
298
$ this ->write ("\n" );
280
299
}
281
300
$ this ->processLine ();
0 commit comments