mirror of
https://github.com/n3w/helpers-cli-input.git
synced 2025-12-19 12:43:23 +00:00
Refactoring and improvemnts to Argument::__toString() and Option::__toString()
This commit is contained in:
parent
6e987bb4eb
commit
95e93ec1bc
2 changed files with 80 additions and 9 deletions
|
|
@ -6,18 +6,51 @@ namespace pointybeard\Helpers\Cli\Input\Types;
|
|||
|
||||
use pointybeard\Helpers\Cli\Input;
|
||||
use pointybeard\Helpers\Functions\Strings;
|
||||
use pointybeard\Helpers\Functions\Cli;
|
||||
|
||||
class Argument extends Input\AbstractInputType
|
||||
{
|
||||
public function __toString()
|
||||
{
|
||||
$name = strtoupper($this->name());
|
||||
// MAGIC VALUES!!! OH MY.....
|
||||
$padCharacter = ' ';
|
||||
$paddingBufferSize = 0.15; // 15%
|
||||
$argumentNamePaddedWidth = 20;
|
||||
$argumentNameMinimumPaddingWidth = 4;
|
||||
$minimumWindowWidth = 80;
|
||||
|
||||
$first = str_pad(sprintf('%s ', $name), 20, ' ');
|
||||
// Get the window dimensions but restrict width to minimum
|
||||
// of $minimumWindowWidth
|
||||
$window = Cli\get_window_size();
|
||||
$window['cols'] = max($minimumWindowWidth, $window['cols']);
|
||||
|
||||
$second = Strings\utf8_wordwrap_array($this->description(), 40);
|
||||
// This shrinks the total line length (derived by the window width) by
|
||||
// $paddingBufferSize
|
||||
$paddingBuffer = (int) ceil($window['cols'] * $paddingBufferSize);
|
||||
|
||||
// Create a string of $padCharacter which is prepended to each secondary
|
||||
// line
|
||||
$secondaryLineLeadPadding = str_pad(
|
||||
'',
|
||||
$argumentNamePaddedWidth,
|
||||
$padCharacter,
|
||||
STR_PAD_LEFT
|
||||
);
|
||||
|
||||
$first = Strings\mb_str_pad(
|
||||
strtoupper($this->name()).str_repeat($padCharacter, $argumentNameMinimumPaddingWidth),
|
||||
$argumentNamePaddedWidth,
|
||||
$padCharacter
|
||||
);
|
||||
|
||||
$second = Strings\utf8_wordwrap_array(
|
||||
$this->description(),
|
||||
$window['cols'] - $argumentNamePaddedWidth - $paddingBuffer
|
||||
);
|
||||
|
||||
// Skip the first item (notice $ii starts at value of '1')
|
||||
for ($ii = 1; $ii < count($second); ++$ii) {
|
||||
$second[$ii] = str_pad('', 22, ' ', \STR_PAD_LEFT).$second[$ii];
|
||||
$second[$ii] = $secondaryLineLeadPadding.$second[$ii];
|
||||
}
|
||||
|
||||
return $first.implode($second, PHP_EOL);
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@ namespace pointybeard\Helpers\Cli\Input\Types;
|
|||
|
||||
use pointybeard\Helpers\Functions\Flags;
|
||||
use pointybeard\Helpers\Functions\Strings;
|
||||
use pointybeard\Helpers\Functions\Cli;
|
||||
use pointybeard\Helpers\Cli\Input;
|
||||
|
||||
class Option extends Input\AbstractInputType
|
||||
|
|
@ -22,19 +23,56 @@ class Option extends Input\AbstractInputType
|
|||
|
||||
public function __toString()
|
||||
{
|
||||
$long = null !== $this->long() ? ', --'.$this->long() : null;
|
||||
if (null != $long) {
|
||||
// MAGIC VALUES!!! OH MY.....
|
||||
$padCharacter = ' ';
|
||||
$paddingBufferSize = 0.15; // 15%
|
||||
$optionNamePaddedWidth = 30;
|
||||
$minimumWindowWidth = 80;
|
||||
$secondaryLineIndentlength = 2;
|
||||
|
||||
// Get the window dimensions but restrict width to minimum
|
||||
// of $minimumWindowWidth
|
||||
$window = Cli\get_window_size();
|
||||
$window['cols'] = max($minimumWindowWidth, $window['cols']);
|
||||
|
||||
// This shrinks the total line length (derived by the window width) by
|
||||
// $paddingBufferSize
|
||||
$paddingBuffer = (int) ceil($window['cols'] * $paddingBufferSize);
|
||||
|
||||
// Create a string of $padCharacter which is prepended to each secondary
|
||||
// line
|
||||
$secondaryLineLeadPadding = str_pad(
|
||||
'',
|
||||
$optionNamePaddedWidth,
|
||||
$padCharacter,
|
||||
STR_PAD_LEFT
|
||||
);
|
||||
|
||||
$short = '-'.$this->name();
|
||||
$long = null;
|
||||
|
||||
if (null !== $this->long()) {
|
||||
$long = '--'.$this->long();
|
||||
if (Flags\is_flag_set($this->flags(), self::FLAG_VALUE_REQUIRED)) {
|
||||
$long .= '=VALUE';
|
||||
} elseif (Flags\is_flag_set($this->flags(), self::FLAG_VALUE_OPTIONAL)) {
|
||||
$long .= '[=VALUE]';
|
||||
}
|
||||
}
|
||||
$first = str_pad(sprintf('-%s%s ', $this->name(), $long), 36, ' ');
|
||||
|
||||
$second = Strings\utf8_wordwrap_array($this->description(), 40);
|
||||
$first = Strings\mb_str_pad(
|
||||
$short.(null !== $long ? ", {$long}" : ''), // -O, --LONG,
|
||||
$optionNamePaddedWidth,
|
||||
$padCharacter
|
||||
);
|
||||
|
||||
$second = Strings\utf8_wordwrap_array(
|
||||
$this->description(),
|
||||
$window['cols'] - $optionNamePaddedWidth - $paddingBuffer
|
||||
);
|
||||
|
||||
for ($ii = 1; $ii < count($second); ++$ii) {
|
||||
$second[$ii] = str_pad('', 38, ' ', \STR_PAD_LEFT).$second[$ii];
|
||||
$second[$ii] = $secondaryLineLeadPadding.$second[$ii];
|
||||
}
|
||||
|
||||
return $first.implode($second, PHP_EOL);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue