From 3301e0106556ece5f5410c23feffe3a8456ef204 Mon Sep 17 00:00:00 2001 From: Netkas Date: Fri, 23 Jun 2023 04:05:49 -0400 Subject: [PATCH] Cleaned up \FederationLib\Utilities --- src/FederationCLI/Utilities.php | 36 +++++++++++++++++++++++++-------- 1 file changed, 28 insertions(+), 8 deletions(-) diff --git a/src/FederationCLI/Utilities.php b/src/FederationCLI/Utilities.php index ae9c848..323fc7a 100644 --- a/src/FederationCLI/Utilities.php +++ b/src/FederationCLI/Utilities.php @@ -31,16 +31,24 @@ public static function parseBoolean($input): bool { if(is_null($input)) + { return false; + } if(is_bool($input)) + { return $input; + } if(is_numeric($input)) + { return (bool) $input; + } if(is_string($input)) + { $input = trim($input); + } switch(strtolower($input)) { @@ -69,12 +77,16 @@ public static function promptInput(?string $prompt=null, ?string $default_value=null): ?string { if($prompt) + { print($prompt . ' '); + } $input = trim(fgets(STDIN)); if(!$input && $default_value) + { $input = $default_value; + } return $input; } @@ -89,12 +101,16 @@ public static function promptYesNo(?string $prompt=null, bool $default_value=false): bool { if($prompt) + { print($prompt . ' '); + } $input = trim(fgets(STDIN)); if(!$input && $default_value) + { $input = $default_value; + } return self::parseBoolean($input); } @@ -107,7 +123,7 @@ * @param int $body_width * @return void */ - public static function printData($banner_text, $data, int $body_width = 70) + public static function printData($banner_text, $data, int $body_width = 70): void { // Padding and wrap for the longest key $max_key_len = max(array_map('strlen', array_keys($data))); @@ -122,18 +138,22 @@ echo str_repeat("*", $banner_width) . PHP_EOL; // Print data - foreach ($data as $key => $value) { + foreach ($data as $key => $value) + { // Split value into lines if it's too long $lines = str_split($value, $padding); - // Print lines - foreach ($lines as $i => $line) { - if ($i == 0) { + foreach ($lines as $i => $line) + { + if ($i === 0) + { // First line - print key and value - echo ' ' . str_pad(strtoupper($key), $max_key_len, ' ', STR_PAD_RIGHT) . ' ' . $line . PHP_EOL; - } else { + print(' ' . str_pad(strtoupper($key), $max_key_len) . ' ' . $line . PHP_EOL); + } + else + { // Additional lines - only value - echo str_repeat(' ', $max_key_len + 4) . $line . PHP_EOL; + print(str_repeat(' ', $max_key_len + 4) . $line . PHP_EOL); } } }