Refactoring of manpage() to hide 'Options' and/or 'Arguments' if there are none to show.

This commit is contained in:
Alannah Kearney 2019-05-24 14:01:38 +10:00
commit 3f07310dfe

View file

@ -62,54 +62,10 @@ if (!function_exists(__NAMESPACE__.'usage')) {
}
}
if (!function_exists(__NAMESPACE__.'manpage')) {
function manpage(string $name, string $version, string $description, Input\InputCollection $collection, $foregroundColour=Colour\Colour::FG_DEFAULT, $headingColour=Colour\Colour::FG_WHITE, array $additional=[]): string
{
$arguments = $options = [];
foreach ($collection->getArguments() as $a) {
$arguments[] = (string) $a;
}
foreach ($collection->getOptions() as $o) {
$options[] = (string) $o;
}
$arguments = implode($arguments, PHP_EOL);
$options = implode($options, PHP_EOL);
// Convienence function for wrapping a heading with colour
$heading = function(string $input) use ($headingColour) {
return Colour\Colour::colourise($input, $headingColour);
};
// Convienence function for wrapping input in a specified colour
$colourise = function(string $input) use ($foregroundColour) {
return Colour\Colour::colourise($input, $foregroundColour);
};
$sections = [
$colourise(sprintf(
"%s %s, %s\r\n%s\r\n",
$name,
$version,
$description,
usage($name, $collection)
)),
$heading('Arguments:'),
$colourise($arguments) . PHP_EOL,
$heading('Options:'),
$colourise($options) . PHP_EOL
];
foreach($additional as $name => $contents) {
$sections[] = $heading("{$name}:");
$sections[] = $colourise($contents) . PHP_EOL;
}
return implode($sections, PHP_EOL);
}
}
/**
* Uses tput to find out the size of the window (columns and lines)
* @return array an array containing exactly 2 items: 'cols' and 'lines'
*/
if (!function_exists(__NAMESPACE__.'get_window_size')) {
function get_window_size(): array
{
@ -119,3 +75,59 @@ if (!function_exists(__NAMESPACE__.'get_window_size')) {
];
}
}
if (!function_exists(__NAMESPACE__.'manpage')) {
function manpage(string $name, string $version, string $description, Input\InputCollection $collection, $foregroundColour=Colour\Colour::FG_DEFAULT, $headingColour=Colour\Colour::FG_WHITE, array $additionalSections=[]): string
{
// Convienence function for wrapping a heading with colour
$heading = function(string $input) use ($headingColour) {
return Colour\Colour::colourise($input, $headingColour);
};
// Convienence function for wrapping input in a specified colour
$colourise = function(string $input) use ($foregroundColour) {
return Colour\Colour::colourise($input, $foregroundColour);
};
$sections = [
$colourise(sprintf(
"%s %s, %s\r\n%s\r\n",
$name,
$version,
$description,
usage($name, $collection)
)),
];
$arguments = [];
$options = [];
foreach ($collection->getArguments() as $a) {
$arguments[] = (string) $a;
}
foreach ($collection->getOptions() as $o) {
$options[] = (string) $o;
}
// Add the arguments, if there are any.
if(false === empty($arguments)){
$sections[] = $heading("Arguments:");
$sections[] = $colourise(implode($arguments, PHP_EOL)) . PHP_EOL;
}
// Add the options, if there are any.
if(false === empty($options)){
$sections[] = $heading("Options:");
$sections[] = $colourise(implode($options, PHP_EOL)) . PHP_EOL;
}
// Iterate over all additional items and add them as new sections
foreach($additionalSections as $name => $contents) {
$sections[] = $heading("{$name}:");
$sections[] = $colourise($contents) . PHP_EOL;
}
return implode($sections, PHP_EOL);
}
}