Cleaned up \FederationLib\Utilities

This commit is contained in:
Netkas 2023-06-23 04:05:49 -04:00
parent ba9cbf6a60
commit 3301e01065
No known key found for this signature in database
GPG key ID: 5DAF58535614062B

View file

@ -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);
}
}
}