Skip to content

Commit

Permalink
docs: improve docblocks for TextRenderer
Browse files Browse the repository at this point in the history
  • Loading branch information
RobinTheHood committed Nov 16, 2023
1 parent c3a9654 commit c15b064
Showing 1 changed file with 126 additions and 21 deletions.
147 changes: 126 additions & 21 deletions src/Classes/Cli/TextRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,72 @@

namespace RobinTheHood\ModifiedModuleLoaderClient\Cli;

/**
* Class TextRenderer
*
* A utility class for rendering text with various formatting options.
*
* @package RobinTheHood\ModifiedModuleLoaderClient\Cli
*/
class TextRenderer
{
/**
* ANSI color code for red.
*/
public const COLOR_RED = 31;

/**
* ANSI color code for green.
*/
public const COLOR_GREEN = 32;

/**
* ANSI color code for yellow.
*/
public const COLOR_YELLOW = 33;

/**
* Generate a module link with a given archive name and title.
*
* @param string $archiveName The name of the module archive.
* @param string $title The title of the module link.
* @return string The generated module link.
*/
public static function moduleLink(string $archiveName, string $titel): string
{
return self::link('https://module-loader.de/modules/' . $archiveName, $titel);
}

/**
* Generate a link with a given URL and title.
*
* @param string $url The URL for the link.
* @param string $title The title of the link.
* @return string The generated link.
*/
public static function link(string $url, string $titel): string
{
return "\e]8;;$url\e\\$titel\e]8;;\e\\";
}

/**
* Apply color to a given text using ANSI color codes.
*
* @param string $text The text to be colored.
* @param int $color The ANSI color code.
* @return string The colored text.
*/
public static function color(string $text, int $color): string
{
return "\e[" . $color . "m" . $text . "\e[0m";
}

/**
* Remove ANSI escape sequences related to colors from a given text.
*
* @param string $text The text with escape sequences.
* @return string The text with escape sequences removed.
*/
public static function stripEscapeSequencesColor(string $text): string
{
// Muster für Escape-Sequenzen finden und ersetzen
Expand All @@ -46,6 +91,12 @@ public static function stripEscapeSequencesColor(string $text): string
return $filteredString;
}

/**
* Remove ANSI escape sequences related to links from a given text.
*
* @param string $text The text with escape sequences.
* @return string The text with escape sequences removed.
*/
public static function stripEscapeSequenceLink(string $text): string
{
// Muster für Escape-Sequenzen finden und ersetzen
Expand All @@ -58,6 +109,12 @@ public static function stripEscapeSequenceLink(string $text): string
return $filteredString;
}

/**
* Remove both color and link-related ANSI escape sequences from a given text.
*
* @param string $text The text with escape sequences.
* @return string The text with escape sequences removed.
*/
public static function stripEscapeSequences(string $text): string
{
$strippedText = $text;
Expand All @@ -66,27 +123,54 @@ public static function stripEscapeSequences(string $text): string
return $strippedText;
}

/**
* Get the length of the text without ANSI escape sequences.
*
* @param string $text The text with or without escape sequences.
* @return int The length of the text without escape sequences.
*/
public static function getTextLength(string $text): int
{
$strippedText = self::stripEscapeSequences($text);
$textLength = strlen($strippedText);
return $textLength;
}

/**
* Right pad a text with spaces to meet the specified total length.
*
* @param string $text The text to be padded.
* @param int $totalLength The desired total length.
* @return string The padded text.
*/
public static function rightPad(string $text, int $totalLength): string
{
$paddingLength = self::getPadLength($text, $totalLength);
$padding = str_repeat(' ', $paddingLength);
return $text . $padding;
}

/**
* Left pad a text with spaces to meet the specified total length.
*
* @param string $text The text to be padded.
* @param int $totalLength The desired total length.
* @return string The padded text.
*/
public static function leftPad(string $text, int $totalLength): string
{
$paddingLength = self::getPadLength($text, $totalLength);
$padding = str_repeat(' ', $paddingLength);
return $padding . $text;
}

/**
* Get the padding length required to meet the specified total length.
*
* @param string $text The text to be padded.
* @param int $totalLength The desired total length.
* @return int The calculated padding length.
*/
private static function getPadLength(string $text, int $totalLength): int
{
$textLength = self::getTextLength($text);
Expand All @@ -99,6 +183,12 @@ private static function getPadLength(string $text, int $totalLength): int
return $paddingLength;
}

/**
* Get the maximum length among an array of strings.
*
* @param string[] $items An array of strings.
* @return int The maximum length among the strings.
*/
public static function getMaxLength(array $items): int
{
$maxLength = 0;
Expand All @@ -112,25 +202,27 @@ public static function getMaxLength(array $items): int
}

/**
* @deprecated 1.21.0 Use a `HelpRenderer` instead.
* Render a heading for help information (deprecated).
*
* @param string $heading
* @deprecated 1.21.0 Use a `HelpRenderer` instead.
*
* @return string
* @param string $heading The heading text.
* @return string The rendered heading.
*/
public static function renderHelpHeading(string $heading): string
{
return self::color($heading, self::COLOR_YELLOW) . "\n";
}

/**
* @deprecated 1.21.0 Use a `HelpRenderer` instead.
* Render a command for help information (deprecated).
*
* @param string $name
* @param string $description
* @param int $pad
* @deprecated 1.21.0 Use a `HelpRenderer` instead.
*
* @return string
* @param string $name The command name.
* @param string $description The command description.
* @param int $pad The padding length.
* @return string The rendered command.
*/
public static function renderHelpCommand(string $name, string $description, int $pad = 20): string
{
Expand All @@ -139,13 +231,14 @@ public static function renderHelpCommand(string $name, string $description, int
}

/**
* @deprecated 1.21.0 Use a `HelpRenderer` instead.
* Render an argument for help information (deprecated).
*
* @param string $name
* @param string $description
* @param int $pad
* @deprecated 1.21.0 Use a `HelpRenderer` instead.
*
* @return string
* @param string $name The argument name.
* @param string $description The argument description.
* @param int $pad The padding length.
* @return string The rendered argument.
*/
public static function renderHelpArgument(string $name, string $description, int $pad = 20): string
{
Expand All @@ -154,17 +247,22 @@ public static function renderHelpArgument(string $name, string $description, int
}

/**
* @deprecated 1.21.0 Use a `HelpRenderer` instead.
* Render an option for help information (deprecated).
*
* @param string $shortName
* @param string $longName
* @param string $description
* @param int $pad
* @deprecated 1.21.0 Use a `HelpRenderer` instead.
*
* @return string
* @param string $shortName The short option name.
* @param string $longName The long option name.
* @param string $description The option description.
* @param int $pad The padding length.
* @return string The rendered option.
*/
public static function renderHelpOption(string $shortName, string $longName, string $description, int $pad = 20): string
{
public static function renderHelpOption(
string $shortName,
string $longName,
string $description,
int $pad = 20
): string {
$name = '';

if ($shortName && $longName) {
Expand All @@ -179,6 +277,13 @@ public static function renderHelpOption(string $shortName, string $longName, str
return " " . self::color($name, self::COLOR_GREEN) . " $description\n";
}

/**
* Render a table with the given content and settings.
*
* @param array $content The content of the table.
* @param array $settings The settings for each column (left or right alignment).
* @return string The rendered table.
*/
public static function renderTable(array $content, array $settings): string
{
$columns = [];
Expand Down

0 comments on commit c15b064

Please sign in to comment.