Skip to content

Commit 4a1f4cd

Browse files
Merge pull request #35 from igorsantos07/master
Adding a hiding option to \cli\prompt
2 parents 64f0431 + af985c0 commit 4a1f4cd

3 files changed

Lines changed: 34 additions & 14 deletions

File tree

lib/cli/Shell.php

100644100755
Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,11 @@
1313
namespace cli;
1414

1515
/**
16-
* The `Shell` class is a utility class for shell related information such as
17-
* width.
16+
* The `Shell` class is a utility class for shell related tasks such as
17+
* information on width.
1818
*/
1919
class Shell {
20+
2021
/**
2122
* Returns the number of columns the current shell has for display.
2223
*
@@ -62,6 +63,14 @@ static public function isPiped() {
6263
return (function_exists('posix_isatty') && !posix_isatty(STDOUT));
6364
}
6465
}
66+
67+
/**
68+
* Uses `stty` to hide input/output completely.
69+
* @param boolean $hidden Will hide/show the next data. Defaults to true.
70+
*/
71+
static public function hide($hidden = true) {
72+
system( 'stty ' . ( $hidden? '-echo' : 'echo' ) );
73+
}
6574
}
6675

6776
?>

lib/cli/Streams.php

100644100755
Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -116,16 +116,25 @@ public static function err( $msg = '' ) {
116116
* @param string $format A valid input format. See `fscanf` for documentation.
117117
* If none is given, all input up to the first newline
118118
* is accepted.
119+
* @param boolean $hide If true will hide what the user types in.
119120
* @return string The input with whitespace trimmed.
120121
* @throws \Exception Thrown if ctrl-D (EOT) is sent as input.
121122
*/
122-
public static function input( $format = null ) {
123+
public static function input( $format = null, $hide = false ) {
124+
if ( $hide )
125+
Shell::hide();
126+
123127
if( $format ) {
124128
fscanf( static::$in, $format . "\n", $line );
125129
} else {
126130
$line = fgets( static::$in );
127131
}
128132

133+
if ( $hide ) {
134+
Shell::hide( false );
135+
echo "\n";
136+
}
137+
129138
if( $line === false ) {
130139
throw new \Exception( 'Caught ^D during input' );
131140
}
@@ -137,21 +146,22 @@ public static function input( $format = null ) {
137146
* Displays an input prompt. If no default value is provided the prompt will
138147
* continue displaying until input is received.
139148
*
140-
* @param string $question The question to ask the user.
141-
* @param string $default A default value if the user provides no input.
142-
* @param string $marker A string to append to the question and default value
143-
* on display.
149+
* @param string $question The question to ask the user.
150+
* @param bool|string $default A default value if the user provides no input.
151+
* @param string $marker A string to append to the question and default value
152+
* on display.
153+
* @param boolean $hide Optionally hides what the user types in.
144154
* @return string The users input.
145155
* @see cli\input()
146156
*/
147-
public static function prompt( $question, $default = null, $marker = ': ' ) {
157+
public static function prompt( $question, $default = null, $marker = ': ', $hide = false ) {
148158
if( $default && strpos( $question, '[' ) === false ) {
149159
$question .= ' [' . $default . ']';
150160
}
151161

152162
while( true ) {
153163
self::out( $question . $marker );
154-
$line = self::input();
164+
$line = self::input( null, $hide );
155165

156166
if( !empty( $line ) )
157167
return $line;

lib/cli/cli.php

100644100755
Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -93,14 +93,15 @@ function input( $format = null ) {
9393
* Displays an input prompt. If no default value is provided the prompt will
9494
* continue displaying until input is received.
9595
*
96-
* @param string $question The question to ask the user.
97-
* @param string $default A default value if the user provides no input.
98-
* @param string $marker A string to append to the question and default value on display.
96+
* @param string $question The question to ask the user.
97+
* @param string $default A default value if the user provides no input.
98+
* @param string $marker A string to append to the question and default value on display.
99+
* @param boolean $hide If the user input should be hidden
99100
* @return string The users input.
100101
* @see cli\input()
101102
*/
102-
function prompt( $question, $default = null, $marker = ': ' ) {
103-
return Streams::prompt( $question, $default, $marker );
103+
function prompt( $question, $default = false, $marker = ': ', $hide = false ) {
104+
return Streams::prompt( $question, $default, $marker, $hide );
104105
}
105106

106107
/**

0 commit comments

Comments
 (0)