Skip to content

Commit

Permalink
fix(utils.php) handling of prompt default answers
Browse files Browse the repository at this point in the history
  • Loading branch information
lucatume committed Aug 17, 2020
1 parent b62ce93 commit 137b1ef
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Changed

- Update the WordPress version used in the stack to `5.5`.
- Fix handling of default answers in prompts.

## [0.5.2] - 2020-08-10
### Changed
Expand Down
10 changes: 10 additions & 0 deletions src/utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,9 @@ function ask( $question, $default = null ) {
$prompt .= " ({$default})";
}

// Add an empty space after the prompt to separate visual confusion.
$prompt .= ' ';

$is_boolean = false;
if ( is_bool( $default ) || preg_match( '/(^yes|no)$/i', $default ) ) {
// It's a yes or no question, cast to boolean at the end.
Expand All @@ -511,6 +514,13 @@ function ask( $question, $default = null ) {
$value = readline();
}

/*
* If the answer is an empty line, then the user just pressed Enter: use the default value.
*/
if ( $default !== '' ) {
$value = '' === trim( $value ) ? $default : $value;
}

if ( $is_boolean ) {
return preg_match( '/^y/i', $value );
}
Expand Down

0 comments on commit 137b1ef

Please sign in to comment.