Skip to content

Commit 892871c

Browse files
committed
tty check
1 parent c0c0bc6 commit 892871c

1 file changed

Lines changed: 27 additions & 7 deletions

File tree

‎src/WP_CLI/Shell/REPL.php‎

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -144,19 +144,24 @@ private function prompt() {
144144
// @phpstan-ignore booleanNot.alwaysTrue
145145
$prompt = ( ! $done && false !== $full_line ) ? '--> ' : $this->prompt;
146146

147-
$fp = popen( self::create_prompt_cmd( $prompt, $this->history_file ), 'r' );
148-
149-
$line = $fp ? fgets( $fp ) : '';
150-
151-
if ( $fp ) {
152-
pclose( $fp );
147+
if ( ! self::is_tty() ) {
148+
if ( getenv( 'WP_CLI_CUSTOM_SHELL' ) ) {
149+
self::create_prompt_cmd( $prompt, $this->history_file );
150+
}
151+
$line = fgets( STDIN );
152+
} else {
153+
$fp = popen( self::create_prompt_cmd( $prompt, $this->history_file ), 'r' );
154+
$line = $fp ? fgets( $fp ) : '';
155+
if ( $fp ) {
156+
pclose( $fp );
157+
}
153158
}
154159

155160
if ( ! $line ) {
156161
break;
157162
}
158163

159-
$line = rtrim( $line, "\n" );
164+
$line = rtrim( $line, "\r\n" );
160165

161166
if ( $line && '\\' === $line[ strlen( $line ) - 1 ] ) {
162167
$line = substr( $line, 0, -1 );
@@ -347,4 +352,19 @@ private function get_recursive_mtime( $path ) {
347352

348353
return $mtime;
349354
}
355+
356+
/**
357+
* Detect if STDIN is an interactive terminal.
358+
*
359+
* @return bool True if interactive, false otherwise.
360+
*/
361+
private static function is_tty() {
362+
if ( function_exists( 'stream_isatty' ) ) {
363+
return stream_isatty( STDIN );
364+
}
365+
if ( function_exists( 'posix_isatty' ) ) {
366+
return posix_isatty( STDIN );
367+
}
368+
return true;
369+
}
350370
}

0 commit comments

Comments
 (0)