-
Notifications
You must be signed in to change notification settings - Fork 1k
Check whether less pager exists before trying to pipe content through it
#6020
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
f7504c3
8d13310
b88c438
18a5bef
dd7a62f
e07156e
349dfaf
29be05a
d12602b
ece7dbd
8e0f5b6
0f9e386
2a669d1
d53c4ba
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,6 +3,7 @@ | |
| use cli\Shell; | ||
| use WP_CLI\Dispatcher; | ||
| use WP_CLI\Utils; | ||
| use WP_CLI\Process; | ||
|
|
||
| class Help_Command extends WP_CLI_Command { | ||
|
|
||
|
|
@@ -115,6 +116,68 @@ private static function indent( $whitespace, $text ) { | |
| return implode( "\n", $lines ); | ||
| } | ||
|
|
||
| /** | ||
| * Locate an executable binary or command by name using a platform-appropriate detector. | ||
| * | ||
| * On Windows, this uses `where`, and on POSIX systems it uses `command -v`. | ||
| * This may not work accurately in PowerShell. | ||
| * | ||
| * @param string $binary Name of the binary or command to be found. | ||
| * @return bool True if this command has determined that the binary or other command exists, false otherwise. | ||
| */ | ||
| public static function binary_exists( $binary ) { | ||
| if ( Utils\is_windows() ) { | ||
| // This may not work in PowerShell; see https://stackoverflow.com/a/304447 | ||
| // If this needs to be adjusted to use 'where.exe' for PowerShell, | ||
| // then we will need to add a way of detecting whether wp-cli is running in PowerShell. | ||
| $detector = 'where'; | ||
| } else { | ||
| // POSIX method to detect whether a command exists | ||
| // This sometimes detects aliases. | ||
| $detector = 'command -v'; | ||
| } | ||
|
|
||
| $result = Process::create( $detector . ' ' . escapeshellarg( $binary ), null, null )->run(); | ||
|
|
||
| if ( 0 !== $result->return_code ) { | ||
| // We could not reliably determine that the binary exists | ||
| return false; | ||
| } else { | ||
| // POSIX binaries: command -v will return the path and exit 0 | ||
| // aliases: command -v may return the alias command and exit 0 | ||
| return true; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Determine whether to use `less` or `more` as a pager | ||
| * | ||
| * This caches the determined pager. | ||
| * | ||
| * @return string The command to use for the pager. Defaults to `more`. | ||
| */ | ||
| public static function locate_pager() { | ||
| static $pager = null; | ||
|
|
||
| if ( empty( $pager ) ) { | ||
| if ( self::binary_exists( 'less' ) ) { | ||
| // less is not available in all systems | ||
| $pager = 'less -R'; | ||
| } else { | ||
| // more is part of the POSIX definition, and is also available on Windows. | ||
| $pager = 'more'; | ||
| } | ||
|
Comment on lines
+159
to
+169
|
||
| } | ||
|
|
||
| return $pager; | ||
| } | ||
|
|
||
| /** | ||
| * Pass a given set of output through the system's terminal pager. | ||
| * | ||
| * @param string $out The output to be run through the pager. | ||
| * @return mixed Termination status of the pager as reported by https://www.php.net/manual/en/function.proc-close.php | ||
| */ | ||
| private static function pass_through_pager( $out ) { | ||
|
|
||
| if ( ! Utils\check_proc_available( null /*context*/, true /*return*/ ) ) { | ||
|
|
@@ -124,8 +187,13 @@ private static function pass_through_pager( $out ) { | |
| } | ||
|
|
||
| $pager = getenv( 'PAGER' ); | ||
| // if '' we should assume that the user has explicitly disabled the pager by setting `PAGER=` | ||
swissspidy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if ( '' === $pager ) { | ||
| WP_CLI::line( $out ); | ||
| return 0; | ||
| } | ||
| if ( false === $pager ) { | ||
| $pager = Utils\is_windows() ? 'more' : 'less -R'; | ||
| $pager = self::locate_pager(); | ||
| } | ||
|
|
||
| // For Windows 7 need to set code page to something other than Unicode (65001) to get around "Not enough memory." error with `more.com` on PHP 7.1+. | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This
if/elseblock can be simplified to a singlereturnstatement. This will make the code more concise and easier to read.