Luminova Framework

PHP Luminova: Formatting Command Outputs Using the Text Utils Module

Last updated: 2024-08-30 19:57:36

The command Text utils class is a helper class for Luminova Command. It provides a common useful method for output text processing to help while developing your CLI application.

Text Utils is one of the many powerful features offered by Luminova's Framework command-line interface. With this versatile class, you can enhance the appearance of your output text, taking your CLI tool to a new level of elegance and professionalism. Whether you're aiming to beautify your output for end users or simply improve readability during development, Text Utils provides functions to help you achieve your goals with ease.


  • Class namespace: \Luminova\Command\Utils\Text

Constants

ConstantDescriptionTypeValue
ANSI_RESETANSI character reset flag.int0
ANSI_BOLDANSI character bold flag.int1
ANSI_ITALICANSI character italic flag.int3
ANSI_UNDERLINEANSI character underline flag.int4
ANSI_STRIKETHROUGHANSI character strikethrough flag.int9

Methods

padStart

Pads string left

public static padStart(string $text, int $length, string $char = ' '): string

Parameters:

ParameterTypeDescription
$textstringThe text string to pad.
$lengthintThe maximum length of padding.
$charstringThe padding character to use (default: ' ').

Return Value:

string - Return the the text with left padded.


padEnd

Pads string right

public static padEnd(string $text, int $length, string $char = ' '): string

Parameters:

ParameterTypeDescription
$textstringThe text string to pad.
$lengthintThe maximum length of padding.
$charstringThe padding character to use (default: ' ').

Return Value:

string - Return the the text with right padded.


border

Create a border around text.

public static border(string $text, ?int $padding = null): string

Parameters:

ParameterTypeDescription
$textstringThe string to add border.
$paddingintAdditional optional padding to apply (default: null).

Return Value:

string - Return text with border round.


center

Create a centered text

public static center(string $text, ?int $padding = null): string

Parameters:

ParameterTypeDescription
$textstringThe text string to center.
$paddingintAdditional optional padding to apply (default: null).

Return Value:

string - Return centered text.


padding

Pads string both left and right.

public static padding(string $text, int $length, string $char = ' ', int $position = STR_PAD_BOTH): string

Parameters:

ParameterTypeDescription
$textstringThe text string to apply padding.
$lengthintThe maximum length of padding.
$charstringThe padding character to use (default: ' ').
$padintThe position of the text to apply padding (e.g, STR_PAD_*), (default: STR_PAD_BOTH).

fit

Pads string to fit same length.

public static fit(string $text, int $max, int $extra = 2, int $indent = 0): string

Parameters:

ParameterTypeDescription
$textstringThe text string to apply fit length.
$maxintThe maximum padding to apply.
$extraintHow many extra spaces to add at the end (default: 2)
$indentintAn optional indent to apply (default: 0).

strlen

Get the length of characters in a string and ignore ANSI that was applied to the text.

public static strlen(?string $string = null, string $encoding = 'UTF-8'): int

Parameters:

ParameterTypeDescription
$stringstringThe string to calculate it's length.
$encodingstringText encoding to use (default: UTF-8).

Return Value:

int - Return the number of characters in the string.

It replace all ANSI color codes and styles with an empty string before calculating the string length.


style

Apply ANSI style formatting to a text string.

public static style(string $text, int|null $format = null, bool $formatted = true): string

Parameters:

ParameterTypeDescription
$textstringText to apply style.
$formatint|nullThe style to apply constant code identifier to use (e.g, Text::ANSI_*).
$formattedboolWeather to return a text with applied style or just style code (default: true).

Return Value:

string - Return style formatted string or ansi string.


hasAnsi

Check if text already has ANSI method in place

public static hasAnsi(string $text): bool

Parameters:

ParameterTypeDescription
$textstringThe text string to check.

Return Value:

bool - Return true if text contains ANSI, otherwise false.


largest

Get the longest line from a text string that may contain various newline formats.

public static largest(string $text): array<int,mixed>

Parameters:

ParameterTypeDescription
$textstringText to process.

Return Value:

array<int,mixed> - Returns the longest line as the first element and its length as the second element.